Thread: Tic Tac Toe

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    Question Tic Tac Toe

    Hi, I am having a weird problem with my compiler; After finally straightening my code out for a tic tac toe game(which I was having trouble with in the first place, so it is using VERY primitive techniques...but anyway, that's not the point) So after I fixed all the code errors, I get this error when the compiler goes to the linking stage; Here is the message
    Code:
    ------ Build started: Project: ttttest, Configuration: Debug Win32 ------
    Compiling...
    source2.cpp
    Linking...
    source2.obj : error LNK2019: unresolved external symbol "void __cdecl board(int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?board@@YAXHHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
    C:\Documents and Settings\Mike\My Documents\Visual Studio 2005\Projects\ttttest\Debug\ttttest.exe : fatal error LNK1120: 1 unresolved externals
    Build log was saved at "file://c:\Documents and Settings\Mike\My Documents\Visual Studio 2005\Projects\ttttest\ttttest\Debug\BuildLog.htm"
    ttttest - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I have NO IDEA what in the heck this means, so some help would be greatly appreciated...

    Umm, not sure if this is legal, but here is my source code as well...(it's 183 lines!)

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    void clrscr();
    void board(int, int, string);
    
    int main()
    {
    	int x, y, player;
    	string v;
    	string xy[3][3];
    	player = 1;
    	cout << "\t\t__TIC-TAC-TOE__\n";
    	cout << "\t\tTwo Player Game\n\n";
    	cout << "\t\t  1  2  3 \n\n";
    	cout << "\t\t1[ ][ ][ ]\n";
    	cout << "\t\t2[ ][ ][ ]\n";
    	cout << "\t\t3[ ][ ][ ]\n";
    	cout << "Player " << player << ", enter the number of the row and then the column you wish to move to: ";
    	cin >> x;
    	cout << endl;
    	cin >> y;
    	if (player==1)
    	{
    		if (xy[x][y]=="o")
    		{
    			cout << "That square is taken by the other player";
    			main();
    		}
    		else if (xy[x][y]=="x")
    		{
    			cout << "That square is take by you!";
    			main();
    		}
    		else
    		{
    			xy[x][y]="x";
    			board(x, y, xy[3][3]);
    			player=2;
    			clrscr();
    		}
    	}
    	else if (player==2)
    	{
    		if (xy[x][y]=="x")
    		{
    			cout << "That square is taken by the other player";
    			main();
    		}
    		else if (xy[x][y]=="o")
    		{
    			cout << "That square is take by you!";
    			main();
    		}
    		else
    		{
    			xy[x][y]="o";
    			board(x, y, xy[3][3]);
    			player=1;
    			clrscr();
    		}
    	}
    
    	return 0;
    }
    
    void clrscr() //Clears the screen
    {
        COORD coordScreen = { 0, 0 };
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    void board(int x, int y, string xy[3][3])
    {
    	string f;
    	f = xy[x][y] ;
    	if (x==1)
    	{
    		if (y==1)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 ["<<f<<"][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    		}
    		else if (y==2)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ]["<<f<<"][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    		}
    		else if (y==3)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ][ ]["<<f<<"]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    		}
    	}
    	else if (x==2)
    	{
    		if (y==1)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 ["<<f<<"][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    		}
    		else if (y==2)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ]["<<f<<"][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    		}
    		else if (y==3)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ]["<<f<<"]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    		}
    	}
    	else if (x==3)
    	{
    		if (y==1)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 ["<<f<<"][ ][ ]\n";
    		}
    		else if (y==2)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ]["<<f<<"][ ]\n";
    		}
    		else if (y==3)
    		{
    			cout << "\t\t__TIC-TAC-TOE__\n";
    			cout << "\t\tTwo Player Game\n\n";
    			cout << "\t\t   1  2  3\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ][ ]\n";
    			cout << "\t\t1 [ ][ ]["<<f<<"]\n";
    		}
    	}
    	else
    	{
    		cout << "Some sort of error occurred!";
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void board(int, int, string);
    The prototype

    > void board(int x, int y, string xy[3][3])
    does not match the definition.

    > board(x, y, xy[3][3]);
    The attempted call matches your prototype, but it is not what you want - this is an out of bound array access.
    If you want to pass the whole array, you need to use this call
    board(x, y, xy);
    and make the prototype match the definition.

    The essence of the error message is that the compiler knew about a board which took a single string, but could only find a board which took a 3x3 array of strings.
    Ain't C++ overloaded functions grand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's possible to keep track of the board in one 32-bit int. I made a tictactoe game once and to handle the board it was:
    Code:
    #define PLYR1 0x1
    #define PLYR2 0x2
    #define SQEMPTY 0x0
    
    #define ONESQ(shift) (((board) >> ((shift) * 2)) & 0x3)
    
    unsigned int board = 0;
    char symbols[] = { 0, 'X', 'O' };
    
    void set_square(int square, int player)
    {
      board |= player << ((square - 1) * 2);
    }
    
    int square_is_empty(int sqnum)
    {
      return ONESQ(sqnum - 1) == SQEMPTY;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Quote Originally Posted by Salem
    > board(x, y, xy[3][3]);
    The attempted call matches your prototype, but it is not what you want - this is an out of bound array access.
    If you want to pass the whole array, you need to use this call
    board(x, y, xy);
    and make the prototype match the definition.

    The essence of the error message is that the compiler knew about a board which took a single string, but could only find a board which took a 3x3 array of strings.
    Ain't C++ overloaded functions grand.
    Wow...I never would have thought of that; Thanks Salem, you really are God of the C!(god that sounds corny :-p)

    Quote Originally Posted by itsme86
    It's possible to keep track of the board in one 32-bit int. I made a tictactoe game once and to handle the board it was:
    Code:
    #define PLYR1 0x1
    #define PLYR2 0x2
    #define SQEMPTY 0x0
    
    #define ONESQ(shift) (((board) >> ((shift) * 2)) & 0x3)
    
    unsigned int board = 0;
    char symbols[] = { 0, 'X', 'O' };
    
    void set_square(int square, int player)
    {
      board |= player << ((square - 1) * 2);
    }
    
    int square_is_empty(int sqnum)
    {
      return ONESQ(sqnum - 1) == SQEMPTY;
    }
    Umm...Yeah, that's all fine and well except that I have no idea what any of that says...I have only been using C++ for a little over a week; I finally have a grasp of functions(at least until arrays come into the picture) and I know a little about classes and structs; The clrscr() function was something that I got from this site because I found it to be useful; However I'm unfamiliar with the #define command, what's that about? Is it the same thing as saying int plyr 0x1? What kind of variable is this making? How do I take information from this function? Basically, could you explain it please? I'm rather new to this as I've said...
    Last edited by mkylman; 08-28-2006 at 10:36 AM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    #define is a preprocessor directive (just like #include is). If you have #define x y it basically replaces every instance of x with y in your code before doing any compiling.

    Forget the code I posted until you get into bit operators. Didn't mean to confuse you.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Sorry, thanks for trying...Wait, one last question, when you say it basically replaces every instance of x with y, does that mean that when you say
    Code:
    #define PLYR1 0x1
    #define PLYR2 0x2
    #define SQEMPTY 0x0
    You're replacing every instance of PLYR1 with 0x1? I know you said forget the code till I get to bit operators, but...

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's right. gcc has an option (-E) that just runs your code through the preprocessor and then spits out the resulting code. When I do that on my tictactoe program I get this for the square_is_empty() function:
    Code:
    int square_is_empty(int sqnum)
    {
      return (((board) >> ((sqnum - 1) * 2)) & 0x3) == 0x0;
    }
    In my source file the function is:
    Code:
    int square_is_empty(int sqnum)
    {
      return ONESQ(sqnum - 1) == SQEMPTY;
    }
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    This may be a dumb question, but I'm not too worried about that at this point; Is using the #define preprocessor a neccesity at times, or is it just to make the code less cluttered? also, I noticed a tutorial on here called bitwise operators in c and c++, is that what you were talking about when you said "wait until you get into bit operators"? I'm assuming that bit and bitwise are the same, but with how much I have been wrong about so far, I guess it doesn't hurt to ask;
    Last edited by mkylman; 08-28-2006 at 11:24 AM.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's just for readability (would you rather see == 0x0 or == SQEMPTY? The latter gives you an indication of what you're testing and some kind of significance to the value) and ease of maintenance (would you rather have to comb through your code replacing every 0x0 with a new value or just change a single #define?).

    It does allow you to do some tricky things that aren't possible without using #define, but those are much more advanced than you're ready for at this point.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Okay; Thanks a million man, you've cleared a lot of things up for me; Just out of curiousity but how long have you been programming?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I've never had any formal training in programming (I learned from various books and playing with existing code), but I've been tinkering around with it for 20 years.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Wow...........Strike what I said about Salem, YOU'RE God of the C!

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm not sure how much blasphemy is tolerated here
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    lol; By the way you talked about some things I figured you were a teacher or something; And Salem, I apologize, don't strike me dead with your Trident...

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. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM