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
|
#include <iostream>
#include <cstdlib>
using namespace std;
int inputMove(char b[],int size,int move,bool p,bool mk[]);
bool checkMove(char a[],int size,bool p);
int main()
{
int temp;
bool player = true;
bool result=1;
const int size = 9;
char board[size] = {'1','2','3','4','5','6','7','8','9'};
bool marked[size] = {0,0,0,0,0,0,0,0,0};
int move;
while(result)
{
cout << board[0] << "|" << board[1] << "|" << board[2] << "\n-----" << endl; // Draw the Board
cout << board[3] << "|" << board[4] << "|" << board[5] << "\n-----" << endl;
cout << board[6] << "|" << board[7] << "|" << board[8] << "\n-----" << endl;
cout << "Enter number:";
cin >> move; // Input Move
temp=inputMove(board,size,move,player,marked);// Draw the move on the board
if(temp==0)
{
player = player;
}
else
{
player=!player;
}
result = checkMove(board,size,player); //Check for win or draw
}
}
int inputMove(char b[],int size,int move,bool p,bool mk[])
{
char temp;
if(p==true) //Determine who's move is it.
{
temp='X';
}
else
{
temp='O';
};
if(move>0 && move<10) //Make the move and mark it.
{
system("CLS");
if(mk[move-1]==1)
{
cout << "Already Marked" << endl;
return 0;
}
b[move-1]=temp;
mk[move-1]=1;
}
else //If move is not between 1-9,ask the user to re-enter
{
cout << "INVALID MOVE" << endl;
cin.get();
cin.get();
system("CLS");
};
};
bool checkMove(char a[],int size,bool p)
{
char temp;
if(p==false)
{
temp='X';
}
else
{
temp='O';
}
if((a[0]==temp) && (a[1]==temp) && (a[2]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
else if((a[2]==temp) && (a[5]==temp) && (a[8]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
else if((a[3]==temp) && (a[4]==temp) && (a[5]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
else if((a[6]==temp) && (a[7]==temp) && (a[8]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
else if((a[0]==temp) && (a[3]==temp) && (a[6]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
else if((a[1]==temp) && (a[4]==temp) && (a[7]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
else if((a[0]==temp) && (a[4]==temp) && (a[8]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
else if((a[2]==temp) && (a[4]==temp) && (a[6]==temp))
{
cout << temp << " WINS!" << endl;
return 0;
}
if((a[0] != '1') && (a[1] != '2') && (a[2] != '3') && (a[3] != '4') //Check for Draw
&& (a[4] != '5') && (a[5] != '6') && (a[6] != '7') && (a[7] != '8')
&& (a[8] != '9'))
{
system("CLS");
cout << "DRAW!";
return 0;
}
return 1;
}
|