Peg Solitaire

So … there I am, looking at my peg solitaire board. Looks nice…

But how could I possible figure out what would be a legal move or not? I think I’ve missed a few part in my lousy design.

I have a Vector that contains 9x9 Jpanels that makes upp the Squares of my board-
I have an Array containing all the illegal squares(in the corners, the ones painted black that shouldn’t have been in the first place… GridLayout…)

So, when I press my mouse, I see if I’m grabbing a JLabel (my Gamepiece) and then I get the index of the parent Jpanel. When I release my mousebutton I check if the square is empty and then I can get the index of that Jpanel

But… then what, I have two indexnumbers…? My Math isn’t very good… is there really a magic formula to control that thing?
Or should I store every objects row and column somehow, then I could see if the difference between the “fromSquare” and “targetSquare” row,column are 0,2 or 2,0. One of them must be 0 and I can only jump one square.

Then… of course… I must check wether or not the square I’m jumping contains a Jlabel too … :confused:

As said… my math… my logic… my brilliance… that never were… :wink:

It would probably be easy to store the thing as an array, but if you do just want to use an index… assuming your vector goes across the board first…

xpos = index % 9
ypos = index / 9

valid move = ((oldx = newx) && (Math.abs(oldy-newy) == 2) && middle square occupied)) || ((oldy = newy && (Math.abs(oldx-newx) == 2) && middle square occupied).

Kev

If it’s that easy… I really need too find my old schoolbooks again…

I’ll try that, many thanks for your help! :slight_smile:

And if you ever get into using wierd shapes, like a Risk board style. I found it fast enough to create a ini file that listed all the areas.


[areas]
1=Brazil 12 10
2=Canada 23 69
3=United_States 23 30

[borders]
border1 = 7 2 5
border2 = 9 10 3 12

I use built in java ini reader and a string tokenizer.

The first part holds the area names ( under scores _ = spaces), then follows the x and y for the pieces.

The second part holds all your borders. I found setting up this ini file I could create a board game with some really wierd shaped areas. Works well for games that require a risk look and feel.

I would like to develop a map maker to create the ini file, but havent got around to it yet.

The coolest thing I found, another developer showed me. Was to take the origenal PNG and create a grayscale GIF of the image. The player always sees the hi res color PNG. The GIF is just for processing background things.

Then take area 1 Brazil and make the RBG 1,1,1

Canada would be RBG 2,2,2 and so on.

Thus you can take the GIF image and any x y coordiante and figure out where you are from the RBG value. This also allows you to highlight areas by changing the RBG value to a real color like RED on the file then displaying a sub image, thus it looks like your highlighting the area. Works good for mouse clicks.

I’m still working on the highlighting part though.

Actually, “RISK” like games is exactly what I want to develop, once I get basic knowledge.

Do you have some “real” examples to show? I really like your idea with the .ini file.

To be a real rookie, I can’t write that one. My compiler tells me it’s not legal to use && operator to find out boolean values ???
I’m I drunk or is it just to early…

Sorry, it was just psudo code.

Kev

Oh don’t be sorry, it was too early in the morning… I think I got a hold of it now :slight_smile:

again, thanks for the help!

ok… another silly question.

How do i remove the Jlabel symbolizing the ball i just jumped?
The GamePieces resides in a Vector, but removing them from there does nothing (gamePieces.remove(index)).
I suppose I have to remove the Label itself from the Jpanel where it resides somehow?

Besides I realize I’m stupid. I’m mixing Vectors of Jpanels (representing boardSquares) with vectors of Gamepieces.

Wouldn’t it be easier if I only worked with the Panels and then marked them as empty/notEmpty. I suppose there is a way to see wether or not my Panel holds an object?

So this is were UML comes in… :wink:

Have a nice weekend you all

Can I suggest you think more along these lines (just typed, not tested):


  
public class Sol extends JFrame {

  private Tile[][] board = new Tile[10][10];
  
  public Sol() 
  {
        super("Solitaire");

        // initialise the board to the shape and peg
        // configuration you like here. Read it from a file
        // might be nice
        for (int x=0;x<10;x++) {
           for(int y=0;y<10;y++) {
             board[x][y] = new Tile();
             board[x][y].isValid = true;

             if ((x != 5) && (y != 5))
             {
                 board[x][y].hasPeg = true;
             }
           }
        }

        setSize(640,480);
        setVisible(true);
  }

  public void paint(Graphics g) 
  {
    g.translate(10,40);

    for (int x=0;x<10;x++) {
      for (int y=0;y<10;y++) {
         Tile current = board[x][y];

         if (current.valid)
         {
              g.setColor(Color.blue);
              g.fillRect(x*40,y*40,40,40);

              if (current.hasPeg) {
                  g.setColor(Color.black);
                  g.drawOval((x*40)-15,(y*4)-15,30,30);
              }
         }
      }
    }
  }

  public class MouseHandler extends MouseAdapter {
      public void mouseClicked(MouseEvent e) {
          int xp = (e.getX() - 10) / 40;
          int yp = (e.getY() - 40) / 40;
 
          // Ooer the user clicked at cell xp,yp.. best
          // do something about it

         // after you update the data you'll need to call 
         // this to refresh the screen
         repaint(0);
      }
   }

   public class Tile {
       public boolean isValid;
       public boolean hasPeg;
   }
}

Hope this helps as a pointer. You’ll find it easier than using components to do your board. You’ll also find later that you can make it look much more “gamey” if you do it this way.

Kev

Thanks!

I will try that, as soon as I’ve done the Swing verison. It was my courseleader (I spend my evenings after work to dig into Java) that suggested going the Swing way instead of 2D.

Well, now I have it running, as soon as I can get the Jpanel that I Jump to remove it’s JLabel. i tried the removeAll() which won’t do it. It doesn’t update my view at least. And I can’t use the removeComponent() since I don’t have that Component in a variable. Fells stupid to try to “catch” that one, and actually I don’t know how to do it?

[quote]Actually, “RISK” like games is exactly what I want to develop, once I get basic knowledge.

Do you have some “real” examples to show? I really like your idea with the .ini file.
[/quote]
I have some custom classes and such. Allot to post, but I’ll be putting the project on Source Forge soon. So you’ll be able to look at the source there. I’ll add a link in my signature when I create the project on source forge. So just keep an eye open.

I wrote a Solitaire game a long time ago, you’re welcome to view the source code:
http://woogley.net/java/applet.php?name=Solitaire

I warn you though, that code (not to mention that ugly layout :-/) is very old and very horrid, you can be sure there are better ways to determine legal moves than the way I did. Hope it helps, though :stuck_out_tongue:

So, thanx for all the suggestions helping me out with my lousy peg solitaire.

So far, I’ve come to the conclusion that I should never ever never use Swing components for something like this.
I don’t understand them at all. I can’t get why a removed non-existent component, for example, could be visible? I have to use setVisible(false) before I remove them (I can’t do it afterwards, it’s removed remeber) to get the ugly Icon to disappear? Somehow, I thought the Icon belonged to the Label? Anywya, i could do that it’s ok.

Now, if someone could give me some rookie help… :slight_smile:
I load a saved PlayBoard object using my


ObjectInputStream in = new ObjectInputStream(new FileInputStream("savefile"));
savedBoard=(PlayBoard)in.readObject();
return savedBoard;

Now… to actually update my game and my screen(now I don’t recieve any messages, but nothing at all happens, I don’t get any old board to continue playing with) … what am I supposed to do? Will I have to run some code to actually put my objects back and run a validate() on my 81 JPanel squares or how is it all handled?

Best regards

Feel free to check out my web page on the theory of the game:

http://www.geocities.com/gibell.geo/pegsolitaire/index.html