Skip to main content
gamedev.net
🔒 Locked

[java] Algorithm?

Started by 1st_maza Jan 23, 2010 at 2:51 PM 4 replies 1.1k views
Original Post
1st_maza
1st_maza
Would I need an algoritm to save all positions, that has to be stated to simulate a chessboard or a checker game..? I seem to come to my limit in mathematical intelligence =o] I have the fallowing positions on the board that the pieces HAS to be placed on: Row - Col ..1.......2 ..1.......4 ..1.......6 ..1.......8 ..2.......1 ..2.......3 ..2.......5 ..2.......7 ..3.......2 ..3.......4 ..3.......6 ..3.......8 ..6.......2 ..6.......4 ..6.......6 ..6.......8 ..7.......1 ..7.......3 ..7.......5 ..7.......7 ..8.......2 ..8.......4 ..8.......6 ..8.......8 Those numbers would placed the pieces like the picture shows: http://mcvey.files.wordpress.com/2007/01/checkers.jpg But instead of making 64 lines of code with almost identical coding, with the changes of the numbers. I could use a loop of some sort but I cant seem to find the algoritm or whatever you might call it, to solve this problem. Spent last two days with this problem and I just cant seem to figure out what I have to do. Any ideas or keywords I should look for when figuring this out?
Sappharos
Sappharos
Well, this is just a newbie guess, but you could make an array of pointers, with each pointing to a different square. Edit the pointers each time to reflect the squares you want, and set all unused pointers to zero. Then just iterate through the non-zero pointers in the array. I did a similar thing when I had to select cells in a game of Tic-tac-toe. I hope that's of use.
1st_maza
1st_maza
I was hoping I could use some kind of funktion that calls itself and increase the value that way.. if I was hoping I wouldnt have to use an array, not difficult to do but I wanted to see if I could create something in the moment of creation of the program.

edit:
ok, Ill go with this idea.

Thx =o]
YasuoDancez
YasuoDancez
There is a algorithm called minmax, check out this youtube berkely video...
Alrecenk
Alrecenk
I'm not entirely sure what you are asking, but I think I can help. First off, those coordinates you have posted are incorrect. In a checkerboard the possible locations you can move to have the property that the x coordinate + y coordinate is always even(or always odd depending on how you set your axes). The pieces of a player either go in the last three or the first 3 rows. So, You can place the pieces algorithmically like this:
for(x=1;x<=8;x++){  for(y==1;y<=8;y++){  if((x+y)%2 == 0){//if on a possible square(may need to be == 1 depending on convention)    if(x <=3){      //place black piece at (x,y)    }else if(x >=6){      //place red piece at (x,y)    }else{      //leave empty    }  }else{    //leave empty  }}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.