Trouble with Tic-Tac-Toe

I have been trying to make a tic-tac-toe game. So far, I have it display the board and place pieces. The problem is that I'm having trouble with placing pieces, as it will only place them in the top right corner, and it won't place them in any of the other corners.

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <SDL.h>
#include <SDL_Image.h>

SDL_Surface* load_image(std::string filename, bool maketransparent = false)
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Log the image loading
        std::cout << "Successfully loaded: " << filename << "\n";

        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );

        //If the image was optimized just fine and the user specifies transparency
        if( optimizedImage != NULL && maketransparent == true )
        {
            //Log the optimization
            std::cout << "Generated optimized image for: " << filename << "\n";

            //Map the color key
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xED, 0x0B, 0xB5 );

            //Set all pixels of color R 0xED, G 0x0B, B 0xB5 to be transparent
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );

            //Log the image transparency
            std::cout << "Color-keyed: " << filename << "\n";
        }
        else
        {
            //Log the generated image
            std::cout << "Generated optimized image for: " << filename << ", no color key\n";
        }
    }
    else
    {
        std::cerr << "Failed to load: " << filename;
    }

    //Return the optimized image
    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;

    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;

    //Blit the surface
    SDL_BlitSurface( source, nullptr, destination, &offset );
}

//Board states
enum class BoardPieces
{
    Board_Empty,
    Board_X,
    Board_O,
    Board_Error
};

//Player enumaration
enum class PlayerTurn
{
    Player_One,
    Player_Two
};

int main ( int argc, char* argv[] )
{
    //Surfaces
    SDL_Surface* main_screen = nullptr;
    SDL_Surface* background_image = nullptr;
    SDL_Surface* x_image = nullptr;
    SDL_Surface* o_image = nullptr;

    //Events
    SDL_Event event_var;

    //Constants
    const short screen_width = 600;
    const short screen_height = 600;
    const short screen_depth = 32;
    const short board_width = 3;
    const short board_height = 3;

    //The game board
    BoardPieces game_board[board_height][board_width] = {{BoardPieces::Board_Empty, BoardPieces::Board_Empty, BoardPieces::Board_Empty},
                                                         {BoardPieces::Board_Empty, BoardPieces::Board_Empty, BoardPieces::Board_Empty},
                                                         {BoardPieces::Board_Empty, BoardPieces::Board_Empty, BoardPieces::Board_Empty}};

    //Game spaces
    SDL_Rect board_rectangles[board_height][board_width] = {{1, 2, 3},
                                                            {4, 5, 6},
                                                            {7, 8, 9}};
    for(int i = 0; i < board_height; i++)
    {
        for(int j = 0; j < board_width; j++)
        {
            board_rectangles[i][j].x = i*200;
            board_rectangles[i][j].y = j*200;
            board_rectangles[i][j].h = 200;
            board_rectangles[i][j].w = 200;

            std::cout << "x: " << board_rectangles[i][j].x << "\n";
            std::cout << "y: " << board_rectangles[i][j].y << "\n";
            std::cout << "h: " << board_rectangles[i][j].h << "\n";
            std::cout << "w: " << board_rectangles[i][j].w << "\n";
            std::cout << "\n";
        }
    }

    //Other variables
    bool quit_test = false;
    PlayerTurn current_player = PlayerTurn::Player_One;
    int mouse_x = 0;
    int mouse_y = 0;

    //Start SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        return 1;
    }

    //Set the screen and initialize images
    main_screen = SDL_SetVideoMode(screen_width, screen_height, screen_depth, SDL_SWSURFACE);
    background_image = load_image("Background.png");
    x_image = load_image("X-image.png", true);
    o_image = load_image("O-image.png", true);

    //Set background
    for(int i = 0; i < 3; i ++)
    {
        for(int j = 0; j < 3; j ++)
        {
            apply_surface(i*200, j*200, background_image, main_screen);
        }
    }
    if(SDL_Flip(main_screen) != 0)
    {
        return 1;
        std::cerr << "Error flipping the screen at line " << __LINE__ << "\n";
    }

    //Main loop
    while(quit_test == false)
    {
        //Event handling
        while(SDL_PollEvent(&event_var))
        {
            if(event_var.type == SDL_QUIT)
            {
                quit_test = true;
            }
            //Check for a mouse button
            if(event_var.type == SDL_MOUSEBUTTONDOWN)
            {
                if(event_var.button.button == SDL_BUTTON_LEFT)
                {
                    mouse_x = event_var.button.x;
                    mouse_y = event_var.button.y;
                }
            }
        }
        //Logic handling
        for(int i = 0; i < 3; i ++)
        {
            for(int j = 0; j < board_width; j++)
            {
                if(current_player == PlayerTurn::Player_One)
                {
                    if(mouse_x > board_rectangles[i][j].x && mouse_x < board_rectangles[i][j].w && mouse_y > board_rectangles[i][j].y && mouse_y < board_rectangles[i][j].h)
                    {
                        game_board[i][j] = BoardPieces::Board_X;
                        current_player = PlayerTurn::Player_Two;
                        mouse_x = 0;
                        mouse_y = 0;

                        apply_surface(i*200, j*200, background_image, main_screen);
                        apply_surface(i*200, j*200, x_image, main_screen);
                        if(SDL_Flip(main_screen) != 0)
                        {
                            std::cerr << "Error flipping the screen at line " << __LINE__ << "\n";
                        }
                    }
                }
                if(current_player == PlayerTurn::Player_Two)
                {
                    if(mouse_x > board_rectangles[i][j].x && mouse_x < board_rectangles[i][j].w && mouse_y > board_rectangles[i][j].y && mouse_y < board_rectangles[i][j].h)
                    {
                        game_board[i][j] = BoardPieces::Board_O;
                        current_player = PlayerTurn::Player_One;
                        mouse_x = 0;
                        mouse_y = 0;

                        apply_surface(i*200, j*200, background_image, main_screen);
                        apply_surface(i*200, j*200, o_image, main_screen);
                        if(SDL_Flip(main_screen) != 0)
                        {
                            std::cerr << "Error flipping the screen at line " << __LINE__ << "\n";
                        }
                    }
                }
            }
        }
    }

    //Free all memory and quit
    SDL_FreeSurface(main_screen);
    SDL_FreeSurface(background_image);
    SDL_FreeSurface(x_image);
    SDL_FreeSurface(o_image);
    SDL_Quit();
    return 0;
}


It prints out the values of the rectangles as I would expect them to, but it won't change any besides the top right. I know I'm not checking to see if a piece has already been placed, but I'm going to after I get it placing pieces in the other spaces.
Last edited on
mouse_x > board_rectangles[i][j].x && mouse_x < board_rectangles[i][j].w

You are treating the width (w) as if it were the right side. mouse_x would have to be < x+w, not < w. Same with the height.
Last edited on
That worked. Thanks!
Topic archived. No new replies allowed.