Oct 20, 2012 at 3:50am UTC
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void printGrid(const char []);
void goX(char []);
int main()
{
char grid[] = " " ;
cout<<"Q" <<"|" <<"W" <<"|" <<"E" <<endl;
cout<<"-" <<"+" <<"-" <<"+" <<"-" <<endl;
cout<<"A" <<"|" <<"S" <<"|" <<"D" <<endl;
cout<<"-" <<"+" <<"-" <<"+" <<"-" <<endl;
cout<<"Z" <<"|" <<"X" <<"|" <<"C" <<endl;
cout<<endl;
printGrid(grid);
goX(grid);
printGrid(grid);
goX(grid);
printGrid(grid);
}
void printGrid(const char grid[])
{
cout<<grid[0]<<"|" <<grid[1]<<"|" <<grid[2]<<endl;
cout<<"-" <<"+" <<"-" <<"+" <<"-" <<endl;
cout<<grid[3]<<"|" <<grid[4]<<"|" <<grid[5]<<endl;
cout<<"-" <<"+" <<"-" <<"+" <<"-" <<endl;
cout<<grid[6]<<"|" <<grid[7]<<"|" <<grid[8]<<endl;
}
void goX(char grid[])
{
char xchoice;
cout<<"Input your choice." <<endl;
cin>>xchoice;
while (true )
{
if ((xchoice='Q' )||(xchoice='q' ))
{
if (grid[0]=' ' )
grid[0]='X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='W' )||(xchoice='w' ))
{
if (grid[1] = ' ' )
{
grid[1] = 'X' ;
}
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='E' )||(xchoice='e' ))
{
if (grid[2] = ' ' )
grid[2] = 'X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='A' )||(xchoice='a' ))
{
if (grid[3]=' ' )
grid[3]='X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='S' )||(xchoice='s' ))
{
if (grid[4]='0' )
grid[4]='X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='D' )||(xchoice='d' ))
{
if (grid[5] = '0' )
grid[5] = 'X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='Z' )||(xchoice='z' ))
{
if (grid[6]='0' )
grid[6]='X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='X' )||(xchoice='x' ))
{
if (grid[7]='0' )
grid[7]='X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
if ((xchoice='C' )||(xchoice='c' ))
{
if (grid[8]='0' )
grid[8]='X' ;
else
cout<<"choose another one. " <<endl;
}
break ;
}
}
This is just the beginning, and it's not working right, the output is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Q|W|E
-+-+-
A|S|D
-+-+-
Z|X|C
| |
-+-+-
| |
-+-+-
| |
Input your choice.
W
X| |
-+-+-
| |
-+-+-
| |
Input your choice.
E
X| |
-+-+-
| |
-+-+-
| |
I can't find where is wrong. I think the logic is right.
Can anyone help me?
Last edited on Oct 20, 2012 at 5:45am UTC
Oct 20, 2012 at 4:01am UTC
In all of your if statements, you should be using the equals sign
==
instead of the assignment operator
=
There is a big difference between the two. The equals sign tests to see if the left hand side is equal to the right hand side. The assignment operator assigns the rhs to the lhs. As long as the rhs is not zero,
if (lhs = rhs)
will evaluate to TRUE.
While you're at it, why not use numbers to represent the spaces instead of letters? That way, you won't have to bother converting back and forth, and you won't have do distinguish between upercase and lowercase.
Last edited on Oct 20, 2012 at 4:06am UTC
Oct 20, 2012 at 4:40am UTC
I also tried other options, only q worked.
Can anyone help?
Thanx!
Oct 21, 2012 at 2:39am UTC
You never update xChoice within the while loop.