我目前的教学尝试使用数组时自己的C++和遇到了一个问题:简单数组操作错误?
我试图做一个井字棋游戏,用户将输入两个整数公布他想移动的位置,然后为他们打印棋盘。
除此之外,当我尝试一次修改我的主板,它往往会改变两个值!
示例)如果我输入的前四个数字是{0,1,1,0}
我期望绘制两个字母 - 一个“x”和一个“o”。
但不是多个 “O” s的画!
谢谢你的帮助!
#include <string>
#include <iostream>
using namespace std;
string board[2][2];
int xpos, ypos;
string turn;
int main()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j]="-";
}
}
for(int i=0;i<9;i++)
{
if(i%2==0)
turn = "x";
else
turn = "o";
cout<< "Where are you moving?\n";
cin>> xpos >> ypos;
board[xpos][ypos] = turn;
cout<<"The board is:\n";
cout<< board[0][0]+board[0][1]+board[0][2]+ "\n";
cout<< board[1][0]+board[1][1]+board[1][2]+ "\n";
cout<< board[2][0]+board[2][1]+board[2][2]+ "\n";
}
return 0;
}
你要走出阵列。这是未定义的行为。 – chris