Skip to content

Instantly share code, notes, and snippets.

@tmathmeyer
Last active December 19, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmathmeyer/5906083 to your computer and use it in GitHub Desktop.
Save tmathmeyer/5906083 to your computer and use it in GitHub Desktop.
lol omesh
public class RPSGame extends JFrame implements ActionListener
{
int wins = 0;
int ties = 0;
int total = 0;
JPanel scores;
public RPSGame(String title)
{
super(title);
JPanel game = new JPanel();
game.setPreferredSize(new Dimension(300,300));
game.setLayout(new GridLayout(1,2)); //or 2,1, I cant remember which
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(4,1)); //or 1,3, i cant remember which way is which
scores = new JPanel();
scores.setLayout(new GridLayout(1,1));
GameButton rock = new GameButton(1, "pathToFile");
GameButton paper = new GameButton(2, "pathToFile");
GameButton scisors = new GameButton(3, "pathToFile");
GameButton quit = new GameButton(10, "pathTofile");
buttons.add(rock);
buttons.add(paper);
buttons.add(scisors);
buttons.add(quit);
rock.addActionListener(this);
paper.addActionListener(this);
scisors.addActionListener(this);
quit.addActionListener(this);
game.add(scores);
game.add(buttons);
this.setLayout(new GridLayout(1,1));
this.add(game);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
GameButton source = (GameButton)(e.getSource());
int computerHas = (int)(Math.random() * 3)+1;
int differnce = source.value - computerHas;
if (difference > 5)
{ //quit
System.exit(0);
}
if (difference==1 || difference==-2)
{ //human wins
wins++;
}
if (difference == 0)
{ //tie
ties++
}
total++;
scores.removeAll();
scores.add(new JLable("wins: "+wins+"\nlosses: "+(total-wins-ties)+"\nties: "+ties+"\npercentage: %"+(wins/total)));
scores.repaint();
}
}
class GameButton extends JButton
{
int value; //1=rock, 2=paper, 3=scissors
public GameButton(int val, String imgLog)
{
super("", imgLog);
this.value = val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment