if ((turn==1)&&(board[0]!=''X'')&&(board[0]!=''O'')){board[0]=''X''; break;}
else ((board[0]!=''X'')&&(board[0]!=''O''));{board[0]=''O''; continue;}
The thing is I barely know what any of that means. Can someone point to me where I can learn about this or give me a brief explanations. Thanks
(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.
what does this all mean ???
I ask some people while back about a ttt code to check to see if a space has been taking, this is what they suggested:
if ((turn==1)&&(board[0]!=''X'')&&(board[0]!=''O'')){board[0]=''X''; break;}
This line is saying
IF turn is = 1
and board[0] is not = to ''X''
and board[0] is not = to ''0''
THEN
set board[0]=''X''
and then break;
Break means jump out of any loop that the code is in. For example:
for(int x=0; x<100; x++){
if(x==50)
break;
}
this will loop up to 50 and then it will jump out of the loop because of the break statement.
then the line:
else ((board[0]!=''X'')&&(board[0]!=''O''));{board[0]=''O''; continue;}
this says:
If the first if wasn''t true THEN
IF board[0] not = ''X''
AND board[0] not = ''0''
THEN
board[0]=O;
continue in the loop;
I hope this helps.
~Wave
This line is saying
IF turn is = 1
and board[0] is not = to ''X''
and board[0] is not = to ''0''
THEN
set board[0]=''X''
and then break;
Break means jump out of any loop that the code is in. For example:
for(int x=0; x<100; x++){
if(x==50)
break;
}
this will loop up to 50 and then it will jump out of the loop because of the break statement.
then the line:
else ((board[0]!=''X'')&&(board[0]!=''O''));{board[0]=''O''; continue;}
this says:
If the first if wasn''t true THEN
IF board[0] not = ''X''
AND board[0] not = ''0''
THEN
board[0]=O;
continue in the loop;
I hope this helps.
~Wave
Yes it does... so && is and, ! is not. Can i only use break in switch statements or in any loop. Also when I run the code it sets the spot where the first player chooses to an ''0'' and not an ''X'', and if a space is an ''X'' and the first player chooses the same place it turns into an ''O'' I don''t know why. The other thing is how can I print to the screen to tell the player to choose a different spot when in the switch statements. Here is my whole ttt code:
Thanks
(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.
#include <stdio.h>#include <windows.h>int turn, move;char name1[25], name2[25];char board [9]; // The tic-tac-toe Boardvoid DrawBoard();void DrawBoard() //* Draws the board *//{printf("\n\n");printf(" %c | %c | %c \n", board[0], board[1], board[2]);printf(" --------- \n");printf(" %c | %c | %c \n", board[3], board[4], board[5]);printf(" --------- \n");printf(" %c | %c | %c \n", board[6], board[7], board[8]);printf("\n\n");}void CheckSpace(); void CheckSpace() // Checks for double use of space{}int main(){BOOL StillPlaying=TRUE;int move=0;int turn=1;printf("Welcome to tic-tac-toe!!!\n\n");//players namesprintf("What is the first player''s name: "); scanf("%s", name1);printf("\nWhat is the second player''s name: "); scanf("%s", name2); while (StillPlaying) { DrawBoard(); if (turn == 1){ printf("It is %s turn!\n", name1); scanf("%d", &move); turn=2; printf("\n"); } else{ printf("It is %s turn!\n", name2); scanf("%d", &move); turn=1; printf("\n"); } switch(move) { case 1: if ((turn==1)&&(board[0]!=''X'')&&(board[0]!=''O'')){board[0]=''X''; break;} else ((board[0]!=''X'')&&(board[0]!=''O''));{board[0]=''O''; continue;} case 2: if ((turn==1)&&(board[1]!=''X'')&&(board[1]!=''O'')){board[1]=''X''; break;} else ((board[1]!=''X'')&&(board[1]!=''O''));{board[1]=''O''; continue;} case 3: if ((turn==1)&&(board[2]!=''X'')&&(board[2]!=''O'')){board[2]=''X''; break;} else ((board[2]!=''X'')&&(board[2]!=''O''));{board[2]=''O''; continue;} case 4: if ((turn==1)&&(board[3]!=''X'')&&(board[3]!=''O'')){board[3]=''X''; break;} else ((board[3]!=''X'')&&(board[3]!=''O''));{board[3]=''O''; continue;} case 5: if ((turn==1)&&(board[4]!=''X'')&&(board[4]!=''O'')){board[4]=''X''; break;} else ((board[4]!=''X'')&&(board[4]!=''O''));{board[4]=''O''; continue;} case 6: if ((turn==1)&&(board[5]!=''X'')&&(board[5]!=''O'')){board[5]=''X''; break;} else ((board[5]!=''X'')&&(board[5]!=''O''));{board[5]=''O''; continue;} case 7: if ((turn==1)&&(board[6]!=''X'')&&(board[6]!=''O'')){board[6]=''X''; break;} else ((board[6]!=''X'')&&(board[6]!=''O''));{board[6]=''O''; continue;} case 8: if ((turn==1)&&(board[7]!=''X'')&&(board[7]!=''O'')){board[7]=''X''; break;} else ((board[7]!=''X'')&&(board[7]!=''O''));{board[7]=''O''; continue;} case 9: if ((turn==1)&&(board[8]!=''X'')&&(board[8]!=''O'')){board[8]=''X''; break;} else ((board[8]!=''X'')&&(board[8]!=''O''));{board[8]=''O''; continue;} case 10: StillPlaying = FALSE; {printf("Leaving so soon?\n"); break;} default: {printf("Choose a number not a letter"); break;} } // switch(move) }//While Winnerreturn 0; }// int main()
Thanks
(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.
You can use 'break' to break from any loop.
Since your using C you would print to the screen like this ...
printf("Choose a different spot");
edit:Removing bad info
[edited by - Tiffany Smith on July 7, 2003 10:30:46 PM]
Since your using C you would print to the screen like this ...
printf("Choose a different spot");
edit:Removing bad info
[edited by - Tiffany Smith on July 7, 2003 10:30:46 PM]
Hmm I tried the code and still no luck. Wouldn''t it go to the first statement and bypass the printf if the first one is true.
ugh don''t code a switch if you don''t need it.
and if you do, use ''1'', ''2''... etc otherwise it''ll never work.
and if you do, use ''1'', ''2''... etc otherwise it''ll never work.
char PlayerToken[2] = {''O'', ''X''};scanf("%i",&move);if (Board[move-1] == 0) Board[move-1] = PlayerToken[turn-1];
Ohh my gosh im such a twit sometimes ...
forget what i said before ...
The reason the 'O' s are replacing the 'X's could be because your 'else' statements are wrong ...
you need to remove the semi colons from the end of all the 'else' lines and make them into 'else if's'.
like this ...
else ((board[0]!='X')&&(board[0]!='O'));
{
board[0]='O';
continue;
}
should be ...
else if ((board[0]!='X')&&(board[0]!='O'))
{
board[0]='O';
continue;
}
Hope that helps.
sorry about before.
[edited by - Tiffany Smith on July 7, 2003 10:50:42 PM]
forget what i said before ...
The reason the 'O' s are replacing the 'X's could be because your 'else' statements are wrong ...
you need to remove the semi colons from the end of all the 'else' lines and make them into 'else if's'.
like this ...
else ((board[0]!='X')&&(board[0]!='O'));
{
board[0]='O';
continue;
}
should be ...
else if ((board[0]!='X')&&(board[0]!='O'))
{
board[0]='O';
continue;
}
Hope that helps.
sorry about before.
[edited by - Tiffany Smith on July 7, 2003 10:50:42 PM]
I don't know if this will help or not - but it might
[edited by - lessbread on July 8, 2003 4:13:35 AM]
De Morgans laws1. !(a && b) == (!a || !b)2. !(a || b) == (!a && !b)To not a logical expression, not both terms and change the logical operator. Apply this rule recursively as needed.
[edited by - lessbread on July 8, 2003 4:13:35 AM]
Hey librab103, i took the liberty of making a few modifications to make it a little more readble.
Hopefully its helpful to you...
[edited by - Tiffany Smith on July 8, 2003 9:26:32 PM]
Hopefully its helpful to you...
#include <stdio.h>#include <windows.h>int turn, move;char name1[25], name2[25];char board [9]; // The tic-tac-toe Boardvoid DrawBoard() // Draws the board { printf("\n\n"); printf(" %c | %c | %c \n", board[0], board[1], board[2]); printf(" --------- \n"); printf(" %c | %c | %c \n", board[3], board[4], board[5]); printf(" --------- \n"); printf(" %c | %c | %c \n", board[6], board[7], board[8]); printf("\n\n"); }int main(){BOOL StillPlaying=TRUE;int move=0;int turn=1;printf("Welcome to tic-tac-toe!!!\n\n");printf("What is the first player's name: "); scanf("%s", name1);printf("\nWhat is the second player's name: "); scanf("%s", name2); while (StillPlaying) { DrawBoard(); scanf("%d", &move); if( move < 1 || move > 9 ) printf("please choose a number from 1-9 only\n"); else { if (turn == 1) { printf("It is %s's turn!\n", name1); printf("\n"); if( (board[move-1]!='X')&&(board[move-1]!='O') ) { board[move-1]='X'; turn=2; } else printf("invalid move\n"); } else { printf("It is %s's turn!\n", name2); printf("\n"); if( (board[move-1]!='X')&&(board[move-1]!='O') ) { board[move-1]='O'; turn=1; } else printf("invalid move\n"); } }}//While Winnerreturn 0;}// int main()
[edited by - Tiffany Smith on July 8, 2003 9:26:32 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement