Wednesday, August 5, 2009

Tic Tac Toe Source

Whoa!! finally I managed to complete these humble source codes for Tic Tac Toe . It took me about 2 weeks to deal with the obstacles . Now , I understand what does a programmer feels when someone download/crack their files illegally . Please note that the codes shown below are not the ideal source codes for Tic Tac Toe . It is just a simpe version of the codes that my mind can understand . Please enjoy !

//===========codes starts here=======
#include
#include
#include

using namespace std;

char X = 'X' ;
char O = 'O' ;
char EMPTY = ' ' ;
char TIE = 'T' ;
char NO_ONE = 'N' ;
char playAgain ;


void instructions() ; //display instructions //
char askYesNo(string question) ; //ask whether you want to go 1st
char humanPiece() ; //determine human piece . returns 'x' / 'o' //
char opponent(char piece) ; //returns 'x'/'o' based on the previous choice //
void displayBoard(const vector &board) ; //display board on e screen //
int computerMove(vector board) ; //calculate computer's move,receives board n computer's piece,retutns com's move //
int isLegal(const vector board , int index) ; //
char winningCondition(vectorboard) ; //determines the game winner //




int main()

{
do
{
bool TrueFalse ;
char move , a ;
//int NUM_SQUARES = 9 ;
char alpha[] = { 'a','b','c','d','e','f','g','h','i' } ;
vector board(alpha , alpha+9) ;
int size = board.size() ;
vector :: iterator it ;
instructions() ; //
char human = humanPiece() ; //
char computer = opponent(human) ; //
char turn = X ;
displayBoard(board) ; //
char winner = NO_ONE ;
int legal = 0 ;
char check ;
int index ;
while(winner == NO_ONE ) //
{
if(turn == human)
{
do
{
cout << "Please enter an alphabet to choose that move : " ;
cin >> move ;


it = find(board.begin(), board.end(), move); //
index = it-board.begin() ; //replace it with this //
//cout << index ;
//cout << it ;
//cout << *it ;
legal = isLegal(board , index) ; // it replaced with index //

if(legal == 0)
{
cout << "invalid move , please enter the correct alphabet! " << endl ;
}
else
{
cout << "That move is valid" << endl ;
}
}while(legal != 1 ) ;
if(move == 'a') //
{
board.erase(board.begin()) ;
board.insert(board.begin() , 1 , human) ;
}
if(move == 'b') //
{
board.erase(board.begin()+1) ;
board.insert(board.begin()+1 , 1 , human) ;
}
if(move == 'c') //
{
board.erase(board.begin()+2) ;
board.insert(board.begin()+2 , 1 , human) ;
}
if(move == 'd') //
{
board.erase(board.begin()+3) ;
board.insert(board.begin()+3 , 1 , human) ;
}
if(move == 'e') //
{
board.erase(board.begin()+4) ;
board.insert(board.begin()+4 , 1 , human) ;
}
if(move == 'f') //
{
board.erase(board.begin()+5) ;
board.insert(board.begin()+5 , 1 , human) ;
}
if(move == 'g') //
{
board.erase(board.begin()+6) ;
board.insert(board.begin()+6 , 1 , human) ;
}
if(move == 'h') //
{
board.erase(board.begin()+7) ;
board.insert(board.begin()+7 , 1 , human) ;
}
if(move == 'i') //
{
board.erase(board.begin()+8) ;
board.insert(board.begin()+8 , 1 , human) ;
}
}
else
{
int ComMove = computerMove(board) ;
board.erase(board.begin()+ComMove) ;
board.insert(board.begin()+ComMove , 1 , computer) ;
}
displayBoard(board) ; //
turn = opponent(turn) ; //
check = winningCondition(board) ; //
//cout << "check is : " << check ;
//cout << "winner is : " << winner ;
winner = check ;
//cout << "winner is : " << winner ;

}
if((winner=='w') && (human == X)) // X condition for human
{
cout << "Congratulation human! you have won ... I'll defeat you next time!" << endl ;
}
if((winner=='w') && (computer==X)) //X for computer condition
{
cout << "I have defeated you human ... Is that all you got?! " << endl ;
}
if((winner=='c') && (human==O)) // O's condition for human
{
cout << "Congratulation human! you have won ... I'll defeat you next time!" << endl ;
}
if((winner=='c') && (computer==O))
{
cout << "I have defeated you human ... Is that all you got?! " << endl ;
}
if(winner == TIE)
{
cout << "Seems that our skills are equal .. Hence , its a draw! " << endl ;
}

cout << " do you want to play again ? " ;
cin >> playAgain ;


}while(playAgain == 'y') ;
return 0;

}

void instructions()
{
cout << "Welcome to the Tic Tac Toe game." << endl ;
cout << "You will be playing against the Computer (Com)." << endl ;
cout << "You need to make your move by entering character a-i" << endl ;
cout << "The number corresponds to the desired board postion as shown" << endl ;

cout << " a | b | c " << endl ;
cout << " --------- " << endl ;
cout << " d | e | f " << endl ;
cout << " --------- " << endl ;
cout << " g | h | i " << endl ;

cout << "Now, prepare yourself! The game is about to begin.." << endl<}

char askYesNo(string question)
{
char response ;
do
{
cout << question << " Yes (y) / No (n) : " << endl ;
cin >> response ;
}
while (response!='y' && response !='n') ;

return response ;
}

/*int askNumber(string question , int high , int low)
{
int number ;
do
{
cout << question << " (" << low << " - " << high << ") : " ;
cin >> number ;
}while(number>high || number
return number ;
}*/

