Whats wrong with my array?

This is a discussion on Whats wrong with my array? within the C++ Programming forums, part of the General Programming Boards category; Munkey01... To get back to the first case for a moment if I could... You seem to have missed the ...

  1. #16
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Munkey01...

    To get back to the first case for a moment if I could... You seem to have missed the point that you are declaring the array wrong (in the case of your original post with strings)...

    It needs to be like this

    char board[3][3][4]

    You have 3 Rows, and 3 columns, each containing a 4 character string (including the 0 terminator).

    The way that you had declared it only allowed for one character in each cell of the matrix. You need to allocate space for the 4 characters.

    In the case of ints, as Salem has written it, each digit is stored in one int....

    To do this with char you would be limited to 1 char per cell...

    Code:
    char board[3][3] = {{'1','2','3'},{'4','5,''6,'},{'7','8','9'}};

  2. #17
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    or an array of doubles

    double board[3][3] = {0.1, 0.234, 0.45, 34.56, 0.9, 8.7, 0.00001, 9.3, 1.0};
    int i, j;
    for(i = 0; i < 3; ++i)
    {
    for(j = 0; j < 3; ++j)
    {
    cout << board[i][j] << ' ';
    }
    cout << endl;
    }

  3. #18
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Thanks so much for all of your help. I just realized what I had done, lol, I intialized the board in the class declaration (right word?) so I changed it so that the constructor initailized it. I also changed around some other stuff to make it more legible and reasonable. I am not done yet, but I did compile it to see what errors I would get and I had none!!! I ended up using an integer array still. I also, instead of using X and O i used 50 and 70 to substitute the numbers, just whenever I display the board I check to see if I need to display O or X or if I need to just display the number there.

    I have also changed it like some of you mentioned, I used 1..9 instead of 01..22

    The only thing left to do is the AI, lol. This will be my first program to include AI, but I might just go ahead and make a sperate version with two player capabilities until I finish my AI for the game, then once the AI is done I will implement them into the same game. Sound good?

    If anyone can give me a little example of how I could design the AI? I could do it like I check for a win (search the board for that), but that seems like it would be rather ugly code.

  4. #19
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Ok, I have it running But whenever I call Display() the program gets caught in an infinate loop. I cannot figure out why it is catching Any ideas? Plus I am not sure if this would even work, but I cannot tell when I have a whole bunch of numbers flying around.
    Code:
    for(int j=0; j<3; j++) 
    	{
    		for(int i=0; i<3; i++)
    		{
    			if (board[j][i] == firstP)
    				cout << "\tX";
    			else if (board[j][i] == secP)
    				cout << "\tO";
    			else
    				cout << "\t" << board[j][i];
    			if (i % 3 == 0)
    				cout << endl;
    		}
    	}

  5. #20
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    This piece of code in itself won't create an infinate loop. It must be some other part of the logic. Better post the display function and the function that calls it... if not the whole thing.

  6. #21
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Code:
    void TickTacToe::Display()
    {
    	for(int j=0; j<3; j++)  //nested for loops to display board
    	{
    		for(int i=0; i<3; i++)
    		{
    			if (board[j][i] == firstP)
    				cout << "\tX";
    			else if (board[j][i] == secP)
    				cout << "\tO";
    			else
    				cout << "\t" << board[j][i];
    			if (i % 3 == 0)   //checking for the end of the row
    				cout << endl;
    		}
    	}
    	MakeMove();
    }
    Function that calls it-
    Code:
    void TickTacToe::MakeMove()
    {
    	Display();
    	if (CurPlayer == firstP)
    		cout << "\n\n\nIt's player one's turn.\n";
    	else
    		cout << "\n\n\nIt's player two's turn.\n";
    	cout << "\n\n\nSelect a location: ";
    	cin >> location;
    	if (CheckLocation() == 1)    //if CheckLocation() returns a non-zero value
    		MakeMove();    //restart function
    	switch(location)
    	{
    	case 1: board[0][0] = CurPlayer; break;  //CurPlayer claims the selected position
    	case 2: board[0][1] = CurPlayer; break;
    	case 3: board[0][2] = CurPlayer; break;
    	case 4: board[1][0] = CurPlayer; break;
    	case 5: board[1][1] = CurPlayer; break;
    	case 6: board[1][2] = CurPlayer; break;
    	case 7: board[2][0] = CurPlayer; break;
    	case 8: board[2][1] = CurPlayer; break;
    	case 9: board[2][2] = CurPlayer; break;
    	}
    	CheckWin();
    	if (CurPlayer == secP)   //switch current player
    		CurPlayer = firstP;
    	else
    		CurPlayer = secP;
    	system("cls");
    	Display();
    }
    Oh, and since I posted MakeMove() could I use
    Code:
    if (!CheckLocation())
            MakeMove();
    Instead of
    Code:
    if (CheckLocation() == 1)
            MakeMove();

  7. #22
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Unhappy

    I guess one of two things happened. No one can answer my question because of its complexity (very unlikely). Or it fell down a couple of pages so no one has seen it.

    I know that there is a rule against bumping threads. But I didn't think creating a new thread would be appropriate.

  8. #23
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    it should be where you are calling MakeMove() at the end of display. since MakeMove() calls Display(), it runs through Display() and then calls MakeMove().
    i.e.

    MakeMove() -> Display() -> MakeMove() -> Display() -> ...

    this will just keep going infinitely.

  9. #24
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Ahhhh. That fixes that problem. Thanks man.

    Now I need to fix the switch current player and to fix the display. But atleast I don't have to fix an infinate loop anymore.

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

Similar Threads

  1. dont know what im doing wrong... array
    By jamort in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2009, 01:23 PM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM

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