Solutions for TicTacToe

I am a junior programmer and I know the basics of pascal and C ++. I made a Tic Tac Toe game with Player-Computer, and the game is over.

The computer generates a random place where Os goes on the table, and this is bad.

I thought I needed several procedures that check each winning position, and the computer should also try to block the Xs player or make a winning position, BUT this would be a lot of time lost due to all if.

Then I thought of a simpler version with some types of ifs, but it would still take a lot of time.

Then I thought deeper: how about game 4? How on earth someone will be able to check every available space and how it would be possible for someone to perform a function that absolutely checks any gain or progress in the position of the player / computer, Oh and wait, which is NOT ALL, what if the player does some tricks, why does it lock the computer? How would a computer know this?!? Of course, this will take time to program. And I'm not talking about something that seems more impossible: Chess.

So here I am, asking myself what SHOULD be a simpler way for a computer to search and solve some problems than tons of ifs.

In this case, if any of you know any way to resolve this issue, how can I manage to make the simplest procedure for blocking and beating a player in the game TicTacToe?

If someone wants to check my code or use it: http://pastebin.com/jhyUn7d1

+4
source share
6 answers

What are you looking for, Minimax .

Using this algorithm, the computer will win every Tic Tac Toe game or you can adjust the depth at which the computer analyzes movements in order to achieve some average difficulty.

It’s not difficult to implement, you should be familiar with recursion, and you are set up, of course, the implementation is different according to your code, but the wikipedia page offers a good starting point.

+3
source

The Tic Tac Toe algorithm looks something like this:

  • Take a seat if you win.
  • Take a seat if you lose
  • Take the corner
  • Take a non-circular off-center
  • Take center
+1
source

I have been doing this recently, although my code was in C #.

I came up with a way to score every candidate. The approach I made creates an estimate based on the number of moves that will then be needed to win (fewer moves require results in a higher ballat).

My algorithm also takes into account the combined number of moves for several squares. As a result, the algorithm favors movements that will produce multiple potential wins (the only real tactic I know about Tic Tac Toe). For example, sometimes you can make a move that creates two potential wins that must be blocked. Since the opponent can block only one, he wins.

I posted all of my code and its description in the Tic-Tac-Toe Game Engine article.

0
source

Short answer: "Try all the different moves until the game is won, and do not write down which of them lead to the victory of the computer."

Long answer ...

For a limited-size TTT game, the number of possible moves before the game is won, this is not so, just try every possible move, then recursively try all possible movements of the opponent and continue moving until the game ends. Give each step an β€œassessment” of how well it went (for example how many different solutions you received, which were successful for the computer, and how many successes the opponent reached, and choose the one that has the β€œbest” result). Beware that you are likely to find yourself in something that cannot be won if you succeed.

0
source

I did it once, a long time ago. I do not know if I have code lying around ...

In any case, I created a function that returns an int, which was the square in which the computer should place its part (provided that 0 is the upper left and 8 is the lower right square). Yours uses a 2D array, so it will be slightly different.

In any case, for each row, column and diagonal, check whether any two parts of this row belong to the player. If they do not, check for the same, but belong to the computer. In the first line that this is true, check the rest - if available, put it there to win. If you have a line with a dominated player, make sure you do not have a part yet and do not insert it into the block.

const int PlayerPiece = 1; const int CPiece = 2; const int Empty = 0; int board[3][3]; if(board[0][0] == PlayerPiece && board[0][1] == PlayerPiece && board [0][2] == Empty) { //Put_Your_Piece_In_[0][2] } 

Then you can change it so that it can check every line i.e.

 int numRows = 3; for(int i = 0; i < numRows; i++) { if(board[i][0] == PlayerPiece && board[i][1] == PlayerPiece && board[i][2] == Empty) { //Put_Piece_In_[i][2] } } 

Then do the same for the strings.

You would always think that Tic-Tac-Toe is just a magic square, described quite well here: http://www.sciforums.com/showthread.php?134281-An-isomorphism-Tic-Tac-Toe-on -Magic-Square

0
source

There is an excellent strategy for Tic-Tac-Toe available on wikipedia . It is really easy. Due to the small size of the grid, the number of cases that you need to check (for example, check if there are 2 blocks in a row) is very small.

0
source

Source: https://habr.com/ru/post/1480157/


All Articles