Tic-Tac-Toe errors

This is a discussion on Tic-Tac-Toe errors within the C++ Programming forums, part of the General Programming Boards category; I get lots of errors for this: Code: #include<iostream> #include<windows.h> #include<conio.h> #include<cstdlib> using namespace std; char a1=' '; char a2=' ...

  1. #1
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176

    Tic-Tac-Toe errors

    I get lots of errors for this:
    Code:
    #include<iostream>
    #include<windows.h>
    #include<conio.h>
    #include<cstdlib>
    using namespace std;
    
    char a1=' ';
    char a2=' ';
    char a3=' ';
    char b1=' ';
    char b2=' ';
    char b3=' ';
    char c1=' ';
    char c2=' ';
    char c3=' ';
    
    
    bool Check();
    void DrawBoard();
    void DrawHelpBoard();
    
    int main(){
    	cout<<"Tic-Tac-Toe!\n";
    	int move;
    	char X_or_O;
    	char O_or_X;
    	int xo=rand()%2;
    	if(xo=0){
    		X_or_O='X';
    		O_or_X='O';
    		cout<<"You are X";
    	if(xo=1){
    		X_or_O='O';
    		O_or_X='X';
    		cout<<"You are O";
    		}
    	Sleep(2000);
    	while(Check())
    		clrscr();
    		DrawBoard();
    		DrawHelpBoard();
    		cin>>move;
    		if(move=1 && a1!=X_or_O){a1=X_or_O;}
    			else if(move==2 && a2!=X_or_O){a2=X_or_O;}
    			else if(move==3 && a3!=X_or_O){a3=X_or_O;}
    			else if(move==4 && b1!=X_or_O){b1=X_or_O;}
    			else if(move==5 && b2!=X_or_O){b2=X_or_O;}
    			else if(move==6 && b3!=X_or_O){b3=X_or_O;}
    			else if(move==7 && c1!=X_or_O){c1=X_or_O;}
    			else if(move==8 && c2!=X_or_O){c2=X_or_O;}
    			else if(move==9 && c3!=X_or_O){a1=X_or_O;}
    			else{
    				MessageBox(NULL, "Not a valid space", "ERROR", MB_OK);
    };
    }
    
    
    void DrawBoard(){
    	cout<<a1<<"|"<<a2<<"|"<<*a3<<"|\n------"<<b1<<"|"<<b2<<"|"<<b3<<"|\n------"<<c1<<"|"<<c2<<"|"<<c3<<"|\n\n";
    }
    void DrawHelpBoard(){
    	cout<<"1|2|3|\n------4|5|6|\n------7|8|9|";
    }
    bool Check(){
    	if (a1='X' && a2='X' && a3='X'){ return FALSE;}
    	else if (b1=='X' && b2='X' && b3='X'){ return FALSE;}
    	else if (c1=='X' && c2='X' && c3='X'){ return FALSE;}
    	else if (a1=='X' && b1='X' && c1='X'){ return FALSE;}
    	else if (a2=='X' && b2='X' && c2='X'){ return FALSE;}
    	else if (a3=='X' && b3='X' && c3='X'){ return FALSE;}
    	else if (a1=='X' && b2='X' && c3='X'){ return FALSE;}
    	else if (a3=='X' && b2='X' && c1='X'){ return FALSE;}
    
    	else if (a1=='O' && a2='O' && a3='O'){ return FALSE;}
    	else if (b1=='O' && b2='O' && b3='O'){ return FALSE;}
    	else if (c1=='O' && c2='O' && c3='O'){ return FALSE;}
    	else if (a1=='O' && b1='O' && c1='O'){ return FALSE;}
    	else if (a2=='O' && b2='O' && c2='O'){ return FALSE;}
    	else if (a3=='O' && b3='O' && c3='O'){ return FALSE;}
    	else if (a1=='O' && b2='O' && c3='O'){ return FALSE;}
    	else if (a3=='O' && b2='O' && c1='O'){ return FALSE;}
    	else{
    		return TRUE;
    };
    }
    Errors:
    Code:
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    tictactoe.cpp:
    Warning W8060 tictactoe.cpp 28: Possibly incorrect assignment in function main(
    
    Warning W8060 tictactoe.cpp 32: Possibly incorrect assignment in function main(
    
    Warning W8060 tictactoe.cpp 43: Possibly incorrect assignment in function main(
    
    Error E2141 tictactoe.cpp 58: Declaration syntax error in function main()
    Error E2139 tictactoe.cpp 85: Declaration missing ; in function main()
    Error E2134 tictactoe.cpp 85: Compound statement missing } in function main()
    Warning W8004 tictactoe.cpp 85: 'xo' is assigned a value that is never used in
    unction main()
    Warning W8004 tictactoe.cpp 85: 'O_or_X' is assigned a value that is never used
    in function main()
    *** 3 errors in Compile ***
    Edited code and posted errors
    Last edited by fuh; 06-19-2003 at 07:14 PM.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Indent your code, please.

    Remember: anything in double quotes "" is a const char*. You're defining your types as char.

    Remember: a single equals sign means assignment. int a = 1 will always be true because 1 is true. You want to say if (a == 1) ...

    You also have extra '}' or '{' signs.

    You're working way too hard to create this program. You don't need so many else if statements. Look for shortcuts.

  3. #3
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176

    Changed Code

    I changed some code in my post above and added the errors. Sorry for delay. Technical difficulties.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Re: Changed Code

    Originally posted by fuh
    I changed some code in my post above and added the errors. Sorry for delay. Technical difficulties.
    You only changed the first comparisons from '=' to '=='. There are more than one of those in a line you need to change.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    instead of using
    Code:
    char a1=' ';
    char a2=' ';
    char a3=' ';
    char b1=' ';
    char b2=' ';
    char b3=' ';
    char c1=' ';
    char c2=' ';
    char c3=' ';
    you should use an array of chars...just because it's simpler

    Code:
    char board[9]={' ',' ',' ',' ',' ',' ',' ',' ',' '};
    Away.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Also, 'false' is a C++ keyword (a bool), not 'FALSE'. 'FALSE' and 'TRUE' probably have one of the following definitions somewhere:

    #define FALSE 0
    #define TRUE 1

    or

    const int FALSE = 0;
    const int TRUE = 1;

    or

    enum BOOL = { FALSE = 0, TRUE };

    1 and 0 will always evaluate to the boolean true or false, resplectively, but the all caps versions are not actually keywords.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Zach L.
    enum BOOL = { FALSE = 0, TRUE };
    Two minor points:

    1) The first = sign is incorrect. Remove it.
    2) The second = sign, and the following 0 are actually not needed. Yet I myself include them for clarity. The first enum value, if not assigned to any specific value, will always be 0, with the next one being 1 and the next being 2 and so on...

    Thus:

    enum BOOL { FALSE, TRUE };

    Would suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    I got rid of most errors and warnings (1 or 2 was I hadn't put the computer in), but still get these:
    Code:
    C:\Borland\BCC55\zack>bcc32 -I..\include -L..\lib tictactoe
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    tictactoe.cpp:
    Error E2379 tictactoe.cpp 47: Statement missing ; in function main() Huh?
    Error E2134 tictactoe.cpp 74: Compound statement missing } in function main() For what? 
    Warning W8004 tictactoe.cpp 74: 'O_or_X' is assigned a value that is never used No AI yet error, I'll fix that later.
    in function main()
    *** 2 errors in Compile ***
    For this:
    Code:
    #include<iostream>
    #include<windows.h>
    #include<conio.h>
    #include<cstdlib>
    using namespace std;
    
    char board[9]={' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    
    bool Check();
    void DrawBoard();
    void DrawHelpBoard();
    
    int main(){
    	cout<<"Tic-Tac-Toe!\n";
    	int move;
    	char X_or_O;
    	char O_or_X;
    	int xo=rand()%2;
    	if(xo==0){
    		X_or_O='X';
    		O_or_X='O';
    		cout<<"You are X";
    	if(xo==1){
    		X_or_O='O';
    		O_or_X='X';
    		cout<<"You are O";
    		}
    	Sleep(2000);
    	while(Check())
    		clrscr();
    		DrawBoard();
    		DrawHelpBoard();
    		cin>>move;
    		if(move==1 && board[0]!=X_or_O){board[0]=X_or_O;}
    			else if(move==2 && board[1]!=X_or_O){board[1]=X_or_O;}
    			else if(move==3 && board[2]!=X_or_O){board[2]=X_or_O;}
    			else if(move==4 && board[3]!=X_or_O){board[3]=X_or_O;}
    			else if(move==5 && board[4]!=X_or_O){board[4]=X_or_O;}
    			else if(move==6 && board[5]!=X_or_O){board[5]=X_or_O;}
    			else if(move==7 && board[6]!=X_or_O){board[6]=X_or_O;}
    			else if(move==8 && board[7]!=X_or_O){board[7]=X_or_O;}
    			else if(move==9 && board[8]!=X_or_O){board[8]=X_or_O;}
    			else{
    				MessageBox(NULL, "Not a valid space", "ERROR", MB_OK);
    };
    }
    DrawBoard(){
    	cout<<board[1]<<"|"<<board[2]<<"|"<<*board[3]<<"|\n------"<<board[4]<<"|"<<board[4]<<"|"<<board[4]<<"|\n------"<<board[4]<<"|"<<board[8]<<"|"<<board[8]<<"|\n\n";
    }
    DrawHelpBoard(){
    	cout<<"1|2|3|\n------4|5|6|\n------7|8|9|";
    }
    Check(){
    	if (board[0]='X' && board[1]='X' && board[2]='X'){ return false;}
    	else if (board[3]=='X' && board[4]=='X' && board[5]=='X'){ return false;}
    	else if (board[6]=='X' && board[7]=='X' && board[8]=='X'){ return false;}
    	else if (board[0]=='X' && board[4]=='X' && board[8]=='X'){ return false;}
    	else if (board[0]=='X' && board[3]=='X' && board[6]=='X'){ return false;}
    	else if (board[1]=='X' && board[4]=='X' && board[7]=='X'){ return false;}
    	else if (board[2]=='X' && board[5]=='X' && board[8]=='X'){ return false;}
    	else if (board[2]=='X' && board[4]=='X' && board[6]=='X'){ return false;}
    
    	else if (board[1]=='O' && board[2]=='O' && board[3]='O'){ return false;}
    	else if (board[4]=='O' && board[4]=='O' && board[4]='O'){ return false;}
    	else if (board[4]=='O' && board[8]=='O' && board[8]='O'){ return false;}
    	else if (board[1]=='O' && board[4]=='O' && board[4]='O'){ return false;}
    	else if (board[2]=='O' && board[4]=='O' && board[8]='O'){ return false;}
    	else if (board[3]=='O' && board[4]=='O' && board[8]='O'){ return false;}
    	else if (board[1]=='O' && board[4]=='O' && board[8]='O'){ return false;}
    	else if (board[3]=='O' && board[4]=='O' && board[4]='O'){ return false;}
    	else{
    		return true;
    };
    }
    Thanks, guys!
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(xo==0){
    		X_or_O='X';
    		O_or_X='O';
    		cout<<"You are X";
    You are missing your closing } here.
    That is, unless you actually meant to have all of the rest of the entire code as a sub-block of this if statement...
    Hope is the first step on the road to disappointment.

  10. #10
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    So you can do this
    Code:
    if(x==0)
         //stuff
    but not this?
    Code:
    while(x==0)
         //stuff

  11. #11
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    You can do either of those, but only the line following the if/else/while/for/etc., will be executed. The brackets are there to enclose exactly what you want to be executed given a certain condition.

  12. #12
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    >>You can do either of those, but only the line following the if/else/while/for/etc., will be executed

    Oh. I thought he wanted to do that.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by gcn_zelda
    So you can do this
    ...snip...
    but not this?
    ...snip...
    You're missing the problem. The problem is you don't have braces around a block. After an if/for/do-while/while statement, you only get one code statement or block to execute. Illustrated:
    Code:
    if ( foo == bar )
        this_line_on_true( );
    this_line_always( );
    If you wanted both to happen if foo == bar, then you need to wrap them in braces:
    Code:
    if ( foo == bar )
    {
        this_line_on_true( );
        this_line_always( );
    }
    Now then, the second function call will only happen if foo==bar, because they are both enclosed in the same braces pair. (ie: the came scope block)

    Otherwise, without braces, only the next following statement executes.

    [edit] Curses, foiled again. [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    I fixed the braces error, but can't figure out the semicolon error.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  15. #15
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Originally posted by Salem

    Then I suggest you repost your latest code then
    Here it is:
    Code:
    #include<iostream>
    #include<windows.h>
    #include<conio.h>
    #include<cstdlib>
    using namespace std;
    
    char board[9]={' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    
    bool Check();
    void DrawBoard();
    void DrawHelpBoard();
    
    int main(){
    	cout<<"Tic-Tac-Toe!\n";
    	int move;
    	char X_or_O;
    	char O_or_X;
    	int xo=rand()%2;
    	if(xo==0){
    		X_or_O='X';
    		O_or_X='O';
    		cout<<"You are X";}
    	if(xo==1){
    		X_or_O='O';
    		O_or_X='X';
    		cout<<"You are O";
    		}
    	Sleep(2000);
    	while(Check()){
    		clrscr();
    		DrawBoard();
    		DrawHelpBoard();
    		cin>>move;
    		if(move==1 && board[0]!=X_or_O){board[0]=X_or_O;}
    			else if(move==2 && board[1]!=X_or_O){board[1]=X_or_O;}
    			else if(move==3 && board[2]!=X_or_O){board[2]=X_or_O;}
    			else if(move==4 && board[3]!=X_or_O){board[3]=X_or_O;}
    			else if(move==5 && board[4]!=X_or_O){board[4]=X_or_O;}
    			else if(move==6 && board[5]!=X_or_O){board[5]=X_or_O;}
    			else if(move==7 && board[6]!=X_or_O){board[6]=X_or_O;}
    			else if(move==8 && board[7]!=X_or_O){board[7]=X_or_O;}
    			else if(move==9 && board[8]!=X_or_O){board[8]=X_or_O;}
    			else{
    				MessageBox(NULL, "Not a valid space", "ERROR", MB_OK);
    };
    };
    DrawBoard()
    {
    	cout<<board[0]<<"|"<<board[1]<<"|"<<board[2]<<"|\n------"<<board[3]<<"|"<<board[4]<<"|"<<board[5]<<"|\n------"<<board[6]<<"|"<<board[7]<<"|"<<board[8]<<"|\n\n";
    }
    DrawHelpBoard(){
    	cout<<"1|2|3|\n------4|5|6|\n------7|8|9|";
    }
    Check(){
    	if (board[0]='X' && board[1]='X' && board[2]='X'){ return false;}
    	else if (board[3]=='X' && board[4]=='X' && board[5]=='X'){ return false;}
    	else if (board[6]=='X' && board[7]=='X' && board[8]=='X'){ return false;}
    	else if (board[0]=='X' && board[4]=='X' && board[8]=='X'){ return false;}
    	else if (board[0]=='X' && board[3]=='X' && board[6]=='X'){ return false;}
    	else if (board[1]=='X' && board[4]=='X' && board[7]=='X'){ return false;}
    	else if (board[2]=='X' && board[5]=='X' && board[8]=='X'){ return false;}
    	else if (board[2]=='X' && board[4]=='X' && board[6]=='X'){ return false;}
    
    	else if (board[1]=='O' && board[2]=='O' && board[3]='O'){ return false;}
    	else if (board[4]=='O' && board[4]=='O' && board[4]='O'){ return false;}
    	else if (board[4]=='O' && board[8]=='O' && board[8]='O'){ return false;}
    	else if (board[1]=='O' && board[4]=='O' && board[4]='O'){ return false;}
    	else if (board[2]=='O' && board[4]=='O' && board[8]='O'){ return false;}
    	else if (board[3]=='O' && board[4]=='O' && board[8]='O'){ return false;}
    	else if (board[1]=='O' && board[4]=='O' && board[8]='O'){ return false;}
    	else if (board[3]=='O' && board[4]=='O' && board[4]='O'){ return false;}
    	else{
    		return true;
    };
    }
    }
    I still havn't added the opponent yet.
    Last edited by fuh; 06-21-2003 at 02:21 PM.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  3. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:16 PM
  4. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 11:52 AM
  5. Replies: 22
    Last Post: 11-08-2001, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21