Advertisement
Guest User

TicTacToeClient

a guest
Apr 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 KB | None | 0 0
  1. package edu.lmu.cs.networking;
  2.  
  3. import java.awt.Color;
  4. import java.awt.GridLayout;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.io.PrintWriter;
  10. import java.net.Socket;
  11.  
  12. import javax.swing.Icon;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18.  
  19.     // By Patryk Puślecki and Szymon Polak
  20. public class TicTacToeClient {
  21.  
  22.     private JFrame frame = new JFrame("Tic Tac Toe");
  23.     private JLabel messageLabel = new JLabel("");
  24.     private ImageIcon icon;
  25.     private ImageIcon opponentIcon;
  26.  
  27.     private Square[] board = new Square[9];
  28.     private Square currentSquare;
  29.  
  30.     private static int PORT = 8901;
  31.     private Socket socket;
  32.     private BufferedReader in;
  33.     private PrintWriter out;
  34.  
  35.     /**
  36.      * uruchomienie GUI i nasłuchiwania
  37.      */
  38.     public TicTacToeClient(String serverAddress) throws Exception {
  39.  
  40.         // Setup networking
  41.         socket = new Socket(serverAddress, PORT);
  42.         in = new BufferedReader(new InputStreamReader(
  43.                 socket.getInputStream()));
  44.         out = new PrintWriter(socket.getOutputStream(), true);
  45.  
  46.         // Layout GUI
  47.         messageLabel.setBackground(Color.lightGray);
  48.         frame.getContentPane().add(messageLabel, "South");
  49.  
  50.         JPanel boardPanel = new JPanel();
  51.         boardPanel.setBackground(Color.black);
  52.         boardPanel.setLayout(new GridLayout(3, 3, 2, 2));
  53.         for (int i = 0; i < board.length; i++) {
  54.             final int j = i;
  55.             board[i] = new Square();
  56.             board[i].addMouseListener(new MouseAdapter() {
  57.                 public void mousePressed(MouseEvent e) {
  58.                     currentSquare = board[j];
  59.                     out.println("MOVE " + j);}});
  60.             boardPanel.add(board[i]);
  61.         }
  62.         frame.getContentPane().add(boardPanel, "Center");
  63.     }
  64.  
  65.     /**
  66.      * Działanie klienta polega na wysyłaniu i odbiorze informacji z serwera,
  67.      * po których następuje uruchomienie kolejnych klas programu
  68.      */
  69.     public void play() throws Exception {
  70.         String response;
  71.         try {
  72.             response = in.readLine();
  73.             if (response.startsWith("WELCOME")) {
  74.                 char mark = response.charAt(8);
  75.                 icon = new ImageIcon(mark == 'X' ? "x.gif" : "o.gif");
  76.                 opponentIcon  = new ImageIcon(mark == 'X' ? "o.gif" : "x.gif");
  77.                 frame.setTitle("Tic Tac Toe - Player " + mark);
  78.             }
  79.             while (true) {
  80.                 response = in.readLine();
  81.                 if (response.startsWith("VALID_MOVE")) {
  82.                     messageLabel.setText("Valid move, please wait");
  83.                     currentSquare.setIcon(icon);
  84.                     currentSquare.repaint();
  85.                 } else if (response.startsWith("OPPONENT_MOVED")) {
  86.                     int loc = Integer.parseInt(response.substring(15));
  87.                     board[loc].setIcon(opponentIcon);
  88.                     board[loc].repaint();
  89.                     messageLabel.setText("Opponent moved, your turn");
  90.                 } else if (response.startsWith("VICTORY")) {
  91.                     messageLabel.setText("You win");
  92.                     break;
  93.                 } else if (response.startsWith("DEFEAT")) {
  94.                     messageLabel.setText("You lose");
  95.                     break;
  96.                 } else if (response.startsWith("TIE")) {
  97.                     messageLabel.setText("You tied");
  98.                     break;
  99.                 } else if (response.startsWith("MESSAGE")) {
  100.                     messageLabel.setText(response.substring(8));
  101.                 }
  102.             }
  103.             out.println("QUIT");
  104.         }
  105.         finally {
  106.             socket.close();
  107.         }
  108.     }
  109.  
  110.     private boolean wantsToPlayAgain() {
  111.         int response = JOptionPane.showConfirmDialog(frame,
  112.                 "Want to play again?",
  113.                 "Kółko i krzyżyk",
  114.                 JOptionPane.YES_NO_OPTION);
  115.         frame.dispose();
  116.         return response == JOptionPane.YES_OPTION;
  117.     }
  118.  
  119.     /**
  120.      * tworzenie layoutu klienta
  121.      */
  122.     static class Square extends JPanel {
  123.         JLabel label = new JLabel((Icon)null);
  124.  
  125.         public Square() {
  126.             setBackground(Color.white);
  127.             add(label);
  128.         }
  129.  
  130.         public void setIcon(Icon icon) {
  131.             label.setIcon(icon);
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * Uruchomienie klienta
  137.      */
  138.     public static void main(String[] args) throws Exception {
  139.         while (true) {
  140.             String serverAddress = (args.length == 0) ? "localhost" : args[1];
  141.             TicTacToeClient client = new TicTacToeClient(serverAddress);
  142.             client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  143.             client.frame.setSize(240, 160);
  144.             client.frame.setVisible(true);
  145.             client.frame.setResizable(false);
  146.             client.play();
  147.             if (!client.wantsToPlayAgain()) {
  148.                 break;
  149.             }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement