| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
AI for Marble Solitaire game
I am programming a game of Marble Solitaire for a first year asssignment in a Computer games technology course, and one of the requirements is to make it so that at some direction from the user, the game will play itself so as to perhaps discover the furthest that a marble could possibly move upwards on a 28 by 12 grid with the bottom 7 rows all filled with marbles. I have created the "game board" using a 2d array with array addresses 0-139 as 0's and address 140-335 as 1's and as the marble moves, the space that the marble jumps to having to be a 0 in the first place becomes a 1, and the space that it just left along with the marble that it has to jump according to the rules of Marble Solitaire becoming 0. I have never before had to even attempt any form of AI in programming before, however I have gone about doing it by creating a loop, which is to be called up every time a certain button is pushed. I have developed this function which runs through every value of the array and moves each piece up unless it cannot then it will move either right or left at random:
void cmovepiece()//Runs rather unintelligent Artificial Intelligence { int i; int j; int b; srand(time(NULL)); for(j=0;j<LEVELHEIGHT;j++){ for(i=0;i<LEVELWIDTH;i++) { if(level[j][i]==1)// 1:if current position is 1 { if(level[j-1][i]==1) // 1a:and if position above current position is 1 { if(level[j-2][i]!=1) // 1aa:and if position 2 above current position is 1 { if(j-2<0){ //1aaa:if position 2 above current positions address is less than 0 in array do nothing break; }else // close of loop 1aaa level[j][i]==0; //otherwise set current position to 0 level[j-1][i]=0; //set postion above current postion to 0 level[j-2][i]=1; //set position 2 above to 1 } //close of loop 1aa } //close of loop 1a if(level[j-1][i]!=1) // 1b: if position above current is not 1 { b=rand()%2; // randomly pick between 0 and 1 switch(b){ case 0: //if b=0 if(level[j][i+2]=0){ //and if position 2 right of current is 0 if(level[j][i+1]=1){ // and if position 1 right of current is 1 level[j][i+1]=0; //set position 1 right of current to 0 level[j][i+2]=1; //set position 2 right of current to 1 } else break; //otherwise do nothing }else break; //otherwise do nothing case 1: // if b=1 if(level[j][i-2]=0){ //if space 2 left of current is 0 if(level[j][i-1]=1){ //and if space 1 left of current is 1 level[j][i-1]=0; // set 1 left of current to 0 level[j][i-2]=1; // set 2 left of current to 1 } else break; //otherwise do nothing }else break; //otherwise do nothing default: //if my math sucks :P break; // do nothing } } } //close of loop 1b } } drawpiece(); //go to function that updates screen after the new values } Now that that is out of the way, I will explain the problem I have been having. First of all, I need to make it so that only 1 move is done per button push, this function scans and moves for every piece in the array. Secondly, some of the pieces appear to completely ignore the "rules" and move upwards without jumping any other marbles, and in the end when the computer decides that no more pieces can be moved, there are always quite a few that could quite obviously jump left or right, which would then lead to a jump or two upwards. Any suggestions would be most appreciated, as I have never done AI before, and this was just what I could come up after a random bit of inspiration. If I have not explained well enough that someone could help me from providing this one function, I could include my entire source, but it uses the SDL library to display the graphics, which would involve quite a few files to keep up with. I have also been having trouble getting my program to store the x and y coordinates so that I move the pieces manually. If anyone knows the SDL library, and can do that please try and catch me on MSN. My MSN e-mail is Cefwyn69@hotmail.com. Im on nearly 24-7 so it shouldnt be too hard. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > AI for Marble Solitaire game |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|