Tech Support Forum banner
Status
Not open for further replies.

Problem with simple Java game

2K views 11 replies 3 participants last post by  DangerousClick 
#1 ·
Hello TSF, I've been learning Java recently, and I've been developing a small game that I've been having some problems with now that I'm in beta. It's mostly for me to test my skills, and I really didn't want to have to ask for help, but I can't figure this out for the life of me.

So the way the game works in real life is you hold out both of your hands, and on both of your hands you start with one point (one finger out) and when you hit your opponents hand they gain as many points as you have on the hand you hit them with. If they have 5 points on one hand then that hand is out and can't be hit, if it's greater than 5 is reverts back to zero plus the remainder of what it took to make it to 5. You can also do a split, which is if you have an even number in one hand, and nothing in the other then you can split it half with the empty one.

Now that the rules are out of the way, here's my problem: It seems to use the sum of both the points of the virtual left hand and the virtual right hand.

Here's all of my code for it, there's no official anything tied to it, but please don't just take it and call it yours.

Start class
Code:
package Stix;

public class Start {
    public static void main(String[] args) {
        new StixMain();
    }
}
StixMain class
Code:
package Stix;

import sun.plugin2.os.windows.Windows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StixMain extends JFrame implements ActionListener {
    static Player Player1 = new Player();
    static Player Player2 = new Player();

    private static JLabel playerTurn;
    private static JLabel P1LeftHand;
    private static JLabel P1RightHand;
    private static JLabel P2LeftHand;
    private static JLabel P2RightHand;

    private JButton hitLeftwithLeft;
    private JButton hitLeftwithRight;
    private JButton hitRightwithLeft;
    private JButton hitRightwithRight;
    private JButton split;
    private static int turn = 1;

    public StixMain() {
        super("Stix");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setSize(400,300);
        this.setLocationRelativeTo(getRootPane());

        playerTurn = new JLabel("Player turn is: P" + turn);
        playerTurn.setHorizontalTextPosition(SwingConstants.CENTER);
        playerTurn.setSize(100, 25);
        playerTurn.setLocation(140, 5);
        this.add(playerTurn);

        P1LeftHand = new JLabel("P1 Left Hand: " + Player1.getLeft());
        P1LeftHand.setHorizontalTextPosition(SwingConstants.CENTER);
        P1LeftHand.setSize(100, 25);
        P1LeftHand.setLocation(30,30);
        this.add(P1LeftHand);

        P1RightHand = new JLabel("P1 Right Hand: " + Player1.getRight());
        P1RightHand.setHorizontalTextPosition(SwingConstants.CENTER);
        P1RightHand.setSize(100, 25);
        P1RightHand.setLocation(260,30);
        this.add(P1RightHand);

        P2LeftHand = new JLabel("P2 Left Hand: " + Player2.getLeft());
        P2LeftHand.setHorizontalTextPosition(SwingConstants.CENTER);
        P2LeftHand.setSize(100, 25);
        P2LeftHand.setLocation(30,55);
        this.add(P2LeftHand);

        P2RightHand = new JLabel("P2 Right Hand: " + Player2.getRight());
        P2RightHand.setHorizontalTextPosition(SwingConstants.CENTER);
        P2RightHand.setSize(100, 25);
        P2RightHand.setLocation(260,55);
        this.add(P2RightHand);

        hitLeftwithLeft = new JButton("Hit their left with left");
        hitLeftwithLeft.setSize(165, 30);
        hitLeftwithLeft.setLocation(10, 110);
        hitLeftwithLeft.addActionListener(this);
        this.add(hitLeftwithLeft);

        hitLeftwithRight = new JButton("Hit their left with right");
        hitLeftwithRight.setSize(165, 30);
        hitLeftwithRight.setLocation(10, 165);
        hitLeftwithRight.addActionListener(this);
        this.add(hitLeftwithRight);

        hitRightwithLeft = new JButton("Hit their right with left");
        hitRightwithLeft.setSize(165, 30);
        hitRightwithLeft.setLocation(210,110);
        hitRightwithLeft.addActionListener(this);
        this.add(hitRightwithLeft);

        hitRightwithRight = new JButton("Hit their right with right");
        hitRightwithRight.setSize(165, 30);
        hitRightwithRight.setLocation(210,165);
        hitRightwithRight.addActionListener(this);
        this.add(hitRightwithRight);

        split = new JButton("Split");
        split.setSize(75,30);
        split.setLocation(155,220);
        split.addActionListener(this);
        this.add(split);


        playerTurn.setVisible(true);
        P1LeftHand.setVisible(true);
        P1RightHand.setVisible(true);
        P2LeftHand.setVisible(true);
        P2RightHand.setVisible(true);
        hitLeftwithLeft.setVisible(true);
        hitLeftwithRight.setVisible(true);
        hitRightwithLeft.setVisible(true);
        hitRightwithRight.setVisible(true);
        split.setVisible(true);
        this.setVisible(true);
    }


    /* Getting actionlistener stuffs */
    public void actionPerformed (ActionEvent e) {
        if (e.getActionCommand().equals("Hit their left with left"))
            hitLeftwithLeftDo();
        else if (e.getActionCommand().equals("Hit their left with right"))
            hitLeftwithRightDo();
        else if (e.getActionCommand().equals("Hit their right with left"))
            hitRightwithLeftDo();
        else if (e.getActionCommand().equals("Hit their right with right"))
            hitRightwithRightDo();
        else if (e.getActionCommand().equals("Split"))
            splitDo();
    }



    /* Actions */
    private static void hitLeftwithLeftDo() {
        if (turn == 1) {
            int i = Player1.getLeft();
            Player2.hitLeft(i);
            P2LeftHand.setText("P2 Left Hand: " + Player2.getLeft());
            if (Player2.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 1 won", "You won!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
            turn = 2;
        }
        else if (turn == 2) {
            int i = Player2.getLeft();
            Player1.hitLeft(i);
            P1LeftHand.setText("P1 Left Hand: " + Player1.getLeft());
            if (Player1.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 2 won", "You won!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
            turn = 1;
        }
        playerTurn.setText("Player turn: P" + turn);
    }

    private static void hitLeftwithRightDo() {
        if (turn == 1) {
            int i = Player1.getRight();
            JOptionPane.showMessageDialog(null, i);
            Player2.hitLeft(i);
            JOptionPane.showMessageDialog(null, Player2.getLeft());
            P2LeftHand.setText("P2 Left Hand: " + Player2.getLeft());
            if (Player2.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 1 won", "You won!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
            turn = 2;
        }
        else if (turn == 2) {
            int i = Player1.getRight();
            Player1.hitLeft(i);
            P1LeftHand.setText("P1 Left Hand: " + Player1.getLeft());
            if (Player1.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 2 won", "You won!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
            turn = 1;
        }
        playerTurn.setText("Player turn: P" + turn);
    }

    private static void hitRightwithLeftDo() {
        if (turn == 1) {
            int i = Player1.getLeft();
            Player2.hitRight(i);
            P2RightHand.setText("P2 Right Hand: " + Player2.getRight());
            if (Player2.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 1 won", "You won!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
            turn = 2;
        }
        else if (turn == 2) {
            int i = Player2.getLeft();
            Player1.hitRight(i);
            P1RightHand.setText("P1 Right Hand: " + Player1.getRight());
            if (Player1.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 2 won", "You won!", JOptionPane.PLAIN_MESSAGE);
            }
            turn = 1;
        }
        playerTurn.setText("Player turn: P" + turn);
    }

    private static void hitRightwithRightDo() {
        if (turn == 1) {
            int i = Player1.getRight();
            Player2.hitRight(i);
            P2RightHand.setText("P2 Right Hand: " + Player2.getRight());
            if (Player2.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 1 won", "You won!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
            turn = 2;
        }
        else if (turn == 2) {
            int i = Player2.getRight();
            Player1.hitRight(i);
            P1RightHand.setText("P1 Right Hand: " + Player1.getRight());
            if (Player1.isWin()) {
                JOptionPane.showMessageDialog(null, "Player 2 won", "You won!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
            turn = 1;
        }
        playerTurn.setText("Player turn: P" + turn);
    }

    private static void splitDo() {
        if (turn == 1) {
            if (Player1.isSplitable()) {
                Player1.split();
                P1LeftHand.setText("P1 Left Hand: " + Player1.getLeft());
                P1RightHand.setText("P1 Right Hand: " + Player1.getRight());
                turn = 2;
            }
            else
                JOptionPane.showMessageDialog(null, "Invalid move!", "Invalid", JOptionPane.PLAIN_MESSAGE);
        }
        else if (turn == 2) {
            if (Player2.isSplitable()) {
                Player2.split();
                P2LeftHand.setText("P2 Left Hand: " + Player2.getLeft());
                P2RightHand.setText("P2 Right Hand: " + Player2.getRight());
                turn = 1;
            }
        }
        playerTurn.setText("Player turn: P" + turn);
    }
}
And finally, the Player class
Code:
package Stix;

import javax.swing.*;

public class Player {

    private static int leftHand;
    private static int rightHand;

    public Player() {
        leftHand = 1;
        rightHand = 1;
    }

    public int hitLeft(int i) {
        leftHand += i;
        if (leftHand > 5)
            leftHand -= 5;
        else if (leftHand == 5)
            leftHand = 0;

        return leftHand;
    }

    public int hitRight(int i) {
        rightHand += i;
        if (rightHand > 5)
            rightHand -= 5;
        else if (leftHand == 5)
            rightHand = 0;

        return rightHand;
    }

    public int getLeft() {
        return leftHand;
    }

    public int getRight() {
        return rightHand;
    }

    public boolean isWin() {
        if (getLeft() == 0 && getRight() == 0)
            return true;
        else
            return false;
    }

    public boolean isSplitable() {
        if (leftHand == 0 && rightHand % 2 == 0)
            return true;
        else if (leftHand % 2 == 0 && rightHand == 0)
            return true;
        else
            return false;
    }

    public void split() {
        if (leftHand == 0 && rightHand % 2 == 0) {
            leftHand += rightHand / 2;
            rightHand /= 2;
        }
        else if (leftHand % 2 == 0 && rightHand == 0) {
            rightHand += leftHand / 2;
            leftHand /= 2;
        }
    }
}
As I said, I've no idea why this is happening, but if you see something that could be wrong, please tell me.

Thank you
Crockeo

P.S. I'm willing to bet that it'll be a simple rookie mistake that I just couldn't catch...
 
See less See more
#2 ·
Hello Crockeo.

I've played that game before with a friend, it was pretty interesting. Now, on topic, could you tell us where it's taking the sum of both hands when it's not supposed to? Like, what action is performed that seems to create this issue? Example: When the right hand of player two hits the left hand of player 1?
 
#3 ·
Sorry, I finished writing that at about 1:00 AM. What happens is when I use the hitLeft() or hitRight() method after the 1st turn it always makes the hand that got hit a strange value, and I can't seem to figure out why.

Now for off topic, my friends and I play it whenever we're just sitting somewhere doing nothing, it's surprisingly entertaining.

Thank you!
Crockeo
 
#4 ·
I see nothing wrong with the hitLeft() hitRight() methods, they seem fine to me. Comments would be nice to see what each section is doing without having to re-work the problem in your head.

Could you provide the 'strange values' given from the program that aren't correct? Like the output of the program? Screenshots are OK if you'd like. I'd like to see where the logic fails and look in the code for details, as I'm having a bit of trouble finding the problem.
 
#5 · (Edited)
Exactly my thought, I immediately went to the hitLeft() and hitRight() but couldn't find anything, I'll post again with some exact examples.

EDIT: I just got this one:

Player 1 hits Player 2's left hand with its left hand, which is 1, which brings up Player 2's left hand to 2. Player 2 hits Player 1's right hand with its right hand, which brings up Player 1's left hand to 3.
 
#6 ·
I went through your code line by line, and other than lack of comments it looks really well structured. I simply cannot find the source of the problem your having. I'm curious that it might be something to do with ActionEvent though. You said that it works fine the first loop around correct? Then that could also point to an event being stuck in cue with others piling up from behind. Are you able to debug your program line by line? That way, you can check to see if the values your giving into the event cue are actually being removed from the cue upon being used. It could also point to see if a value is being assigned incorrectly.

By the way, what IDE do you use ( if any )?
 
#7 ·
I usually only add comments after I'm done with writing the code, but thank you for complementing my code. I've been working a lot recently on creating clean code because before I was a little sloppy. As for IDE I use IntelliJ Idea Community Edition (because it's free).

I'll try to debug it even more, but right now I can't find the reason. I'll look more closely at ActionEvent with your suggestion. Thank you for trying anyway, it's a shame neither of us could figure it out.
 
#8 ·
Lol it's all good, though that's a real crippling on the program if you can't get past turn 2. If the first turn works fine ( from what you've said ), then it really just points to either an event cue issue or the formula to assign values in the program. I doubt it's the second one from what I saw while going through the code, but then again, I did it all in my head.

Several IDE's have a nice debugging feature where you can set a line to where the debugger will stop and display all the information the program would have in the background at that point. If you take it slow and keep pointing it a few lines lower ( chunks of the program code if you'd prefer ), you can observe the values and see when the program goes wrong.
 
#9 · (Edited)
Thank you for your continuous help, I'll try the debug feature next, but what I've just found is that rather than the sum of the left and right hands, it's the hand you're hitting with, but doubled (besides on the first turn).

EDIT: I just thought about it, and it even does it on the first turn, it's just that is seemed normal because 1*2=2 and you're supposed to get 2 on the first turn.

Back to working on Stix!

EDIT: I lied, I'm working on a Snake game now, I'll come back to this at some point.
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top