Tic Tac Toe display problem

This is a discussion on Tic Tac Toe display problem within the Game Programming forums, part of the General Programming Boards category; I am planning out a TTT game. I am planing to store the board in a 2 deminsional char array. ...

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

    Tic Tac Toe display problem

    I am planning out a TTT game. I am planing to store the board in a 2 deminsional char array. I will be using x for X, o for O, and e for empty. I think it would be a little inconveinent to not display a board to show all current posistions. How would you all display the board? I thought about createing a function to check each element of the array and depending on the value it will display the correct thing and after it comes to 1.3 it will bring it down to the next line and do the same until it finishes (bringing down to a new line after 2.3 also. Is this how you all would do it?

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I would just make a function with a few imbeded for loops to display the array. Then just convert the correct letter to the correct display charector. For example:
    Code:
    char Board[3][3]
    for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
    printf("%c\t", Board[i][j]=='e' ? ' ' : Board[i][j]);
    }
    printf("\n");
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Here is the exact method I used in my (dynamic) tic-tac-toe game. It's dynamic in the sense that you can play on a board from size 3 to size 9.

    Code:
    int map::display(short x, short y)
     {
    short x_coord, y_coord;
    short repos_x, repos_y;
     
      for(short h = 0; h < size; h++)
       {
        for(short i = 0; i < size; i++)
         {
          if(grid[i][h] == 'X' || grid[i][h] == 'O')
           {
    
           gotoxy(value_x[i][h]-1, value_y[i][h]);
           cout << " ";
           if(grid[i][h] == 'X')
            {
             textcolor(LIGHTBLUE);
             cout << grid[i][h];
             textcolor(LIGHTGRAY);
            }
           else if(grid[i][h] == 'O')
            {
             textcolor(LIGHTRED);
             cout << grid[i][h];
             textcolor(LIGHTGRAY);
            }
      /*  else
             cout << grid[i][h]; //unneccessary */
           cout << " ";
           }
         }
       }
     
      gotoxy((size*2)+size-1, (size*2));  
      
     }
    You can download the game/source by following the link in my site. There is a lot of code in the game, because I made it in console and I put a lot of extra stuff in there to liven up the game. You should be able to find an effective way to display the grid by referring to my source. Good luck.

    EDIT: hmm I noticed a small flaw; I commented it out

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Hmmmm, I thought you could have multiple 'arguments' (would that be the word) for what a for loop needs to do (increment i and h in the same for loop?). Not sure never tried it, but thanks alot guys.

  5. #5
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Yes, it is possible to have two iterators in one for loop, Munkey01, however, it's not going to be what you need to do.

    Code:
    char board[3][3];
    
    //you can't have multiple arguments
    //such as ( i < 3; j < 3 ) only multiple iterators
    
    for(short i=0, j=0; i < 3; i++; j++)
     {
       board[i][j] = 'X';
     }
    This is going to make element [0][0], [1][1], and [2][2] equal X. You need to make the loop so that each element will be changed.

    i.e.
    Code:
    char board[3][3];
    
    for(short i=0; i < 3; i++)
     {
      for(short j=0; j < 3; j++)
       {
        board[i][j] = 'X';
       }
     }
    This is going to set all of the elements of the array to X.

    ***Note this was only an example and doesn't exactly relate to making the tic-tac-toe game.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >//you can't have multiple arguments
    >//such as ( i < 3; j < 3 ) only multiple iterators

    actually this is allowed if done this way,
    Code:
     
    int j =10;
    for(int i=0; i < 10, i < j ; i++, j--)
         cout << i << ' ' << j << '\n';
    however, it wont help munkey's situation as you've already pointed out and is pretty usless in this regard but its still has correct syntax

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I will just post this in this thread. But I have started to code it but I have ran into a problem, I really didn't think out how I was going to check for a win (right now it is only going to be player vs player, but once I get the checking for a win in there I will try adding AI). I thought about something like-
    Code:
    if (Board[1][1] == Board[1][2] == Board[1][3])
        cout << Board[1][1] << " wins the game!!!";
    But I am not sure if if statements can be done that way. I also am having a problem knowing how two demisional arrays are set up.

    Would the location "Board[1][3]" be (if being displayed as a TTT board) in the top right hand corner? Or would It be in the bottom left hand corner?

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >>if (Board[1][1] == Board[1][2] == Board[1][3])
    >> cout << Board[1][1] << " wins the game!!!";

    more like this...
    Code:
    if (Board[0][0] == Board[0][1] &&
        Board[0][0] == Board[0][2])
        cout << Board[0][0] << " wins the game!!!";
    >Would the location "Board[1][3]" be (if being displayed as a TTT board) in the top right hand corner? Or would It be in the bottom left hand corner?

    board[0][2] would be the top right corner (remember numbers start at 0 in c++)

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    board[0][2] would be the top right corner (remember numbers start at 0 in c++)
    I had a brain fart. I have been having those recently though I need something like Beano. Atleast I didn't do that in my code though (it would have sucked if I did).

    My brain must have really had gas, I just noticed that I did do that in my code a few times LOL.
    Last edited by Munkey01; 02-27-2003 at 07:34 PM.

  10. #10
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    actually this is allowed if done this way,
    Yeah, you're right; I never even thought about it being practical, so I always assumed it wasn't valid. . .

    Munkey01, all that info wasn't neccessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 09:58 PM
  2. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 04:24 PM
  3. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 10:59 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, 05:21 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM

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