148 lines
4.5 KiB
Java
148 lines
4.5 KiB
Java
|
/*
|
||
|
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;
|
||
|
|
||
|
import java.awt.event.*;
|
||
|
import java.util.Scanner;
|
||
|
import java.io.IOException;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.net.Socket;
|
||
|
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 RemotePlayerClient extends Player implements PlayerMoveListener {
|
||
|
|
||
|
private Socket socket;
|
||
|
private Scanner in;
|
||
|
private PrintWriter out;
|
||
|
|
||
|
private Player opponent;
|
||
|
|
||
|
/**
|
||
|
* Constructor
|
||
|
*
|
||
|
* @param controlledByButtonRow The button row used to control the player.
|
||
|
*/
|
||
|
public RemotePlayerClient(ButtonRow gameControllingButtonRow, String serverAddress, Player opponent) {
|
||
|
super(gameControllingButtonRow, null);
|
||
|
|
||
|
this.opponent = opponent;
|
||
|
opponent.addMoveListener(this);
|
||
|
|
||
|
try {
|
||
|
socket = new Socket(serverAddress, 4444);
|
||
|
in = new Scanner(socket.getInputStream());
|
||
|
out = new PrintWriter(socket.getOutputStream(), true);
|
||
|
System.out.println("Connected to Connect4 server");
|
||
|
|
||
|
// get our plate type
|
||
|
String response = in.nextLine();
|
||
|
if (response.split(" ")[0].equals("setUsingPlateType")) {
|
||
|
this.usingPlateType = PlateType.valueOf(response.split(" ")[1]);
|
||
|
this.opponent.usingPlateType = this.usingPlateType.equals(PlateType.O) ? PlateType.X : PlateType.O;
|
||
|
}
|
||
|
|
||
|
// check if it is out turn
|
||
|
response = in.nextLine();
|
||
|
if (response.split(" ")[0].equals("setIsMyTurn")) {
|
||
|
this.setIsMyTurn(response.split(" ")[1].equals("true"));
|
||
|
opponent.setIsMyTurn(response.split(" ")[1].equals("false"));
|
||
|
}
|
||
|
|
||
|
// listen to the socket
|
||
|
ExecutorService pool = Executors.newFixedThreadPool(200);
|
||
|
pool.execute(this.new ClientListener(in, this));
|
||
|
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public void handleResponse(String response) {
|
||
|
System.out.println("[CLIENT]GOT: " + response);
|
||
|
|
||
|
if(response.startsWith("setIsMyTurn")) {
|
||
|
this.setIsMyTurn(response.split(" ")[1].equals("true"));
|
||
|
opponent.setIsMyTurn(response.split(" ")[1].equals("false"));
|
||
|
} else 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);
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void movePerformed(int column, Player src) {
|
||
|
try {
|
||
|
this.out.println("movePerformed " + column);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class ClientListener implements Runnable {
|
||
|
|
||
|
RemotePlayerClient parent;
|
||
|
Scanner in;
|
||
|
|
||
|
public ClientListener(Scanner in, RemotePlayerClient 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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|