Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.69 KB | None | 0 0
  1. import java.awt.GridBagConstraints;
  2. import java.awt.GridBagLayout;
  3. import java.awt.Insets;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.ArrayList;
  7. import javax.swing.ComboBoxModel;
  8. import javax.swing.DefaultComboBoxModel;
  9.  
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JButton;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JProgressBar;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTextArea;
  18. import javax.swing.JTextField;
  19.  
  20. import javax.swing.WindowConstants;
  21. import javax.swing.SwingUtilities;
  22.  
  23. @SuppressWarnings("serial")
  24. public class C0158_GUI extends javax.swing.JFrame {
  25.     private JPanel jPanel_Console;
  26.     private JButton[] buttons = new JButton[5];
  27.     private ImageIcon[] icons = new ImageIcon[]{
  28.             new ImageIcon("img/Rock.png"),
  29.             new ImageIcon("img/Paper.png"),
  30.             new ImageIcon("img/Scissors.png"),
  31.             new ImageIcon("img/Lizard.png"),
  32.             new ImageIcon("img/Spock.png")
  33.     };
  34.     private JScrollPane jScrollPane_Console;
  35.     private JTextArea jTextArea_Console;
  36.     private JTextField jTextField_lossCount;
  37.     private JTextField jTextField_drawCount;
  38.     private JTextField jTextField_winCount;
  39.     private JProgressBar jProgressBar_drawPerc;
  40.     private JProgressBar jProgressBar_winPerc;
  41.     private JLabel jLabel_Wins;
  42.     private JLabel jLabel_Draws;
  43.     private JLabel jLabel_Losses;
  44.     private JButton jButton_Stats;
  45.     private JComboBox jComboBox_Level;
  46.     private JLabel jLabel_Level;
  47.     private JProgressBar jProgressBar_losePerc;
  48.  
  49.     ArrayList<Integer> playerMoves = new ArrayList<Integer>();
  50.     final String[] moveNames = new String[] {"Rock", "Paper", "Scissors", "Lizard", "Spock"};
  51.     int[] moveCount = new int[5];
  52.     private int wins = 0;
  53.     private int draws = 0;
  54.     private int losses = 0;
  55.  
  56.     public static void main(String[] args) {
  57.         SwingUtilities.invokeLater(new Runnable() {
  58.             public void run() {
  59.                 C0158_GUI inst = new C0158_GUI();
  60.                 inst.setLocationRelativeTo(null);
  61.                 inst.setVisible(true);
  62.             }
  63.         });
  64.     }
  65.  
  66.     public C0158_GUI() {
  67.         super();
  68.         initGUI();
  69.     }
  70.  
  71.     private void initGUI() {
  72.         try {
  73.             GridBagLayout thisLayout = new GridBagLayout();
  74.             setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  75.             thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
  76.             thisLayout.rowHeights = new int[] {7, 7, 7, 7};
  77.             thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1};
  78.             thisLayout.columnWidths = new int[] {7, 7, 7, 7, 7};
  79.             getContentPane().setLayout(thisLayout);
  80.             {
  81.                 jPanel_Console = new JPanel();
  82.                 GridBagLayout jPanel_ConsoleLayout = new GridBagLayout();
  83.                 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));
  84.                 jPanel_ConsoleLayout.rowWeights = new double[] {0.1, 0.2, 0.2, 0.1};
  85.                 jPanel_ConsoleLayout.rowHeights = new int[] {7, 7, 7, 7};
  86.                 jPanel_ConsoleLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
  87.                 jPanel_ConsoleLayout.columnWidths = new int[] {7, 7, 7, 7};
  88.                 jPanel_Console.setLayout(jPanel_ConsoleLayout);
  89.                 jScrollPane_Console = new JScrollPane();
  90.                 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));
  91.                 jTextArea_Console = new JTextArea();
  92.                 jTextArea_Console.setEditable(false);
  93.                 jScrollPane_Console.setViewportView(jTextArea_Console);
  94.             }
  95.             {
  96.                 jLabel_Wins = new JLabel();
  97.                 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));
  98.                 jLabel_Wins.setText("Wins:");
  99.                 jLabel_Wins.setFont(new java.awt.Font("Segoe UI",1,15));
  100.             }
  101.             {
  102.                 jLabel_Draws = new JLabel();
  103.                 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));
  104.                 jLabel_Draws.setText("Draws:");
  105.                 jLabel_Draws.setFont(new java.awt.Font("Segoe UI",1,15));
  106.             }
  107.             {
  108.                 jLabel_Losses = new JLabel();
  109.                 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));
  110.                 jLabel_Losses.setText("Losses:");
  111.                 jLabel_Losses.setFont(new java.awt.Font("Segoe UI",1,15));
  112.             }
  113.             {
  114.                 jTextField_winCount = new JTextField();
  115.                 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));
  116.                 jTextField_winCount.setEditable(false);
  117.                 jTextField_winCount.setText("0");
  118.             }
  119.             {
  120.                 jTextField_drawCount = new JTextField();
  121.                 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));
  122.                 jTextField_drawCount.setEditable(false);
  123.                 jTextField_drawCount.setText("0");
  124.             }
  125.             {
  126.                 jTextField_lossCount = new JTextField();
  127.                 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));
  128.                 jTextField_lossCount.setEditable(false);
  129.                 jTextField_lossCount.setText("0");
  130.             }
  131.             {
  132.                 jProgressBar_winPerc = new JProgressBar();
  133.                 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));
  134.             }
  135.             {
  136.                 jProgressBar_drawPerc = new JProgressBar();
  137.                 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));
  138.             }
  139.             {
  140.                 jProgressBar_losePerc = new JProgressBar();
  141.                 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));
  142.             }
  143.             {
  144.                 jLabel_Level = new JLabel();
  145.                 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));
  146.                 jLabel_Level.setText("AI Level:");
  147.                 jLabel_Level.setFont(new java.awt.Font("Segoe UI",1,15));
  148.             }
  149.             {
  150.                 ComboBoxModel jComboBox_LevelModel =
  151.                         new DefaultComboBoxModel(
  152.                                 new String[] { "Easy", "Medium", "Hard"});
  153.                 jComboBox_Level = new JComboBox();
  154.                 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));
  155.                 jComboBox_Level.setModel(jComboBox_LevelModel);
  156.             }
  157.             {
  158.                 jButton_Stats = new JButton();
  159.                 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));
  160.                 jButton_Stats.setText("Stats");
  161.                 jButton_Stats.addActionListener(new ActionListener() {
  162.                     public void actionPerformed(ActionEvent e) {
  163.                         for(int i = 0; i < moveCount.length; i++){
  164.                             printToConsole(moveNames[i] + ": " + moveCount[i]);
  165.                         }
  166.                     }
  167.                 });
  168.             }
  169.             {
  170.                 for(int i = 0; i < buttons.length; i++){
  171.  
  172.                     buttons[i] = new JButton(icons[i]);
  173.                     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));
  174.                     final int n = i;
  175.                     buttons[i].addActionListener(new ActionListener() {
  176.                         public void actionPerformed(ActionEvent e){
  177.                             play(n);
  178.                             moveCount[n]++;
  179.                         }
  180.                     });
  181.                 }
  182.             }
  183.  
  184.             pack();
  185.             setSize(600, 500);
  186.             setTitle("Rock Paper Scissors Lizard Spock!");
  187.             setResizable(false);
  188.         } catch (Exception e) {
  189.             e.printStackTrace();
  190.         }
  191.     }
  192.  
  193.     public void play(int move){
  194.         printToConsole("Human played " + moveNames[move] + "! ");
  195.         int cm = getComputerMove();
  196.         printToConsole("Computer played " + moveNames[cm] + "! ");
  197.         if(isWin(move, cm) == 1){
  198.             printToConsole("You win!");
  199.             wins++;
  200.         } else if(isWin(move, cm) == 0){
  201.             printToConsole("It was a draw!");
  202.             draws++;
  203.         } else{
  204.             printToConsole("You lose!");
  205.             losses++;
  206.         }
  207.         printToConsole("-");
  208.  
  209.         playerMoves.add(move);
  210.         redrawProgressBars();
  211.         updateScores();
  212.     }
  213.  
  214.     public int getComputerMove(){
  215.         if(jComboBox_Level.getSelectedIndex() == 0){
  216.             return (int)(Math.random()*5);
  217.         } else if(jComboBox_Level.getSelectedIndex() == 1){
  218.             int mostPlayed = getHighestIndex(); //This is the most played hand by player
  219.             int oppositeMove = getOppositeMove(mostPlayed);
  220.             return oppositeMove;
  221.         } else{
  222.             int[] mostRecent = new int[5];
  223.             for(int i = 0; i < mostRecent.length; i++){
  224.                 for(int j = 0; j < playerMoves.size(); j++){
  225.                     if(playerMoves.get(j) == i) mostRecent[i] = playerMoves.size()-j;
  226.                 }
  227.             }
  228.             int lowest = Integer.MAX_VALUE;
  229.             for(int i = 0; i < mostRecent.length; i++){
  230.                 mostRecent[i] = Math.abs(mostRecent[i]-getAverageDifference(i));
  231.                 if(mostRecent[i]<lowest)    lowest = i;
  232.             }
  233.             return lowest;
  234.         }
  235.        
  236.     }
  237.    
  238.     public int getAverageDifference(int move){
  239.         int moveC = 1;
  240.         int moveSum = 0;
  241.         int diff = 0;
  242.         for(int i = 0; i < playerMoves.size(); i++){
  243.             if(playerMoves.get(i) == move){
  244.                 moveC++;
  245.                 moveSum += (i-diff);
  246.                 diff = i;
  247.             }
  248.         }
  249.         return (int)((double)(moveSum/moveC));
  250.     }
  251.    
  252.     public int getOppositeMove(int n){
  253.         int rand = (int)(Math.random()*2);
  254.         if(n == 0){
  255.             return new int[] {4,1}[rand];
  256.         } else if(n == 1){
  257.             return new int[] {2,3}[rand];
  258.         } else if(n == 2){
  259.             return new int[] {0,4}[rand];
  260.         } else if(n == 3){
  261.             return new int[] {2,0}[rand];
  262.         } else if(n == 4){
  263.             return new int[] {1,3}[rand];
  264.         }
  265.         return (int)(Math.random()*5);
  266.     }
  267.  
  268.     public int getHighestIndex(){
  269.         int[] n = new int[5];
  270.         for(int i = 0; i < playerMoves.size(); i++) n[playerMoves.get(i)]++;
  271.         int highest = -1, highestIndex = 0;
  272.         for(int i = 0; i < n.length; i++){
  273.             if(n[i] > highest){
  274.                 highestIndex = i;
  275.                 highest = n[i];
  276.             }
  277.         }
  278.         return highestIndex;
  279.     }
  280.  
  281.     public int isWin(int human, int computer){
  282.         //-1 dictates a loss, 0 is a draw, 1 is a win
  283.         if(human == computer)   return 0;
  284.         if(human == 0){
  285.             if(computer == 1 || computer == 4)  return -1;
  286.             else return 1;
  287.         } else if(human == 1){
  288.             if(computer == 2 || computer == 3)  return -1;
  289.             else return 1;
  290.         } else if(human == 2){
  291.             if(computer == 0 || computer == 4)  return -1;
  292.             else return 1;
  293.         } else if(human == 3){
  294.             if(computer == 0 || computer == 2)  return -1;
  295.             else return 1;
  296.         } else if(human == 4){
  297.             if(computer == 3 || computer == 1)  return -1;
  298.             else return 1;
  299.         }
  300.        
  301.         return 0;
  302.     }
  303.  
  304.     public void printToConsole(String s){
  305.         jTextArea_Console.setText(jTextArea_Console.getText() + "\n" + s);
  306.     }
  307.  
  308.     public void redrawProgressBars(){
  309.         jProgressBar_winPerc.setMaximum(wins+draws+losses);
  310.         jProgressBar_winPerc.setValue(wins);
  311.         jProgressBar_drawPerc.setMaximum(wins+draws+losses);
  312.         jProgressBar_drawPerc.setValue(draws);
  313.         jProgressBar_losePerc.setMaximum(wins+draws+losses);
  314.         jProgressBar_losePerc.setValue(losses);
  315.     }
  316.  
  317.     public void updateScores(){
  318.         double winPerc = (double)(Math.round((wins/(wins+losses+draws + 0.0))*100.0));
  319.         jTextField_winCount.setText(wins + " (" + winPerc + "%)");
  320.         double drawPerc = (double)(Math.round((draws/(wins+losses+draws + 0.0))*100.0));
  321.         jTextField_drawCount.setText(draws + " (" + drawPerc + "%)");
  322.         double losePerc = (double)(Math.round((losses/(wins+losses+draws + 0.0))*100.0));
  323.         jTextField_lossCount.setText(losses + " (" + losePerc + "%)");
  324.     }
  325.  
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement