# rotating the board

• 02-24-2003
axon
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
• 02-25-2003
RoD
I could be wrong but i think you might have to implement some array usage to accomplish this.....
• 02-25-2003
napkin111
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
• 02-25-2003
PJYelton
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 :D
• 02-25-2003
axon
Quote:

Originally posted by PJYelton
Let me know if this doesn't make sense and I'll try to explain it better :D
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
• 02-26-2003
PJYelton
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;
}