C++ if 语句未正确测试
C++ if statement is not testing correctly
我正在尝试制作一个井字游戏,它可以工作到我可以插入Xs但计算机只能插入一个O的程度。由于这是一个井字游戏,我需要计算机能够插入多个零。每次运行此代码时,如果已经有一个变量的值为 o,它不会将另一个变量更改为值 o。我认为问题在于 if 语句。很抱歉没有清楚地说明我的问题。如果它仍然没有意义,您可以编译我的其余代码。那你也许会明白我的意思。
这是其余的
这就是我认为的问题所在。
bool j=true ;
int lo=0;
string hi [9]={a,b,c,d,e,f,g,h,i};
while(j==true)
{
if (lo>8)
{
j=false;
}
else if(hi[lo]!="x"&&hi[lo]!="o")
{
hi[lo]="o";
j=false;
}
else
{
lo=lo+1;
j=true;
}
}
如果我能重写你的代码:
for (lo = 0; lo < 9; lo++){
if (hi[lo] == " "){
hi[lo] = "o";
break;
}
}
所以你是对的,它插入了一个"o"。
问题出在哪里?
编辑:由于您正在尝试编写井字游戏,因此让我建议一些整体伪代码:
repeat
ask user where they want to place an "x" (i.e. get a number from 0 to 8)
if that square is not blank, say so and ask again
else put the "x" in the square
If this completes a row, column, or diagonal, exit saying "You win!"
-> enter the loop here if you are making the first move
then find out where to put an "o". You do this by looking at all unoccupied squares.
for each unoccupied square
look at the row, column, and diagonal it is on
if putting an "o" there would make you the winner, give it a really high score
if putting an "o" there would block the user from winning, give it a high score
if putting an "o" there would mean that you could win on the following move, give it a medium score
Otherwise give it a score that is the number of rows, columns, and diagonals it could possibly tie up
end for each
Then put the "o" on the highest-scored square (or one of the highest-scored squares if there are more than one)
If you completed a row, column, or diagonal, exit saying "I win!".
If you can't find a square to mark, exit saying "I give up".
end repeat
我相信有比这更短的版本。它就在我头顶上。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 多个 if-else 测试的更简单方法
- 在单个if语句中测试多个布尔值
- 卡在测试'if':另一个代码的一部分
- C++ if 语句未正确测试
- ifs 是否按照它们的编写顺序进行测试是一个 if-else..如果-否则..如果-否则块
- 在函数开始时测试并返回,而不是嵌套的 if
- 如何使用if语句测试函数
- if/else优化测试结果令人震惊
- 有没有真正巧妙的方法来进行测试'if none in collection'?
- 对于循环数组迭代,if循环测试索引是否为真/假,计算结果不正确
- 是否有更快/更短的方法在C++而不是使用"if"进行这些测试?
- if语句-测试字符串或字符的大写,小写和数字
- c++基础- If语句测试
- C++ || 的排序和 &&在 if 语句中测试等式时
- 是否可以将任何类对象作为测试表达式传递给任何测试表达式,例如if,while like ifstream对象