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

Tic Tac Toe game

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

Problem

I don't have any questions about this piece of code; it compiles just fine in Visual Studio 2010. I just wanted to post it to see if anyone has any good ideas on how to simplify it, or pointing out any bad practices I have have done.

```
// Tic Tac Toe Game
#include
#include
using namespace std;

class TicTacToe
{
private:
char Board[3][3];
int play();
void playermove();
int turn(int);
int check (int, int);
void boardProgress(char x[][3]);
int Winner(char x[][3]);
int player1, player2, draw, player,winner,done;
int row, col;

public:
int TG, T1, T2, TD;
void intBoard();
int Stats(int);

char playAgain(char);
}playBall;

int main()
{
playBall.TG = 0,playBall.T1 = 0,playBall.T2 = 0,playBall.TD = 0;
int Winner = 0;
// This will only display when the program is first run
cout > row;
row--;
cout > col;
col--;
answer = check(row,col);
}
player = turn(player);
if (player == 1)
Board[row][col] = 'X';
else if (player == 2)
Board[row][col] = 'O';
else
cout 0)
{
int row, col, r, c, d, ro, co, dO;
for (row=0; row 0)
{
done = true;
}
return done;
}

int TicTacToe::Stats(int winner)
{
playBall.TG++;
switch(winner)
{
case 1: T1++;
{
break;
}
case 2: T2++;
{
break;
}
case 3: TD++;
{
break;
}
}
cout> done;
if(done == 'Y' || done == 'y')
{
done = false;
}
else
{
done = true;
}
return done;
}
void TicTacToe::boardProgress(char x[][3])
{
cout << " | | \n";
cout << " " << x[0][0] << " | " << x[0][1] << " | " << x[0][2] <<" \n";
cout << " _____|_____|_____\n";
cout << " | | \n";
cout << " " << x[1][0] << " | " << x[1][1] << " | " << x[1][2] <<" \n";
cout << " _____|_____|_____\n";

Solution

Well for one, you've got some unreachable code:

if(Board[row][col] == 0)
{
    return true;
}
else if (Board[row][col] != 0)
{
    cout <<"Can go there, Dude!\n\n";
    return false;
}
else
{
    // The cell is neither 0 nor non-0!!!
    // cout << "What the hell, man?";
    cout << "Going through Check";
    Winner(Board);
}
return false;


Since you're using C++, you should create a Board class that houses the cells and the win check routine. Your class should look like this:

enum Piece {NONE, X, O};

public class Board {
private:
    Piece cells[3][3];

public:
    void put(int x, int y, Piece piece);
    Piece get(int x, int y);
    Piece checkWin();
    void clear();
}

Code Snippets

if(Board[row][col] == 0)
{
    return true;
}
else if (Board[row][col] != 0)
{
    cout <<"Can go there, Dude!\n\n";
    return false;
}
else
{
    // The cell is neither 0 nor non-0!!!
    // cout << "What the hell, man?";
    cout << "Going through Check";
    Winner(Board);
}
return false;
enum Piece {NONE, X, O};

public class Board {
private:
    Piece cells[3][3];

public:
    void put(int x, int y, Piece piece);
    Piece get(int x, int y);
    Piece checkWin();
    void clear();
}

Context

StackExchange Code Review Q#2032, answer score: 11

Revisions (0)

No revisions yet.