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
|
#include <iostream>
const int board[9]={-1,1,-1, -1, -1, 1, 1,-1, 1};
const int orig[9]={2,3,4,5,1,2,3,4,5};
const int test[9]={1,1,1,4,3,3,3,3,1};
const int X = 1;
const int O = -1;
const int None = 0;
int index(int i, int j)// Pass the array to the function
{
int ans=i*3+j;
std::cout << "The number " << board[ans] << " is at that location.."<<std::endl;
return board[ans];
}
void copyBoard(const int orig[9], int copy[9])// Again, pass the array, plus the second one
{
for (int i=0;i<9;i++)
copy[i]=orig[i];
}
/*
* @return X if the X side has won
* O if the O side has won
* None if neither X nor O has won
*/
int checkBoard(const int board[9]){
if (board[0]== 1 && board[1]== 1 && board[2]== 1)
return X;
else if (board[3]== 1 && board[4]== 1 && board[5]== 1)
return X;
else if (board[6]== 1 && board[7]== 1 && board[8]== 1)
return X;
else if (board[0]== 1 && board[3]== 1 && board[6]== 1)
return X;
else if (board[1]== 1 && board[4]== 1 && board[7]== 1)
return X;
else if (board[2]== 1 && board[5]== 1 && board[8]== 1)
return X;
else if (board[0]== 1 && board[4]== 1 && board[8]== 1)
return X;
else if (board[2]== 1 && board[4]== 1 && board[6]== 1)
return X;
if (board[0]== -1 && board[1]== -1 && board[2]== -1)
return O;
else if (board[3]== -1 && board[4]== -1 && board[5]== -1)
return O;
else if (board[6]== -1 && board[7]== -1 && board[8]== -1)
return O;
else if (board[0]== -1 && board[3]== -1 && board[6]== -1)
return O;
else if (board[1]== -1 && board[4]== -1 && board[7]== -1)
return O;
else if (board[2]== -1 && board[5]== -1 && board[8]== -1)
return O;
else if (board[0]== -1 && board[4]== -1 && board[8]== -1)
return O;
else if (board[2]== -1 && board[4]== -1 && board[6]== -1)
return O;
else
return None;
}
int main(){
int win = checkBoard(board);
if(win == -1)
std::cout << "O is the winner" << std::endl;
else if(win == 1)
std::cout << "X is the winner" << std::endl;
else
std::cout << "It's a DRAW!" << std::endl;
return 0;
}
|