## Suggestion on Tic Tac Toe

I am designing my implementation strategy for Tic-Tac-Toe game. Since this is my 1st game implementation, I am a bit confused and need some general pointers.

Now, the total number of winning combinations in a Tic-Tac-Toe are 8. Currently, I plan to store these winning combinations in an array. Once the end user has made at least 3 moves, I would start checking if the Player has won the game by comparing the current positions used by a Player against this array. However, I am sure this is not an efficient way to check if the player has a winning combination.

Can anyone please suggest me on how to go about with design the logic for the game?

Don't worry about efficiency. I wrote a backtracking solution and there are only 549,945 possible game states. It takes less than 0.25 seconds to run through these on my laptop. Here was my logic to see if the game was over - clearly not very efficient, but it doesn't matter:

private boolean isWinningMove(int row, int col) { int piece = board[row][col]; // check current row boolean w = true; for (int x = 0; x < 3; x++) { w = w && (board[row][x] == piece); } if (w) { return true; } // check current column w = true; for (int x = 0; x < 3; x++) { w = w && (board[x][col] == piece); } if (w) { return true; } // check 0,0 diagonal w = true; for (int x = 0; x < 3; x++) { w = w && (board[x][x] == piece); } if (w) { return true; } // check 0,2 diagonal w = true; for (int x = 0; x < 3; x++) { w = w && (board[x][2 - x] == piece); } return w; }

Here were my results, which match data on the Wikipedia page for tic-tac-toe:

Moves Simulated: 549945 Draws=46080 Player1-Wins=131184 Player2-Wins=77904 Perfect Strategy Implies: Always a tie. Games won in 0 moves? 0 Games won in 1 moves? 0 Games won in 2 moves? 0 Games won in 3 moves? 0 Games won in 4 moves? 0 Games won in 5 moves? 1440 Games won in 6 moves? 5328 Games won in 7 moves? 47952 Games won in 8 moves? 72576 Games won in 9 moves? 81792

**[PDF] Game Suggestions: Tic-Tac-Toe,** Words! Traditional Tic-Tac-Toe. Accessories: Chalk and Bean bags. Accessories: Plastic rings and cones. Fill the 9 fields in the square with; Nouns,. Verbs� Tic-Tac-Toe. Play a retro version of tic-tac-toe (noughts and crosses, tres en raya) against the computer or with two players.

**Challenge Suggestion: tic-tac-toe,** Looking at the 'Bingo' Challenge I think it would be great to adapt tic-tac-toe into a Fitbit Challenge as well! The way I see it would be:� Since the state space for tic-tac-toe is so small, you could store all the possible end game positions, and use rotations, but I think you're overthinking it a little. Instead of storing a 3x3 array for the game board, use a 7x7 array, with the inner-most 3x3 for the game board.

Consider representing the board by integers.

-1 = X 0 = empty 1 = O

now, add up the value of the squares for each of the 8 possibilities (3 up and down, 3 left and right, 2 diagionals).

if the sum is 3, O wins if the sum is -3, X wins

if the sum is 2, then O has a winning move in one of those positions if the sum i -2, then X has a winning move in one of those positions.

The AI can use that as a basis for making decisions. A one move look ahead is sufficient to never lose.

If the AI starts the game, the best move is a corner. If the opponent fails to take the center, the AI wins. If he does take the center, then either the AI wins, or draws.

**Tic-Tac-Toe game suggestion,** You need to fix indentation for the program to run (the fix is easy though, just look at the elif branches on line 102 onwards). What you've� Tic Tac Toe Tips & Tricks. Control the corners. Most players go for the middle space whenever they can, but don't ignore the corners! You can use the corners to set up multiple winning moves at once, leaving your opponent no way to block your win. Watch your opponent. Take note of where they place their O’s.

Instead of iterating over something I just wrote the 8 combinations.

My evaluation func does the following: if A is side to move & if there is two A elements and a 0 (empty) in one of all combinations then it's a win:

boolean player_can_win(int value) { //value is side's element*2 return board[0] + board[1] + board[2] == value || board[3] + board[4] + board[5] == value || board[6] + board[7] + board[8] == value || board[0] + board[3] + board[6] == value || board[1] + board[4] + board[7] == value || board[2] + board[5] + board[8] == value || board[0] + board[4] + board[8] == value || board[2] + board[4] + board[6] == value; }

**Tic Tac Toe Free | 1st Viewer Suggestion!,** Here is my first viewer suggested video! Long-time subscriber, Cam Thedasher1, suggested Tic Duration: 12:53
Posted: 22 Jul 2019 Tic-Tac-Toe game suggestion. Ask Question Asked 4 months ago. Active 4 months ago. Viewed 45 times 0 \$\begingroup\$ I am a beginner so please keep it in mind while

Explicitly storing and comparing to the solutions isnt the most efficient if you were playing generalized N X N tic-tac-toe, but since it's such as small board and there are only 8 such combos there is nothing wrong with explicitly storing solutions like this.

The bigger issue is that depending on storage style, spaces that aren't relevant to the solution might be an issue.

O - - - O - X X X vs. X X X O - O O - O

comparing 3x3 state arrays these are different and as such this method would require well over 8 end states

I presume you keep something like a gameState 3x3 array with blank=0, X=1, O=2 ?

Besides those explicit comparisons, you can do something like

win = false // rows/columns for i in 0,1,2 if (state[i][0] != BLANK && state[i][0] == state[i][1] == state[i][2]) win = true #extensible to NxN - all(j == state[i][0] for j in state[i]) if (state[0][i] != BLANK && state[0][i] == state[1][i] == state[2][i]) win = true #extensible to NxN - all(j == state[0][i] for j in zip(*state)[i]) //diagonals if (state[0][0] != BLANK && state[0][0] == state[1][1] == state[2][2]) win = true #extensible to NxN - all(state[j][j] == state[0][0] for j in range(len(state)) if (state [2][0] != BLANK && state[2][0] == state[1][1] == state[0][2]) win = true

If you want win to store the winner rather than flag, then make win = BLANK up top, and set to the value of any of the involved squares. Shouldn't be necessary tho, winner is obviously the most recent move!

I think the part of writing tic-tac-toe that you may find most challenging, but not too hard, would the AI. It is not too difficult, but not exactly trivial, to write an AI that wont lose (can always at least force a tie). If you want a relatively good AI that is capable of losing occasionally, youll need to add some randomness or something.

**Tkinter Tic Tac Toe With Enhanced Features – Suggestions Invited,** As my first python project, I intend to develop an advanced version of Tic Tac Toe on Tkinter, incorporating enhanced features for better user� Tic-tac-toe (American English), noughts and crosses (Commonwealth English), or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner.

**Motivating With Tic Tac Toe,** The suggestions are in- tended primarily for middle school game of tic tac toe. Students practice and reinforce spelling, abbrevi-. Open to Suggestion 355� This is a really neat and easy project. I saw this on Pinterest but added my own twist and style. Perfect for a summer porch, beach house or cabin. The grandkids will love it! Gather your supplies (yard sale finds) I picked up this wooden drawer type box and little basket at a yard sale. What fun to marry these two items for a unique combo. Plus I’m always collecting drawer handles and wood

**Tic tac toe | Artificial Intelligence Question,** Tic-tac-toe is a pencil-and-paper game for two players, X and O, who take turns marking the spaces on a 3�3 grid. The player who succeeds in placing three� About Tic Tac Toe. Tic Tac Toe is a timeless paper and pencil game that players of all ages know and love. Also known as Noughts and Crosses in the United Kingdom, Tic Tac Toe is one of the most-played games of all time, and for good reason! It's easy to learn, but difficult to master because your opponent likely knows the optimal strategy.

**Tic Tac Toe,** Apr 14, 2020 - Explore Nancy M's board "Tic Tac Toe" on Pinterest. See more ideas about Tic tac toe, Tic tacs, Tic tac toe game. Pieces with my TTT Word Study and Math centers that can be found in my TpT store. Questions or suggestions? Mark off the tic-tac-toe board on top and paint in the lines with white acrylic paint. Here we have everything painted and ready to go to the next step! For Animal, begin by painting in the eyes with white, and the mouth with black, leaving a red area for his tongue.

##### Comments

- There are at most 9! states, which is 362880 (there are actually less because many of those states reach a winning condition before the last move). Not sure how you managed to get 549945 states.
- The backtracking tree size is 549,945 - it includes all the games in progress.
- I did not understand the need for 7x7 array. Can you please elaborate? Also, I am not clear on the statement:
`check all around the square that was chosen for for 3-in-a-row; 2 above, 2 below, 2 left, 2 right, 2 upper left, 2 lower right, 2 upper right, 2 lower left`

. Thank you - I added clarification for why the 7x7 array is handy.
- This seems way more complicated than just checking the 8 possible combinations. You haven't even covered all cases (1 above & 1 below, 1 right and 1 left, etc). This is the overthinking way. The OP's way is the obvious and straight-forward way.
- True, I missed 2 cases. However, this solution generalizes to any NxM board and is highly efficient in terms of CPU and memory.
- Common, high CPU efficiency to compute a tic-tac-toe winning position ?
- @Shaggy: How is that not exactly what jon said? Jon referred generically to NxN and you're saying what he said is true for N=10. What part of what he said is "not necessarily true"?