char humanPiece()
{
char go_first = askYesNo("Do you want to move first ? ") ;
if(go_first == 'y')
{
cout << "Then , take the 1st move ! You'll be using X as your character piece" << endl ;
return X ;
}
else
{
cout << "Then, I'll go first" << endl ;
return O ;
}
}

char opponent(char piece)
{
if(piece == O)
return X ;
else
return O ;
}

void displayBoard(const vector&board)
{
cout << board[0] << " | " << board[1] << " | " << board[2] << endl ;
cout << "---------" << endl ;
cout << board[3] << " | " << board[4] << " | " << board[5] << endl ;
cout << "---------" << endl ;
cout << board[6] << " | " << board[7] << " | " << board[8] << endl ;
cout << endl ;
}

int isLegal(const vector board , int index)
{

//if((board[index]==X)&&(board[index]==O)) //('a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'))
if(board[index] == 'a')
{
return 1 ;
}
if(board[index] == 'b')
{
return 1 ;
}
if(board[index] == 'c')
{
return 1 ;
}
if(board[index] == 'd')
{
return 1 ;
}
if(board[index] == 'e')
{
return 1 ;
}
if(board[index] == 'f')
{
return 1 ;
}
if(board[index] == 'g')
{
return 1 ;
}
if(board[index] == 'h')
{
return 1 ;
}
if(board[index] == 'i')
{
return 1 ;
}
else
{
return 0 ;
}
}

/*bool isLegal(int move , const vector &board)
{
return (board[move] != (X||O)) ;
}*/
int computerMove(vectorboard)
{
int move ;
cout << "I shall take " ;
//if no one can win
if(board[4] == 'e')
{
move = 4 ;
cout << " e " << endl ;
return move ;
}
if(board[2] == 'c')
{
move = 2 ;
cout << " c " << endl ;
return move ;
}
if(board[6] == 'g')
{
move = 6 ;
cout << " g " << endl ;
return move ;
}
if(board[8] == 'i')
{
move = 8 ;
cout << " c " << endl ;
return move ;
}
if(board[3] == 'd')
{
move = 3 ;
cout << " d " << endl ;
return move ;
}
if(board[4] == 'a')
{
move = 4 ;
cout << " a " << endl ;
return move ;
}
if(board[5] == 'f')
{
move = 5 ;
cout << " f " << endl ;
return move ;
}
if(board[1] == 'b')
{
move = 1 ;
cout << " b " << endl ;
return move ;
}
if(board[7] == 'h')
{
move = 7 ;
cout << " h " << endl ;
return move ;
}
}

char winningCondition(vector board)
{
if((board[0]==X)&&(board[1]==X)&&(board[2]==X))
{
return 'w' ; //someone has won
}
if((board[3]==X)&&(board[4]==X)&&(board[5]==X))
{
return 'w' ;
}
if((board[6]==X)&&(board[7]==X)&&(board[8]==X))
{
return 'w' ;
}
if((board[0]==X)&&(board[3]==X)&&(board[6]==X))
{
return 'w' ;
}
if((board[1]==X)&&(board[4]==X)&&(board[7]==X))
{
return 'w' ;
}
if((board[2]==X)&&(board[5]==X)&&(board[8]==X))
{
return 'w' ;
}
if((board[2]==X)&&(board[4]==X)&&(board[6]==X))
{
return 'w' ;
}
if((board[0]==X)&&(board[4]==X)&&(board[8]==X))
{
return 'w' ;
}

//O's winning condition return 2
if((board[0]==O)&&(board[1]==O)&&(board[2]==O))
{
return 'c' ; //O's user has won
}
if((board[3]==O)&&(board[4]==O)&&(board[5]==O))
{
return 'c' ;
}
if((board[6]==O)&&(board[7]==O)&&(board[8]==O))
{
return 'c' ;
}
if((board[0]==O)&&(board[3]==O)&&(board[6]==O))
{
return 'c' ;
}
if((board[1]==O)&&(board[4]==O)&&(board[7]==O))
{
return 'c' ;
}
if((board[2]==O)&&(board[5]==O)&&(board[8]==O))
{
return 'c' ;
}
if((board[2]==O)&&(board[4]==O)&&(board[6]==O))
{
return 'c' ;
}
if((board[0]==O)&&(board[4]==O)&&(board[8]==O))
{
return 'c' ;
}
//if( (board[0]==(X&&O)) , (board[1]==(X&&O)) , (board[2]==(X&&O)) , (board[3]==(X&&O)) , (board[4]==(X&&O)) , (board[5]==(X&&O)) , (board[6]==(X&&O)) , (board[7]==(X&&O)) , (board[8]==(X&&O)) )
//if(board[0],board[1],board[2],board[3],board[4],board[5],board[6],board[7],board[8]==(X||O))
if( ((board[0]==X)||(board[0]==O)) && ((board[1]==X)||(board[1]==O)) && ((board[2]==X)||board[2]==O) && ((board[3]==X)||(board[3]==O)) && ((board[4]==X)||(board[4]==O)) && ((board[5]==X)||(board[5]==O)) && ((board[6]==X)||(board[6]==O)) && ((board[7]==X)||(board[7]==O)) && ((board[8]==X)||(board[8]==O)) )
{
return TIE ;
}
else
{
return NO_ONE ; // indicate no one has won
}
}

2 comments:

  1. wa.syahmul amek programming ke?

    ReplyDelete
  2. haha ... boleh katekan lah.. To be exact probably , in between harware and software(Computer engineering)... Syafiq is the one who is taking progrmming(Computer Science)

    ReplyDelete