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
StixMain class
And finally, the Player class
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...
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();
}
}
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);
}
}
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;
}
}
}
Thank you
Crockeo
P.S. I'm willing to bet that it'll be a simple rookie mistake that I just couldn't catch...