2016-03-25 28 views
-4

編輯:現在解決了謝謝你triple_r和AJNeufield你對我遇到的這個問題的幫助。不知道如何循環playerturns和移動井字遊戲(C + +)

我已經瀏覽過多個網站和YouTube的相關內容,我似乎無法找到任何關於我特別尋找的內容,因爲我的程序格式與其他版本有很大不同。因此,我很難破譯我需要的東西,而我需要的東西需要我知道。

請注意,我對C++相對來說比較新,所以我會很感謝您可能會提供給我的所有反饋或批評。

此外,請注意我的代碼確實編譯並運行它只是不允許我輸入多個輸入,並且可能不允許切換玩家輪流。

快速編輯:切換代碼與triple_r建議的新設置,但我似乎已經搞砸了沿線的某處,它編譯(除了X和Y沒有被利用和一個其他錯誤),但它總是以玩家2首先開始,一旦它接收到輸入,它會自動結束分段錯誤。

#include <iostream> 
#include <cstdlib> 
using namespace std; 
////////////////////////////////////////////////////////// 
void initboard(char board[3][3]) 
{ 
    int x,y; 
    for (x=0;x<3;x++) 
      for (y=0;y<3;y++) 
      board[x][y]=' '; 
    return; 
} 
////////////////////////////////////////////////////////// 
void printboard(char board[3][3]) 
{ 
    int x,y; 
    for (x=0;x<3;x++) 
    { 
     cout<<"\n"; 
     for (y=0;y<3;y++) 
     { 
      cout<<" "<<board[x][y]<<" "; 
      if (y<2) cout<<"|"; 
     } 
     if (x<2) cout<<"\n==========="; 
    } 
    return; 
} 
////////////////////////////////////////////////////////// 
void getmove(char board[3][3], int player) 
{ 
    return; 
} 
////////////////////////////////////////////////////////// 
int main() 
{ 
    bool done=false; 
    char board[3][3]; 
    int x,y,player=1,turn,playerchoice,playermark; 

    initboard(board); 
    turn=0; 
    do 
    { 
     if (player==1) 
      playermark='X'; 
     else 
      playermark='O'; 
     if (turn%2) 
      player=1; 
     else 
      player=2; 

     cout<<"Player "<<player<<" where do you want to move?: "; 
     cin>>playerchoice; 
     if (playerchoice==1) 
     { 
      board[0][0]=playermark; 
     } 
     else if (playerchoice==2) 
     { 
      board[0][1]=playermark; 
     } 
     else if (playerchoice==3) 
     { 
      board[0][2]=playermark; 
     } 
     else if (playerchoice==4) 
     { 
      board[1][0]=playermark; 
     } 
     else if (playerchoice==5) 
     { 
      board[1][1]=playermark; 
     } 
     else if (playerchoice==6) 
     { 
      board[1][2]=playermark; 
     } 
     else if (playerchoice==7) 
     { 
      board[2][0]=playermark; 
     } 
     else if (playerchoice==8) 
     { 
      board[2][1]=playermark; 
     } 
     else if (playerchoice==9) 
     { 
      board[2][2]=playermark; 
     } 
     else 
     { 
      cout<<"Invalid move "; 
     } 
     if (board[x][y]!=' ') 
      cout<<"Move is already taken."; 
     board[x][y]=playermark; 

     if(board[x][y]==' ') 
      turn++; 
    }while (!done); 
    void printboard(char board[3][3]); 
    return 0; 
} 

回答

0

編輯:根據更新後的代碼

所以,我能看到的第一件事情是,你正在使用你的程序xy但你不初始化它們或指定任何值,給他們。另外,嘗試使用函數/類/ ...讓你的代碼更具可讀性。你已經有了一個玩家移動的功能,但你沒有使用它。您可以移動該函數中的大if語句,這將使您的主代碼更短,更易讀。

這是我對你的程序的主要部分評論:

