gpt4 book ai didi

c++ - 简单的数组操作错误?

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:12 25 4
gpt4 key购买 nike

我目前正在自学 C++,但在尝试使用数组时遇到了问题:

我正在尝试制作一个tic-tac-toe 游戏,用户将输入两个整数来宣布他想移动到哪里,然后为他们打印出棋盘。

除了,当我尝试修改我的板一次时,它经常会改变两个值!

EXAMPLE) 如果我输入的前四个数字是 {0,1,1,0} 我希望绘制两个字母 - 一个“x”和一个“o”。

而是绘制了多个“o”!

谢谢你的帮助!

#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;
}

最佳答案

开始改变

string board[2][2];

string board[3][3];

因为你的循环是从 0 到 2 - 即 3 个项目

关于c++ - 简单的数组操作错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com