by tomcant » Thu Feb 24, 2005 2:24 pm
I have this structure which represents a single chip.
[syntax="cpp"]struct chip_info{int color, move, x_f, y_f, x_t, y_t;}[/syntax]
`x_f' and `y_f' are x-from and y-from. `x_t' and `y_t' are x-to and y-to. (x_t and y_t will only hold values if were moving that particular chip). `move' holds the score for each chip (don't know why I called it move). `color' will hold a value of 1 if its red (computer), a value of 0 if its black (human) and a value of 2 if its a blank square.
Then, I have the following array to represent each square on the 8x8 board.
[syntax="cpp"]chip_info board[8][8];[/syntax]
When the user clicks a square, x_f and y_f are given the x/y values of the chip they clicked on, then when the user selects another square, x_t and y_t are given values corresponding to the new square.
Heres what happens when the user has made a move, ie. heres the comps AI:
[syntax="cpp"]bool comp_turn() {
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(board[i,j].color==1){
if(i<7&&j<7&&board[i+1,j+1].color==2)
board[i,j].move=0;//forward right
if(i<7&&j>0&&board[i+1,j-1].color==2)
board[i,j].move=1;//forward left
if(i<5&&j>0&&j<7&&board[i+2,j].color==0)
board[i,j].move=2;//to a taking position left/right
if(i<5&&j>2&&board[i+2,j-2].color==0)
board[i,j].move=3;//to a taking position left
if(i<5&&j<5&&board[i+2,j+2].color==0)
board[i,j].move=4;//to a taking position right
if(i<5&&j<6&&j>0&&board[i+1,j+1].color==2&&board[i,j+2].color==1&&board[i+3,j-1].color==2)
board[i,j].move=5;//logical-move to take right
if(i<5&&j<7&&j>1&&board[i+1,j-1].color==2&&board[i,j-2].color==1&&board[i+2,j].color==0&&board[i+3,j+1].color==2)
board[i,j].move=6;//logical-move to take left
if(i==6&&j>0&&board[i+1,j-1].color==2)
board[i,j].move=7;//to king left
if(i==6&&j<7&&board[i+1,j+1].color==2)
board[i,j].move=8;//to king right
if(i<6&&j<6&&board[i+1,j+1].color==0&&board[i+2,j+2].color==2)
board[i,j].move=9;//can take right
if(i<6&&j>1&&board[i+1,j-1].color==0&&board[i+2,j-2].color==2)
board[i,j].move=10;//can take left
}
}
}
hi_chip.move=0;
for(int k=0;k<8;k++){
for(int l=0;l<8;l++){
if(board[k,l].move>hi_chip.move){
hi_chip.move=board[k,l].move;
hi_chip.x_f=k;hi_chip.y_f=l;
}
}
}
switch(hi_chip.move){
case 0://forward right
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f+1;
break;
case 1://forward right
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f-1;
break;
case 2://to a taking position left/right
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f+1;
break;
case 3://to a taking position left
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f-1;
break;
case 4://to a taking position right
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f+1;
break;
case 5://logical-move to take right
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f+1;
break;
case 6://logical-move to take left
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f-1;
break;
case 7://to king left
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f-1;
break;
case 8://to king right
hi_chip.x_t=hi_chip.x_f+1;
hi_chip.y_t=hi_chip.y_f+1;
break;
case 9://can take right
hi_chip.x_t=hi_chip.x_f+2;
hi_chip.y_t=hi_chip.y_f+2;
board[hi_chip.x_f+1,hi_chip.y_f+1].color=2;
break;
case 10://can take left
hi_chip.x_t=hi_chip.x_f+2;
hi_chip.y_t=hi_chip.y_f-2;
board[hi_chip.x_f+1,hi_chip.y_f-1].color=2;
break;
}
move_chip(ref hi_chip,3);
for(int m=0;m<8;m++)
for(int n=0;n<8;n++)
board[m,n].move=0;
return false;
}
}[/syntax]
[edit]: Hmm, that wasn't very well explained. But basically, the nested for loop gives each chip a score based on what it can do, then the next for loop finds the chip with the greatest score. Then I move that chip according to it's score.
If it wasn't for C, we would be using BASI, PASAL and OBOL.