Thread: rotating the board

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    rotating the board

    hey guys,

    I'm trying to improve the VERY SIMPLE ai version of my tic-tac-toe game. Right now I have about 200 lines worth of if statements, and it looks ugly.

    I'm pretty sure there is a way of handling it another way. Keep in mind that I'm only using functions and not arrays or strings. I was thinking about rotating the board. If, for example, the board looks like this:
    Code:
            p1 | p2 | p3
            ---------------
            p4 | p5 | p6
            ---------------
            p7 | p8 | p9
    where px is a position on the board. How could I make the board "rotate"? basically what I mean, if lets say player X takes p1 and than the program O chooses p5, on the next move player X chooses p7, and O should go for the block at p4. But a similar situation could start from all four corners. So, instead of writing all the possibilities I would like to make it simpler.

    any ideas?

    axon

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I could be wrong but i think you might have to implement some array usage to accomplish this.....

  3. #3
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    You could have it so that each p1,p2,px etc is 0 when it is not taken, 1 when it is taken by an X, and -1 when taken by a . Then something like this:

    Code:
    if(p1==1&&p4==1&&p7!=-1)//if p1 and p4 are X and p7 is not already Y
    {
    p7=-1;//takes p7 as Y
    }
    This way you would have to program in every possibility though, and while it would be really easy, it may be time consuming. I'm sure this could be accompished much easier with an array and a while loop...

    //napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You can use a two dimensional array that keeps the coordinates of the grid depending on what direction the board is facing, and a separate integer that says what direction the board is currently facing. Like so:
    Code:
    int gridIndex[4][9]={{0,1,2,3,4,5,6,7,8},{6,3,0,7,4,1,8,5,2},{8,7,6,5,4,3,2,1,0},{2,5,8,1,4,7,0,3,6}};
    int direction=0;  // 0=up 1=right 2=down 3=left
    Now if grid[8] is your array that holds the information of your board, then all you would have to do is:
    Code:
    grid[gridIndex[direction][0]]
    to access the top left corner of your board irregardless of which way the board is facing. Let me know if this doesn't make sense and I'll try to explain it better
    Last edited by PJYelton; 02-25-2003 at 10:10 AM.

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by PJYelton
    Let me know if this doesn't make sense and I'll try to explain it better
    If you could expain it a bot more PJYelton, I would appreciate it. I just begun arrays today in school. So i grasp the basic concept...but farther elaboration would be great!

    thanks,

    axon

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Well, I'm not going to go into arrays too much since thats a huge topic unto itself. But you have your tic tac toe grid set up in an array where grid[0] is the top left corner, grid[1] is the top spot, etc all the way to grid[8] being the bottom right corner. Then to print out your grid you would do:
    Code:
    for (int x=0; x<9; x++)
    {
        cout<<grid[x];
        if ((x+1)%3==0)
            cout<<endl;
    }
    Now to "rotate" the board. I put rotate in quotation marks because in reality you really aren't rotating anything. All you are doing is calling the index numbers of the grid array in a different order. If the grid is point up, then the top left corner is 0. If it is pointing to the right, then the top left corner is 6. Down its 3 and left its 8. So to keep track of the order of the all the numbers depending on which way the board is pointing I use the two-dimensional array and another integer which keeps track of which direction the board is currently facing. So put this into your compiler and watch it rotate:
    Code:
    char grid[9]={'X','0','0','0','X','X','0','X','0'};
    
    int gridIndex[4][9]={{0,1,2,3,4,5,6,7,8},{6,3,0,7,4,1,8,5,2},{8,7,6,5,4,3,2,1,0},{2,5,8,1,4,7,0,3,6}};
    
    int direction=0;  // 0=up 1=right 2=down 3=left
    
    for (direction=0; direction<4; direction++)
    {
       for (int x=0; x<9; x++)
       {
           cout<<grid[gridIndex[direction][x]];
           if ((x+1)%3==0)
               cout<<endl;
       }
       cout<<endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a second opinion - code error and i cant see it
    By bigfootneedhelp in forum C Programming
    Replies: 19
    Last Post: 10-25-2007, 06:02 AM
  2. Constructor problem
    By rebel in forum C++ Programming
    Replies: 22
    Last Post: 01-11-2006, 06:45 AM
  3. function trouble
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2005, 05:23 AM
  4. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 07:27 AM