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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <iostream>
#include <cstdlib>
#include <string>
#ifndef TIC_TAC_TOE_H
#define TIC_TAC_TOE_H
//header files declaration
class tic_tac_toe {
public:
void prnt();
void get_move();
void winner();
};
#endif
tic_tac_toe TICTACTOE;
//class declaration
using namespace std;
char player1='X';
char player2='O';
int inputcounter=0;
const int a=3,b=3;
char board[a][b]={ {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };
//declarations
void tic_tac_toe::prnt() {
int x;
cout << " 1 2 3\n"
<< " *=*=*=*\n";
for(x=0;x<=2;x++) {
cout << x+1 << " |" << board[x][0] << "|" << board[x][1] << "|" << board[x][2] << "|" << endl
<< " *=*=*=*\n";
}
}
//function will print the tic tac toe box
void tic_tac_toe::get_move() {
int x=1;
int row[20]={'\0','\0'}, column[20]={'\0','\0'}, row1[20]={'\0','\0'}, column1[20]={'\0','\0'};
/* row[20] and column[20] are for the storage of the inputs of player1
while row1[20] and column1[20] are for the storage of the inputs of player2 */
bool tf=false, ft=false, check=false;
while(!check) {
while(!tf) {
cout << "\nPlayer1: enter the row and column of desired move: ";
cin >> row[x] >> column[x];
//will ask the user to input the row and column
if(inputcounter==0) {
if((row[x]>3 || row[x]<=0) || (column[x]>3 || column[x]<=0) ) {
cout << "\nError-tile already occupied"; tf=false; }
}
board[row[x]-1][column[x]-1]=player1; tf=true;
TICTACTOE.prnt();
//will output the "error" line if the condition was met else it outputs the result after the input or where the user wishes to have his/her move
if(inputcounter>=1 && inputcounter<=8) {
TICTACTOE.winner();
for(;inputcounter>=0;inputcounter--) {
if( (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][1]) || (board[row[inputcounter]-1][1]==board[row[inputcounter]-1][2]) || (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][2]) ){
cout << "\nError-tile already occupied"; tf=false; }
else {board[row[x]-1][column[x]-1]=player1; tf=true; TICTACTOE.winner();}
TICTACTOE.prnt();}
}
}
TICTACTOE.winner();
// same with line 51, except only runs within the values 1 to 8 of inputcounter
while (!ft && inputcounter!=8) {
cout << "\nPlayer2: enter the row and column of desired move: ";
cin >> row1[x] >> column1[x];
if(inputcounter==0) {
if((row[x]>3 || row[x]<=0) || (column[x]>3 || column[x]<=0) ) {
cout << "\nError-tile already occupied"; tf=false; }
}
if( (board[row[x]-1][column[x]-1]==board[row1[x]-1][column1[x]-1]) ) {
cout << "\nError-tile already occpuied" ; ft=false;
}
else {board[row1[x]-1][column1[x]-1]=player2; x++; ft=true;}
system("cls");TICTACTOE.prnt();
if(inputcounter>=1 && inputcounter<=7) {
for(;inputcounter>=0;inputcounter--) {
if( (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][1]) || (board[row[inputcounter]-1][1]==board[row[inputcounter]-1][2]) || (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][2]) ){
cout << "\nError-tile already occupied"; ft=false; }
else {board[row[x]-1][column[x]-1]=player2; x++; ft=true; TICTACTOE.winner();}
system("cls");TICTACTOE.prnt();}
}
}
}
tf=false; ft=false;
}
void tic_tac_toe::winner ()
{
string congrats="\nWe have a winner!";
string nothing="\nNext Move : \n";
//checks every row
for (int x=0;x<3;x++)
{
if (board[x][0]==board[x][0+1] && board[x][0]==board[x][2])
{
if (board[x][0]==player1)
cout <<"\nPlayer1 wins!";
else if (board[x][0]==player2)
cout <<"\nPlayer2 wins!";
}
}
//checks every column
for (int x=0;x<3;x++)
{
if (board[x][0]==board[x][1] && board[x][0]==board[x][2] && board[x][1]==board[x][2])
{
if (board[x][0]==player1)
cout <<"\nPlayer1 wins!";
else if (board[x][0]==player2)
cout <<"\nPlayer2 wins!";
}
}
//checks left to right diagonal
if (board[0][0]==board[1][1]&&board[0][0]==board[2][2]){
if (board[0][0]==player1)
cout <<"\nPlayer1 wins!";
else if (board[0][0]==player2)
cout <<"\nPlayer2 wins!";
}
//checks right to left diagonal
if (board[2][0]==board[1][1]&&board[2][0]==board[0][2]) {
if (board[2][0]==player1)
cout <<"\nPlayer1 wins!";
else if (board[2][0]==player2)
cout <<"\nPlayer2 wins!";
}
if(inputcounter==9) {
//This is where i check if it's a draw or not
if( (board[0][0]!=board[0][1] &&board[0][0]!=board[0][2] &&board[0][1]!=board[0][2])
|| (board[1][0]!=board[1][1] && board[1][0]!=board[1][2] && board[1][1]!=board[1][2])
|| (board[2][0]!=board[2][1] && board[2][0]!=board[2][2] && board[2][1]!=board[2][2])
|| (board[0][0]!=board[1][1] && board[0][0]!=board[2][2] && board[1][1]!=board[2][2])
|| (board[0][2]!=board[1][1] && board[0][2]!=board[2][0] && board[1][1]!=board[2][0])
|| (board[0][1]!=board[1][1] && board[0][1]!=board[2][1] && board[1][1]!=board[2][1])
|| (board[0][2]!=board[1][2] && board[0][2]!=board[2][2] && board[1][2]!=board[2][2]) ) {
cout << "Draw!";
}
}
cout << nothing;
}
int main() {
cout << "This is a Tic Tac Toe Game made into a program. \nLegend:Player1='X' and Player2='O'\n Please Enjoy!";
system("pause>0"); system("cls");
for(inputcounter=0;inputcounter<=8;inputcounter++) {
TICTACTOE.prnt();
TICTACTOE.get_move(); system("pause>0"); system("cls");
}
system("pause>0");
return 0;
}
|