I think everything is in order, but After each player makes a move the board keeps reappering along with putting the X or the O in the slot selected. Does anyone know how to fix this and also if anyone see's a way to make this better than I have done that would be cool too. Thanks.
Code:
#include <iostream>
using namespace::std;
#include <stdlib.h>
void Game_Board();
bool checkWin();
void move(bool);
bool isLegal(int);
char spot[9] = {'1','2','3','4','5','6','7','8','9'};
void main()
{
bool player = false; // true = X false = O
Game_Board();
while(!checkWin())
{
if(player == true)
player = false;
else
player = true;
move(player);
}
if(player == true)
cout << "Player 1 WINS" << endl;
else
cout << "Player 2 WINS" << endl;
}
void Game_Board()
{
cout << "\n " << spot[0] << " | " << spot[1] << " | " << spot[2] << endl
<< " ---------" << endl
<< " " << spot[3] << " | " << spot[4] << " | " << spot[5] << endl
<< " ---------" << endl
<< " " << spot[6] << " | " << spot[7] << " | " << spot[8] << endl;
}
void move(bool who)
{
int potition;
if(who == true)
cout << "\nSelect number to place your X in Player 1, and press enter: ";
else
cout << "\nSelect number to place your O in Player 2, and press enter: ";
cin >> potition;
if(isLegal(potition))
{
if(who == true)
spot[potition-1] = 'X';
else
spot[potition-1] = 'O';
}
else
move(who);
Game_Board();
}
bool isLegal(int potition)
{
if(spot[potition-1] == 'X' || spot[potition-1] == 'O')
return false;
else
return true;
}
bool checkWin()
{
if(spot[0] == spot[1] && spot[2] == spot[0] )
return true;
else if(spot[3] == spot[4] && spot[5] == spot[3])
return true;
else if(spot[6] == spot[7] && spot[8] == spot[6])
return true;
else if(spot[0] == spot[3] && spot[6] == spot[0])
return true;
else if(spot[1] == spot[4] && spot[7] == spot[1])
return true;
else if(spot[2] == spot[5] && spot[8] == spot[2])
return true;
else if(spot[0] == spot[4] && spot[8] == spot[0])
return true;
else if(spot[2] == spot[4] && spot[6] == spot[2])
return true;
else
return false;
}