1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <Windows.h>
#include <cstdlib>
using namespace std;
char slot[3][3]={'1','2','3','4','5','6','7','8','9'};
char *pointer[9]={&slot[0][0], &slot[0][1], &slot[0][2], &slot[1][0], &slot[1][1], &slot[1][2], &slot[2][0], &slot[2][1], &slot[2][2]};
char player_turn='2';
char X_or_O;
char Player_Move;
char restart_game;
int game_number=1;
bool valid_move=true;
bool game_over=false;
void print_board();
void Marker_Set();
void Switch_marker();
void Switch_marker()
{
switch (player_turn)
{
case '1': player_turn='2';
break;
case '2': player_turn='1';
break;
}
}
void print_board()
{
cout<<slot[0][0]<<" "<<slot[0][1]<<" "<<slot[0][2]<<endl;
cout<<slot[1][0]<<" "<<slot[1][1]<<" "<<slot[1][2]<<endl;
cout<<slot[2][0]<<" "<<slot[2][1]<<" "<<slot[2][2]<<endl;
}
void Marker_Set()
{
cout<<"player "<<player_turn<<"'s turn: ";
if (player_turn=='1')
X_or_O='X';
else
X_or_O='O';
}
int main()
{
do
{
do
{
game_over==false;
do
{
print_board(); // function prints out the board
Switch_marker(); //switches marker from X to O
Marker_Set(); //sets player 1 to X and player two to O
cin>>Player_Move; //input and checks to see if the move is valid or not
if (Player_Move=='1' && slot[0][0]=='1')
slot[0][0]=X_or_O;
else if (Player_Move=='2' && slot[0][1]=='2')
slot[0][1]=X_or_O;
else if (Player_Move=='3' && slot[0][2]=='3')
slot[0][2]=X_or_O;
else if (Player_Move=='4' && slot[1][0]=='4')
slot[1][0]=X_or_O;
else if (Player_Move=='5' && slot[1][1]=='5')
slot[1][1]=X_or_O;
else if (Player_Move=='6' && slot[1][2]=='6')
slot[1][2]=X_or_O;
else if (Player_Move=='7' && slot[2][0]=='7')
slot[2][0]=X_or_O;
else if (Player_Move=='8' && slot[2][1]=='8')
slot[2][1]=X_or_O;
else if (Player_Move=='9' && slot[2][2]=='9')
slot[2][2]=X_or_O;
else
{ cout<<"Invalid move, have another go."<<endl;
valid_move=false; }
}
while (valid_move==false); // repeat input and checks to see if its valid
if (slot[0][0]==slot[0][1] && slot[0][2]==slot[0][1]) //checks to see if someone has won the game
game_over=true;
if (slot[1][0]==slot[1][1] && slot[1][2]==slot[1][1])
game_over=true;
if (slot[2][0]==slot[2][1] && slot[2][2]==slot[2][1])
game_over=true;
if (slot[0][0]==slot[1][0] && slot[2][0]==slot[1][0])
game_over=true;
if (slot[0][1]==slot[1][1] && slot[2][1]==slot[1][1])
game_over=true;
if (slot[0][2]==slot[1][2] && slot[2][2]==slot[1][2])
game_over=true;
if (slot[0][0]==slot[1][1] && slot[2][2]==slot[1][1])
game_over=true;
if (slot[0][2]==slot[1][1] && slot[0][2]==slot[1][1])
game_over=true;
if (game_over==true)
cout<<"player "<<player_turn<<" wins!!"<<endl;
if (slot[0][0]!='1' && slot[0][1]!='2' && slot[0][2]!='3'
&& slot[1][0]!='4' && slot[1][1]!='5' && slot[1][2]!='6' &&
slot[2][0]!='7' && slot[2][1]!='8' && slot[2][2]!='9')
game_over==true;
}
while(game_over==false);
print_board();
cout<<"would you like to play again?"<<"y / n"<<endl;
cin>>restart_game;
if (restart_game=='y')
{
game_number+=1;
cout<<"okay tic tac toe game number: "<<game_number<<endl;
if(player_turn=='1')
player_turn++;
slot[0][0]='1';
slot[0][1]='2';
slot[0][2]='3';
slot[1][0]='4';
slot[1][1]='5';
slot[1][2]='6';
slot[2][0]='7';
slot[2][1]='8';
slot[2][2]='9';
}
else cout<<"okay goodbye, come back later";
Sleep(1000);
}
while (restart_game=='y');
}
|