Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. public class Player
  2.  
  3. {
  4. private String name;
  5. private String choice;
  6.  
  7. public Player(String nm)
  8. {
  9. }
  10.  
  11. public Player(String nm, String ch)
  12. {
  13. setName(nm);
  14. setChoice(ch);
  15. }
  16.  
  17. public void setName( String nm)
  18. {
  19. name = nm;
  20. }
  21.  
  22. public void setChoice( String ch )
  23. {
  24. choice = ch;
  25. }
  26.  
  27. public String getChoice()
  28. {
  29. if (choice == "rock")
  30. {
  31. choice = "rock";
  32.  
  33. }
  34. else if (choice == "paper")
  35. {
  36. choice = "paper";
  37. }
  38. else
  39. {
  40. choice = "scissors";
  41. }
  42. return choice;
  43.  
  44. }
  45.  
  46.  
  47. public String getName()
  48. {
  49.  
  50. return name;
  51. }
  52.  
  53. public String toString()
  54. {
  55. return "";
  56. }
  57. }
  58.  
  59. public class PlayerRunner
  60.  
  61. {
  62. public static void main(String[] args)
  63. {
  64. //Player s = new Player("Michael Jackson", "rock");
  65. Player s = new Player ("Janet", "scissors");
  66.  
  67. System.out.println(s.getChoice()); //outs rock
  68. //call the getName() method //outs Michael Jackson
  69. System.out.println(s); //outs Michael Jackson rock
  70.  
  71. //set the choice to paper
  72. System.out.println(s); //outs Michael Jackson paper
  73.  
  74. //instantiate a new Player named jb named Jim Bob that chose scissors
  75. //print out Jim Bob
  76. }
  77.  
  78. public class Computer
  79.  
  80. {
  81. //instance / member variables
  82. private String choice;
  83.  
  84. public Computer()
  85. {
  86. // call random set Choice
  87. }
  88.  
  89. public String getChoice()
  90. {
  91. return "";
  92. }
  93. public void randomSetChoice()
  94. {
  95.  
  96. }
  97.  
  98. /*
  99. didIWin(Player p) will return the following values
  100. 0 - both players have the same choice
  101. 1 - the computer had the higher ranking choice
  102. -1 - the player had the higher ranking choice
  103. */
  104. public int didIWin(Player p)
  105. {
  106.  
  107. return -1;
  108.  
  109. }
  110.  
  111. public String toString()
  112. {
  113. return "";
  114. }
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. public class ComputerRunner
  125.  
  126.  
  127. {
  128. public static void main(String[] args)
  129. {
  130. //instantiate a new Computer named c
  131. Computer c = new Computer();
  132. //call and print out getChoice() //outs random choice
  133. System.out.println(c); //outs pooter random choice
  134.  
  135. //call randomChoice
  136. System.out.println(c); //outs pooter random choice
  137.  
  138. //this code will test didIWin( Player p )
  139. System.out.println(c.didIWin(new Player("dude","rock")));
  140. System.out.println(c.didIWin(new Player("dude","paper")));
  141. System.out.println(c.didIWin(new Player("dude","scissors")));
  142. }
  143.  
  144. import javax.swing.JFrame;
  145. import javax.swing.BoxLayout;
  146. import javax.swing.JPanel;
  147. import java.awt.event.ActionEvent;
  148. import java.awt.event.ActionListener;
  149. import java.awt.TextField;
  150. import javax.swing.JTextArea;
  151. import javax.swing.JButton;
  152. import javax.swing.JPanel;
  153.  
  154. public class RockPaperScissorsRunner extends JFrame implements ActionListener
  155. {
  156. private static final int WIDTH = 400;
  157. private static final int HEIGHT = 200;
  158.  
  159. //this variable is the area to which all text is written
  160. private JTextArea text;
  161.  
  162. //these are the buttons that can be clicked
  163. private JButton rock;
  164. private JButton paper;
  165. private JButton scissors;
  166.  
  167. //use these variables to keep track of the players
  168. private Player player;
  169. private Computer pooter;
  170.  
  171. //use these variables to keep track of the score
  172. private int playerWinCount;
  173. private int pooterWinCount;
  174.  
  175. public RockPaperScissorsRunner()
  176. {
  177. super("RockPaperScissors Runner");
  178.  
  179. setSize(WIDTH,HEIGHT);
  180.  
  181. //initialize all variables - player / pooter and the counts
  182. player = new Player("dude");
  183. pooter = new Computer();
  184.  
  185. //this is the panel to which all objects will be added
  186. JPanel main = new JPanel();
  187. //use X.AXIS or Y.AXIS
  188. main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
  189.  
  190. //these panels will be used to divide the screen
  191. JPanel top = new JPanel();
  192. JPanel bot = new JPanel();
  193.  
  194. top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
  195.  
  196. //instantiate a new JTextArea
  197. text = new JTextArea();
  198. text.setText("WELCOME TO ROCK-PAPER-SCISSORS!!!nn");
  199.  
  200. //instantiate a new JButton and refer rock to it
  201. rock = new JButton("Rock");
  202. rock.addActionListener(this);
  203. rock.setActionCommand("rock");
  204.  
  205. //instantiate a new JButton and refer paper to it
  206.  
  207.  
  208. //instantiate a new JButton and refer scissors to it
  209.  
  210.  
  211. //add all three buttons to the bottom panel
  212. bot.add(rock);
  213.  
  214.  
  215.  
  216. //add the text to the top panel
  217. top.add(text);
  218.  
  219. //add both panels to the main panel
  220. main.add(top);
  221.  
  222.  
  223. //add the main panel to the frame
  224. getContentPane().add(main);
  225.  
  226. setVisible(true);
  227.  
  228. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  229. }
  230.  
  231. public void actionPerformed(ActionEvent e)
  232. {
  233. //instantiate a new Computer and refer pooter to it
  234. pooter = new Computer();
  235.  
  236. //get the String value from the button pressed
  237. player.setChoice(e.getActionCommand());
  238.  
  239. //set screenText to the toString() value of each player reference
  240. String screenText = "";
  241.  
  242. //call the Computer's didIWin(Player p) method to find the winner
  243. //you must pass in the Player as a parameter to the method
  244. int result = 0;
  245.  
  246. //if the result is 0 - draw
  247. if(result == 0)
  248. screenText = screenText + "n" + "DRAW!";
  249. //if the result is 1 - pooter wins
  250.  
  251.  
  252. //if the results is -1 - player wins
  253.  
  254.  
  255.  
  256. screenText = screenText + "n" + "Pick Again Sucka!";
  257.  
  258. //set the text to the screen using setText( String txt )
  259. text.setText(screenText);
  260. }
  261.  
  262. public static void main( String args[] )
  263. {
  264. RockPaperScissorsRunner run = new RockPaperScissorsRunner();
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement