vendredi 28 avril 2017

Peg Solitaire buttons in Java

I'm very new to coding, and struggling with a part of my final project. The project is very open ended: make a graphical java application. I've chosen a peg solitaire game, which as you'll see from the image below:

http://ift.tt/2oQgfRF

Incorporates a similar concept of 'jumping' pieces that a game like checkers uses. This is what I made so far, a board made up of a 7x7 grid of jbuttons, each uses a square pic to represent a part of the board; though I've left buttons intentionally blank in place of the spaces that would be on a normal board.

The problem I've run into is the next step, the game logic itself; I'm trying to figure out how to implement a system where I can click on a peg, and then click on an empty space to 'move' the peg (Which in this instance, would just be switching the jbutton image to the 'slotted' square). However, the game has to recognize whether or not the space clicked on is filled, whether or not the second space clicked is empty, and whether or not the space BETWEEN these two spaces is filled - because in this game, pegs can only move over another peg, and only into a space that is empty.

The pegs cannot move diagonally, only on the four compass directions. I put together this with my instructor's help:

I tried to add to it and figure out how to implement what I described above but I just can't really grasp it. I know what I need to do, but struggling on how to execute it. This code allows me to click on the identified square and 'empty' it, and also allows me to click on the empty space to remove the peg from the square between this and the first square.

But I don't know what the code for identifying when a space is filled or not, or the code for even differing between the empty space and filled space. I assume it's some kind of if statement and the name of the space and tied to which picture is being currently used (if it's the empty hole pic or the filled slotted pic). And then an else statement and...

Well, like I said, I'm very new to all this, and google searching on the subject isn't helping me because I only half understand it, and don't know how to translate the information given to the project I have.

    private class ButtonHandler implements ActionListener

    {
        public void actionPerformed(ActionEvent event)

    {

        if(event.getSource() == boxEleven)
        boxEleven.setIcon(holePic);

        if(event.getSource() == boxTwentyFive)
{
    boxTwentyFive.setIcon(peggedHolePic);
    boxEighteen.setIcon(holePic);
}

    else if(event.getSource() == boxEleven)
    boxEleven.setIcon(


    }

}

Aucun commentaire:

Enregistrer un commentaire