Skip to content

Feature/multiplayer #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/main/java/pl/nogacz/snake/Snake.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,53 @@
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import pl.nogacz.snake.application.Design;
import pl.nogacz.snake.application.TwoPlayerDesign;
import pl.nogacz.snake.application.UserKeyDefiner;
import pl.nogacz.snake.board.Board;
import pl.nogacz.snake.board.TwoPlayerBoard;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import java.util.Optional;

/**
* @author Dawid Nogacz on 19.05.2019
*/
public class Snake extends Application {
Design design = new Design();
Board board = new Board(design);

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(design.getGridPane(), 715, 715, Color.BLACK);
scene.setOnKeyReleased(event -> board.readKeyboard(event));

Alert alert = new Alert(Alert.AlertType.NONE);
alert.setTitle("ModeSellection");
alert.setContentText("Please select how do you want to play");

ButtonType singlePlayerButton = new ButtonType("SinglePlayer");
ButtonType twoPlayerButton = new ButtonType("TwoPlayer");

alert.getButtonTypes().setAll(singlePlayerButton, twoPlayerButton);

Optional<ButtonType> result = alert.showAndWait();

Scene scene;

if (result.get() == singlePlayerButton){
Design design = new Design();
Board board = new Board(design);
scene = new Scene(design.getGridPane(), 715, 715, Color.BLACK);
scene.setOnKeyReleased(event -> board.readKeyboard(event));
} else {
UserKeyDefiner userKeyDefiner = new UserKeyDefiner();
userKeyDefiner.approver();
char[] newKeys = userKeyDefiner.getNewKeys();
TwoPlayerDesign design = new TwoPlayerDesign();
TwoPlayerBoard board = new TwoPlayerBoard(design, newKeys);
scene = new Scene(design.getGridPane(), 1430, 715, Color.BLACK);
scene.setOnKeyReleased(event -> board.readKeyboard(event));
}
primaryStage.setTitle("JavaSnake");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/pl/nogacz/snake/application/TwoPlayerDesign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package pl.nogacz.snake.application;


import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import pl.nogacz.snake.board.TwoPlayerBoard;
import pl.nogacz.snake.board.Coordinates;
import pl.nogacz.snake.pawn.PawnClass;

public class TwoPlayerDesign {
private GridPane gridPane = new GridPane();

public TwoPlayerDesign() {
createBoardBackground();
generateEmptyBoard();
}

private void createBoardBackground() {
Image background = new Image(Resources.getPath("twoPlayerBackground.jpg"));
BackgroundSize backgroundSize = new BackgroundSize(1430, 715, false, false, true, false);
BackgroundImage backgroundImage = new BackgroundImage(background, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, backgroundSize);
gridPane.setBackground(new Background(backgroundImage));
}

private void generateEmptyBoard() {
gridPane.setMinSize(1430, 715);
gridPane.setMaxSize(1430, 715);

for(int i = 0; i < 44; i++) {
ColumnConstraints column = new ColumnConstraints(32);
column.setHgrow(Priority.ALWAYS);
column.setHalignment(HPos.CENTER);
gridPane.getColumnConstraints().add(column);

RowConstraints row = new RowConstraints(32);
row.setVgrow(Priority.ALWAYS);
row.setValignment(VPos.CENTER);
gridPane.getRowConstraints().add(row);
}

gridPane.setPadding(new Insets(10, 0, 0, 10));
}

public void addPawn(Coordinates coordinates, PawnClass pawn) {
if(pawn.getPawn().isHead()) {
gridPane.add(pawn.getImageDirection(TwoPlayerBoard.getDirection()), coordinates.getX(), coordinates.getY());
} else if(pawn.getPawn().isHead2()) {
gridPane.add(pawn.getImageDirection(TwoPlayerBoard.getDirection2()), coordinates.getX(), coordinates.getY());
} else {
gridPane.add(pawn.getImage(), coordinates.getX(), coordinates.getY());
}
}

public void removePawn(Coordinates coordinates) {
gridPane.getChildren().removeIf(node -> GridPane.getColumnIndex(node) == coordinates.getX() && GridPane.getRowIndex(node) == coordinates.getY());
}

public GridPane getGridPane() {
return gridPane;
}
}
169 changes: 169 additions & 0 deletions src/main/java/pl/nogacz/snake/application/UserKeyDefiner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package pl.nogacz.snake.application;
import javax.swing.JFrame;
import java.util.Optional;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;

public class UserKeyDefiner extends JFrame{

JLabel message1;
JLabel message2;
JLabel message3;
JLabel message4;
JLabel message5;
JLabel message6;
JLabel message7;
JLabel message8;

JTextField input1;
JTextField input2;
JTextField input3;
JTextField input4;
JTextField input5;
JTextField input6;
JTextField input7;
JTextField input8;

JPanel panel;

private String[] inputsAsString;
private boolean inputsAreValid = false;
private char[] inputsAsChar;

public UserKeyDefiner(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(300,300);

message1 = new JLabel("First Player Up Button");
message2 = new JLabel("First Player Down Button");
message3 = new JLabel("First Player Left Button");
message4 = new JLabel("First Player Right Button");

message5 = new JLabel("Second Player Up Button");
message6 = new JLabel("Second Player Down Button");
message7 = new JLabel("Second Player Left Button");
message8 = new JLabel("Second Player Right Button");

input1 = new JTextField(10);
input2 = new JTextField(10);
input3 = new JTextField(10);
input4 = new JTextField(10);
input5 = new JTextField(10);
input6 = new JTextField(10);
input7 = new JTextField(10);
input8 = new JTextField(10);

inputsAsString = new String[8];

panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

panel.add(message1);
panel.add(input1);
panel.add(message2);
panel.add(input2);
panel.add(message3);
panel.add(input3);
panel.add(message4);
panel.add(input4);
panel.add(message5);
panel.add(input5);
panel.add(message6);
panel.add(input6);
panel.add(message7);
panel.add(input7);
panel.add(message8);
panel.add(input8);

this.add(panel);
this.pack();
this.setVisible(true);
}

public void approver(){
Alert alert;
ButtonType approve;
Optional<ButtonType> result;

alert = new Alert(Alert.AlertType.NONE);
alert.setTitle("Approve");
alert.setContentText("Please approve");

approve = new ButtonType("Approve");

alert.getButtonTypes().setAll(approve);

result = alert.showAndWait();

if(result.get() == approve){
inputsAsString[0] = input1.getText();
inputsAsString[1] = input2.getText();
inputsAsString[2] = input3.getText();
inputsAsString[3] = input4.getText();
inputsAsString[4] = input5.getText();
inputsAsString[5] = input6.getText();
inputsAsString[6] = input7.getText();
inputsAsString[7] = input8.getText();
upperCaseAllArray(inputsAsString);
if(areInputsValid(inputsAsString)){
prepareNewKeys();
inputsAreValid = true;
this.dispose();
}else{
System.out.println("Keys Entered are same or empty");
System.exit(0);
}
}
}

public char[] getNewKeys(){
return inputsAsChar;
}

public void prepareNewKeys(){
inputsAsChar = new char[8];
for(int i = 0 ; i < inputsAsString.length ; i++){
inputsAsChar[i] = inputsAsString[i].charAt(0);
}
}

public boolean areInputsReady(){
return inputsAreValid;
}

public boolean areThereEmptyInputs(String[] inputArray){
for(int i = 0 ; i < inputArray.length ; i++){
if(inputArray[i].length() == 0){
return false;
}
}
return true;
}

public boolean areInputsValid(String[] inputArray){
if(!areThereEmptyInputs(inputArray)){
return false;
}
else{
for(int i = 0 ; i < inputArray.length ; i++){
for(int j = i+1 ; j < inputArray.length ; j++){
if(inputArray[i].charAt(0) == inputArray[j].charAt(0)){
return false;
}
}
}
return true;
}
}

public void upperCaseAllArray(String[] inputArray){
for(int i = 0 ; i < inputArray.length ; i++){
inputArray[i] = inputArray[i].toUpperCase();
}
}

}
4 changes: 4 additions & 0 deletions src/main/java/pl/nogacz/snake/board/Coordinates.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public boolean isValid() {
return x <= 21 && x >= 0 && y <= 21 && y >= 0;
}

public boolean isValidForSecond() {
return x <= 43 && x >= 22 && y <= 21 && y >= 0;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Loading