Questions tagged [tic-tac-toe]

Tic Tac Toe is a popular exercise for beginning coders, as the finite resources and game mechanics can be easily grasped and represented in many ways. As it is a short game, it is possible to create an algorithm that never loses.

The X player usually goes first. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game. The following example game is won by the first player, X.

screenshot

1551 questions
122
votes
26 answers

Algorithm for Determining Tic Tac Toe Game Over

I've written a game of tic-tac-toe in Java, and my current method of determining the end of the game accounts for the following possible scenarios for the game being over: The board is full, and no winner has yet been declared: Game is a…
dreadwail
  • 15,098
  • 21
  • 65
  • 96
64
votes
10 answers

What algorithm for a tic-tac-toe game can I use to determine the "best move" for the AI?

In a tic-tac-toe implementation I guess that the challenging part is to determine the best move to be played by the machine. What are the algorithms that can pursued? I'm looking into implementations from simple to complex. How would I go about…
42
votes
35 answers

Code Golf: Tic Tac Toe

Post your shortest code, by character count, to check if a player has won, and if so, which. Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where: 0 = nothing set 1 = player…
Aistina
  • 12,435
  • 13
  • 69
  • 89
18
votes
4 answers

Monte Carlo Tree Search: Implementation for Tic-Tac-Toe

Edit: Uploded the full source code if you want to see if you can get the AI to perform better: https://www.dropbox.com/s/ous72hidygbnqv6/MCTS_TTT.rar Edit: The search space is searched and moves resulting in losses are found. But moves resulting in…
MortenGR
  • 803
  • 1
  • 8
  • 22
17
votes
1 answer

Tic Tac Toe perfect AI algorithm: deeper in "create fork" step

I've already read many Tic Tac Toe topics on StackOverflow. And I found the strategy on Wikipedia is suitable for my presentation project: A player can play perfect tic-tac-toe if they choose the move with the highest priority in the following…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
17
votes
1 answer

Is it possible to check for the winning condition of a game of TicTacToe using jGraphT?

I found this working solution: private int[] winningPatterns = { 0b111000000, 0b000111000, 0b000000111, // rows 0b100100100, 0b010010010, 0b001001001, // cols 0b100010001, 0b001010100 // diagonals }; /** Returns true if thePlayer…
Ray Hulha
  • 10,701
  • 5
  • 53
  • 53
16
votes
3 answers

Minimax explained for an idiot

I've wasted my entire day trying to use the minimax algorithm to make an unbeatable tictactoe AI. I missed something along the way (brain fried). I'm not looking for code here, just a better explanation of where I went wrong. Here is my current code…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
14
votes
8 answers

Is using too many 'if' statements bad programming?

I am curious as to if I am using too many if/else if statements. I am writing a tic-tac-toe program using javascript, and to determine if the computer should block the player I am using about 9 if statements and I use about 9 when determining if…
user5187524
14
votes
5 answers

Tic-Tac-Toe AI: How to Make the Tree?

I'm having a huge block trying to understand "trees" while making a Tic-Tac-Toe bot. I understand the concept, but I can't figure out to implement them. Can someone show me an example of how a tree should be generated for such a case? Or a good…
cam
  • 8,725
  • 18
  • 57
  • 81
13
votes
7 answers

Flutter W/zipro ( 2568): Error opening archive build\app\outputs\apk\app.apk: ERROR: dump failed because no AndroidManifest.xml found

I built a simple tic tac toe game. At first, when I ran the program on a physical android device, it worked but the day after that it didn't anymore. It shows this error. W/zipro ( 2568): Error opening archive build\app\outputs\apk\app.apk: …
Paolo Pepito
  • 131
  • 1
  • 1
  • 3
12
votes
7 answers

Generate a list of all unique Tic Tac Toe boards

I would like to generate a text file containing all 19,683 Tic-Tac-Toe board layouts in the structure of 0 = Blank, 1 = X, and 2 = O. Unfortunately math is not my strong suit and I cannot seem to find any examples of this anywhere. This isn't for…
Keith Adler
  • 20,880
  • 28
  • 119
  • 189
11
votes
1 answer

How do I fix the error "void value expression"?

I wrote a tic-tac-toe program. The part of the program that I am having trouble with is that i am getting the error: tac.rb:63: void value expression tac.rb:65: void value expression This error is coming from my check_win method. if str == "xxx" …
user2759592
  • 261
  • 1
  • 2
  • 10
11
votes
2 answers

In game programming, how can I test whether a heuristic used is consistent or not?

I have thought of some heuristics for a big (higher dimensions) tic-tac-toe game. How do I check which of them are actually consistent? What is meant by consistency anyways?
Lazer
  • 90,700
  • 113
  • 281
  • 364
10
votes
1 answer

Tic Tac Toe and Minimax - Creating an imperfect AI on a microcontroller

I have created a Tic-Tac-Toe game on a microcontroller, including a perfect AI (perfect meaning that it doesn't lose). I did not use a minimax algorithm for that, just a little state machine with all possible and optimal moves. My problem now is…
10
votes
7 answers

TicTacToe strategic reduction

I decided to write a small program that solves TicTacToe in order to try out the effect of some pruning techniques on a trivial game. The full game tree using minimax to solve it only ends up with 549,946 possible games. With alpha-beta pruning,…
Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
1
2 3
99 100