Skip to content

Instantly share code, notes, and snippets.

@emilyboynton
Last active December 15, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyboynton/5268296 to your computer and use it in GitHub Desktop.
Save emilyboynton/5268296 to your computer and use it in GitHub Desktop.
This simple Tic Tac Toe game will invite two players to enter their names and to play a quick round. Player 1 is 'x' and player 2 is 'o.' The players will be given a 3x3 board that will have a number, 0 through 8, assigned to each space. On their turn, the players will enter the number of the space that they wish to play. When a player has marke…
#include <iostream>
#include <string>
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 turn = 1; //the number assigned to turn will indicate which player's turn it is (1 or 2)
int move;
int total_turns = 0; //counter for total moves of the game
//declaration of board spaces
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<<"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{
//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);
//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;
else if (winner() == 2)
cout<<"Congrats "<<player_2<<", you have won!"<<endl;
else
cout<<"Looks like you're an even match..."<<endl;
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 will return a Boolean value
bool validate(int z){
if (board[z] != 'x' && board[z] != 'o')
return true;
else
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