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/RemotePlayerServer.java

115 lines
3.6 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.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.util.Scanner;
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 RemotePlayer {
private ServerSocket serverSocket;
/**
* 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)
throws Exception {
super(gameControllingButtonRow, usingPlateType, opponent);
// also catch own actions
this.addActionListener(this);
this.serverSocket = new ServerSocket(4444);
System.out.println("Connect4 Server is Running...");
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);
System.out.println("Client connected");
out.println("setUsingPlateType " + this.usingPlateType.name());
this.startListening();
}
@Override
public void handleResponse(String response) {
if (response.startsWith("doMove")) {
if (this.isMyTurn)
this.doMove(Integer.parseInt(response.split(" ")[1]));
else
this.out.println("invalidMove " + response.split(" ")[1]);
}
}
public void notifyGameOver(String reason) {
this.out.println("gameOver " + reason);
}
public void notifyGameReset() {
this.out.println("gameReset");
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Catch actions of opponent and forward them to client
*/
@Override
public void actionPerformed(ActionEvent e) {
if(!(e.getSource() instanceof Player))
return;
Player src = (Player)e.getSource();
if (e.getActionCommand().startsWith("doMove")) {
int column = Integer.parseInt(e.getActionCommand().split(" ")[1]);
try {
this.out.println("movePerformed " + src.getUsedPlateType().toString() + " " + column);
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (e.getActionCommand().startsWith("isMyTurnChanged")) {
out.println("setIsMyTurn " + src.getUsedPlateType().toString() + " " + (e.getActionCommand().split(" ")[1]));
}
}
}