C++ 和 SDL 井字游戏 - 改变轮流

标签 c++ sdl tic-tac-toe

我在游戏中改变玩家之间的回合时遇到问题。当我运行它时,它似乎按任何顺序进行。因此,显示轮到谁的文本被弄乱了。

            #include <iostream>
            #include "SDLSetup.h"
            using namespace std;

            int p = 1;

            void Player1()
            {
                Player = TTF_RenderText_Solid(font, "Player 1", textColor);
                apply_surface(0, 630, Player, screen);
                SDL_Flip(screen);
                p = 2;
            }

            void Player2()
            {
                Player = TTF_RenderText_Solid(font, "Player 2", textColor);
                apply_surface(0, 630, Player, screen);
                SDL_Flip(screen);
                p = 1;
            }

            int main(int argc, char* args[])
            {
                bool quit = false;

if(init() == false)
    return 1;

if(load_files() == false)
    return 1;

apply_surface(0, 0, board, screen);

if(SDL_Flip(screen) == -1)
            return 1;
turn:

do{
    if(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
            quit=true;
    }

        if(event.type == SDL_MOUSEBUTTONDOWN)
                {
                    if(event.button.button == SDL_BUTTON_LEFT)
                    {
                        //Top Left
                        if(event.button.x < 175 && event.button.y < 175)
                        {
                            if(p == 1)
                            {
                                apply_surface(45, 40, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(45, 40, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Top Middle
                        else if(event.button.x > 175 && event.button.x < 375 && event.button.y < 175)
                        {
                            if(p == 1)
                            {
                                apply_surface(250, 40, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(250, 40, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Top Right
                        else if(event.button.x > 375 && event.button.x < 600 && event.button.y < 175)
                        {
                            if(p == 1)
                            {
                                apply_surface(450, 40, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(450, 40, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Middle Left
                        else if(event.button.x < 175 && event.button.y > 175 && event.button.y < 380)
                        {
                            if(p == 1)
                            {
                                apply_surface(45, 240, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(45, 235, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Center
                        else if(event.button.x > 175 && event.button.x < 375 && event.button.y > 175 && event.button.y < 380)
                        {
                            if(p == 1)
                            {
                                apply_surface(250, 235, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(250, 235, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Middle Right
                        else if(event.button.x > 375 && event.button.x < 600 && event.button.y >175 && event.button.y < 380)
                        {
                            if(p == 1)
                            {
                                apply_surface(450, 235, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(450, 235, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Bottom Left
                        else if(event.button.x < 175 && event.button.y > 380 && event.button.y < 600)
                        {
                            if(p == 1)
                            {
                                apply_surface(45, 450, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(45, 450, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Bottom Middle
                        else if(event.button.x > 175 && event.button.x < 375 && event.button.y > 380 && event.button.y < 600)
                        {
                            if(p == 1)
                            {
                                apply_surface(250, 450, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(250, 450, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }
                        //Bottom Right
                        else if(event.button.x > 375 && event.button.x < 600 && event.button.y > 380 && event.button.y < 600)
                        {
                            if(p == 1)
                            {
                                apply_surface(450, 450, X, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                            else if(p == 2)
                            {
                                apply_surface(450, 450, O, screen);
                                SDL_Flip(screen);
                                goto turn;
                            }
                        }

                    }
                }

        if(p == 1)
            Player1();
        else
            Player2();

} while(quit == false);




clean_up();

return 0;
            }

最佳答案

邪恶的 goto turn; 导致了这个问题。切换播放器的代码可能不会执行。 在您的 C++ 教科书中,查找关键字 continuebreak,因为它们适用于您的 do-while 循环。

例如,您可以替换

goto turn;

continue;

一些建议:

  1. 先确定棋盘方格,无其他代码。
  2. 可能是每个棋盘位置的 switch 语句(例如 0 .. 8)。

精简和适当的缩进会告诉你为什么切换播放器时函数底部有问题。

使用调试器可以轻松解决此问题。

关于C++ 和 SDL 井字游戏 - 改变轮流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8044225/

相关文章:

c++ - 如何制作 Web 浏览器工具栏?

c++ - 二叉树的前序遍历可视化

c++ - std::string' 没有名为 'pop_back' 的成员

java - 下面代码的算法名称是什么?

java - Java 中的 2D 数组 TicTacToe 游戏..验证问题

c++ - 使用 C++ 操作 protobuf 中的数据结构

c++ - 像 JAXB 一样在 QT 中生成 XML

c++ - 使用 OpenGL 风扇而不是根据不同的位置创建圆有什么好处?

c++ - 将透明的 .PNG 图像传输到屏幕上

lisp - 用 lisp 实现的井字游戏是完成游戏而不是一步一步