Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. package networking;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.Socket;
  7.  
  8. public class HandleASession implements Runnable, TicTacToeConstants {
  9.         private Socket player1;
  10.         private Socket player2;
  11.    
  12.         // Create and initialize cells
  13.             private char[][] cell = new char[3][3];
  14.            
  15.             private DataInputStream fromPlayer1;
  16.             private DataOutputStream toPlayer1;
  17.             private DataInputStream fromPlayer2;
  18.             private DataOutputStream toPlayer2;
  19.            
  20.             // Continue to play
  21.                 private boolean continueToPlay = true;
  22.                
  23.                
  24.    
  25.      /** Construct a thread */
  26.      public HandleASession(Socket player1,Socket player2){
  27.      this.player1 = player1;
  28.      this.player2 = player2;
  29.    
  30.      // Initialize cells
  31.      for (int i = 0; i < 3; i++)
  32.      for (int j = 0; j < 3; j++)
  33.      cell[i][j] = ' ';
  34.      }
  35.      
  36.    
  37.      @Override /** Implement the run() method for the thread */
  38.         public void run(){
  39.          try {
  40.              DataInputStream fromPlayer1 = new DataInputStream(player1.getInputStream());
  41.              DataOutputStream toPlayer1 = new DataOutputStream(player1.getOutputStream());
  42.              DataInputStream fromPlayer2 = new DataInputStream(player2.getInputStream());
  43.              DataOutputStream toPlayer2 = new DataOutputStream(player2.getOutputStream());
  44.                      
  45.         // Write anything to notify player 1 to start
  46.         // This is just to let player 1 know to start
  47.         toPlayer1.writeInt(1);
  48.                      
  49.         // Continuously serve the players and determine and report
  50.         // the game status to the players
  51.         while (true) {
  52.                 // Receive a move from player 1
  53.                 int row = fromPlayer1.readInt();
  54.                 int column = fromPlayer1.readInt();
  55.                 cell[row][column] = 'X';
  56.                      
  57.                 // Check if Player 1 wins
  58.                 if (isWon('X')) {
  59.                     toPlayer1.writeInt(PLAYER1_WON);
  60.                     toPlayer2.writeInt(PLAYER1_WON);
  61.                     sendMove(toPlayer2, row, column);
  62.                     break; // Break the loop
  63.                 }
  64.                 else if (isFull()) { // Check if all cells are filled
  65.                     toPlayer1.writeInt(DRAW);
  66.                     toPlayer2.writeInt(DRAW);
  67.                     sendMove(toPlayer2, row, column);
  68.                     break;
  69.                 }
  70.                 else {
  71.                     // Notify player 2 to take the turn
  72.                     toPlayer2.writeInt(CONTINUE);
  73.                                        
  74.                     // Send player 1's selected row and column to player 2
  75.                     sendMove(toPlayer2, row, column);
  76.                 }
  77.                                        
  78.                 // Receive a move from Player 2
  79.                 row = fromPlayer2.readInt();
  80.                 column = fromPlayer2.readInt();
  81.                 cell[row][column] = 'O';
  82.                                    
  83.                 // Check if Player 2 wins
  84.                 if (isWon('O')) {
  85.                 toPlayer1.writeInt(PLAYER2_WON);
  86.                 toPlayer2.writeInt(PLAYER2_WON);
  87.                 sendMove(toPlayer1, row, column);
  88.                 break;
  89.             }
  90.              else {
  91.                 // Notify player 1 to take the turn
  92.                 toPlayer1.writeInt(CONTINUE);
  93.                 sendMove(toPlayer1, row, column);
  94.              }
  95.         }
  96.     }
  97.    
  98.     catch(IOException ex) {
  99.         System.err.println(ex);
  100.     }
  101. }
  102.      private void sendMove(DataOutputStream out,int row,int column) throws IOException {
  103.             out.writeInt(row); // Send row index
  104.             out.writeInt(column); // Send column index
  105.     }
  106.      
  107.     private boolean isFull(){
  108.        
  109.         for (int i = 0; i < 3; i++)
  110.             for (int j = 0; j < 3; j++)
  111.                  if (cell[i][j] == ' ')
  112.                      return false; // At least one cell is not filled
  113.            
  114.              // All cells are filled
  115.              return true;
  116.     }
  117.    
  118.     private boolean isWon(char token)
  119.     {
  120.         for (int i = 0; i < 3; i++)
  121.                 if ((cell[i][0] == token) && (cell[i][1] == token) && (cell[i][2] == token)) {
  122.                     return true;
  123.                 }
  124.                
  125.             /** Check all columns */
  126.             for (int j = 0; j < 3; j++)
  127.                 if ((cell[0][j] == token)&& (cell[1][j] == token) && (cell[2][j] == token)) {
  128.                     return true;
  129.                 }
  130.                
  131.                 /** Check major diagonal */
  132.                 if ((cell[0][0] == token) && (cell[1][1] == token) && (cell[2][2] == token)) {
  133.                     return true;
  134.                 }
  135.                
  136.                 if ((cell[0][2] == token) && (cell[1][1] == token) && (cell[2][0] == token)) {
  137.                          return true;
  138.                          }
  139.                
  140.                 return false;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement