Advertisement
Guest User

server

a guest
Apr 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. package edu.lmu.cs.networking;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9.  
  10.     // By Patryk Puślecki and Szymon Polak
  11. public class TicTacToeServer {
  12.  
  13.     // uruchomienie programu
  14.     public static void main(String[] args) throws Exception {
  15.         ServerSocket listener = new ServerSocket(8901);
  16.         System.out.println("Tic Tac Toe Server is Running");
  17.         try {
  18.             while (true) {
  19.                 Game game = new Game();
  20.                 Game.Player playerX = game.new Player(listener.accept(), 'X');
  21.                 Game.Player playerO = game.new Player(listener.accept(), 'O');
  22.                 playerX.setOpponent(playerO);
  23.                 playerO.setOpponent(playerX);
  24.                 game.currentPlayer = playerX;
  25.                 playerX.start();
  26.                 playerO.start();
  27.             }
  28.         } finally {
  29.             listener.close();
  30.         }
  31.     }
  32. }
  33.  
  34. /**
  35.  * Gra dla dwóch graczy
  36.  */
  37. class Game {
  38.  
  39.  
  40.     private Player[] board = {
  41.             null, null, null,
  42.             null, null, null,
  43.             null, null, null};
  44.  
  45.  
  46.     Player currentPlayer;
  47.  
  48.  
  49.     public boolean hasWinner() {
  50.         return
  51.                 (board[0] != null && board[0] == board[1] && board[0] == board[2])
  52.                         ||(board[3] != null && board[3] == board[4] && board[3] == board[5])
  53.                         ||(board[6] != null && board[6] == board[7] && board[6] == board[8])
  54.                         ||(board[0] != null && board[0] == board[3] && board[0] == board[6])
  55.                         ||(board[1] != null && board[1] == board[4] && board[1] == board[7])
  56.                         ||(board[2] != null && board[2] == board[5] && board[2] == board[8])
  57.                         ||(board[0] != null && board[0] == board[4] && board[0] == board[8])
  58.                         ||(board[2] != null && board[2] == board[4] && board[2] == board[6]);
  59.     }
  60.  
  61.  
  62.     public boolean boardFilledUp() {
  63.         for (int i = 0; i < board.length; i++) {
  64.             if (board[i] == null) {
  65.                 return false;
  66.             }
  67.         }
  68.         return true;
  69.     }
  70.  
  71.  
  72.     public synchronized boolean legalMove(int location, Player player) {
  73.         if (player == currentPlayer && board[location] == null) {
  74.             board[location] = currentPlayer;
  75.             currentPlayer = currentPlayer.opponent;
  76.             currentPlayer.otherPlayerMoved(location);
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.  
  82.  
  83.     class Player extends Thread {
  84.         char mark;
  85.         Player opponent;
  86.         Socket socket;
  87.         BufferedReader input;
  88.         PrintWriter output;
  89.  
  90.  
  91.         public Player(Socket socket, char mark) {
  92.             this.socket = socket;
  93.             this.mark = mark;
  94.             try {
  95.                 input = new BufferedReader(
  96.                         new InputStreamReader(socket.getInputStream()));
  97.                 output = new PrintWriter(socket.getOutputStream(), true);
  98.                 output.println("WELCOME " + mark);
  99.                 output.println("MESSAGE Waiting for opponent to connect");
  100.             } catch (IOException e) {
  101.                 System.out.println("Player died: " + e);
  102.             }
  103.         }
  104.  
  105.  
  106.         public void setOpponent(Player opponent) {
  107.             this.opponent = opponent;
  108.         }
  109.  
  110.  
  111.         public void otherPlayerMoved(int location) {
  112.             output.println("OPPONENT_MOVED " + location);
  113.             output.println(
  114.                     hasWinner() ? "DEFEAT" : boardFilledUp() ? "TIE" : "");
  115.         }
  116.  
  117.  
  118.         public void run() {
  119.             try {
  120.                 // info o podłączeniu wszystkich graczy
  121.                 output.println("MESSAGE All players connected");
  122.  
  123.                 // info kogo kolej
  124.                 if (mark == 'X') {
  125.                     output.println("MESSAGE Your move");
  126.                 }
  127.  
  128.  
  129.                 while (true) {
  130.                     String command = input.readLine();
  131.                     if (command.startsWith("MOVE")) {
  132.                         int location = Integer.parseInt(command.substring(5));
  133.                         if (legalMove(location, this)) {
  134.                             output.println("VALID_MOVE");
  135.                             output.println(hasWinner() ? "VICTORY"
  136.                                     : boardFilledUp() ? "TIE"
  137.                                     : "");
  138.                         } else {
  139.                             output.println("MESSAGE ?");
  140.                         }
  141.                     } else if (command.startsWith("QUIT")) {
  142.                         return;
  143.                     }
  144.                 }
  145.             } catch (IOException e) {
  146.                 System.out.println("Player died: " + e);
  147.             } finally {
  148.                 try {socket.close();} catch (IOException e) {}
  149.             }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement