2
\$\begingroup\$

I am doing C++ for over a year and this is my game I tried making tic-tac-toe. And it's also my first program that uses multiple source files.

main.cpp

#include"Header.h"
#include"GUI.h"
#include"UI.h"

int main()
{
    int* board = new int[9] {0, 0, 0, 0, 0, 0, 0, 0, 0};
     // 1 2 3
     // 4 5 6
     // 7 8 9
    int place{ 0 };  //   /\
                     //   ||  -1
    bool turn{ false }; // true = O, false = X
    bool win{ false }; // flase = no win, true = win
    int count{ 0 }; // counts ammount of moves

    

    while (!win && count < 9)
    {
        place = question(board) - 1;
        system("cls");
        turn = change_board(board, turn, place);
        switch (check_win(board))
        {
        case 0:
            break;
        case 1:
            system("cls");
            print(board);
            change_color(3, 0);
            cout << "\nPlayer Two won\n";
            win = !win;
            break;
        case 2:
            system("cls");
            print(board);
            change_color(4, 0);
            cout << "\nPlayer one won\n";
            win = !win;
            break;
        }
        count++;
    }
    if (!win)
    {
        system("cls");
        print(board);
        change_color(2, 0);
        cout << "\nIt's a draw\n";
    }
    change_color(7, 0);
    return 0;
}

Header.h

using namespace std;
#include <iostream>
#include<windows.h>

GUI.h

void change_color(int text, int background);
void print(int* board);
int question(int* board);

GUI.cpp

#include"Header.h"
char symbols[3]{ ' ', 'X', 'O' };



void change_color(int text, int background)
{
    HANDLE  hConsole;
    int color{ text + 16 * background };

    //0 = Black 8 = Gray
    //1 = Blue 9 = Light Blue
    //2 = Green a = Light Green
    //3 = Aqua b = Light Aqua
    //4 = Red c = Light Red
    //5 = Purple d = Light Purple
    //6 = Yellow e = Light Yellow
    //7 = White f = Bright White

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, color);
}

void color_print(char symbol)
{
    switch (symbol)
    {
    case 'X':
        change_color(4, 0);
        cout << symbol;
        change_color(7, 0);
        break;
    case 'O':
        change_color(3, 0);
        cout << symbol;
        change_color(7, 0);
        break;
    default:
        cout << ' ';
        break;
    }
    return;
}
void color_print(int symbol, int place)
{
    switch (symbol)
    {
    case 1:
        change_color(4, 0);
        cout << 'X';
        change_color(7, 0);
        break;
    case 2:
        change_color(3, 0);
        cout << 'O';
        change_color(7, 0);
        break;
    default:
        change_color(2, 0);
        cout << place + 1;
        change_color(7, 0);
        break;
    }
    return;
}
void print(int* board)
{
    for (int column{ 0 }; column < 2; column++)
    {

        for (int row{ 0 };row<2;row++)
        {
            cout << "  "; color_print(symbols[board[3 * column + row]]); cout << "  |";
        }
        cout << "  "; color_print(symbols[board[3 * column + 2]]); cout << endl;
        cout << "-----------------\n";
    }
    for (int row{ 0 }; row < 2; row++)
    {
        cout << "  "; color_print(symbols[board[row + 6]]); cout << "  |";
    }
    cout << "  "; color_print(symbols[board[8]]); cout << endl;
    return;
}
void print_to_ask(int* board)
{
    for (int column{ 0 }; column < 2; column++)
    {
        for (int row{ 0 }; row < 2; row++)
        {
            cout << "  "; color_print(board[3 * column + row], 3 * column + row); cout << "  |";
        }
        cout << "  "; color_print(board[3 * column + 2], 3 * column + 2); cout << endl;
        cout << "-----------------\n";
    }
    for (int row{ 0 }; row < 2; row++)
    {
        cout << "  "; color_print(board[row + 6], row + 6); cout << "  |";
    }
    cout << "  "; color_print(board[8], 8); cout << endl;
    return;
    
}
int question(int* board)
{
    int place{ 0 };
    print_to_ask(board);
    cout << "\n\n   Please, input the place where you want to do your turn: "; cin >> place;
    if (place < 1 || place > 9 || board[place - 1] != 0)
    {
        system("cls");
        change_color(0, 4);
        cout << "Invalid option\nPlease retry\n\n";
        change_color(7, 0);
        place = question(board);
    }
    return place;
}

UI.h

bool change_board(int* board, bool turn, int place);
int check_win(int* board );

UI.cpp

#include"Header.h"
bool change_board(int* board, bool turn, int place)
{
    board[place] = int(turn) + 1;
    return !turn;
}
bool check_win_side(int* board, int side)
{
    for (int x{ 0 }; x < 3; x++)
    {
        if (board[x] == board[x + 3] && board[x] == board[x + 6] && board[x] == side)
            return true;
    }
    for (int x{ 0 }; x < 3; x++)
{
    if (board[x * 3] == board[x * 3 + 1] && board[x * 3] == board[x * 3 + 2] && board[x * 3] == side)
        return true;
}
    if ((board[0] == board[4] && board[0] == board[8] && board[0] && board[0] == side) || (board[2] == board[4] && board[2] == board[6] && board[2] == side))
    {
        return true;
    }
    return false;
}
int check_win(int*board)
{
    if (check_win_side(board, 1))
    {
        return 2;
    } else if (check_win_side(board, 2))
    {
        return 1;
    }
    return 0;
}
\$\endgroup\$
1

1 Answer 1

2
\$\begingroup\$

In your check_win function make sure to check if board[0] is not equal to zero before comparing it to side. If board[0] is not equal to zero, it means that this cell is not empty, and you need to check if it belongs to the side player before comparing it.

Consider the scenario:

board[0] contains 1, indicating that player one (X) has placed their symbol in the top-left cell.

Now you want to check if player one (X) has won. In this case, side would be 1 (representing player one).

If you didn't check board[0] before comparing it to side, the condition board[0] == side would evaluate to true because 1 (player one's symbol) is indeed equal to 1 (the side being checked). However, this would be incorrect because board[0] already contains a move by player one, and it doesn't necessarily mean they have won yet.

Aside from this, the code looks acceptable for test or interview material.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for you feedback, @TheLeb, I have fixed it now and it works. \$\endgroup\$ Oct 16 at 22:41
  • \$\begingroup\$ You're welcome. Good work on this. Please click the check ✔️ next to my answer to make it the accepted answer? \$\endgroup\$
    – TheLeb
    Oct 17 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.