int main() 
{ 
    // add a new variable to see if the move was valid or not: 
    bool done=false, validmove = true; 
    char board[3][3]; 
    int x, y, player = 1, turn = 0, playerchoice, playermark; 

    initboard(board); 
    do 
    { 
     // swap the two `if`s so you decide who`s turn it is then assign the player mark, 
     // also, reverse the condition to make sure turn '0' is player 1's turn. 
     if (!(turn % 2)) 
      player = 1; 
     else 
      player = 2; 
     if (player == 1) 
      playermark = 'X'; 
     else 
      playermark = 'O'; 

     cout << "Player " << player << " where do you want to move?: "; 
     cin >> playerchoice; 

     // Assign `x` and `y` here instead of updating the board, because you want to make 
     // sure that the move is valid before putting the mark: 
     validmove = true; 
     if (playerchoice == 1) 
     { 
      x = 0; y = 0; 
     } 
     else if (playerchoice == 2) 
     { 
      x = 0; y = 1; 
     } 
     else if (playerchoice == 3) 
     { 
      x = 0; y = 2; 
     } 
     else if (playerchoice == 4) 
     { 
      x = 1; y = 0; 
     } 
     else if (playerchoice == 5) 
     { 
      x = 1; y = 1; 
     } 
     else if (playerchoice == 6) 
     { 
      x = 1; y = 2; 
     } 
     else if (playerchoice == 7) 
     { 
      x = 2; y = 0; 
     } 
     else if (playerchoice == 8) 
     { 
      x = 2; y = 1; 
     } 
     else if (playerchoice == 9) 
     { 
      x = 2; y = 2; 
     } 
     else 
     { 
      cout << "Invalid move, try again!"; 

      // Make sure to mark the move as invalid so they get a chance to 
      // change their move: 
      validmove = false; 
     } 

     // check to see if the turn was valid: 
     if(validmove) 
     { 
      if (board[x][y] != ' ') 
      { 
       cout << "Move is already taken, try again"; 
      } 
      else 
      { 
       board[x][y] = playermark; 
       turn++; 
      } 
     } 

     // have to make sure you have a condition for end of game. A simple 
     // one is to check if turn is less than `9`, otherwise the board is 
     // full: 
     if(turn == 9) 
      done = true; 

     // you probably want to add a few more checks to see who won the game. 
    }while (!done); 

    // when calling a function, no need to put the return type or parameter type: 
    printboard(board); 
    return 0; 
} 

=========================== =============================================

There在你的程序中有兩個do-while循環,這兩個循環似乎都是遊戲循環。我會做的是:

initboard(...); 
turn = 0; 
do{ 
    //this is the game loop 
    ...; 
    if(validturn) 
     turn++; 
}while(!done); 
release_resources(...); 
return 0; 

所以,你把所有東西都放到一個循環中。在這種循環中,你想:

  1. 找到誰是把它就是:

    if (turn % 2) 
        player = 1; 
    else 
        player = 2; 
    
  2. 讓用戶輸入:

    std::cin >> playerchoice; 
    ... 
    
  3. 轉換玩家選擇網格位置:

    switch (move) 
    { 
        case 0: 
         x = 0; 
         y = 0; 
         break; 
        case 1: 
         ...; 
        ... 
        default: 
         //invalid move 
    } 
    
  4. 看,此舉是有效的:

    if(board[x][y] != ' ') 
        //already taken, invalid move 
    
  5. 然後應用招:

    board[x][y] = playermark; 
    

我希望這有助於。

+0

謝謝你的幫助,我會嘗試triple_r。 – Sollux

+0

好吧,我按照你的建議triple_r做了,但是現在它將播放器自動設置爲播放器2,並在我輸入移動時給我分段錯誤。這是我改變它之後,當我改變它到原來的建議與開關語句,讓我不斷說這一舉措是無效的,而不是輸入移動和輸出董事會與它的動作。我應該爲你重新發布我的代碼嗎?順便說一下,我很抱歉給您帶來不便。 – Sollux

+0

如果你重新發布你的代碼會更容易:-)你應該編輯你的問題來添加新的代碼,這樣更多的人可以看到更新後的代碼,並幫助你找到問題所在。 –

0

cin >> playerchoice是你do { ... } while (moves != 9);循環之外。把它移進去。

+0

謝謝您的迴應AJNeufield,一旦我這樣做,但它使得程序無法完全運行。一旦收集到輸入,它就會將其留在那裏,而不會繼續循環並打印出紙板。 – Sollux