Created
May 3, 2013 17:56
-
-
Save emilyboynton/5512000 to your computer and use it in GitHub Desktop.
tic tac toe after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cctype> | |
using namespace std; | |
char board [9]; | |
void display_board (void); | |
bool validate (int z); | |
int winner (); //returns a 0 for draw, 1 for player 1, and 2 for player 2 | |
int main() | |
{ | |
//delcaration of local variables | |
string player_1; | |
string player_2; | |
int move; | |
char play_again = 'n'; | |
do { //play the game while the users enter Y or y to play again | |
int turn = 1; //the number assigned to turn will indicate which player's turn it is (1 or 2) | |
int total_turns = 0; //counter for total moves of the game | |
//declaration of board spaces. Reset for each game | |
board[0] = '0'; | |
board[1] = '1'; | |
board[2] = '2'; | |
board[3] = '3'; | |
board[4] = '4'; | |
board[5] = '5'; | |
board[6] = '6'; | |
board[7] = '7'; | |
board[8] = '8'; | |
//obtain and store player names | |
cout<<endl<<endl<<endl; | |
cout<<"Player 1, please enter your name: "<<endl; | |
cin>>player_1; | |
cout<<"Player 2, please enter your name: "<<endl; | |
cin>>player_2; | |
while (winner() == 0 && total_turns < 9){ | |
do{ //do the following until the player enters a valid move | |
//display the board | |
display_board(); | |
//indicate to players whose turn it is | |
if (turn == 1){ | |
cout<< player_1 << ": it's your turn!"<<endl; | |
} | |
else { | |
cout<< player_2 << ": it's your turn!"<<endl; | |
} | |
//ask player to make a move | |
cout<<"Please enter the number of the space where you'd like to move: "<<endl; | |
cin>>move; | |
} while (validate (move) != true); //if != true, validate() will print an error message | |
//increment the total number of moves for the game | |
total_turns++; | |
//mark the board and switch to the other player's turn | |
switch (turn) { | |
case (1): { | |
board[move] = 'x'; | |
turn = 2; | |
break; | |
} | |
case (2): { | |
board[move] = 'o'; | |
turn = 1; | |
break; | |
} | |
} | |
} | |
//display the board | |
display_board(); | |
if (winner() == 1){ | |
cout<<"Congrats "<<player_1<<", you have won!"<<endl<<endl; | |
cout<<"Would you like to play again? Y/N?"<<endl; | |
cin>>play_again; | |
} | |
else if (winner() == 2){ | |
cout<<"Congrats "<<player_2<<", you have won!"<<endl; | |
cout<<"Would you like to play again? Y/N?"<<endl; | |
cin>>play_again; | |
} | |
else { | |
cout<<"Looks like you're an even match..."<<endl; | |
cout<<"Would you like to play again? Y/N?"<<endl; | |
cin>>play_again; | |
} | |
} while (tolower(play_again) == 'y'); //will play the game again if y. any other letter to exit. | |
return 0; | |
} | |
//this function will show the current state of the board in the game and will return nothing to main | |
void display_board(){ | |
cout<<endl; | |
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; | |
} | |
//take the players move and determin if it as valid move on the | |
//current board and if the value entered is a digit between 0 and 8. | |
//Returns a Boolean value to main. | |
bool validate(int z){ | |
if (isdigit(board[z]) && z >= 0 && z < 9 ) | |
return true; | |
else { | |
cout<<endl<<"I'm sorry, you've entered an invalid value."<<endl<<endl; | |
cout<<"Please try again."<<endl<<endl; | |
return false; | |
} | |
} | |
//This function will check for the eight instances in which one of the players may have | |
//won the game returning 1 for player 1, 2 for player 2, or a 0 for a draw | |
int winner (){ | |
//check for winning horizontally | |
if (board[0] == board[1] && board[1] == board[2]){ | |
if (board[0] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} | |
if (board[3] == board[4] && board[4] == board[5]){ | |
if (board[3] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} | |
if (board[6] == board[7] && board[7] == board[8]){ | |
if (board[6] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} | |
//check for winning vertically | |
if (board[0] == board[3] && board[3] == board[6]){ | |
if (board[0] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} | |
if (board[1] == board[4] && board[4] == board[7]){ | |
if (board[1] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} | |
if (board[2] == board[5] && board[5] == board[8]){ | |
if (board[2] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} | |
//check for winning diagonally | |
if (board[0] == board[4] && board[4] == board[8]){ | |
if (board[0] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} | |
if (board[2] == board[4] && board[4] == board[6]){ | |
if (board[2] == 'x' ) | |
return 1; | |
else | |
return 2; | |
} else //else return 0 for a draw | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment