HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Simple class-oriented Tic Tac Toe game in C++

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simpletoetictacgameorientedclass

Problem

This is the code for my Tic Tac Toe game. The player functions are coded in the class tictactoe and the rest of the functions, which include checking the board for completion, are global functions. Any tips, advice, or even corrections you guys have, please feel free to share them. I am almost brand-new to programming and I am very open to criticism. I want to learn as much as I can because I'm hoping to become a computer programmer in the future.

Do you agree that the references in bool isGameover() to arr[] should be deleted?

#include 
#include 

int game_start();
void instructions();
bool isGameover(char (&arr)[3][3]);
char d = '-';

int main()
{
  std::cout > input;

  switch(input){
    case 1:
      game_start();
      break;
    case 2:
      instructions();
      break;
    case 3:
      return 0;
      break;
    default:
      std::cout > uinput;
  tictactoe Player1;
  Player1.name = uinput;
  Player1.symbol = 'X';
  std::cout > uinput;
  tictactoe Player2;
  Player2.name = uinput;
  Player2.symbol = 'O';
  int turn = 1;
  char board[3][3] = {d, d, d, d, d, d, d, d, d};

  while (!isGameover(board)) ///main game loop
  {
    while (turn == 1)
    {
      Player1.update(board);
      std::cout > uinput;
      uinput.erase(uinput.find("("));/// gets rid of the symbols in a user's input
      uinput.erase(uinput.find(","));/// so it can be used in the play() function
      uinput.erase(uinput.find(")"));///
      if (!(Player1.play(board, uinput[0], uinput[1])))///checks to   make sure the move is valid
      {
        std::cout > uinput;
        uinput.erase(uinput.find("("));
        uinput.erase(uinput.find(","));
        uinput.erase(uinput.find(")"));
        if (!(Player2.play(board, uinput[0], uinput[1])))
        {
            std::cout > usr;
    switch(usr)
    {
        case 1:
            game_start();
            break;
        case 2:
            break;
        default:
            break;
    }
}

Solution

-
if (arry[x][y] = d) is most likely a typo. You probably meant

if (arry[x][y] == d)


-
The condition

(&arr)[0][0] == (&arr)[1][1] && (&arr)[0][0] == (&arr)[2][2]


does not depend on neither i nor j. There is no point of testing it 9 times.

-
More important, other tests are plain wrong. The code attempts to access elements like (&arr)[2][4], which is certainly not what you want. The only reason you are not getting undefined behaviour is that this code is never executed: isGameover always returns from the very first iteration.

-
An idiom

if (condition) {
    return true;
} else {
    return false;
}


is synonymous to much simpler

return condition;

Code Snippets

if (arry[x][y] == d)
(&arr)[0][0] == (&arr)[1][1] && (&arr)[0][0] == (&arr)[2][2]
if (condition) {
    return true;
} else {
    return false;
}
return condition;

Context

StackExchange Code Review Q#88268, answer score: 3

Revisions (0)

No revisions yet.