Simple tic tac toe

Hey, I got bored so wrote a simple tic tac toe game, feedback will be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <stdio.h>
#include <windows.h>

using namespace std;

enum TILE_OWNERS
{
    NO_OWNER = 0,
    X = 1,
    O = 2
};

class Tile
{
 public:
     Tile()
     {
         owner = NO_OWNER;
     }
     char getOwner()
     {
         return owner;
     }

     void setOwner(char _owner)
     {
         owner = _owner;
     }
 private:
        char owner;
};
class Board
{
private:
    Tile tile[9];
public:
    void Display()
    {
        for (int y = 0; y < 3; y++)
        {
            for (int x = 0; x < 3; x++)
            {
                if (getTileOwner(x, y) == NO_OWNER)
                    printf("#");
                else if(getTileOwner(x, y) == X)
                    printf("X");
                else if(getTileOwner(x, y) == O)
                    printf("O");
            }
            printf("\n");
        }
    }

    void setTile(char tileID, char owner)
    {
        tile[tileID-1].setOwner(owner);
    }

    char getTileOwner(char x, char y)
    {
        return tile[(y*3)+x].getOwner();
    }

    char getTileOwner(int tileID)
    {
        return tile[tileID].getOwner();
    }

    bool tileTaken(int tileID)
    {
        if (tile[tileID-1].getOwner() != NO_OWNER)
            return true;
        return false;
    }
};

class Game
{
 public:
    Board* board;
    bool isPlaying;

    Game()
    {
        isPlaying = false;
        curTurn = X;
    }

    virtual ~Game()
    {
        stop();
    }
    void start()
    {
        board = new Board();
        isPlaying = true;
    }

    void stop()
    {
        if (board != 0)
            delete board;
        board = 0;
        isPlaying = false;
    }

    char getPlayerFromCurTurn()
    {
        return curTurn;
    }

    bool hasBeenWon()
    {
        if (
           (board->getTileOwner(0) == curTurn && board->getTileOwner(1) == curTurn
            && board->getTileOwner(2) == curTurn) ||
            (board->getTileOwner(3) == curTurn && board->getTileOwner(4) == curTurn
            && board->getTileOwner(5) == curTurn) ||
            (board->getTileOwner(6) == curTurn && board->getTileOwner(7) == curTurn
            && board->getTileOwner(8) == curTurn) ||
            (board->getTileOwner(0) == curTurn && board->getTileOwner(3) == curTurn
            && board->getTileOwner(6) == curTurn) ||
            (board->getTileOwner(1) == curTurn && board->getTileOwner(4) == curTurn
            && board->getTileOwner(7) == curTurn) ||
            (board->getTileOwner(2) == curTurn && board->getTileOwner(5) == curTurn
            && board->getTileOwner(8) == curTurn) ||
            (board->getTileOwner(1) == curTurn && board->getTileOwner(4) == curTurn
            && board->getTileOwner(7) == curTurn) ||
            (board->getTileOwner(0) == curTurn && board->getTileOwner(4) == curTurn
            && board->getTileOwner(8) == curTurn) ||
            (board->getTileOwner(2) == curTurn && board->getTileOwner(4) == curTurn
            && board->getTileOwner(6) == curTurn)
           )
            return true;
        return false;
    }

    void endTurn()
    {
        if (curTurn == X)
            curTurn = O;
            else
                curTurn = X;
    }
 private:
    char curTurn;

};

Game game;

void checkWon()
{
    if (game.hasBeenWon())
    {
        cout << "Player: " << (int)game.getPlayerFromCurTurn() << " has won!" << endl;
        game.board->Display();
        game.isPlaying = false;
    }
}
int main()
{
    game.start();
    while(game.isPlaying)
    {
        int input;
        char curPlayer = game.getPlayerFromCurTurn();

        system("cls");
        cout << "Welcome to tic tac toe!" << endl;
        cout << "Player: " << (int)curPlayer << "s turn" << endl;
        game.board->Display();

        bool stillPlayersTurn;
        do
        {
            stillPlayersTurn = false;
            cout << "Enter a position [1-9]: ";
            scanf("%i", &input);
            if (input < 1 || input > 9)
            {
                cout << "Their are only tiles between [1-9] in tic tac toe..." << endl;
                stillPlayersTurn = true;
                continue;
            }
            if (!game.board->tileTaken(input))
            {
                game.board->setTile(input, curPlayer);
                checkWon();
                game.endTurn();
            }
            else
            {
                cout << "This tile is already taken try another.." << endl;
                stillPlayersTurn = true;
            }
        } while(stillPlayersTurn);
    }
    game.stop();
    system("PAUSE");
    return 0;
}
Last edited on
1) In several places you mix the TILE_OWNERS type with char variables (owner, cur_turn). X, O and NO_OWNER are binary values, but you assign and use these as char values. I would suggest changing the type of owner and cur_turn to TILE_OWNERS.

2) The logic at lines 44-49 should be moved to a Tile->Display method. Board should not know or care how Tiles are represented.

3) Why is Board allocated dynamically? There is no need to do this.

4) In Board, consider replacing getTileOwner by overloading the [] operator. This will make your code much cleaner by allowing you to refer to board[i].

5) line 157, 172: Why are you casting to an int?

6) Line 180: Whay are you using scanf instead of cin >> input;

7) You're mixing printf and cout. That will work, but if you're going to be writing in C++, then you should use cout and not printf.

8) Line 153: Why is checkWon not a method of Game?

9) Why are board and isplaying public inside Game?
Topic archived. No new replies allowed.