2012-06-30 51 views
-8

我正在尝试制作一个tic tac脚趾游戏,它可以在我可以插入X的范围内工作,但计算机只能插入一个O.因为这是一个游戏tic tac toe我需要计算机能够插入多个零。每次运行代码时,如果已经有一个变量的值为o,它不会将另一个变量更改为值o。我认为这个问题是一个if语句。对不起,没有清楚地说明我的问题。如果它仍然没有意义,你可以编译我的代码的其余部分。那么你可能会看到我的意思。C++ if语句没有正确测试

这里的the rest

这是我认为的问题。

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; 
    } 
} 
+8

你的问题是什么?你的代码是专门编写插入一个“o”并退出(马上将'j'设置为'false')。那么问题是什么?什么是“如果陈述不正确测试”应该是什么意思? – AnT

+0

该代码首次运行,但是当某个值已经存储在其中一个值中时,它不会输入第二个o。这就是为什么我说它没有正确测试。 – user1108980

+0

认真的我不介意这会被投票,但你可以做的最少的投票是从链接编译代码,看看我自己的意思。 – user1108980

回答

4

如果我可以重新编写代码:

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 

我敢肯定有一个较短的版本莫过于此。 它只是我的头顶。

+0

对于我第一次写这些乱七八糟的代码抱歉。然后我陷入了困境,所以我试着用其他方法重写它。 – user1108980