Jump to content
  • Advertisement
Sign in to follow this  

Tic Tac Toe Game logic question

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i a using a hash table to store the seperate moves of the players. when it coemes to checking there is a winner ther is only one problem. i have got it working for all horizontals and verticals but one of the verticals isnt working here is my code int num = htPlayerMoves->Count; String *strWinnerId; if (num >= 5) { int counter =0; int startRow = 1 ; int midRow = 2; int endRow = 3; while ((counter < 9) && (winner==false)) { //check if possible win String *a = String::Concat("btn",startRow.ToString()); String *b = String::Concat("btn",midRow.ToString()); String *c = String::Concat("btn",endRow.ToString()); String *htItem1; String *htItem2; String *htItem3; //String *fullmessage = String::Concat(Sender, S" : ", msg); if (htPlayerMoves->ContainsKey(a) && htPlayerMoves->ContainsKey(b) && htPlayerMoves->ContainsKey(c)) { //posiible win htItem1 =htPlayerMoves->get_Item(a)->ToString(); htItem2 = htPlayerMoves->get_Item(b)->ToString(); htItem3 = htPlayerMoves->get_Item(c)->ToString(); if (htItem1->Equals(htItem2) && htItem2->Equals(htItem3)) { /// win winner = true; strWinnerId = htItem1; } } if (counter < 3) { startRow = startRow + 3; midRow = midRow + 3; endRow = endRow + 3; } else if (counter ==3) { //startRow = startRow + 1; //midRow = midRow + 4; //endRow = endRow + 7; startRow = 1; midRow = 4; endRow = 7; } else if (counter>3 && counter <7) { startRow = startRow + 1; midRow = midRow + 1; endRow = endRow + 1; } else if (counter == 7) { startRow = 1; midRow = 5; endRow = 9; } else if (counter ==8) { startRow = 3; midRow = 5; endRow = 7; } counter ++; }//whileloop the problem comes with the if statement 'else (if counter == 8). it doesnt seem to work with this one. any1 got any ideas?

Share this post


Link to post
Share on other sites
Advertisement
1) There are 8 possible ways to make a line, not 9.
2) You set the start/mid/end variables at the *end* of the last loop iteration, so it never gets to actually *check* with those values.
3) Your logic is much, much more complicated than it needs to be. You are effectively doing something slightly different on every loop iteration - changing a few variables and then running the same detection code. Then you have a bunch of complicated if-logic to cover the fact that you can't really calculate those variable values cleanly.

Instead, set up a function to do the win detection given the locations to look at, and just call it 8 times.

Also:
Quote:

i a using a hash table to store the seperate moves of the players.


Why on Earth would you do it that way? Just have an array of 9 'squares' which know what piece is located there. The fact that you need to construct button names is a serious design smell.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!