2011-11-04 43 views
0

嗨,我是一個初學者,我正在做一個控制檯tic tac腳趾C++,然後我嘗試在unity3d中完成它,但這是另一個故事,我卡在get_move1()函數,至於如何檢查數組是否有1或-1,如果它是滿的,並且函數應該說是無效移動,除了2d數組之外,這可以通過任何其他方式完成,我的程序只是掛起沒有錯誤,我還沒有在其他功能上工作,所以他們現在只是空的。謝謝你的幫助。停留在TicTacToe檢查移動是否有效C++

*init_board and board_state array 
get_move function to determine if the move is legal if it is then 
fill board_state array and show X or O on the board with another array 
check_win 
*/ 

/* layout of board 
cout << " 1 | 2 | 3 \n" 
     "  | | \n" 
     " - - - - - - \n" 
     " 4 | 5 | 6 \n" 
     "  | | \n" 
     " - - - - - - \n" 
     " 7 | 8 | 9 \n" 
     "  | | \n" << endl; 

cout << " " <<board_array[0] <<" | " << board_array[1] << " | " << board_array[2] << "\n" 
     "  | | \n" 
     " - - - - - - \n" 
     " " <<board_array[3] <<" | " << board_array[4] << " | " << board_array[5] << "\n" 
     "  | | \n" 
     " - - - - - - \n" 
     " " <<board_array[6] <<" | " << board_array[7] << " | " << board_array[8] << "\n" 
     "  | | \n" << endl; 

     */ 

#include <iostream> 

using namespace std; 

//declare global variables 

int board_state[3][3] = { {0,0,0}, //here the 2d array is initialized and set to 0 for empty 
          {0,1,0}, 
          {0,0,0} }; 

char gui_state[3][3];    //2d array to represent the X and O on the board 




//declare functions 

void init_board(); 
void get_move1(); 



int main() 
{ 
    init_board(); 
     get_move1(); 
} 

void init_board() 
{ 
    cout << " 1 | 2 | 3 \n" 
      "  | | \n" 
      " - - - - - - \n" 
      " 4 | 5 | 6 \n" 
      "  | | \n" 
      " - - - - - - \n" 
      " 7 | 8 | 9 \n" 
      "  | | \n" 
      " **Tic Tac Toe ** " << endl; 
} 

void get_move1() // In this function player's moves are taken and make sure they are valid if so update arrays 
{ 
    int player1_move; 
    int i; 
    int j; 
    bool invalid_move; 


    cout << "Player 1 enter move" << endl; 
    cin >> player1_move; 

    //need to check if the players's move is valid 

    for (i=0; i < 3; i++)   //if array is 1 or -1 then move is invalid 
    { 
     for(j = 0; j < 3; i++) 
      if (board_state[i][j] == 1 || board_state[i][j] == -1) 
      { 
       invalid_move = true; 
      } 


    } cout << "You entered an invalid move" << endl; 

} 

void get_move2() 
{ 

} 

void game_state() 
{ 

} 

void check_win() 
{ 

} 

,所以我決定去一個維數組來保持它的簡單,現在

int board_state[9] = {0,1,0,0,0,0,0,0,0}; //here the 1d array is initialized and set to 0 for empty 

void get_move1() // In this function player's moves are taken and make sure they are valid if so update arrays 
{ 
    int player1_move; 
    int array_index; 
    int player1_fill = 1; 


    cout << "Player 1 enter move" << endl; 
    cin >> player1_move; 

    //need to check if the players's move is valid 

    array_index = player1_move -1; 

    if (board_state[array_index] == 1 || board_state[array_index] == -1) 
    { 
     cout << "Invalid move" << endl; 
    } 

    else board_state[array_index] = player1_fill; 

感謝您的幫助球員。

回答

4

這條線:

for(j = 0; j < 3; i++) 

應該是:

​​
0

invalid_move不正確intialized

此外,如果細胞被標記1.9 ..你需要映射它(我, j)正確並更新數組。 您未更新陣列player1_move。爲此,您不需要在for循環在所有

+0

所以我改變了invalid_move = false;是的,一旦移動有效,我還沒有那麼遠,那麼我的下一步就是更新數組 – kodaman

+0

'invalid_move'確實已正確初始化。 在C++中,所有的左值都是默認初始化的,默認的ctor,也就是說,標量是零賦值,所以它被聲明爲false。 閱讀C++標準的第8.5節。 – moshbear

0

for循環後添加以下代碼:

if (invalid_move) 
{ 
    cout << "Invalid move.\n"; 
} 
+0

是的,我只是補充說,在我認爲2D陣列只是我的聯盟的方式,我會代表一切在1D陣列 – kodaman

0

被發現不合適的舉動後的問題,循環不停止,因此invalid_move被覆蓋。

invalid_move=false;//and here 
for (i=0; i < 3&&invalid_move==false; i++) //Watch out here 
    { 
     for(j = 0; j < 3&&invalid_move==false; j++) 
      if (board_state[i][j] == 1 || board_state[i][j] == -1) 
      { 
       invalid_move = true; 
      } 


    } cout << "You entered an invalid move" << endl;