Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Peg Solitaire  (Read 3221 times)
0 Members and 1 Guest are viewing this topic.
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Posted 2005-01-06 17:55:39 »

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 .. :/

As said.. my math.. my logic.. my brilliance.. that never were... Wink
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-01-06 18:13:24 »

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

Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #2 - Posted 2005-01-06 18:23:15 »

If it's that easy.. I really need too find my old schoolbooks again..

I'll try that, many thanks for your help!  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #3 - Posted 2005-01-06 19:04:06 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
[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.
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #4 - Posted 2005-01-07 04:26:43 »

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.
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #5 - Posted 2005-01-07 04:52:12 »

Quote


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

Kev


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..
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2005-01-07 05:28:03 »

Sorry, it was just psudo code.

Kev

Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #7 - Posted 2005-01-07 05:56:03 »

Oh don't be sorry, it was too early in the morning.. I think I got a hold of it now Smiley

again, thanks for the help!
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #8 - Posted 2005-01-07 13:32:41 »

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
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2005-01-07 14:26:56 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
  
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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #10 - Posted 2005-01-07 14:46:19 »

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?
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #11 - Posted 2005-01-07 15:15:25 »

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.


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.
Offline woogley
« Reply #12 - Posted 2005-01-07 21:42:05 »

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 Tongue
Offline elwiz

Senior Newbie




Bom-Shackalack, Jatack!


« Reply #13 - Posted 2005-01-11 16:33:32 »

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..  Smiley
I load a saved PlayBoard object using my
1  
2  
3  
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
Offline gibell

Innocent Bystander




Java games rock!


« Reply #14 - Posted 2005-01-18 00:00:20 »

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

http://www.geocities.com/gibell.geo/pegsolitaire/index.html
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!