ok so the idea behind this was to make it for two players for one-on-one competition and eventually start working on AI for it...
so with the code thats here it has the ability to determine if somebody wins, it also has the ability to figure out what to display(X, O or -)... The only problem is that when I run it right now it only displays two rows and I'm not positive why its doing it but evidentaly something in the code is not right;
in the switch/cases of play() is where it is decided what it needs to display...
help appreciated,
CJ
Code:
#include <iostream>
using namespace std;
class game{
int board[3][3];
int row1, row2, row3;
int col1, col2, col3;
int dig1, dig2;
bool winner;
string player1, player2;
public:
game(){
board[0][0] = 0;//makes all variables of the board array 0
board[0][1] = 0;
board[0][2] = 0;
board[1][0] = 0;
board[1][1] = 0;
board[1][2] = 0;
board[2][0] = 0;
board[2][1] = 0;
board[2][2] = 0;
winner = false; //makes winner false
player1 = "X";//player 1 is X
player2 = "O";//player 2 is O
}
int row(){
row1 = board[0][0] + board[0][1] + board[0][2]; //adds all of the rows up
row2 = board[1][0] + board[1][1] + board[1][2];
row3 = board[2][0] + board[2][1] + board[2][2];
if (row1 || row2 || row3 >= 3){ //checks to see if any rows are greater than or = to 3
if(board[0][0] == board[0][1] == board[0][2]){ //if so it checks to see if anybody won by comparing values of the variables in the row
winner = true;
}
if(board[0][0] == board[0][1] == board[0][2]){
winner = true;
}
if(board[2][0] == board[2][1] == board[2][2]){
winner = true;
}
}
}
int col(){
col1 = board[0][0] + board[1][0] + board[2][0];//adds all columns up
col2 = board[0][1] + board[1][1] + board[2][1];
col3 = board[0][2] + board[1][2] + board[2][2];
if (col1 || col2 || col3 >= 3){ //checks to see if any colum is greater or = to 3
if (board[0][0] == board[1][0] == board[2][0]){
winner = true;
}
if (board[0][1] == board[1][1] == board[2][1]){
winner = true;
}
if (board[0][2] == board[1][2] == board[2][2]){
winner = true;
}
}
}
int dig(){
dig1 = board[0][0] + board[1][1] + board[2][2];//adds the diagonals up
dig2 = board[0][2] + board[1][1] +board[2][0];
if (dig1 || dig2 >= 3){ // checks to see if any of the diagonals values is greater than 3
if (dig1 >= 3){
if (board[0][0] == board[1][1] == board[2][2]){
winner = true;
}
}
if (dig2 >= 3){
if (board[2][0] == board[2][1] == board[0][2]){
winner = true;
}
}
}
}
int disp(){
switch(board[0][0]){ // looks to see if each value is 0, 1 or 2 for each position on the board to determine what to display in its spot
case 0:
cout<< "- ";
break;
case 1:
cout<< player1 << " ";
break;
case 2:
cout<< player2 << " ";
break;
}
switch(board[0][1]){//same for the rest of the switch/cases
case 0:
cout<< " - ";
break;
case 1:
cout<< player1 << " ";
break;
case 2:
cout<< player2 << " ";
break;
}
switch(board[0][2]){
case 0:
cout<< " -" << endl;
break;
case 1:
cout<< player1 << endl;
break;
case 2:
cout<< player2 << endl;
break;
}
switch(board[1][0]){
case 0:
cout<< "- ";
break;
case 1:
cout<< player1 << " ";
break;
case 2:
cout<< player2 << " ";
break;
}
switch(board[1][1]){
case 0:
cout<< " - ";
break;
case 1:
cout<< player1 << " ";
break;
case 2:
cout<< player2 << " ";
break;
}
switch(board[1][2]){
case 0:
cout<< " -" << endl;
break;
case 1:
cout<< player1 << endl;
break;
case 2:
cout<< player2 << endl;
break;
switch(board[2][0]){
case 0:
cout<< "- ";
break;
case 1:
cout<< player1 << " ";
break;
case 2:
cout<< player2 << " ";
break;
}
switch(board[2][1]){
case 0:
cout<< " - ";
break;
case 1:
cout<< player1 << " ";
break;
case 2:
cout<< player2 << " ";
break;
}
switch(board[2][2]){
case 0:
cout<< " - " << endl;
break;
case 1:
cout<< player1 << endl;
break;
case 2:
cout<< player2 << endl;
break;
}
}
}
int play(){
row();//does row computation
col();//does column computation
dig();//does diagonal computation
disp();
}
};
int main(){
game toe;
toe.play();
}