This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
connect-four/src/de/itsblue/ConnectFour/player/RemotePlayerClient.java

72 lines
2.6 KiB
Java
Raw Normal View History

/*
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.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 RemotePlayer {
/**
* Constructor
*
* @param controlledByButtonRow The button row used to control the player.
*/
public RemotePlayerClient(ButtonRow gameControllingButtonRow, String serverAddress, Player opponent)
throws IOException {
super(gameControllingButtonRow, null, opponent);
// initialize the socket
socket = new Socket(serverAddress, 4444);
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Connected to Connect4 server");
this.startListening();
}
public void handleResponse(String response) {
if (response.startsWith("setUsingPlateType")) {
// handle PlateType change
this.usingPlateType = PlateType.valueOf(response.split(" ")[1]);
this.opponent.usingPlateType = this.usingPlateType == PlateType.O ? PlateType.X : PlateType.O;
} else if (response.startsWith("setIsMyTurn")) {
// handle turn change
this.setIsMyTurn(response.split(" ")[1].equals("true"));
opponent.setIsMyTurn(response.split(" ")[1].equals("false"));
} else if (response.startsWith("movePerformed")) {
// handle move performed
this.doMove(Integer.parseInt(response.split(" ")[1]));
}
}
}