1.11M Members

tic tac twice

 
1
 

Hello,

I'm a beginner in c++. I'm trying to write a program that simulates the game "Tic Tac Twice" (the game is described below). The program works ok until it's time for my checkForWinner() function to do its job. I can't figure out why it's not working. Any help on this would be hugely appreciated.

Tic Tac Twice starts with two 4x4 boards which each have a pattern of 16 colors. The letters A through P are used to represent the 16 colors. The players (called purple and yellow) take alternate turns. During a player's turn he can place a circle of his color on an empty spot on Board 1. At the same time he also puts a circle of his color on the same colored square on Board 2. A player wins the game by having four of his circles in a row (horizontally, diagonally or vertically) on either of the boards. Note that he don't need four in a row on both boards.

#include <iostream>
#include <cassert>
using namespace std;

void intro();
void game();
void displayBoard(char board[4][4]);
void updateBoard2(char board1Char, char board[4][4], char player);
char checkForWinner(char board[4][4], char c);

int main()
{
    intro();
    cout << endl;
    game();

    system("pause");
    return 0;
}

void intro()
{
    cout << "This is a game called Tic Tack Twice." << endl;
    cout << "The rules are simple." << endl;
    cout << "The game begins with two 4x4 boards." << endl;
    cout << "Each board has a different pattern of 16 colors." << endl;
    cout << "Each color is represented by the letters A through P." << endl;
    cout << "Two players 'yellow' and 'purple' take alternate turns." << endl;
    cout << "During a player's turn, he can place a circle of his color " << endl;
    cout << "on an empty spot on Board 1." << endl;
    cout << "At the same time, a circle of his color goes on the same " << endl;
    cout << "colored spot on Board 2." << endl;
    cout << "You win by having 4 in a row (horizontally, vertically or " << endl;
    cout << "diagonally) on either board." << endl;
}

void game()
{
    char player1 = 'y'; // player yellow
    char player2 = 'p'; // player purple

    int row;
    int col;

    cout << "Player1 please choose yellow or purple by entering y or p:" << endl;
    cin >> player1;
    if(tolower(player1 == 'y'))
    {
        player1 ='y';
        player2 ='p';
        cout << "player1 is yellow and player2 is purple" << endl;
    }
    else if(tolower(player1 == 'p'))
    {
        player1 ='p';
        player2 ='y';
        cout << "player1 is purple and player2 is yellow" << endl;  
    }
    else
    {
        cout << "You didn't enter a valid choice!" << endl;
        game();
    }

    char board1[4][4]={{'A','B','C','D'},{'E','F','G','H'},{'I','J','K','L'},{'M','N','O','P'}};
    char board2[4][4]={{'B','K','M','H'},{'P','E','C','J'},{'G','N','L','A'},{'I','D','F','O'}}; 

    for(int i = 0; i < 16; i++)
    {
        cout << "Board 1: " << endl;
        displayBoard(board1);
        cout << endl;
        cout << "Board 2: " << endl;
        displayBoard(board2);
        cout << endl;

        // player1 plays his turn
        cout << "Player1, choose an available spot on Board l." << endl;
        cout << "First enter the row (0 through 3): " << endl;
        cin >> row;
        cout << "Now enter the column (0 through 3): " << endl;
        cin >> col;
        assert(row >= 0 && row < 4 && col >= 0 && col < 4);
        if (board1[row][col] != 'y' && board1[row][col] != 'p')
        {
            //update board2
            updateBoard2(board1[row][col], board2, player1);
            board1[row][col] = player1;
        }

        if(i > 5)
        {
            //check if game is over
            if((checkForWinner(board1, player1) == 'y') 
                || (checkForWinner(board2, player1) == 'y'))
            {
                cout << "yellow is the winner" << endl;
                displayBoard(board1);
                displayBoard(board2);
                break;
            }
        }

        cout << "Board 1: " << endl;
        displayBoard(board1);
        cout << endl;
        cout << "Board 2: " << endl;
        displayBoard(board2);
        cout << endl;

        // player 2 plays his turn
        cout << "Player2, choose an available spot on Board l." << endl;
        cout << "First enter the row (0 through 3): " << endl;
        cin >> row;
        cout << "Now enter the column (0 through 3): " << endl;
        cin >> col;
        assert(row >= 0 && row < 4 && col >= 0 && col < 4);
        if (board1[row][col] != 'y' && board1[row][col] != 'p')
        {
            // update board2
            updateBoard2(board1[row][col], board2, player2);
            board1[row][col] = player2;
        }

        if(i > 5)
        {
            //check if game is over
            if((checkForWinner(board1, player2) == 'p') 
                || (checkForWinner(board2, player2) == 'p'))
            {
                cout << "purple is the winner" << endl;
                displayBoard(board1);
                displayBoard(board2);
                break;
            }
        }
    }
    cout << endl;
    cout << "The game is a draw!" << endl;

}

void displayBoard(char board[4][4])
{
    for(int r = 0; r<4; r++) // r for array row
    {
        for(int c = 0; c < 4; c++) // c for array column
        {
            cout << board[r][c] << ' ';
        }
        cout << endl;
    }
}

void updateBoard2(char board1Char, char board[4][4], char player)
{
    for(int r = 0; r<4; r++) // r for array row
    {
        for(int c = 0; c < 4; c++) // c for array column
        {
            if(board1Char == board[r][c])
            {
                board[r][c] = player;
                break;
            }
        }
    }
}

char checkForWinner(char board[4][4], char c)
{
    char winner = '-'; // '-' indicates there's no winner yet
    if((board[0][0] ==c && board[0][1] ==c && board[0][2] ==c && board[0][3] ==c)
        || (board[1][0] ==c && board[1][1] ==c && board[1][2] ==c && board[1][3] ==c)
        || (board[2][0] ==c && board[2][1] ==c && board[2][2] ==c && board[2][3] ==c)
        || (board[3][0] ==c && board[3][1] ==c && board[3][2] ==c && board[3][3] ==c)
        || (board[0][0] ==c && board[1][0] ==c && board[2][0] ==c && board[3][0] ==c)
        || (board[0][1] ==c && board[1][1] ==c && board[2][1] ==c && board[3][1] ==c)
        || (board[0][2] ==c && board[1][2] ==c && board[2][2] ==c && board[3][2] ==c)
        || (board[0][3] ==c && board[1][3] ==c && board[2][3] ==c && board[3][3] ==c)
        || (board[0][0] ==c && board[1][1] ==c && board[2][2] ==c && board[3][3] ==c)
        || (board[3][3] ==c && board[2][2] ==c && board[1][1] ==c && board[0][0] ==c))
    {
    winner = c;
    }
    return winner;      
}
 
0
 

One problem is that you wait too long to start checking for a winner. After the players make their fourth move, a win is possible. So your test should be

if( i >= 3 )  //four moves have been made.

Also, your last two tests are the same, checking the upper left to lower right diagonal. So you need to change one to

(board[0][3] ==c && board[1][2] ==c && board[2][1] ==c && board[3][0] ==c))
 
0
 

Here's a descriptive source code for Tic Tac Toe game in C, the source code is downloadable too, free to use See from TheComputerStudents.com/game

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article