Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class RockPaperScissors {
  8.  
  9. /*
  10. * 1 is rock
  11. * 2 is paper
  12. * 3 is scissors
  13. */
  14.  
  15. static int humanWon; // use for statistic
  16. static int win=0;
  17. static int total=0;
  18. static int tie=0;
  19.  
  20. public static void main(String[] args){ // main
  21. gamePanel();// launch main game
  22. introductionPanel(); // launch instruction
  23. }
  24.  
  25. private static void introductionPanel(){ // give the instruction to the game
  26. String text="Rock, Paper, Scissors! This game is fairly simple.\nSimply pick your hands whenever you are ready.\nRock beats scissors, scissors beat paper\nand paper wrap the rock. Yes paper beats rock.";
  27. JOptionPane.showMessageDialog(null,text, "How to play!", 0, new ImageIcon(System.getProperty("user.dir")+"/image/5.gif"));
  28. }
  29.  
  30. private static void gamePanel(){ // the main game panel
  31.  
  32. JFrame frame = new JFrame("Rock, Scissors, Paper"); //the main frame of the game
  33.  
  34. Container panel = frame.getContentPane(); // creating a container panel, so we can place buttons where we pleased
  35. panel.setLayout(null);
  36.  
  37. String[] iconString= new String[3]; // creating icon string name so we can place the directory in with little effort
  38. int[] boundInt= new int[3]; // same idea
  39.  
  40. for(int i=0; i<=2; i++){ // creating the condtions
  41. iconString[i]=System.getProperty("user.dir")+"/image/"+i+".jpg";
  42. boundInt[i]=40+150*i;
  43. }
  44.  
  45. JButton b1 = new JButton (" ", new ImageIcon(iconString[0]));
  46. b1.setBackground(Color.white);
  47. b1.setBounds(40,boundInt[0],150,100);
  48.  
  49.  
  50. JButton b2 = new JButton (" ", new ImageIcon(iconString[1]));
  51. b2.setBackground(Color.white);
  52. b2.setBounds(40,boundInt[1],150,100);
  53.  
  54. JButton b3 = new JButton (" ", new ImageIcon(iconString[2]));
  55. b3.setBackground(Color.white);
  56. b3.setBounds(40,boundInt[2],150,100);//creating three buttons
  57.  
  58. JLabel l1 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/3.jpg"));
  59. l1.setBounds(300, 140, 128, 200);
  60. panel.add(l1);//creating a question button
  61.  
  62.  
  63. JButton b4 = new JButton("Cheat");
  64. b4.setBounds(350, 430, 80, 30); //create a code button, this button will give you an automatic win
  65.  
  66. JButton b5 = new JButton("Quit"); //quit
  67. b5.setBounds(260, 430, 80, 30);
  68.  
  69. panel.add(b1);
  70. panel.add(b2);
  71. panel.add(b3);
  72. panel.add(b4);
  73. panel.add(b5); //place button on panel
  74.  
  75. b1.addActionListener( //next three button will listen for which play pick and calculate the win in computeWinner
  76.  
  77. new ActionListener() {
  78. public void actionPerformed( ActionEvent event ) {
  79. computeWinner(1);
  80. }
  81. }
  82. );
  83.  
  84. b2.addActionListener(
  85.  
  86. new ActionListener() {
  87. public void actionPerformed( ActionEvent event ) {
  88. computeWinner(2);
  89. }
  90. }
  91. );
  92.  
  93. b3.addActionListener(
  94.  
  95. new ActionListener() {
  96. public void actionPerformed( ActionEvent event ) {
  97. computeWinner(3);
  98. }
  99. }
  100. );
  101.  
  102. b4.addActionListener(
  103.  
  104. new ActionListener() {//cheat button, hit the guy and get a win
  105. public void actionPerformed( ActionEvent event ) {
  106. win=win+1;
  107. total=total+1;
  108.  
  109. JOptionPane.showMessageDialog(null,"Rack up another win!"+"\nWin/Loss rate: " + win+"/"+total+"\nTie: "+tie,"Cheater do prosper", 0, new ImageIcon(System.getProperty("user.dir")+"/image/4.jpg"));
  110.  
  111. }
  112. }
  113. );
  114. b5.addActionListener( //quit the game and show three beat up guys
  115.  
  116. new ActionListener() {
  117. public void actionPerformed( ActionEvent event ) {
  118. String text="Paper: Thank goodness you stop playing!\nThe rock keep trying to break free\n and the scissors keep cutting me!\nRock: Let me out!\nScissors: Damn rock! Snip snip.\n\nAuthor: Thank you for playing and I have\ntake these guys to the hospital now.";
  119. JOptionPane.showMessageDialog(null,text, "Thank you for playing!", 0, new ImageIcon(System.getProperty("user.dir")+"/image/6.gif"));
  120. System.exit(0);
  121. }
  122. }
  123. );
  124.  
  125. frame.setSize(500, 500);
  126. frame.setVisible(true);
  127. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set frame size and the game begins!
  128. }
  129.  
  130. public static void computeWinner(int x){ // computing the winner
  131. int computerChoice=computerRandomChoice();
  132. int humanChoice=x;
  133. String text,text1="";
  134. String winningCombination= ""+Math.min(computerChoice, humanChoice)+Math.max(computerChoice, humanChoice);
  135.  
  136. switch(Integer.parseInt(winningCombination)){
  137.  
  138. case 12:
  139. text = "Paper wins!";
  140. if(humanChoice==2) humanWon=1;
  141. break;
  142. case 13:
  143. text = "Rock wins!";
  144. if(humanChoice==1) humanWon=1;
  145. break;
  146. case 23:
  147. text = "Scissors wins!";
  148. if(humanChoice==3) humanWon=1;
  149. break;
  150. default: text="It is a tie!";
  151. humanWon=2;
  152. tie=tie+1;
  153. }
  154.  
  155. if(humanWon==1){
  156. text1="Human wins! ";
  157. humanWon=0;
  158. win=win+1;
  159. total=total+1;
  160. }else if(humanWon==2){
  161. text1="It is a tie! ";
  162. humanWon=0;
  163. }else{
  164. text1="Computer wins! ";
  165. total=total+1;
  166.  
  167. }
  168.  
  169.  
  170. JFrame frame = new JFrame("Rock, Scissors, Paper");
  171. Container panel = frame.getContentPane();
  172. panel.setLayout(null);
  173.  
  174.  
  175. JLabel l0 = new JLabel(text1+text);
  176. l0.setBounds(75, 10, 300, 35);
  177. panel.add(l0);
  178.  
  179.  
  180. //show the result in a new splash screen
  181.  
  182. JLabel l1 = new JLabel("Human's Choice");
  183. l1.setBounds(40, 35, 150, 35);
  184. panel.add(l1);
  185.  
  186. JLabel l2 = new JLabel("Computer's Choice");
  187. l2.setBounds(215, 35, 150, 35);
  188. panel.add(l2);
  189.  
  190. JLabel l3 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/"+(humanChoice-1)+".jpg"));
  191. l3.setBounds(10, 100, 170, 60);
  192. panel.add(l3);
  193.  
  194. JLabel l4 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/"+(computerChoice-1)+".jpg"));
  195. l4.setBounds(200, 100,170, 60);
  196. panel.add(l4);
  197.  
  198. JLabel l5 = new JLabel("Win/Loss rate: " + win+"/"+total);
  199. l5.setBounds(125, 25, 150, 350);
  200. panel.add(l5);
  201.  
  202. JLabel l6 = new JLabel("Tie: "+tie);
  203. l6.setBounds(125, 30, 125, 370);
  204. panel.add(l6);
  205.  
  206. frame.setSize(400, 270);
  207. frame.setVisible(true);
  208.  
  209.  
  210.  
  211. }
  212.  
  213. public static int computerRandomChoice(){// creating a random choice of rock paper or scissors by the computer
  214. int result=(int)(Math.random()*3)+1;
  215. return result;
  216. }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement