Thread: problems with my tic tac toe game

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    problems with my tic tac toe game

    I have already written a tictactoe, but im doing it again because before i had all global variables. im not far right now on the new one, and im having some problems.

    Code:
    #include <iostream.h>
    
    void InitiateBoard(char Board[4][4]);
    
    void PlayerMove();
    void ComputerMove();
    
    void UpdateDisplay(char Board[4][4]);
    
    void CheckForWin();
    
    int main()
    {
    
    	//Create Variables
    	char Board[4][4];
    
    	InitiateBoard(Board);
    	UpdateDisplay(Board);
      
    
    	return 0;
    }
    
    void InitiateBoard(char Board[4][4])
    {
    	int count=0;  //Set as numbers so the user
    	for(int a=1;a<=3;a++)//can just put what number
    	{                    //of the spot they want.
    		for(int b;b<=3;b++)
    		{
    			count++;
    			Board[a][b]=static_cast<char>(count);
    		}
    	}
    }
    
    void UpdateDisplay(char Board[4][4])
    {
    	for(int a=1;a<=3;a++)
    	{
    	    cout<<Board[a][1]<<" "<<Board[a][2]<<" "<<Board[a][3]<<endl;
    	}
    }
    i run it and a windows error pops up and gives me the "send" or "dont send" stuff (xp usere know what im talking about). it doesnt display anything in the console window, just "press any key to continue". also, when i pass arrays to functions, they are automatically converted to pointers, right?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    In your initialization function this little line is the culprit:
    >for(int b;b<=3;b++)
    You declare b but forgot to initialize it to 0. This causes it to have a garbage value that is more often than not out of bounds for the array.

    >Board[a][b]=static_cast<char>(count);
    This probably also won't do what you want judging by your comments

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    wow dude thanks! now the only problems is with that so far is...instead of printing out numbers it gives me the little symbols. probably ascii or hex or some weird thing like that. should i be using something other than static_cast<>()?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >now the only problems is with that so far is...
    That's the other thing I was talking about. Board is a char array, you're trying to place ints in it and when you try to print the board, the ASCII values for those integers will be printed. On Windows it should give you some odd looking characters, blanks spaces, and a bell. Which are all consistent with the first few values in the ASCII character set.

    Forget the cast when you make the assignment, that does nothing for you. Instead cast the values contained in Board to int when you print them and it should work out okay.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    dude, prelude. i gotta hand it to you. you are the bomb at c++! maybe i just suck, but thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe AI roadblock >:-(|)
    By dark_rocket in forum Game Programming
    Replies: 5
    Last Post: 06-12-2006, 05:13 AM
  2. OpenGL tic tac toe
    By asbo60 in forum Game Programming
    Replies: 5
    Last Post: 05-28-2006, 08:14 AM
  3. Tic tac toe game
    By Seth in forum C Programming
    Replies: 9
    Last Post: 05-04-2003, 09:21 AM
  4. Tic - Tac - Toe...
    By TheUnknowingOne in forum Game Programming
    Replies: 1
    Last Post: 09-10-2002, 06:01 AM
  5. 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