c++ - The right way to use != in a While loop ?...Or ..? -


i'm beginner in c++ , while making code create tictactoe game got stuck @ while statement push game keep going until winning conditions fulfilled :

 while(((table[0][0]!='x')&&(table[1][1]!='x')&&(table[2][2]!='x'))) 

this diagonal condition (putting conditions give eyesore...). problem not working if conditions fulfilled (i'm sure because use cout @ end), when change && || condition work ! thought maybe because of != affect ??

edit: minimal example (i removed floating point !) :

#include <iostream> using namespace std; int main() {     int taillex(3),tailley(3); //the size of table.     char table[taillex][tailley]; //tictactoe table.     table[0][0]='n';     table[0][1]='n';     table[0][2]='n';     table[1][0]='n';     table[1][1]='n';          //randomly filling array avoid error     table[1][2]='n';     table[2][0]='n';     table[2][1]='n';     table[2][2]='n';     int coorp1; //the coordinate of square (exp: x=1 , y=2 1.2)     while(((table[0][0]!='x')&&(table[1][1]!='x')&&(table[2][2]!='x'))) //for minimal example made diagonal condition     {     cout<<"player1: enter coordination score: (exemple: 1, 2, 3..) "<<endl;     cin>>coorp1;     switch(coorp1) //filling square depending on coordinates.//i used if because switch not work.     {         case 1:     table[0][0]='x';     break;         case 2:     table[0][1]='x';     break;         case 3:     table[0][2]='x';     break;         case 4:     table[1][0]='x';     break;         case 5:     table[1][1]='x';     break;         case 6:     table[1][2]='x';     break;         case 7:     table[2][0]='x';     break;         case 8:     table[2][1]='x';     break;         case 9:     table[2][2]='x';     break;     }     }     cout<<"you won"<<endl;     return 0; } 

the problem here test condition. loop repeats if enter 2, 3, 4, 6, 7, or 8. enter 1, 5, or 9, loop exits. if enter 1, 5, or 9, 1 of diagonal values set 'x'. while loops while condition true. condition evaluates false, exits. when enter 1, 5, or 9, cause condition false.

imagine, second, table[0][0] 'x', table[1][1] 'n', , table[2][2] 'n'. in other words, board looks this:

x | n | n --+---+--- n | n | n --+---+--- n | n | n 

then test condition is:

table[0][0] != 'x' && table[1][1] != 'x' && table[2][2] != 'x' ^^^^^^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^^ false                 true                  true 

if logically , these (as &&), evaluates false (which makes sense: false , true should evaluate false; true , true should evaluate true).

so should test condition be?

what want loop if user doesn't have 3 in row. in other words, check if user has 3 in row; if not have 3 in row, proceed.

we can construct logical statement as:

// checks if user has 3 in row table[0][0] == 'x' && table[1][1] == 'x' && table[2][2] == 'x'  // want check if user not have 3 in row, // can negate above ! !(table[0][0] == 'x' && table[1][1] == 'x' && table[2][2] == 'x')  // de morgan's laws, can simplify to: table[0][0] != 'x' || table[1][1] != 'x' || table[2][2] != 'x' 

thus, looping condition should 1 of (they're both equivalent; pick whichever 1 makes more sense you):

  • !(table[0][0] == 'x' && table[1][1] == 'x' && table[2][2] == 'x')
    checks if user not have 3 in row
  • table[0][0] != 'x' || table[1][1] != 'x' || table[2][2] != 'x'
    checks if user not have 'x' 1 of needed positions. logically follows if user missing 'x' in 1 of these positions, user cannot have 3 in row. application of de morgan's laws previous logical statement.

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -