2009-10-13 3 views
2

У меня нет опыта работы с темами, к сожалению. Я делаю пару примеров «Hello World», которые кажутся довольно легкими. Однако мне нужно включить потоки в сервер Java Tic Tac Toe, который у меня есть (который использует сокеты). Дизайн моей Toe игры Tic Tac это:Создание Java Tic Tac Toe Server Многопоточное

Provider.java (сервер): Определяет текущий игрок посылает текстовое отображение/игровое поле для текущего игрока, определяет, будет ли игра окончена

Requester.java (клиент): Инициирует объект игрового поля для каждого клиента, держит игровую панель клиентов в синхронизации с игровым полем сервера, проверяет действительные ходы

TBoard.java: Методы, используемые как клиентом, так и сервером (make_move(), is_winner(), current_player() и т. д.)

мне было интересно, если вы, ребята могли бы взглянуть на мой Provider.java (сервер), и дать мне несколько советов о том, как включить темы, чтобы позволить нескольким Tic Tac Toe игры, чтобы играть одновременно ...

/** 
    * Provider.java 
    * 
    * @version  2.0 
    */ 
import java.io.*; 
import java.net.*; 

/** 
    * The Provider class is responsible for any server-side activities with the 
    * Tic-Tac-Toe game. This includes: 
    * 
    * 1. Determining the current player 2. Managing the textual display for the two 
    * individual players 3. Updating the game board with the each player's moves 4. 
    * Determine what player wins, if any 
    */ 
public class Provider { 

TBoard board = new TBoard(); // Instantiate the game board object 

ServerSocket providerSocket; 
Socket connection1 = null, connection2 = null; 
ObjectOutputStream out, out2; // Client 1, Client 2 
ObjectInputStream in, in2; // Client 1, Client 2 
String message; 
Boolean done = false; 
String end = ""; 
int row, col, game_state = 3; 

/** 
* Class constructor. 
*/ 
Provider() { 
} 

void run() { 
    try { 
     // Open a socket and wait for connections 
     providerSocket = new ServerSocket(20092); 

     System.out.println("Waiting for connection..."); 
     connection1 = providerSocket.accept(); 
     System.out.println("Connection received from Player 1 " 
       + connection1.getInetAddress().getHostName()); 
     connection2 = providerSocket.accept(); 
     System.out.println("Connection received from Player 2 " 
       + connection2.getInetAddress().getHostName()); 

     out = new ObjectOutputStream(connection1.getOutputStream()); 
     out2 = new ObjectOutputStream(connection2.getOutputStream()); 

     in = new ObjectInputStream(connection1.getInputStream()); 
     in2 = new ObjectInputStream(connection2.getInputStream()); 

     do { 
      // Send the game game_state to the current player 
      sendInt(board.get_player(), game_state); 

      // If the game state != 3 (There is a winner or board is full) 
      if (game_state != 3) { 
       // Send game state to the other player 
       sendInt(board.get_opposite_player(), game_state); 

       done = true; // Condition to terminate the server 
       // If game is in "play" state 
      } else { 
       // Send both the current player and the Tic Tac Toe board 
       // (2D array) to current player 
       sendInt(board.get_player(), board.get_player()); 
       send2D(board.get_player(), board.print_board()); 

       sendString(board.get_player(), 
         "Please enter a row, press Enter, then enter a column: "); 

       // Receive the tic tac toe board from current player 
       // (after a move has been made) 
       if (board.get_player() == 1) { 
        int[][] c_array = (int[][]) in.readObject(); 
        board.set_array(c_array); 
       } else { 
        int[][] c_array = (int[][]) in2.readObject(); 
        board.set_array(c_array); 
       } 

       // Switch the current player 
       if (board.get_player() == 1) { 
        board.set_player(2); 
       } else { 
        board.set_player(1); 
       } 

       // If there is a winner, set the game state accordingly 
       if (board.winner() != 0) { 

        if (board.get_player() == 1) { 
         game_state = 2; 
        } else { 
         game_state = 1; 
        } 

        // If there is no winner and the board is full, set the 
        // game state accordingly 
       } else if (board.board_full() && board.winner() == 0) { 

        game_state = 0; 

        // Otherwise, stay in the "play" state 
       } else { 

        game_state = 3; 

       } 
      } 

     } while (done != true); 

    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } finally { 
     // Close the input/output streams, and the socket connections 
     try { 

      in.close(); 
      out.close(); 
      in2.close(); 
      out2.close(); 
      providerSocket.close(); 
     } catch (IOException ioException) { 
      ioException.printStackTrace(); 
     } 
    } 
} 

/** 
* Sends a String to the current client 
* 
* @param player the current player 
* @param msg the String to be sent 
*/ 
void sendString(int player, String msg) { 
    try { 
     if (player == 1) { 
      out.writeObject(msg); 
      out.flush(); 

     } else { 
      out2.writeObject(msg); 
      out2.flush(); 
     } 
    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } 
} 

/** 
* Sends a String to the current client 
* 
* @param player the current player 
* @param array the 2D array to be sent 
*/ 
void send2D(int player, int[][] array) { 
    try { 
     if (player == 1) { 
      out.writeObject(array); 
      out.flush(); 

     } else { 
      out2.writeObject(array); 
      out2.flush(); 
     } 
    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } 
} 

/** 
* Sends a int to the current client 
* 
* @param player the current player 
* @param msg the int to be sent 
*/ 
void sendInt(int player, int msg) { 
    try { 
     if (player == 1) { 
      out.writeObject(msg); 
      out.flush(); 

     } else { 
      out2.writeObject(msg); 
      out2.flush(); 
     } 
    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } 
} 
/** 
* Main method, invoking run() method 
*/ 
public static void main(String args[]) { 
    Provider server = new Provider(); 
    server.run(); 
} 
} 

Спасибо!

ответ

1

Во-первых, полностью отделите фактический материал Tic-Tac-Toe от вашего слоя связи. Ваш уровень связи должен в основном получать сообщение от любого клиента, выяснить, какой экземпляр Tic-Tac-Toe принадлежит этому клиенту, а затем отправить сообщение. Аналогично, экземпляру Tic-Tac-Toe может потребоваться отправить сообщение своим игрокам через уровень связи.

Я полагаю, что ваш класс TicTacToe будет иметь очень простой API, который выглядит как:

public Result mark(int row, int col, int playerID); 

Где Результат может быть либо что-то вроде ДЕЙСТВИТЕЛЬНО, INVALID_MOVE, PLAYER_WINS, или рисовать, или что-то в этом роде. С таким интерфейсом вы можете легко создать однопользовательскую версию игры, прежде чем перейти к сетевому материалу.

В конце вам понадобится 1 поток, который исключительно вызывает serverSocket.await(), ждет 2 входящих соединения, а затем создает новый экземпляр Tic-Tac-Toe. Каждый раз, когда сообщение приходит от одного из этих двух клиентов, вы отправляете его в этот конкретный экземпляр. Вам нужно каким-то образом найти экземпляр TicTacToe для данного сокета, извлечь сообщение из входного потока, изменить экземпляр TicTacToe, а затем отправить сообщение обратно клиентам 2 игрока.

Вам также понадобится дополнительная резьба для каждого отдельного подключения сокета. Каждый раз, когда вы пытаетесь читать из входного потока сокета, вам нужно дождаться, когда некоторые данные будут фактически отправлены от клиента. Именно здесь вступают в игру нити.

О, кстати, пакет java.util.concurrent предоставляет способы сделать ваш сетевой сервер более масштабируемым и эффективным, но его довольно сложно. Кроме того, большинство способов его использования на самом деле однопоточные (на самом деле это более масштабируемые, верьте или нет). Я рекомендую ручку очистки для этого задания.

+0

Спасибо за совет, я попытаюсь отделить материал от материалов Tic Tac Toe. – littleK