Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
added data access modifiers, moved GUI and communication outside of c…
Browse files Browse the repository at this point in the history
…lient/Runner, polished MenuScreen and BoardGUI
  • Loading branch information
Adam Procio committed Apr 19, 2019
1 parent b9d2e74 commit 9195ae1
Show file tree
Hide file tree
Showing 27 changed files with 251 additions and 135 deletions.
7 changes: 6 additions & 1 deletion README.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ <h2>How-to</h2>

<h2>TODO</h2>

<p>Client creates it&rsquo;s own thread for server in case of game on 1 machine</p>
<ul>
<li>[ ] Client creates it&rsquo;s own thread for server in case of game on 1 machine</li>
<li>[ ] Gui in own thread</li>
<li>[ ]</li>
</ul>

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Running server for server testing
mvn exec:java -Dexec.arguments=client
`

## How-to
Run client and select new game, that's all the functionality right now.

## TODO
Client creates it's own thread for server in case of game on 1 machine
* [ ] Client creates it's own thread for server in case of game on 1 machine
* [ ] Gui in own thread
* [ ] Add modifiers to class datastructures (for security)
9 changes: 4 additions & 5 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import javafx.stage.Stage;

public class Main {

public static void main(String[] args) throws Exception {
if(args == null || args.length == 0){
/* run client */
System.out.println("Running client");
client.Runner.main(args);
}else if(args.length == 1) {
if (args[0].equals("client")) {
/* run client */
System.out.println("Running client");
client.Runner.main(args);
} else if (args[0].equals("server")) {
/* run server */
server.Runner server = new server.Runner();
System.out.println("Running server");
try {
server.start();
server.Runner.main(args);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/client/GUI/BoardGUI.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
package client.GUI;

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class BoardGUI extends GridPane {
Integer[][] state;
public class BoardGUI{
private Scene scene;
private GridPane pane, boardPane;
private Integer[][] state;
private Button saveGame;

public BoardGUI(Integer[][] state){
public BoardGUI(Stage stage, Integer[][] state){
this.state = state;
this.setMinSize(320,320);
this.setAlignment(Pos.CENTER);
this.setGridLinesVisible(true);
Button button;
this.pane = new GridPane();
this.pane.setAlignment(Pos.CENTER);
this.boardPane = new GridPane();
this.boardPane.setGridLinesVisible(true);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
button = new Button("");
button.setMinSize(40,40);
this.add(button, i, j);
Button button = new Button("");
button.setMinSize(MinDims.BUTTON.width, MinDims.BUTTON.height);
this.boardPane.add(button, i, j);
}
}
this.pane.add(this.boardPane, 0, 0);
this.saveGame = new Button("Save Game");
this.pane.add(this.saveGame, 0, 1);
this.scene = new Scene(this.pane);
stage.setScene(this.scene);
}

public Integer[][] getState() {
return state;
}

}
35 changes: 35 additions & 0 deletions src/main/java/client/GUI/MainGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client.GUI;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class MainGUI extends Application {
public void start(Stage stage) throws Exception {
stage.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println(mouseEvent.getTarget().toString());
}
});
stage.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("uhh");
}
});
/**New game
* vs Computer
* vs player on 1 pc
* vs player on the internet
* Load game (PGN)
* Watch game (PGN)
*/
System.out.println("Setting menu screen");
MenuScreen menuScreen = new MenuScreen(stage);
/* TODO catch messages from MenuScreen gui */
}

public static void main(String[] args) {
launch(args);
}
}
102 changes: 99 additions & 3 deletions src/main/java/client/GUI/MenuScreen.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,105 @@
package client.GUI;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.input.MouseEvent;

public class MenuScreen {
Scene scene;
public MenuScreen(){
class MenuScreen {
private Scene scene;
private Text titleText;
private GridPane pane;
private Button button1, button2, button3;

MenuScreen(Stage stage){
this.pane = new GridPane();
this.pane.setAlignment(Pos.CENTER);
this.pane.setVgap(5);
this.titleText = new Text("Game Menu");
this.pane.add(this.titleText, 0, 0);
this.button1 = new Button("New Game");
this.button1.setOnMouseClicked(newHandler(1, stage));
this.pane.add(this.button1, 0, 1);
this.button2 = new Button("Load Game");
this.button2.setOnMouseClicked(newHandler(2, stage));
this.pane.add(this.button2, 0, 2);
this.button3 = new Button("Watch Game");
this.button3.setOnMouseClicked(newHandler(3, stage));
this.pane.add(this.button3, 0, 3);
this.scene = new Scene(this.pane, MinDims.SCENE.width, MinDims.SCENE.height);
stage.setScene(this.scene);
stage.show();
}

private EventHandler<MouseEvent> newHandler(int i, final Stage stage){
switch(i){
case 1:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("NewGame Button clicked");
newGameClicked(stage);
}
};
case 2:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("LoadGame Button clicked");
loadGameClicked(stage);
}
};
case 3:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("WatchGame Button clicked");
watchGameClicked(stage);
}
};
default:
return null;
}
}

private void newGameClicked(final Stage stage){
this.titleText = new Text("New Game Menu");
this.pane = new GridPane();
this.pane.setAlignment(Pos.CENTER);
this.pane.setVgap(5);
this.button1 = new Button("Local Game");
// this.button1.setOnMouseClicked(new EventHandler<MouseEvent>() {
// public void handle(MouseEvent mouseEvent) {
// System.out.println("stuff");
// }
// });
this.button2 = new Button("Remote Game");
this.pane.add(this.titleText, 0, 0);
this.pane.add(this.button1, 0, 1);
this.pane.add(this.button2, 0, 2);
this.scene = new Scene(this.pane, MinDims.SCENE.width, MinDims.SCENE.height);
stage.setScene(this.scene);
}

private void loadGameClicked(final Stage stage){
this.titleText = new Text("Load Game Menu");
this.pane = new GridPane();
this.pane.setAlignment(Pos.CENTER);
this.pane.setVgap(5);
this.pane.add(this.titleText, 0, 0);
this.scene = new Scene(this.pane, MinDims.SCENE.width, MinDims.SCENE.height);
stage.setScene(this.scene);
}

private void watchGameClicked(final Stage stage){
this.titleText = new Text("Watch Game Menu");
this.pane = new GridPane();
this.pane.setAlignment(Pos.CENTER);
this.pane.setVgap(5);
this.pane.add(this.titleText, 0, 0);
this.scene = new Scene(this.pane, MinDims.SCENE.width, MinDims.SCENE.height);
stage.setScene(this.scene);
}

}
13 changes: 13 additions & 0 deletions src/main/java/client/GUI/MinDims.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package client.GUI;

public enum MinDims{
BUTTON (40, 40),
SCENE (320, 320);

public int width, height;

MinDims(int w, int h) {
this.width = w;
this.height = h;
}
}
106 changes: 11 additions & 95 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,19 @@
package client;

import client.GUI.BoardGUI;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import logic.Player;
import server.Board;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class Runner extends Application{
/* has gui, player and sockets */
BoardGUI gui;
Player player;

public void start(Stage stage) throws Exception {
/* menu screen */
/**TODO
* New game
* vs Computer
* vs player on 1 pc
* vs player on the internet
* Load game
* Watch game (PGN)
*/
menuSetup(stage);
/* Board GUI setup */
import client.GUI.MainGUI;
import client.communication.Utils;

public class Runner{
public static void main(String[] args) throws Exception {
/* TODO get port from user to connect to in the GUI*/
/* TODO if single game, run server from here */
MainGUI.main(args);
int port = 8888;
try {
connect();
System.out.println("Connecting to server");
Utils.connect(port);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void connect() throws Exception {
/**TODO
* if game selected is on 1 machine, create thread with server running in background
*/
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("localhost", 8888);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println(modifiedSentence);
clientSocket.close();
}
public void menuSetup(final Stage stage){
/**TODO
* if button selected, create new scene based on button
*/
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setVgap(5);

Text titleText = new Text("Game Menu");
pane.add(titleText, 0, 0);
Button newGameButton = new Button("New Game");
newGameButton.setOnMouseClicked(
new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("Button click");
/*TODO setup board correctly */
gui = new BoardGUI(null);
Scene scene = new Scene(gui, 400, 400);
stage.setScene(scene);
}
});
pane.add(newGameButton, 0, 1);
Button loadGameButton = new Button("Load Game");
pane.add(loadGameButton, 0, 2);
Button watchGameButton = new Button("Watch Game");
pane.add(watchGameButton, 0, 3);

Scene scene = new Scene(pane, 400, 400);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}
25 changes: 25 additions & 0 deletions src/main/java/client/communication/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package client.communication;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class Utils {
public static void connect(int port) throws Exception {
/**TODO
* if game selected is on 1 machine, create thread with server running in background
*/
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("localhost", port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

String sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
String modifiedSentence = inFromServer.readLine();
System.out.println(modifiedSentence);
clientSocket.close();
}
}
Loading

0 comments on commit 9195ae1

Please sign in to comment.