/* Connect four - written in java Copyright (C) 2020 Oliver Schappacher and Dorian Zedler This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package de.itsblue.ConnectFour.player; import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Arrays; import java.util.Scanner; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import de.itsblue.ConnectFour.Plate.*; import de.itsblue.ConnectFour.*; /** * LocalPlayer is a class meant for usage with de.itsblue.ConnectFour. It is * used for a player controlled by the local buttons in the button row. * * @author Dorian Zedler */ public class RemotePlayerServer extends Player implements PlayerMoveListener { private ServerSocket listener; private Socket clientSocket; private Scanner in; private PrintWriter out; private Player opponent; /** * Constructor * * @param controlledByButtonRow The button row used to control the player. * @param usingPlateType The type of plate the player is using. */ public RemotePlayerServer(ButtonRow gameControllingButtonRow, PlateType usingPlateType, Player opponent) { super(gameControllingButtonRow, usingPlateType); this.opponent = opponent; opponent.addMoveListener(this); try { this.listener = new ServerSocket(4444); System.out.println("Connect4 Server is Running..."); this.clientSocket = listener.accept(); this.in = new Scanner(this.clientSocket.getInputStream()); this.out = new PrintWriter(this.clientSocket.getOutputStream(), true); System.out.println("Client connected"); out.println("setUsingPlateType " + this.opponent.usingPlateType.name()); out.println("setIsMyTurn " + (this.opponent.isMyTurn ? "true":"false")); ExecutorService pool = Executors.newFixedThreadPool(200); pool.execute(this.new ServerListener(in, this)); } catch (Exception e) { e.printStackTrace(); } } public void handleResponse(String response) { System.out.println("GOT: " + response); if(response.startsWith("movePerformed")) { this.doMove(Integer.parseInt(response.split(" ")[1])); } } /** * Function to set wether it is this player's turn */ @Override public void setIsMyTurn(boolean isMyTurn) { super.setIsMyTurn(isMyTurn); if(isMyTurn) this.gameControllingButtonRow.setEnabled(false); else this.gameControllingButtonRow.setEnabled(true); out.println("setIsMyTurn " + (this.isMyTurn ? "false":"true")); } @Override public void movePerformed(int column, Player src) { System.out.println("movePerformed"); try { this.out.println("movePerformed " + column); } catch (Exception e) { e.printStackTrace(); } } class ServerListener implements Runnable { RemotePlayerServer parent; Scanner in; public ServerListener(Scanner in, RemotePlayerServer parent) { this.parent = parent; this.in = in; } @Override public void run() { // listen to the socket try { while (in.hasNextLine()) { String response = in.nextLine(); this.parent.handleResponse(response); } out.println("QUIT"); } catch (Exception e) { e.printStackTrace(); } } } }