Does anyone mind giving this a look and help me spot my logic error? I am able to compile and play a tic tac toe game, however the winning condition isn't checked nor is the DRAW condition. I tried running this through Python Tutor to visualize but my code is too long? Anyway...
#include<iostream>
#include "TicTacToe.hpp"
using std::cout;
using std::cin;
using std::endl;
TicTacToe::TicTacToe(char player1)
{
player = player1;
}
void TicTacToe::play()
{
int gameState = newGame.gameState(); //int to hold relevant enum state
int row;
int col;
do //loop that won't stop if gameState = UNFINISHED
{
cout << "Player " << player <<
": Enter move as X then Y coordinates." << endl;
newGame.print(); //display the board to the screen
cin >> row >> col; //read player's input coordinates
cout << endl;
if (newGame.makeMove(row, col, player))
{
//returns current enum state based on new values input
gameState = newGame.gameState();
//if/else statement to swap players
if (player == 'x')
{
player = 'o';
}
else
player = 'x';
}
else
{
//let the player know that this space is taken
cout << "That space is occupied, try another space. \n"
<< endl;
}
}
while (gameState == UNFINISHED);
if (gameState == X_WON)
{
newGame.print();
cout << "X won this game." << endl;
}
elseif (gameState == O_WON)
{
newGame.print();
cout << "O won this game." << endl;
}
elseif (gameState == DRAW)
{
newGame.print();
cout << "Bummer, this game is a draw..." << endl;
}
}
int main()
{
char player;
cout << "Which player would like to go first, X or O?" << endl;
cin >> player;
if (player == 'x' || player == 'X' || player == 'o' || player == 'O')
{
TicTacToe newGame(player);
newGame.play();
}
return 0;
}
I guess that the variable winner (line 36/40) is a global variable.
Line 9/15/23/28 you create local variables with the same name. They shadow the global variable of the same name and thus do not change the content of winner used on line 36/40.
Remove the type (char) on line 9/15/23/28 so that the global winner is modified.
I included all of my code for full visibility. I'll change the local variables, but shouldn't the counter be incrementing as well which would eventually get to the DRAW condition?
I included all of my code for full visibility. I'll change the local variables, but shouldn't the counter be incrementing as well which would eventually get to the DRAW condition?
The turn member of Board is not initialized to any specific value, so it may be any value to begin with, and it make take quite a few increments to eventually get to the DRAW condition.
Creating a local variable in the constructor with the same name as the member and setting it to a value does not affect the member variable, much like hitting a person name John Smith in the face doesn't give some other John Smith a bloody nose.
Mentioning the name of what you referred to as "the counter" would've been useful.
I've rewritten my code because I was trying to make too many changes to the existing code and was getting myself too confused, I've included it below but I now can't compile, I'm getting an error that I'm illegally declaring a function definition on line 119 and I need a } on line 132 of board.cpp, but I do have those there??? And I'm not declaring a function? Only board.h and board.cpp have been modified which is why I'm not including TTT.h or TTT.cpp
***Ignore that, I was missing an } in a previous definition. I'm leaving this post up in case others come along having a similar problem*** I can delete instead if that is what mods prefer.
board.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef BOARD_HPP
#define BOARD_HPP
enum gameState {X_WON, O_WON, DRAW, UNFINISHED};
class Board
{
private:
char gameBoard[3][3];
int turn;
public:
Board();
bool makeMove(int, int, char);
int gameState();
void print();
};
#endif