0

Hey guys, I'm new to this whole programming deal and have browsed around these forums here or there to learn a few things. My latest assignment is to create a working tic tac toe game, including functions to initialize the game board, print the board, check the game status, checking if moves are valid, and lastly (which is the part I am having the most trouble with), a function to handle user moves.

The game just simply has to be a player vs player type of deal, no computer AI to play against or anything, and this all needs to be done without classes, so just functions and a big ol' main.

Like I said, what I'm really struggling on is wrapping my head around how exactly to handle the user moves. Additionally, I'm using two arrays for this, one of type int that I want to actually store the data in, the other of type char for displaying the progress of the game. I have no idea if this is actually what I am supposed to do, as I said, I'm fairly new to this and not very good at it. Also, in my function to determine a game winner, I feel like what I'm doing with the bool win=true is just totally wrong, but everything compiles as it is at the moment.

Anyway, here is what I have so far. If anyone could help me figure out exactly how to process user moves, I would really appreciate the assistance.

#include <iostream>

using namespace std;


void printBoard(char [3][3]);
void initializeBoard(int [3][3]);
bool checkMove(int [3][3], int, int);
bool checkStatus(char [3][3], int);
void CreateBoard(int [3][3], char [3][3]);


int main()
{

char value;
int row, column;
int player;
int MoveCounter=0;
bool win = false;
int board[3][3];
char TTTboard[3][3];


initializeBoard(board);
CreateBoard(board, TTTboard);
printBoard(TTTboard);

return 0;
}


void printBoard(char TTTboard[3][3])
{
        cout<<"        0       1       2" << endl << endl;
        cout<<"0      " << TTTboard[0][0] << "  |  " << TTTboard[0][1] << "  |  " << TTTboard[0][2] << endl;
        cout<<"     ----------------" << endl;
        cout<<"1      " << TTTboard[1][0] << "  |  " << TTTboard[1][1] << "  |  " << TTTboard[1][2] << endl;
        cout<<"     ----------------" << endl;
        cout<<"2      " << TTTboard[2][0] << "  |  " << TTTboard[2][1] << "  |  " << TTTboard[2][2] << endl;
}

void initializeBoard(int board[3][3])
{
        for (int i=0; i<3; i++)
        {
                for (int j=0; j<3; j++)
                {
                        board[i][j]=0;
                }
        }
}
bool checkMove(int board[3][3], int row, int column)
{
        if ((row >= 0) && (row < 3) && (column >= 0) && (column < 3) && (board[row][column] == 0))
                return true;
        else
                return false;

}

bool checkStatus(int board[3][3])
{

        if (((board[0][0] == 'X') && (board[0][1] == 'X') && (board[0][2] == 'X')) ||
                ((board[1][0] == 'X') && (board[1][1] == 'X') && (board[1][2] == 'X')) ||
                ((board[2][0] == 'X') && (board[2][1] == 'X') && (board[2][2] == 'X')) ||
                ((board[0][0] == 'X') && (board[1][0] == 'X') && (board[2][0] == 'X')) ||
                ((board[0][1] == 'X') && (board[1][1] == 'X') && (board[2][1] == 'X')) ||
                ((board[0][2] == 'X') && (board[1][2] == 'X') && (board[2][2] == 'X')) ||
                ((board[0][0] == 'X') && (board[1][1] == 'X') && (board[2][2] == 'X')) ||
                ((board[0][2] == 'X') && (board[1][1] == 'X') && (board[2][0] == 'X')))
                        bool win=true;
                        return true;

        if (((board[0][0] == 'O') && (board[0][1] == 'O') && (board[0][2] == 'O')) ||
                ((board[1][0] == 'O') && (board[1][1] == 'O') && (board[1][2] == 'O')) ||
                ((board[2][0] == 'O') && (board[2][1] == 'O') && (board[2][2] == 'O')) ||
                ((board[0][0] == 'O') && (board[1][0] == 'O') && (board[2][0] == 'O')) ||
                ((board[0][1] == 'O') && (board[1][1] == 'O') && (board[2][1] == 'O')) ||
                ((board[0][2] == 'O') && (board[1][2] == 'O') && (board[2][2] == 'O')) ||
                ((board[0][0] == 'O') && (board[1][1] == 'O') && (board[2][2] == 'O')) ||
                ((board[0][2] == 'O') && (board[1][1] == 'O') && (board[2][0] == 'O')))
                        bool win=true;
                        return true;


}
void CreateBoard(int board[3][3], char TTTboard[3][3])
{
        for (int i=0; i<3; i++)
        {
                for (int j=0; j<3; j++)
                {
                        if (board[i][j] == 1)
                                TTTboard[i][j] = 'O';

                        else if (board[i][j] == 2)
                                TTTboard[i][j] = 'X';

                        else
                                TTTboard[i][j] = ' ';
                }
        }
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Salem
0

> bool checkStatus(int board[3][3])
You should pass a player parameter, so you don't duplicate all the code for 'X' and 'O'

> if (board[j] == 1)
But your checking function tests 'X' and 'O'
You have two boards (bad), and an inconsistent approach.

The 1 becomes 'X' should only happen in the display function.

Also, use enum { PLAYER_NONE, PLAYER_X, PLAYER_O }; instead of literal constants.

> bool checkMove(int board[3][3], int row, int column)
Add a similar makeMove() function as well.

Then it's just
- read some input
- checkMove()
- makeMove()
- while ( !checkStatus )

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.