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;
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.net.Socket;
|
2020-02-25 15:34:07 +01:00
|
|
|
import java.awt.event.*;
|
2020-02-25 02:10:57 +01:00
|
|
|
|
|
|
|
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 RemotePlayerClient extends RemotePlayer {
|
2020-02-25 02:10:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param controlledByButtonRow The button row used to control the player.
|
|
|
|
*/
|
2020-02-25 11:17:17 +01:00
|
|
|
public RemotePlayerClient(ButtonRow gameControllingButtonRow, String serverAddress, Player opponent)
|
|
|
|
throws IOException {
|
|
|
|
super(gameControllingButtonRow, null, opponent);
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
// 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");
|
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
|
|
|
}
|
|
|
|
|
2020-02-25 21:19:29 +01:00
|
|
|
@Override
|
|
|
|
public void handleResponse(String response) {
|
2020-02-25 15:34:07 +01:00
|
|
|
System.out.println("[SOCKET] Got: " + response);
|
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
if (response.startsWith("setUsingPlateType")) {
|
|
|
|
// handle PlateType change
|
2020-02-25 15:34:07 +01:00
|
|
|
this.opponent.usingPlateType = PlateType.valueOf(response.split(" ")[1]);
|
|
|
|
this.usingPlateType = this.usingPlateType == PlateType.O ? PlateType.X : PlateType.O;
|
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
} else if (response.startsWith("setIsMyTurn")) {
|
|
|
|
// handle turn change
|
2020-02-25 15:34:07 +01:00
|
|
|
PlateType targetPlateType = PlateType.valueOf(response.split(" ")[1]);
|
|
|
|
Player target = this.getUsedPlateType().equals(targetPlateType) ? this:this.opponent;
|
|
|
|
target.setIsMyTurn(response.split(" ")[2].equals("true"));
|
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
} else if (response.startsWith("movePerformed")) {
|
|
|
|
// handle move performed
|
2020-02-25 15:34:07 +01:00
|
|
|
PlateType targetPlateType = PlateType.valueOf(response.split(" ")[1]);
|
|
|
|
Player target = this.getUsedPlateType().equals(targetPlateType) ? this:this.opponent;
|
|
|
|
this.fireActionListeners(target, "doMove " + Integer.parseInt(response.split(" ")[2]));
|
|
|
|
|
|
|
|
} else if(response.startsWith("gameOver") || response.startsWith("gameReset")) {
|
|
|
|
// handle game over
|
|
|
|
this.fireActionListeners(this, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
if(!(e.getSource() instanceof Player))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (e.getActionCommand().startsWith("doMove")) {
|
|
|
|
int column = Integer.parseInt(e.getActionCommand().split(" ")[1]);
|
|
|
|
try {
|
|
|
|
this.out.println("doMove " + column);
|
|
|
|
System.out.println("[SOCKET] SENT: " + "doMove " + column);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2020-02-25 02:10:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|