Tic Tac Toe Game

I have a Tic Tac Toe game but I want it to be that you can choose whether to play aginst a computer or human and how would you make the computer player
Is your Tic Tac Toe a console game?
yes
You would have to create a function that 'simulates' a player. Try to write down for yourself which steps you take when your playing tictactoe:
1) can i win
2) can the other player win
3)...
and translate that into source code:
1
2
3
4
5
if (computer_can_win)
...
else if (player_can_win)
...
else...


Good luck :)
To let the user choose if he wants to play against computer or other player:

1
2
3
4
5
6
7
8
9
10
11
char answer;
while (true)
{
   cout << "Who want to be your opponent?\n1 - Computer\n2 - Human\n\n";
   cin >> answer;
   if ( answer != '1' && answer != '2' )
      cout << "Invalid response!!!\n\n";
   else break;
}
if (answer == '1') //add code for player / computer
else //add code for player1 / player2 
Last edited on
Or something like
1
2
3
4
5
6
7
8
9
10
11
12
13
	int option = 0;

	do
	{
		  std::cout << "Enter Option Value\n" << "1. Play Computer Player\n" << "2. Play Human Player\n" << "0. Quit\n" <<  "  option >> ";
		  std::cin >> option;
	
		  switch (option)
		  {
			  case 1 : FunctionForComputerPlayer(); break;
			  case 2 : FunctionForHumanPlayer(); break;
		  }
	}while (option !=0);
For the computer logic, you could try something along these lines:

1. find all 3-space paths not blocked by the user already
2. sort them in order of how many the computer has already
3. attempt to move to one of the "closest to winning" paths, picking one by random when there are multiple ranked the same.

Actually, you could increase the rank of moves that fit into multiple winning paths, too. Then it would be much more challenging!
Last edited on
Are you using the TicTacToe code from the Dev++ Home page? Or did you make your own heh - just wondering :P
Topic archived. No new replies allowed.