Tic Tac Toe: Problem with computer v player

12/02/2013 02:49 LordGragen.#1
hello guys, i made a tic tac toe game that player is vs computer but now i am trying to make the computer smarter so if player have 2 boxes marked and about to win computer next move should block it, or if the computer have chance to win it should.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>


using namespace std;


class TicTacToe
{
   private:
      int Position[9];
      int Player;
      int TurnCounter;
      int NumberOfPlayers;
      bool PlayerType[20];
   public:
      TicTacToe();
      void PrintTurn();
      bool PlayerHuman();
      void HumanMove();
      void ComputerMove();
      void DrawBoard();
      bool Winner();
      bool FullBoard();
      void NextTurn();

	    
}; 


TicTacToe::TicTacToe()
{
    srand(time(0)); 
    Player = 1; 
    TurnCounter = 0; 
    int i = 0;
    

    NumberOfPlayers = 2;
    PlayerType[1] = 1; 
    PlayerType[2] = 0; 

    for ( i = 0; i < 9; i++)
    {
       Position[i] = 0;
    }
}

void TicTacToe::DrawBoard()
{
	system("cls");
          cout << "+----------------------------+" << endl;
          cout << "|Tic Tac Toe |" << endl;
          cout << "|Made By: Boris Grigoryan |" << endl;
          cout << "|____________________________|" << endl;
          cout << endl;
   cout << endl
        << Position[0] << " | " << Position[1] << " | " << Position[2]
        << "\n--+---+--\n"
        << Position[3] << " | " << Position[4] << " | " << Position[5]
        << "\n--+---+--\n"
        << Position[6] << " | " << Position[7] << " | " << Position[8]
        << endl;
} 

void TicTacToe::PrintTurn()
{
   cout << "\nPlayer " << Player << "'s turn.\n";
} 

void TicTacToe::NextTurn()
{
   TurnCounter++;

   if (++Player > NumberOfPlayers)
   {
      Player = 1;
   }
} 

bool TicTacToe::PlayerHuman()
{
   return PlayerType[Player];
} 

void TicTacToe::HumanMove()
{
   cout << "\nEnter your move (0-8): ";
   int Move;

   do
   {
      do{
 std::cout << "Enter a value: ";
 while(!(std::cin >> Move)){
  std::cin.clear();
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  std::cout << "Error, please enter a new value: ";
 }
} while (Move < 0 || Move > 8);
   }
   while (Move < 0 || Move > 8 || Position[Move] != 0);
   Position[Move] = Player;
}

void TicTacToe::ComputerMove()
{
   int Move;

   do
   {
      Move  = rand() % 9;
   }
   while (Move < 0 || Move > 8 || Position[Move] != 0);
   Position[Move] = Player;

   
} 

bool TicTacToe::Winner()
{
   int i = 0;
   int Board[8][3] = {{0,1,2},
                     {3,4,5},
                     {6,7,8},
                     {0,3,6},
                     {1,4,7},
                     {2,5,8},
                     {0,4,8},
                     {2,4,6}}; 

   for ( i = 0; i < 8; i++) 
   {
      if ((Position[Board[i][0]] == Position[Board[i][1]]) && (Position[Board[i][1]] == Position[Board[i][2]]) && Position[Board[i][0]] != 0)
      {
         cout << "\nPlayer " << Position[Board[i][0]]
              <<  " wins!\n\n";
         cout << "Press Enter to Close...." << endl;
         system("pause");
         return 1;  
      }
   }
   return 0; 
} 

bool TicTacToe::FullBoard()
{
   if (TurnCounter == 9)
   {
      cout << "\nTie game!\n\n";
      return 1;
   }
   else
   {
      return 0;
   }
} 


int main(void)
{
   TicTacToe NewGame;
  
        
   NewGame.DrawBoard();
   
   do
   {
      NewGame.PrintTurn();
      if (NewGame.PlayerHuman()) 
      {
         NewGame.HumanMove();
      }
      else 
      {
         NewGame.ComputerMove();
      }
      
      NewGame.DrawBoard();
      NewGame.NextTurn();
    }
    while (!NewGame.Winner() && !NewGame.FullBoard()); 

    return 0;
}
12/02/2013 08:05 th0rex#2
Just Check it with Some if's. If None of them succeeds do a random move. Dont know where the Problem is.
12/02/2013 12:40 LordGragen.#3
Quote:
Originally Posted by omitma View Post
Just Check it with Some if's. If None of them succeeds do a random move. Dont know where the Problem is.
idk how start it lol a bit confused you think you can give me example?
12/02/2013 18:23 .SkyneT.#4
The Minmax-Algorithm could be usefull..