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;
} 
}

It's kind of hard to solve your problem because:

  • You didn't use code tags
  • You never stated what you were having problems with in your code
  • It's kind of hard to tell what's a syntax error and what is simply cut-off code when you copy+pasted the code here

Soooo... I would suggest you start by adding code tags if it's not too late to edit your post (before 30 minutes). Then, give us the error messages that occur, and show us which lines they occur on. Then we'll try to help you.

(Note: a lot of your functions are trying to access variables which don't exist or are our of scope. You're going to have to pass them as arguments to the function if you want to use them.)

Thanks Joe,
I know I should of presented my case a little better, it's just that I've worked and re-worked this code for too long. I'm fed up with my instructor, he is truly NOT earning his money. By the way this is the original code.

>I should of presented my case a little better, it's just that I've worked and re-worked this code for too long.
Being frustrated is not a good excuse for neglecting to read the rules and stickies at the top of this forum.

>I'm fed up with my instructor, he is truly NOT earning his money.
Well... we hear that a lot. ;) I don't think there are THAT many poor instructors, although I'm not saying you don't have a case.

The key to remember is to read up on C++ using good and reliable sources. I suggest cprogramming.com for good articles and tutorials on using C and C++. Also check out the sticky at the top of this forum for other good websites to visit.

A second thing that you should keep in mind is to pay attention to compiler errors. They usually mean something. If you can't interpret them, post them here for us to see. And usually a cryptic message often means a silly mistake such as:

  • forgetting to declare a variable (usually seen as "not declared in this scope")
  • forgetting a semicolon ("unexpected blah blah blah")
  • forgetting a closing brace ("local function definitions are illegal, unexpected end of file")

And the list goes on. Also look at lines around the one that has the error, as the mistakes often occurs on a different line than the compiler error.

This question has already been answered. Start a new discussion instead.