This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
279 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package de.tilosp.chess.gui; | ||
|
||
import de.tilosp.chess.lib.PlayerColor; | ||
import de.tilosp.chess.localisation.Localisation; | ||
import de.tilosp.chess.player.LocalPlayer; | ||
import de.tilosp.chess.player.NetworkPlayer; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.WindowEvent; | ||
import java.awt.event.WindowListener; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
final class WaitGUI extends GUI implements WindowListener { | ||
|
||
private final ServerSocket socket; | ||
private JLabel label; | ||
|
||
WaitGUI(ServerSocket socket, PlayerColor playerColor) { | ||
super(); | ||
this.socket = socket; | ||
label.setText(String.format(Localisation.getString("wait.text"), socket.getLocalPort())); | ||
|
||
new Thread(() -> { | ||
try { | ||
Socket s = socket.accept(); | ||
OutputStream out = s.getOutputStream(); | ||
out.write(playerColor.otherColor().ordinal()); | ||
out.flush(); | ||
|
||
if (playerColor == PlayerColor.WHITE) | ||
new ChessboardGUI(new LocalPlayer(), new NetworkPlayer(s)).setVisible(true); | ||
else | ||
new ChessboardGUI(new NetworkPlayer(s), new LocalPlayer()).setVisible(true); | ||
dispose(); | ||
} catch (IOException ignored) {} | ||
}).start(); | ||
} | ||
|
||
@Override | ||
void initGUI() { | ||
setTitle(Localisation.getString("wait.title")); | ||
setResizable(false); | ||
setPreferredSize(new Dimension(300, 100)); | ||
addWindowListener(this); | ||
|
||
panel.setLayout(new BorderLayout()); | ||
|
||
label = new JLabel(); | ||
label.setHorizontalAlignment(SwingConstants.CENTER); | ||
panel.add(label); | ||
|
||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||
} | ||
|
||
@Override | ||
void initListeners() { | ||
|
||
} | ||
|
||
@Override | ||
public void windowOpened(WindowEvent e) { | ||
|
||
} | ||
|
||
@Override | ||
public void windowClosing(WindowEvent e) { | ||
|
||
} | ||
|
||
@Override | ||
public void windowClosed(WindowEvent e) { | ||
try { | ||
socket.close(); | ||
} catch (IOException ignore) {} | ||
} | ||
|
||
@Override | ||
public void windowIconified(WindowEvent e) { | ||
|
||
} | ||
|
||
@Override | ||
public void windowDeiconified(WindowEvent e) { | ||
|
||
} | ||
|
||
@Override | ||
public void windowActivated(WindowEvent e) { | ||
|
||
} | ||
|
||
@Override | ||
public void windowDeactivated(WindowEvent e) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,9 @@ public void sendUpdate(Chessboard chessboard) { | |
}).start(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClosed() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.tilosp.chess.player; | ||
|
||
import de.tilosp.chess.lib.ChessEngine; | ||
import de.tilosp.chess.lib.Chessboard; | ||
import de.tilosp.chess.lib.PlayerColor; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.net.Socket; | ||
|
||
public final class NetworkPlayer extends Player { | ||
|
||
private final Socket socket; | ||
private final DataInputStream in; | ||
private final DataOutputStream out; | ||
private volatile boolean running = true; | ||
|
||
public NetworkPlayer(Socket socket) throws IOException { | ||
this.socket = socket; | ||
in = new DataInputStream(socket.getInputStream()); | ||
out = new DataOutputStream(socket.getOutputStream()); | ||
|
||
new Thread(() -> { | ||
while (running) { | ||
try { | ||
if (in.available() > 0) | ||
update(Chessboard.read(in)); | ||
} catch (IOException ignored) {} | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException ignored) {} | ||
} | ||
}).start(); | ||
} | ||
|
||
@Override | ||
public void sendUpdate(Chessboard chessboard) { | ||
if (chessboard.playerColor == color && !chessboard.isDraw() && !chessboard.isWin(PlayerColor.WHITE) && !chessboard.isWin(PlayerColor.BLACK)) { | ||
try { | ||
chessboard.write(out); | ||
out.flush(); | ||
} catch (IOException ignored) {} | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public void onClosed() { | ||
running = false; | ||
try { | ||
in.close(); | ||
out.close(); | ||
socket.close(); | ||
} catch (IOException ignored) {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters