Skip to content

Commit

Permalink
Merge pull request #4 from ThePowerRule/Delta
Browse files Browse the repository at this point in the history
Delta
  • Loading branch information
m516 authored Apr 5, 2017
2 parents fa2dc03 + 6228721 commit cc5cb70
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 48 deletions.
Binary file modified bin/gui/GUIController.class
Binary file not shown.
7 changes: 4 additions & 3 deletions bin/gui/GUILayout.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.text.Font?>

<AnchorPane fx:id="windowPane" maxHeight="512.0" maxWidth="640.0" minHeight="512.0" minWidth="640.0" prefHeight="512.0" prefWidth="640.0" scaleShape="false" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.GUIController">
<AnchorPane fx:id="windowPane" maxHeight="512.0" maxWidth="640.0" minHeight="512.0" minWidth="640.0" prefHeight="512.0" prefWidth="640.0" scaleShape="false" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.GUIController">
<children>
<AnchorPane fx:id="arenaPane" minHeight="384.0" minWidth="384.0" prefHeight="512.0" prefWidth="384.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="256.0" AnchorPane.topAnchor="0.0">
<children>
Expand Down Expand Up @@ -62,9 +62,10 @@
</font>
</Label>
<Button fx:id="buttonServer" layoutX="177.0" layoutY="460.0" mnemonicParsing="false" onAction="#buttonGoPressed" prefHeight="24.0" prefWidth="35.0" text="Go!" AnchorPane.leftAnchor="177.0" />
<TableView fx:id="listSnakes" editable="true" layoutX="28.0" layoutY="241.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="28.0">
<TableView fx:id="listSnakes" editable="true" layoutX="28.0" layoutY="241.0" onContextMenuRequested="#tableEdited" onMouseClicked="#tableEdited" onMouseDragged="#tableEdited" onMouseEntered="#tableEdited" onMouseExited="#tableEdited" onMouseMoved="#tableEdited" onMousePressed="#tableEdited" onMouseReleased="#tableEdited" prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="28.0">
<columns>
<TableColumn fx:id="mySnakes" prefWidth="199.0" text="My snakes" />
<TableColumn fx:id="columnSnakeNames" editable="false" prefWidth="167.0" text="My snakes" />
<TableColumn fx:id="columnID" editable="false" prefWidth="32.0" text="ID" />
</columns>
</TableView>
<TextField fx:id="textfieldServer" layoutX="14.0" layoutY="460.0" onAction="#serverAddressEntered" prefHeight="25.0" prefWidth="163.0" promptText="Go to server" text="127.0.0.1" />
Expand Down
68 changes: 48 additions & 20 deletions src/application/Arena.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//TODO Add functionality for multiple snakes
package application;
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
Expand Down Expand Up @@ -29,9 +28,8 @@ public class Arena{
private static volatile byte[][] arena;
private static int xSize, ySize;
public static Arena instance = new Arena();
static Color[] snakeColors;
private static GraphicsContext graphics;
static Color bkg;
private static Color bkg;
private static Canvas canvas;
private Arena(){
}
Expand All @@ -45,7 +43,7 @@ public static int getYSize() {
* Snakes must not call this method or ANY OTHER MUTATOR METHODS in this class!
* It will throw errors in the application and will not change anything in the server.
*/
public static void init(int new_x_size, int new_y_size, int numSnakes){
public static void init(int new_x_size, int new_y_size){
arena = new byte[new_x_size][new_y_size];
xSize = new_x_size;
ySize = new_y_size;
Expand All @@ -54,10 +52,9 @@ public static void init(int new_x_size, int new_y_size, int numSnakes){
arena[i][j] = ERR;
}
}
snakeColors = new Color[numSnakes+1];
for(int i = 0; i <= numSnakes; i ++){
snakeColors[i] = Color.hsb(360.0*(float)i/(float)numSnakes, 1f, 1f);
}
System.out.print("Arena initialized with ");
System.out.print("x-size: " + xSize);
System.out.println(" and y-size: " + ySize + ".");
}
public static void setBlock(int x, int y, byte type){
arena[x][y] = type;
Expand All @@ -72,13 +69,6 @@ public static void setCanvas(Canvas newCanvas){

public synchronized void repaint(){
Platform.runLater(() -> {
int snakeID = SnakeManager.getSnake(0).getID()-1 - FRUIT;
if(snakeID > 0){
bkg = snakeColors[snakeID].darker().darker();
}
else{
bkg = Color.BLACK;
}
graphics.setFill(bkg);
graphics.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
for(int i = 0; i < arena.length; i ++){
Expand All @@ -99,10 +89,7 @@ public synchronized void repaint(){
c = Color.MAGENTA;
break;
default:
if(cell < snakeColors.length+FRUIT && cell > FRUIT){
c = snakeColors[cell-FRUIT-1];
}
else c = Color.DARKGRAY;
c = getSnakeColor(cell-1-FRUIT);
break;
}
drawCell(i,j,c);
Expand Down Expand Up @@ -140,7 +127,7 @@ public synchronized static boolean retrieveCommand(int commandType, Integer[] co
switch(commandType){
case ARENA_CONFIG:
Console.addText("Arena configured");
init(command[0], command[1], command[2]);
init(command[0], command[1]);
break;
case ARENA_DISPLAY:
for(int i = 0; i < command.length; i ++){
Expand All @@ -160,5 +147,46 @@ public synchronized static boolean retrieveCommand(int commandType, Integer[] co
}
return true;
}

public static Color getSnakeColor(int snakeNumber){
switch(snakeNumber){
case 0: return Color.BLUE;
case 1: return Color.RED;
case 2: return Color.GREEN;
case 3: return Color.YELLOW;
case 4: return Color.ORANGE;
case 5: return Color.PURPLE;
case 6: return Color.DODGERBLUE;
case 7: return Color.INDIGO;
case 8: return Color.AQUA;
case 9: return Color.DARKRED;
case 10: return Color.DARKBLUE;
case 11: return Color.DARKGREEN;
case 12: return Color.DARKORANGE;
case 13: return Color.DARKORCHID;
case 14: return Color.GOLD;
case 15: return Color.PERU;
case 16: return Color.TEAL;
case 17: return Color.YELLOWGREEN;
default:
return Color.hsb(((double)snakeNumber)*11%1, 0.5, 1.0);


}
}

/**
* @return the background color
*/
public static Color getBkg() {
return bkg;
}

/**
* @param bkg the color to set the background color to
*/
public static void setBkg(Color bkg) {
Arena.bkg = bkg;
}
}

13 changes: 8 additions & 5 deletions src/application/ServerBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private int getInt(){
} catch (NumberFormatException | IOException e) {
Console.addText("Error parsing this line");
Console.addText(line);
e.printStackTrace();
closeSocket();

}
Expand Down Expand Up @@ -97,10 +98,10 @@ public void connectToServer(String serverAddress, int port){
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
//Connected! testing connections
Console.addText("Connected to " + portNumber);
writeToServer("Requesting test response");
line = in.readLine();
//writeToServer("Requesting test response");
//line = in.readLine();
//Printing the server's response to this successful operation
Console.addText("Server: " + line);
//Console.addText("Server: " + line);
isLive = true;
//Create a scanner for the new stream
listenAndParse();
Expand All @@ -112,6 +113,8 @@ public void connectToServer(String serverAddress, int port){
catch (IOException e) {
Console.addText("Couldn't get I/O for the connection to " + hostAddress +
", port number " + portNumber);
Console.addText(e.getMessage());
e.printStackTrace();
isLive = false;
}
catch (Exception e){
Expand Down Expand Up @@ -194,8 +197,8 @@ public synchronized void run(){
break;
case SNAKE_CONFIG:
//Get the ID of the snake
snake.setID(intArray[0]);
Console.addText("ID: "+snake.getID());
snake.setId(intArray[0]);
Console.addText("ID: "+snake.getId());
Console.addText("Size: "+(intArray.length-1)/2);
LocI[] locations = new LocI[(intArray.length-1)/2];
for(int i = 1; i < intArray.length-1; i += 2){
Expand Down
22 changes: 16 additions & 6 deletions src/application/Snake.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package application;
import java.util.ArrayList;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

/**
* The <i>SnakeRoot</i> class is an abstract class that users can extend to make
* snakes that compete online. It includes basic functions that most snakes
Expand All @@ -20,7 +23,8 @@ public abstract class Snake {
protected static final int OUTOFBOUNDS = 0, EMPTY = 1, WALL = 2, FRUIT = 3;
int steps = 0;
private ArrayList<LocI> segs = new ArrayList<LocI>();
private int id = -1;
private final SimpleStringProperty name = new SimpleStringProperty(name());
private final SimpleIntegerProperty id = new SimpleIntegerProperty(-1);
public Snake(){};
/**
* The constructor for a snake
Expand Down Expand Up @@ -58,7 +62,7 @@ public void init(LocI[] locations){
/**
* @return the name of the snake
*/
public abstract String getName();
protected abstract String name();
/**
* This method updates the snake's position of the <i>move</i> method.
* It should not be called by snakes.
Expand Down Expand Up @@ -314,15 +318,21 @@ final public int size() {
/**
* @return the ID of the snake
*/
final public int getID(){
return id;
final public int getId(){
return id.get();
}
/**
* Sets the ID of the snake
* @param newID the new ID of the snake
*/
final public void setID(int newID){
id = newID;
final public void setId(int newID){
id.set(newID);
}
/**
* @return the name of the snake
*/
final public String getName(){
return name.get();
}
/**
* This method allows users to get the locations of the segments of their snake.
Expand Down
25 changes: 22 additions & 3 deletions src/application/SnakeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.util.ArrayList;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class SnakeManager {
private volatile ArrayList<ServerBridge> sockets;
private static SnakeManager thisInstance = new SnakeManager();
private volatile static ObservableList<Snake> snakes = FXCollections.observableArrayList();
private volatile static ArrayList<ServerBridge> sockets;
/**
* Constructs a new instance of SnakeManager
*/
private SnakeManager() {
public SnakeManager()
sockets = new ArrayList<ServerBridge>();
System.out.println("Snake Manager initialized");
}
Expand Down Expand Up @@ -63,6 +66,16 @@ public synchronized static String move(int index){
return "" + thisInstance.sockets.get(index).getSnake().update();
}
/**
* Moves a snake
* @param index
* @return an integer value representing the new direction
* of the snake
* @see Snake.move(), Snake.update()
*/
public synchronized static String move(int index){
return "" + snakes.get(index).update();
}
/**
* Closes all of the <code>ServerBridge</code> instances
* and removes them from the SnakeManager's list of
* sockets
Expand All @@ -72,4 +85,10 @@ public synchronized static void closeAllBridges(){
thisInstance.sockets.remove(i).closeSocket();
}
}
/**
* @return the snake list, used for the GUI manager
*/
public static ObservableList<Snake> getSnakes() {
return snakes;
}
}
4 changes: 1 addition & 3 deletions src/application/TestSnake.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ public class TestSnake extends Snake {

@Override
public int move() {
Console.addText(getHead().toString());
timer--;
if(timer <= 0){
target = findFruit();
timer = target.distanceEstimate(getHead());
Console.addText(target.toString());
}
//Are conditions favorable?
boolean isRight = target.getX()>getHead().getX() && canGoRight();
Expand Down Expand Up @@ -66,7 +64,7 @@ private LocI findFruit(){
return new LocI(1,1);
}
@Override
public String getName() {
public String name() {
return "Super Snake";
}
}
2 changes: 1 addition & 1 deletion src/application/TestSnake2.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int move() {
return direction;
}
@Override
public String getName() {
public String name() {
return super.toString();
}
}
32 changes: 28 additions & 4 deletions src/gui/GUIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

public class GUIController implements Initializable {

@FXML
private ResourceBundle resources;

@FXML
private URL location;

@FXML
private AnchorPane windowPane;

Expand Down Expand Up @@ -47,16 +55,19 @@ public class GUIController implements Initializable {
private Label textStatus;

@FXML
private TextField textfieldServer;
private Button buttonServer;

@FXML
private Button buttonServer;
private TableView<Snake> listSnakes;

@FXML
private TableView<?> listSnakes;
private TableColumn<Snake, String> columnSnakeNames;

@FXML
private TableColumn<?, ?> mySnakes;
private TableColumn<Snake, Integer> columnID;

@FXML
private TextField textfieldServer;

AppManager appManager;

Expand All @@ -79,9 +90,15 @@ void serverAddressEntered(ActionEvent event) {
@Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
System.out.println("GUIController initialized");
//Configure the app manager
appManager = AppManager.getCurrentAppManager();
textName.setText(SnakeManager.getSnake(0).getName());
Arena.setCanvas(arena);
//Initialize the table
listSnakes.setItems(SnakeManager.getSnakes());
//Initialize the column
columnSnakeNames.setCellValueFactory(new PropertyValueFactory<Snake,String>("name"));
columnID.setCellValueFactory(new PropertyValueFactory<Snake,Integer>("id"));
System.out.println("Controller AppManager instance: " + appManager);


Expand All @@ -91,5 +108,12 @@ public void setAppManagerInstance(AppManager am){
appManager = am;
System.out.println("Controller AppManager instance: " + appManager);
}


@FXML
void tableEdited(MouseEvent event) {
Snake currentSnake = listSnakes.getSelectionModel().getSelectedItem();
if(currentSnake != null)Arena.setBkg(Arena.getSnakeColor(currentSnake.getId()).darker().darker());
}

}
Loading

0 comments on commit cc5cb70

Please sign in to comment.