Tic-Tac-Toe Challenge Project (C++)

Here’s my project:

I have had help from ChatGpt to correct the logic errors.

i spent the entrety of my day friday and saturday trying to understand how to get this to work.
fixing all kinds of errors and i finally did it. without help. im excited to share this with others because i never been so happy to finish any project ever.
ive noticed other projects uses different types of functions and syntax than mine, i only know and use what codecademy has taught me

Hey guys!

This is my second project here, after the Text Adventure one.
I have also seen that a lot people write a lot of different types of code, I have only use what I learned on Codecademy until now. I haven’t managed to make it restart, but everything else works, including not letting the player write over another player’s symbol.
It does however go in an infinite loop if you write anything other than a number from 1 to 9 when playing haha. Looking forward to suggestions!

I wrote mine to keep the score and iterate to the next game if the user inputs that they wish to continue playing. The game also switches who is X and who is O every round to keep the game fair.

Here’s my solution. Please let me know how it could be improved! Hopefully this is of help to someone.

hope you like it

This was a challenge (and I had to learn not to try and change a vector from within a function, something which still messes with my brain even now)

https://github.com/esapuolakka/tic_tac_toe.git

ttt.cpp

#include
#include
#include “ttt.hpp”

using namespace std;

int main()
{

bool names = true;

bool game;

// Player input
int cell;

// Player points & names
int x_points = 0;
int o_points = 0;

// The turns of the game (total 9 turns)
int turn;

string player_x;
string player_o;

welcome();

do {
cout << “player X, write your name: \n”;
getline(cin, player_x);

cout << "player O, write your name: \n";
getline(cin, player_o);

} while (player_x == “” || player_o == “”);

while(true)
{
turn = 1;

// Player turn
char current_player = ‘X’;

// Reset the board
char board = {‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’};

// Player choice the play or not
char play;

cout << “\nStart the game? (press ‘y’ to play, any else to quit)\n”;
cin >> play;

play = toupper(play);

if (play == ‘Y’)
{
game = true;
}
else
{
return 0;
}

bool single_game = true;

while (single_game)
{
show_board(board);


// Input from the current player
cout << "\nPlayer " << current_player << ". Choose a cell by entering a number (1-9): \n";
cin >> cell;


// Convert input to 0-based index
cell--;


// Check is a chosen cell empty
if (board[cell] == 'X' || board[cell] == 'O')
{
    cout << "The cell is already taken. Choose another one!\n";
    continue;
}


// Update the board
board[cell] = current_player;


// Win check
if (check_win(board, current_player, x_points, o_points))
{
    show_board(board);
    cout << endl;
    cout << "Player " << current_player << " Won!\n";
    cout << endl;

    single_game = false;
}

// Tie check
if (turn == 9)
{
    cout << endl;
    cout << "The game is tie!\n";
    
    single_game = false;
}

// Switch player
if (current_player == 'X')
{
    current_player = 'O';
}
else if (current_player == 'O')
{
    current_player = 'X';
}

    turn++;
}

cout << "Player X: " << player_x << " points: " << x_points;
cout << "\t\tPlayer O: " << player_o << " points: " << o_points << "\n";
cout << endl;

}

cout << “Thank you for playing!\n”;
cout << endl;

return 0;
}

ttt.hpp

void welcome();
void show_board(char board);
bool check_win(char board, char current_player, int& x_points, int& o_points);

ttt_functions

#include

using namespace std;

void welcome()
{
cout << endl;
cout << “============================\n”;
cout << “===== TIC TAC TOE Game =====\n”;
cout << “============================\n”;
cout << endl;
}

void show_board(char board)
{
cout << board[0] << “|” <<board[1] << “|” << board[2] << endl;
cout << board[3] << “|” <<board[4] << “|” << board[5] << endl;
cout << board[6] << “|” <<board[7] << “|” << board[8] << endl;
}

bool check_win(char board, char current_player, int& x_points, int& o_points)
{
// vertical check
for (int i = 0; i < 3; i++)
{
if (board[i] == current_player && board[i+3] == current_player && board[i+6] == current_player)
{
if (current_player == ‘X’)
{
x_points++;
}
else
{
o_points++;
}
return true;

    }
}
// horizontal check
for (int i = 0; i < 9; i += 3)
{
    if (board[i] == current_player && board[i+1] == current_player && board [i+2] == current_player)
    {
        if (current_player == 'X')
        {
            x_points++;
        }
        else
        {
            o_points++;
        }
        return true;
    }
}
// diagonal check
for (int i = 0; i < 9; i++)
{
    if (board[0] == current_player && board[4] == current_player && board[8] == current_player)
    {
        if (current_player == 'X')
        {
            x_points++;
        }
        else
        {
            o_points++;
        }
        return true;
    }
    else if (board[2] == current_player && board[4] == current_player && board[6] == current_player)
    {
        if (current_player == 'X')
        {
            x_points++;
        }
        else
        {
            o_points++;
        }
        return true;
    }
}
return false;

}

Here is an implementation using the built-in find function in the winner criterion:

Let me know what you think guys.

Here’s my solution. Would love to hear any feedback! Thanks!

Main Code
Header File
Functions