2011-11-07 72 views
0

我遇到了遊戲中玩家間轉換的問題。當我運行它時,它似乎以任何順序進行。正因爲如此,顯示輪到誰的文字被搞砸了。C++&SDL TicTacToe - 更改圈數

  #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; 
      } 

回答

1

邪惡goto turn;是造成這個問題。切換播放器的代碼可能無法執行。 在您的C++教科書中,查找關鍵字continuebreak,因爲它們適用於您的do-while循環。

例如,您可以用

continue; 

一些建議更換

goto turn; 

  1. 確定板上方第一,沒有其他的代碼。
  2. 也許一個switch聲明爲每個委員會職位(例如0..8)。

簡化和正確縮進將告訴你爲什麼切換播放器時在函數底部存在問題。

使用調試器可以輕鬆解決此問題。