Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.ComboBoxModel;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JProgressBar;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.WindowConstants;
- import javax.swing.SwingUtilities;
- @SuppressWarnings("serial")
- public class C0158_GUI extends javax.swing.JFrame {
- private JPanel jPanel_Console;
- private JButton[] buttons = new JButton[5];
- private ImageIcon[] icons = new ImageIcon[]{
- new ImageIcon("img/Rock.png"),
- new ImageIcon("img/Paper.png"),
- new ImageIcon("img/Scissors.png"),
- new ImageIcon("img/Lizard.png"),
- new ImageIcon("img/Spock.png")
- };
- private JScrollPane jScrollPane_Console;
- private JTextArea jTextArea_Console;
- private JTextField jTextField_lossCount;
- private JTextField jTextField_drawCount;
- private JTextField jTextField_winCount;
- private JProgressBar jProgressBar_drawPerc;
- private JProgressBar jProgressBar_winPerc;
- private JLabel jLabel_Wins;
- private JLabel jLabel_Draws;
- private JLabel jLabel_Losses;
- private JButton jButton_Stats;
- private JComboBox jComboBox_Level;
- private JLabel jLabel_Level;
- private JProgressBar jProgressBar_losePerc;
- ArrayList<Integer> playerMoves = new ArrayList<Integer>();
- final String[] moveNames = new String[] {"Rock", "Paper", "Scissors", "Lizard", "Spock"};
- int[] moveCount = new int[5];
- private int wins = 0;
- private int draws = 0;
- private int losses = 0;
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- C0158_GUI inst = new C0158_GUI();
- inst.setLocationRelativeTo(null);
- inst.setVisible(true);
- }
- });
- }
- public C0158_GUI() {
- super();
- initGUI();
- }
- private void initGUI() {
- try {
- GridBagLayout thisLayout = new GridBagLayout();
- setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
- thisLayout.rowHeights = new int[] {7, 7, 7, 7};
- thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1};
- thisLayout.columnWidths = new int[] {7, 7, 7, 7, 7};
- getContentPane().setLayout(thisLayout);
- {
- jPanel_Console = new JPanel();
- GridBagLayout jPanel_ConsoleLayout = new GridBagLayout();
- getContentPane().add(jPanel_Console, new GridBagConstraints(0, 1, 5, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
- jPanel_ConsoleLayout.rowWeights = new double[] {0.1, 0.2, 0.2, 0.1};
- jPanel_ConsoleLayout.rowHeights = new int[] {7, 7, 7, 7};
- jPanel_ConsoleLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
- jPanel_ConsoleLayout.columnWidths = new int[] {7, 7, 7, 7};
- jPanel_Console.setLayout(jPanel_ConsoleLayout);
- jScrollPane_Console = new JScrollPane();
- jPanel_Console.add(jScrollPane_Console, new GridBagConstraints(0, 0, 4, 4, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
- jTextArea_Console = new JTextArea();
- jTextArea_Console.setEditable(false);
- jScrollPane_Console.setViewportView(jTextArea_Console);
- }
- {
- jLabel_Wins = new JLabel();
- getContentPane().add(jLabel_Wins, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
- jLabel_Wins.setText("Wins:");
- jLabel_Wins.setFont(new java.awt.Font("Segoe UI",1,15));
- }
- {
- jLabel_Draws = new JLabel();
- getContentPane().add(jLabel_Draws, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
- jLabel_Draws.setText("Draws:");
- jLabel_Draws.setFont(new java.awt.Font("Segoe UI",1,15));
- }
- {
- jLabel_Losses = new JLabel();
- getContentPane().add(jLabel_Losses, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
- jLabel_Losses.setText("Losses:");
- jLabel_Losses.setFont(new java.awt.Font("Segoe UI",1,15));
- }
- {
- jTextField_winCount = new JTextField();
- getContentPane().add(jTextField_winCount, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 20), 0, 0));
- jTextField_winCount.setEditable(false);
- jTextField_winCount.setText("0");
- }
- {
- jTextField_drawCount = new JTextField();
- getContentPane().add(jTextField_drawCount, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 20), 0, 0));
- jTextField_drawCount.setEditable(false);
- jTextField_drawCount.setText("0");
- }
- {
- jTextField_lossCount = new JTextField();
- getContentPane().add(jTextField_lossCount, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 20), 0, 0));
- jTextField_lossCount.setEditable(false);
- jTextField_lossCount.setText("0");
- }
- {
- jProgressBar_winPerc = new JProgressBar();
- getContentPane().add(jProgressBar_winPerc, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 20), 0, 0));
- }
- {
- jProgressBar_drawPerc = new JProgressBar();
- getContentPane().add(jProgressBar_drawPerc, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 20), 0, 0));
- }
- {
- jProgressBar_losePerc = new JProgressBar();
- getContentPane().add(jProgressBar_losePerc, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 20), 0, 0));
- }
- {
- jLabel_Level = new JLabel();
- getContentPane().add(jLabel_Level, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
- jLabel_Level.setText("AI Level:");
- jLabel_Level.setFont(new java.awt.Font("Segoe UI",1,15));
- }
- {
- ComboBoxModel jComboBox_LevelModel =
- new DefaultComboBoxModel(
- new String[] { "Easy", "Medium", "Hard"});
- jComboBox_Level = new JComboBox();
- getContentPane().add(jComboBox_Level, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 20), 0, 0));
- jComboBox_Level.setModel(jComboBox_LevelModel);
- }
- {
- jButton_Stats = new JButton();
- getContentPane().add(jButton_Stats, new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 20), 0, 0));
- jButton_Stats.setText("Stats");
- jButton_Stats.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- for(int i = 0; i < moveCount.length; i++){
- printToConsole(moveNames[i] + ": " + moveCount[i]);
- }
- }
- });
- }
- {
- for(int i = 0; i < buttons.length; i++){
- buttons[i] = new JButton(icons[i]);
- getContentPane().add(buttons[i], new GridBagConstraints(i, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
- final int n = i;
- buttons[i].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e){
- play(n);
- moveCount[n]++;
- }
- });
- }
- }
- pack();
- setSize(600, 500);
- setTitle("Rock Paper Scissors Lizard Spock!");
- setResizable(false);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void play(int move){
- printToConsole("Human played " + moveNames[move] + "! ");
- int cm = getComputerMove();
- printToConsole("Computer played " + moveNames[cm] + "! ");
- if(isWin(move, cm) == 1){
- printToConsole("You win!");
- wins++;
- } else if(isWin(move, cm) == 0){
- printToConsole("It was a draw!");
- draws++;
- } else{
- printToConsole("You lose!");
- losses++;
- }
- printToConsole("-");
- playerMoves.add(move);
- redrawProgressBars();
- updateScores();
- }
- public int getComputerMove(){
- if(jComboBox_Level.getSelectedIndex() == 0){
- return (int)(Math.random()*5);
- } else if(jComboBox_Level.getSelectedIndex() == 1){
- int mostPlayed = getHighestIndex(); //This is the most played hand by player
- int oppositeMove = getOppositeMove(mostPlayed);
- return oppositeMove;
- } else{
- int[] mostRecent = new int[5];
- for(int i = 0; i < mostRecent.length; i++){
- for(int j = 0; j < playerMoves.size(); j++){
- if(playerMoves.get(j) == i) mostRecent[i] = playerMoves.size()-j;
- }
- }
- int lowest = Integer.MAX_VALUE;
- for(int i = 0; i < mostRecent.length; i++){
- mostRecent[i] = Math.abs(mostRecent[i]-getAverageDifference(i));
- if(mostRecent[i]<lowest) lowest = i;
- }
- return lowest;
- }
- }
- public int getAverageDifference(int move){
- int moveC = 1;
- int moveSum = 0;
- int diff = 0;
- for(int i = 0; i < playerMoves.size(); i++){
- if(playerMoves.get(i) == move){
- moveC++;
- moveSum += (i-diff);
- diff = i;
- }
- }
- return (int)((double)(moveSum/moveC));
- }
- public int getOppositeMove(int n){
- int rand = (int)(Math.random()*2);
- if(n == 0){
- return new int[] {4,1}[rand];
- } else if(n == 1){
- return new int[] {2,3}[rand];
- } else if(n == 2){
- return new int[] {0,4}[rand];
- } else if(n == 3){
- return new int[] {2,0}[rand];
- } else if(n == 4){
- return new int[] {1,3}[rand];
- }
- return (int)(Math.random()*5);
- }
- public int getHighestIndex(){
- int[] n = new int[5];
- for(int i = 0; i < playerMoves.size(); i++) n[playerMoves.get(i)]++;
- int highest = -1, highestIndex = 0;
- for(int i = 0; i < n.length; i++){
- if(n[i] > highest){
- highestIndex = i;
- highest = n[i];
- }
- }
- return highestIndex;
- }
- public int isWin(int human, int computer){
- //-1 dictates a loss, 0 is a draw, 1 is a win
- if(human == computer) return 0;
- if(human == 0){
- if(computer == 1 || computer == 4) return -1;
- else return 1;
- } else if(human == 1){
- if(computer == 2 || computer == 3) return -1;
- else return 1;
- } else if(human == 2){
- if(computer == 0 || computer == 4) return -1;
- else return 1;
- } else if(human == 3){
- if(computer == 0 || computer == 2) return -1;
- else return 1;
- } else if(human == 4){
- if(computer == 3 || computer == 1) return -1;
- else return 1;
- }
- return 0;
- }
- public void printToConsole(String s){
- jTextArea_Console.setText(jTextArea_Console.getText() + "\n" + s);
- }
- public void redrawProgressBars(){
- jProgressBar_winPerc.setMaximum(wins+draws+losses);
- jProgressBar_winPerc.setValue(wins);
- jProgressBar_drawPerc.setMaximum(wins+draws+losses);
- jProgressBar_drawPerc.setValue(draws);
- jProgressBar_losePerc.setMaximum(wins+draws+losses);
- jProgressBar_losePerc.setValue(losses);
- }
- public void updateScores(){
- double winPerc = (double)(Math.round((wins/(wins+losses+draws + 0.0))*100.0));
- jTextField_winCount.setText(wins + " (" + winPerc + "%)");
- double drawPerc = (double)(Math.round((draws/(wins+losses+draws + 0.0))*100.0));
- jTextField_drawCount.setText(draws + " (" + drawPerc + "%)");
- double losePerc = (double)(Math.round((losses/(wins+losses+draws + 0.0))*100.0));
- jTextField_lossCount.setText(losses + " (" + losePerc + "%)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement