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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void board_draw();//board drawing function
int game_check();//game check prototype
int pvp();//Player vs player function prototype
int pvc();//player vs computer prototype
int computer_ai(int s);//computer artificial inteligence function prototype
char board[10] = {'0','1','2','3','4','5','6','7','8','9'};//global array
int player1 = 1;//global int for player
int main()
{
char computer_or_player;
cout<<"Would you like to play against the computer? [y]es or [n]o : ";
cin>>computer_or_player;
switch(computer_or_player){
case 'n':
pvp();
break;
case 'y':
pvc();
}
}
void board_draw(){//we are just designing the board here
char *b = board;
cout<<"Welcome to Derrik's Tic Tac Toe Experience!";
cout<<"\n | | "<<endl;
cout<<" "<<b[1]<<" |"<<" "<<b[2]<<" | "<<b[3]<<endl;
cout<<"_____|_____|_____"<<endl;
cout<<" | | "<<endl;
cout<<" "<<b[4]<<" |"<<" "<<b[5]<<" | "<<b[6]<<endl;
cout<<"_____|_____|_____"<<endl;
cout<<" | | "<<endl;
cout<<" "<<b[7]<<" |"<<" "<<b[8]<<" | "<<b[9]<<endl;
cout<<" | | "<<endl;
}
int game_check(){//our game checing function
int game_check = -1;//sets game_check to -1 which means it won't effect anything
if(board[1] == board[2] && board[2] == board[3]){
game_check = 1;
}
else if(board[4] == board[5] && board[5] == board[6]){
game_check = 1;
}
else if(board[7] == board[8] && board[8] == board[9]){
game_check = 1;
}
else if(board[1] == board[4] && board[4] == board[7]){//Checking all possible winning outcomes of this 3 x 3 tic tac toe game.
game_check = 1;
}
else if(board[2] == board[5] && board[5] == board[8]){
game_check = 1;
}
else if(board[3] == board[6] && board[6] == board[9]){
game_check = 1;
}
else if(board[1] == board[5] && board[5] == board[9]){
game_check = 1;
}
else if(board[3] == board[5] && board[5] == board[7]){
game_check = 1;
}
else{
game_check = 0;
}
if(game_check == 1 && player1 == 2){/*If the game has been won then this decides who won. while player1 = 2 Player 1 has won the game.
because game check is at the begining of the loop. so the value for player 2 to equal "true" is set before it checks the game
and i was to lazy to move it 8)*/
cout<<"Player 1 Wins!!!\n\n";
player1 = 3;//sets player1 to 3 to terminate program
}
else if(game_check == 1 && player1 == 1){
cout<<"Player 2 Wins!!!\n\n";
player1 = 3;//sets player1 to 3 to terminate program
}
}
int pvp()
{
cout<<"hello";
int turns = 0;
while(turns < 9)/*cheecks that turn does not exceed 9*/
{
game_check();
board_draw();//Draw board by initilizing function
int selection;
if(player1 == 1) {//if player one is true then do this ie. is player 1
cout<<"\nPlayer 1, Please make a move by selcting your desired space: ";
if( cin >> selection && selection > 0 && selection < 10 )
{
cin.ignore(1000, '\n');
if((board[selection] == 'X') || (board[selection] == 'O'))//checks to ensure that the space has not already been taken
{
cout<<"Please pick a spot that has not been taken";
}
else{//If space had not been taken already then manipulate it
board[selection] = 'X';
player1 = 2;
turns++;
}
}
else{
cin.clear() ; // clear a possible error state
cin.ignore( 1000, '\n' ) ; // and empty the input buffer
cout << "*** invalid move \n\n " ;
}
}
else if(player1 == 2){//opposite of above
cout<<"\nPlayer 2, Please make a move by selcting your desired space: ";
if( cin >> selection && selection > 0 && selection < 10 )
{
cin.ignore(1000, '\n');
if((board[selection] == 'X') || (board[selection] == 'O'))//checks to ensure that the space has not already been taken
{
cout<<"Please pick a spot that has not been taken";
}
else{
board[selection] = 'O';
player1 = 1;
turns++;
}
}
else{
cin.clear() ; // clear a possible error state
cin.ignore( 1000, '\n' ) ; // and empty the input buffer
cout << "*** invalid move\n\n " ;
}
|