0

tictactoe.h

#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <time.h>
using namespace std;

class tictactoe
{
private:
    int move = 0;
    string dummy;
    char currentMove = 'X';
    char player = 'O'; //x or y

    char board[9] = { ' ',' ', ' ',' ', ' ', ' ',' ',' ',' ' };

public:

    void displayMenu() {

        cout << "Tic Tac Toe" << endl;
        cout << "-----------\n" << endl;
        cout << "Objective: make 3 marks, 'X' or 'O', in a row on a 3 x 3 grid.\n" << endl;
        cout << "To place marks on the the grid, enter a number 1-9, 1 being the top left corner, and 9 being the bottom right." << endl;

        cout << "Press enter to continue: ";
        getline(cin, dummy);
    };

    void displayBoard(char b[9]) {

        cout << endl;
        cout << " " << board[0] << " | " << board[1] << " | " << board[2] << " " << endl;
        cout << "---+---+---" << endl;
        cout << " " << board[3] << " | " << board[4] << " | " << board[5] << " " << endl;
        cout << "---+---+---" << endl;
        cout << " " << board[6] << " | " << board[7] << " | " << board[8] << " " << endl;

    };

    char determinePlayerMark() {
        char mark;

        srand(time(NULL));
        int num = rand() % 2;

        if (num == 1)
            mark = 'X';
        else
            mark = 'O';

        cout << "\nYou are " << mark << "!" << endl;
        cout << "------------\n";

        return mark;
    };

    char playerMove(char p, char c) {
        player = p;
        currentMove = c;

        currentMove = player;

        cout << "Select a box to make your move [1-9]: ";
        cin >> move;

        if (board[move - 1] != ' ') {

            cout << "This box is occupied!\n";
            playerMove(player, currentMove);
        }
        else {

            board[move - 1] = player;
        }
        checkWin();
        return currentMove;
    };

    char computerMove(int c) {

        char computer;
        if (player == 'X')
            computer = 'O';
        else
            computer = 'X';

        currentMove = c;
        currentMove = computer;


        // Check for winning moves for the computer
        for (int i = 0; i < 9; ++i) {
            if (board[i] != 'X' && board[i] != 'O') {
                board[i] = computer;
                if (checkWin()) {
                    return currentMove;
                }
                board[i] = ' ';
            }
        }

        // Check for winning moves for the player and block them
        for (int i = 0; i < 9; ++i) {
            if (board[i] != 'X' && board[i] != 'O') {
                board[i] = player;
                if (checkWin()) {
                    board[i] = computer;
                    return currentMove;
                }
                board[i] = ' ';
            }
        }

        // Choose a random move
        int move;
        move = rand() % 9;

        if (board[move - 1] == 'X' || board[move - 1] == 'O') {
            computerMove(computer);
        }
        else {
        board[move - 1] = computer;
        }
        return currentMove;
    }


    bool checkWin() {

        if ((board[0] == board[1] && board[1] == board[2]) && board[0] != ' ') {
            return true;
        }
        if ((board[3] == board[4] && board[4] == board[5]) && board[3] != ' ') {
            return true;
        }
        if ((board[6] == board[7] && board[7] == board[8]) && board[6] != ' ') {
            return true;
        }
        if ((board[0] == board[3] && board[3] == board[6]) && board[0] != ' ') {
            return true;
        }
        if ((board[1] == board[4] && board[4] == board[7]) && board[1] != ' ') {
            return true;
        }
        if ((board[2] == board[5] && board[5] == board[8]) && board[2] != ' ') {
            return true;
        }
        if ((board[0] == board[4] && board[4] == board[8]) && board[0] != ' ') {
            return true;
        }
        if ((board[6] == board[4] && board[4] == board[2]) && board[6] != ' ') {
            return true;
        }
        else
            return false;
    };

    bool checkTie() {

        for (int i = 0; i < 9; i++) {
            if (board[i] = ' ')
                return false;
        }
        return true;
    };
};

tictactoe.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include "tictactoe.h"
using namespace std;

int main() {

    tictactoe game;

    char board[9];
    char currentMove = 'X';

    game.displayMenu();
    char player = game.determinePlayerMark();

    while (!game.checkWin() || !game.checkTie()) {
        if (player == 'X') {
            game.displayBoard(board);
            game.playerMove(player, currentMove);
            if (game.checkWin() || game.checkTie())
                break;
            game.computerMove(currentMove);
        }
        else if (player == 'O') {
            game.computerMove(currentMove);
            if (game.checkWin() || game.checkTie())
                break;
            game.displayBoard(board);
            game.playerMove(player, currentMove);
        }
    }
    game.displayBoard(board);
    if (game.checkWin()) {
        cout << currentMove << " wins!" << endl;
    }
    else if (game.checkTie()) {
        cout << "It is a tie!" << endl;
    }


    cout << "Thank you for playing Tic Tac Toe!";   

    return 0;
}

Whenever I enter "1" in the console, the program is supposed to print an X or O in the top left square in the tic tac toe grid, but it does not change and skips to the next line of code.

It is also supposed to check if a player or the computer has won, but it does not register that the computer has won even though the checkWin() function is for both X and O.

Here is the code for the project:

I have tried changing the function type from char to void and getting rid of some extra parameters like the "currentMove" parameter that is used for determining which symbol (X or O) won, but nothing changed.

I dont understand why only the top left corner does not print, since every other square is fine.

lyanzu
  • 1
  • 1
    Have you tried stepping through the code with a debugger? – Quimby May 17 '23 at 07:18
  • i am using windows visual studio 2022; ive tried using the local windows debugger – lyanzu May 17 '23 at 07:21
  • im not quite sure how to debug – lyanzu May 17 '23 at 07:22
  • 1
    `void displayBoard(char b[9])` this method looks fishy. I has a parameter `b` that is not used. Instead it uses the member `board` – 463035818_is_not_an_ai May 17 '23 at 07:26
  • Two unrelated things: Don't use member variables for things that should be local variables inside functions. Secondly `srand` should be called *once* at the start of your program, not any more. – Some programmer dude May 17 '23 at 07:26
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – 463035818_is_not_an_ai May 17 '23 at 07:26
  • you have a `board` in `main` and one as member. Sometimes you use one sometimes the other. Get rid of one of them – 463035818_is_not_an_ai May 17 '23 at 07:28
  • 1
    @AndrewLiu *im not quite sure how to debug* -- If you plan to be a programmer, you *must* know how to debug the programs that you write. It isn't optional, it is mandatory that you know how to debug your code. – PaulMcKenzie May 17 '23 at 07:28
  • 1
    `if (board[i] = ' ')` should be `if (board[i] == ' ')` – mch May 17 '23 at 07:37
  • @AndrewLiu That's alright, but debugging is really really important and useful skill to learn. Visual studio has a powerful debugger which allows you to step through the code line by line, inspect the values of all variables, set breakpoints...there's plenty of tutorials on the internet regarding this. – Quimby May 17 '23 at 07:50
  • The solution here really is to debug the code - that's what someone would do to find the problem in this code. – lionkor May 17 '23 at 14:24

1 Answers1

0

It might be due to the check condition, the condition is resulting false.

You can check this project for any further assistance: https://gigadevden.com/project-page/4/C-Tic-Tac-Toe-Console-Based

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 03 '23 at 09:19
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34627665) – Codemaker2015 Jul 05 '23 at 11:44