Tic Tac Toe Game

Nov 13, 2008 at 9:50pm
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
Nov 13, 2008 at 9:59pm
Is your Tic Tac Toe a console game?
Nov 13, 2008 at 10:11pm
yes
Nov 13, 2008 at 10:16pm
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 :)
Nov 13, 2008 at 10:21pm
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 Nov 13, 2008 at 10:22pm
Nov 13, 2008 at 11:17pm
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);
Nov 14, 2008 at 1:59am
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 Nov 14, 2008 at 2:02am
Nov 14, 2008 at 2:57am
Are you using the TicTacToe code from the Dev++ Home page? Or did you make your own heh - just wondering :P
Dec 1, 2008 at 4:09am
Topic archived. No new replies allowed.