Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.net.*;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. class RPSSkel extends JFrame implements ActionListener{
  9. Gameboard myboard, computersboard;
  10. int counter; // To count ONE ... TWO and on THREE you play
  11. Socket socket;
  12. BufferedReader in;
  13. PrintWriter out;
  14. JButton closebutton;
  15.  
  16. File win = new File("win.wav");
  17. File lose = new File("lose.wav");
  18. File draw = new File("draw.wav");
  19.  
  20. //RPSSkel (ActionListener listener) {
  21. RPSSkel () {
  22.  
  23. setDefaultCloseOperation(EXIT_ON_CLOSE);
  24. try {
  25. this.socket=new Socket("share-02.csc.kth.se",4713);
  26. this.in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
  27. this.out=new PrintWriter(socket.getOutputStream());
  28. this.out.println("Nisse"); this.out.flush();
  29. String datornsHand = this.in.readLine();
  30. System.out.println(datornsHand);
  31.  
  32. }
  33. catch(IOException a){ // Catchar IOException
  34. System.err.println(a);
  35. }
  36.  
  37. closebutton = new JButton("Close");
  38. closebutton.addActionListener(new CloseListener());
  39. myboard = new Gameboard("Myself",this); // Must be changed
  40. computersboard = new Gameboard("Computer");
  41. JPanel boards = new JPanel();
  42. boards.setLayout(new GridLayout(1,2));
  43. boards.add(myboard);
  44. boards.add(computersboard);
  45. add(boards, BorderLayout.CENTER);
  46. add(closebutton, BorderLayout.SOUTH);
  47. setSize(300, 550);
  48.  
  49. //closebutton.addActionListener(listener);
  50.  
  51. setVisible(true);
  52. out.println("Nisse"); out.flush();
  53.  
  54.  
  55. }
  56.  
  57. public static void main (String[] u) {
  58. RPSSkel spelplan = new RPSSkel();
  59.  
  60.  
  61.  
  62. }
  63.  
  64. public void actionPerformed(ActionEvent e) {
  65.  
  66. Object source = e.getSource();
  67.  
  68.  
  69.  
  70. String minHand = e.getActionCommand();
  71.  
  72. this.counter++;
  73. System.out.println(counter);
  74. myboard.resetColor();
  75. computersboard.resetColor();
  76.  
  77. if (counter==3){
  78.  
  79.  
  80. this.out.println(minHand); this.out.flush();
  81. try{
  82. String datornsHand = this.in.readLine();
  83. System.out.println("Datorns hand - " + datornsHand);
  84. System.out.println("Min hand - " + minHand);
  85.  
  86. myboard.setUpper(minHand);
  87. computersboard.setUpper(datornsHand);
  88. myboard.markPlayed(minHand);
  89. computersboard.markPlayed(datornsHand);
  90.  
  91.  
  92. if (minHand.equals("STEN")){
  93. if (datornsHand.equals("SAX")){
  94. myboard.wins();
  95.  
  96. //If true : spela ljud
  97. Sound.PlaySound(win);
  98. }
  99. if (datornsHand.equals("PASE")){
  100. computersboard.wins();
  101. Sound.PlaySound(lose);
  102.  
  103. }
  104. if (datornsHand.equals("STEN")){
  105. Sound.PlaySound(draw);
  106. }
  107.  
  108. }
  109. if (minHand.equals("SAX")){
  110. if (datornsHand.equals("SAX")){
  111. Sound.PlaySound(draw);
  112. }
  113. if (datornsHand.equals("PASE")){
  114. myboard.wins();
  115. Sound.PlaySound(win);
  116. }
  117. if (datornsHand.equals("STEN")){
  118. computersboard.wins();
  119. Sound.PlaySound(lose);
  120. }
  121. }
  122. if (minHand.equals("PASE")){
  123. if (datornsHand.equals("SAX")){
  124. computersboard.wins();
  125. Sound.PlaySound(lose);
  126. }
  127. if (datornsHand.equals("PASE")){
  128. Sound.PlaySound(draw);
  129. }
  130. if (datornsHand.equals("STEN")){
  131. myboard.wins();
  132. Sound.PlaySound(win);
  133.  
  134. }}
  135.  
  136.  
  137.  
  138. } catch(IOException a){ // Catchar IOException
  139. System.err.println(a);}
  140. this.counter=0;
  141. }
  142.  
  143.  
  144.  
  145. }}
  146.  
  147. class CloseListener implements ActionListener{
  148. @Override
  149. public void actionPerformed(ActionEvent e){
  150. //Do something
  151. System.exit(0);
  152. }
  153.  
  154. }
  155.  
  156. /*
  157. * Vi vill ha en actionlistener som tar till sig vad vi trycker på och vad datorn trycker på
  158. * Och utifrån det räknar poäng.
  159. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement