Riv*_*asa 0 c++ arrays pointers
所以我正在开发一个Tic-Tac-Toe游戏,对于我的输入函数,我得到的是移动播放器作为一个整数存储在一个二维数组中,输入是通过引用指向位置的一维指针获得的在2D数组中.
但是我的问题是,当我似乎通过使用指针将多维数组的平方值设置为某个东西时,没有任何反应.
这是输入功能:
void Game::input(Board b){
int *spots[9]; // Possible spots for the input
bool validInput = false;
spots[0] = &b.board[2][0];
spots[1] = &b.board[2][1];
spots[2] = &b.board[2][2];
spots[3] = &b.board[1][0];
spots[4] = &b.board[1][1];
spots[5] = &b.board[1][2];
spots[6] = &b.board[0][0];
spots[7] = &b.board[0][1];
spots[8] = &b.board[0][2];
redo:
cout << ">> " << endl;
int input; // Input
cin >> input; // Get the input
validInput = cin;
if(!validInput){
cout << "Numbers only please!" << endl;
cin.clear();
while(cin.get() != '\n');
goto redo;
}
if(input > 9 || input <= 0){
cout << "Invalid move!" << endl;
goto redo;
}
input--; // Subtract 1 for array location
if(*spots[input] != 0){
cout << "Square is already being used!" << endl;
goto redo;
}
*spots[input] = 1;
}
Run Code Online (Sandbox Code Playgroud)
现在,假设我输入数字7.它应该将b.board [0] [0]设置为1.但是这似乎不会发生.当我之后运行单元格时,板[0] [0]似乎没有设置为1,并且它不会反映在我的数组中.我在这里弄乱指针吗?
| 归档时间: |
|
| 查看次数: |
264 次 |
| 最近记录: |