0

こんにちは私は初心者で、unity3dで実行する前に、コンソールのtic tac toe c ++を作成していますが、それは別の話です。配列に1または-1がいっぱいになり、関数が無効な動きで言う必要があります。これは2d配列以外の方法で実行できます。プログラムはエラーなしでハングします。他の関数では、作業を行っていません。それでも、今のところ空です。ご協力いただきありがとうございます。

*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

4 に答える 4

4

この行:

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

次のようにする必要があります。

for(j = 0; j < 3; j++)
于 2011-11-04T23:37:54.453 に答える
0

ループの後に、次のforコードを追加します。

if (invalid_move)
{
    cout << "Invalid move.\n";
}
于 2011-11-04T23:47:35.900 に答える
0

invalid_move が正しく初期化されていません

また、セルに 1.9.. というラベルが付けられている場合は、それを (i,j) 正しくマップし、配列を更新する必要があります。player1_moveで配列を更新していません。このため、 for ループはまったく必要ありません

于 2011-11-04T23:39:13.350 に答える
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;
于 2011-11-05T01:27:13.240 に答える