2020-02-25 02:10:57 +01:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.itsblue.ConnectFour.player;
|
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
import java.awt.event.*;
|
|
|
|
|
2020-02-25 02:10:57 +01:00
|
|
|
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
|
|
|
|
*/
|
2020-02-25 11:17:17 +01:00
|
|
|
public class RemotePlayerServer extends RemotePlayer {
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
private ServerSocket serverSocket;
|
2020-02-25 02:10:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param controlledByButtonRow The button row used to control the player.
|
|
|
|
* @param usingPlateType The type of plate the player is using.
|
|
|
|
*/
|
2020-02-25 11:17:17 +01:00
|
|
|
public RemotePlayerServer(ButtonRow gameControllingButtonRow, PlateType usingPlateType, Player opponent)
|
|
|
|
throws Exception {
|
|
|
|
super(gameControllingButtonRow, usingPlateType, opponent);
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
this.serverSocket = new ServerSocket(4444);
|
|
|
|
System.out.println("Connect4 Server is Running...");
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
this.socket = serverSocket.accept(); // gets stuck here until someone connects
|
|
|
|
this.in = new Scanner(this.socket.getInputStream());
|
|
|
|
this.out = new PrintWriter(this.socket.getOutputStream(), true);
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
System.out.println("Client connected");
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
out.println("setUsingPlateType " + this.opponent.usingPlateType.name());
|
|
|
|
out.println("setIsMyTurn " + (this.opponent.isMyTurn ? "true" : "false"));
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
this.startListening();
|
2020-02-25 02:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void handleResponse(String response) {
|
2020-02-25 11:17:17 +01:00
|
|
|
if (response.startsWith("movePerformed")) {
|
2020-02-25 02:10:57 +01:00
|
|
|
this.doMove(Integer.parseInt(response.split(" ")[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-25 11:17:17 +01:00
|
|
|
* Catch actions of opponent and forward them to client
|
2020-02-25 02:10:57 +01:00
|
|
|
*/
|
|
|
|
@Override
|
2020-02-25 11:17:17 +01:00
|
|
|
public void actionPerformed(ActionEvent e) {
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
super.actionPerformed(e);
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
if (e.getActionCommand().startsWith("isMyTurnChanged")) {
|
|
|
|
out.println("setIsMyTurn " + (e.getActionCommand().split(" ")[1]));
|
2020-02-25 02:10:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|