Wednesday, 5 March 2014

tic tac toe

#include <iostream>
#include <string>

using namespace std;

enum players {X = 1, O = 2};                  //Values for each player    
const short strategy[] = {2,1,2,1,3,1,2,1,2};     //Constant array used for comp to decide the move to make

class TicTacToe{
short state[9];                                 //State is the current table position as moves are made
public:
TicTacToe()
{
for(int i = 0; i < 9; i++)               //Initialising blank state
state[i] = 0;
}

/**
* X = 1
* O = 2
*/
bool move(int player, int pos)               //Function to make a move
{
if(pos < 0 || pos > 8 || state[pos] != 0)    //Checking if the correct positions were entered
return false;

state[pos] = player;                         //Setting the state as per the move made and at which position
return true;
}

short possibleWin()
{
//Check possible win for each player
for (int p = 1; p < 3; ++p)          //p=1 is player 1 and p=2 is player 2
{
for(int i = 0; i < 9; i += 3)
{

//Check for win horizontally...and return the empty state which will make you win.
if(state[0+i] == p && state[1+i] == p && state[2+i] == 0)
return 2+i;
else if(state[1+i] == p && state[2+i] == p && state[0+i] == 0)
return 0+i;
else if(state[0+i] == p && state[2+i] == p && state[1+i] == 0)
return 1+i;

//Check for win vertically...and return the empty state which will make you win.
if(state[0+i/3] == p && state[3+i/3] == p && state[6+i/3] == 0)
return 6+i/3;
else if(state[0+i/3] == p && state[6+i/3] == p && state[3+i/3] == 0)
return 3+i/3;
else if(state[3+i/3] == p && state[6+i/3] == p && state[0+i/3] == 0)
return 0+i/3;
}

//Check for win diagonally....and return the empty state which will make you win.
if(state[0] == p && state[4] == p && state[8] == 0)
return 8;
else if(state[4] == p && state[8] == p && state[0] == 0)
return 0;
else if(state[2] == p && state[4] == p && state[6] == 0)
return 6;
else if(state[4] == p && state[6] == p && state[2] == 0)
return 2;
else if(((state[0] == p && state[8]) || (state[2] == p && state[6] == p)) && state[4] == 0)
return 4;
}

//Win not possible
return -1;
}

/**
* 1 = X wins
* 2 = O wins
* 3 = Nobody wins
* 0 = Game in progress
*/
short gameOver()
{
//For each player check if the horizontal or vertical lines have got a win situation
for (int p = 1; p < 3; ++p)
{
for(int i = 0; i < 9; i+=3){            
if(state[0+i] == p && state[1+i] == p && state[2+i] == p)
return p;
else if(state[0+i/3] == p && state[3+i/3] == p && state[6+i/3] == p)
return p;
}

if((state[0] == p && state[4] == p && state[8] == p) || (state[2] == p && state[4] == p && state[6] == p))
return p;
}

for (int i = 0; i < 9; ++i)
{
if(state[i] == 0)
return 0;
}

return 3;
}

void showBoard()      //Displaying the board
{
for (int i = 0; i < 9; ++i)
{
if(state[i] == 0)
cout<<i;
else if(state[i] == 1)
cout<<" X ";
else if(state[i] == 2)
cout<<" O ";

if((i+1)%3 == 0)
cout<<endl;
}
cout<<endl;
}

short getNextMove()
{
short highestPossibleMove = 0;
short highestPossiblePos = -1;
short possible;

if(gameOver())
return -1;
else{

if((possible = possibleWin()) != -1)     //If you get a value for a possible win which was return that as the next move
{
return possible;
}

for (int i = 0; i < 9; ++i){
if(state[i] == 0 && strategy[i] > highestPossibleMove)   //Check for The best move in an empty position
{
highestPossibleMove = strategy[i];
highestPossiblePos = i;
}
}

return highestPossiblePos;
}
}


};

int main(int argc, char** args)
{
TicTacToe lol;
int pos;
bool flag = false;
string name;

cout<<"Enter your name"<<endl;
cin>>name;

while(lol.gameOver() == 0)
{
while(!flag)
{
cout<<"Enter move: ";
cin>>pos;
flag = lol.move(X, pos-1);   //Players turn
if(!flag)
cout<<"Invalid move\n";
}
lol.showBoard();
lol.move(O, lol.getNextMove());   //Comp decides which move next
lol.showBoard();
flag = false;
}

if(lol.gameOver() == 1)
cout<<name<<" wins!\n";
else if(lol.gameOver() == 2)
cout<<name<<" loses\n";
else
cout<<"Nobody wins\n";

system("pause");

return 0;
}
















No comments:

Post a Comment