Can anyone help me with this code? Most of it was written by me for homework. The switch portion was written by my instructor, and it is just pain confusing to me! I beleive I can write a TTT program from scratch (Won't be accepted) easier than cobbling this together. Sorry this is my first post, so all of you helpful coders please excuse my ignorance. Thanks
#include <iostream>
using namespace std;
int checkWinState ();
bool displayBoard(int r, int c);
bool isOpen(int r, int c);
void printBoard();
int board[3][3];
int main() {
printBoard();
}
// takes the row and the column as input and if the spot is open
// it will put an x or an o there depending on which player's turn it is.
// returns true if the move was successful and false if it wasn't.
bool displayBoard(int r, int c)
{
if(isOpen(r, c) == true)
{
board[r][c] = player;
printBoard();
return true;
}
printBoard();
return false; }
//checks if the spot on the board is open. input is rows and columns.
bool isOpen(int r, int c)
{
if(board[r][c] == ' ')
{ return true; }
return false; }
//prints out the board
void printBoard()
{
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
if(y == 0) { cout << " "; }
cout << board[x][y];
if(y < 2) { cout << " | "; }
if(y == 2) { cout << endl; }
if( x < 2) { cout << "-----------------" << endl; }
}
cout << endl;
}
/ User.cpp Responsible for all aspects of the user interface
void user() // function for user interface
{
while(check == 0) // checks for a continuation of the game (0 = continue)
{
if(turn % 2 == 0) // determines the player
player = 2;
else
player = 1;
cout << "Player " << player << " What box do you want?"; // gets box choice
cin >> boxNum;
if(boxNum >=1 && boxNum <= 9) // validates box number
{
switch(boxNum) // converts the boxNum to x and y values for
{ // a two-dimensional array
case 1:
x = 0, y = 0;
break;
case 2:
x = 0, y = 1;
break;
case 3:
x = 0, y = 2;
break;
case 4:
x = 1, y = 0;
break;
case 5:
x = 1, y = 1;
break;
case 6:
x = 1, y = 2;
break;
case 7:
x = 2, y = 0;
break;
case 8:
x = 2, y = 1;
break;
case 9:
x = 2, y = 2;
break;
}
check = checkWin(x, y); // looks for a win or draw
boxPlayed = boardDraw(x, y); // looks for valid box
while(boxPlayed == true) // box is played if true
{
cout << "That box has been played.\nPlayer " << player << " Choose again: ";
cin >> boxNum;
switch(boxNum) // converts the boxNum to x and y values for
{ // a two-dimensional array
case 1:
x = 0, y = 0;
break;
case 2:
x = 0, y = 1;
break;
case 3:
x = 0, y = 2;
break;
case 4:
x = 1, y = 0;
break;
case 5:
x = 1, y = 1;
break;
case 6:
x = 1, y = 2;
break;
case 7:
x = 2, y = 0;
break;
case 8:
x = 2, y = 1;
break;
case 9:
x = 2, y = 2;
break;
}
boxPlayed = boardDraw(x, y); // makes sure for an empty box
}
if(check != 1) // makes sure the right player is indicated as winner
turn++;
}
else
cout << "Enter an empty box number of 1 through 9.\n";
}
if(check == 1) // displays tic-tac-toe or draw
{
cout << "Tic-Tac-Toe! ";
if(turn % 2 == 0)
cout << "O Wins!!\n";
else
cout << "XWins!!\n";
}
else
cout << "Cat's Game!\n";
}
int checkWinState()
{
if (board[0][0] == player && board[0][1] == player && board[0][2] == player ) {
return 1;
} else if (board[1][0] == player && board[1][1] == player && board[1][2] == player ) {
return 1;
} else if (board[2][0] == player && board[2][1] == player && board[2][2] == player ) {
return 1;
} else if (board[0][0] == player && board[1][0] == player && board[2][0] == player ) {
return 1;
} else if (board[0][1] == player && board[1][1] == player && board[2][1] == player ) {
return 1;
} else if (board[0][2] == player && board[1][2] == player && board[2][2] == player ) {
return 1;
} else if (board[0][0] == player && board[1][1] == player && board[2][2] == player ) {
return 1;
} else if (board[0][2] == player && board[1][1] == player && board[2][0] == player ) {
return 1;
// second part, see if there are ANY spaces left on the board
// if so, return "continue"
} else {
for (int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if (board[i][j] == ' '){
return 0;
}
}
}
// if all else fails, return "tie"
return 2;
}
}