Skip to content
Snippets Groups Projects
Commit b8412487 authored by Bariatti Francesco's avatar Bariatti Francesco
Browse files
Conflicts:
	src/model/Board.java
parents 03e4e948 2c6a2b6b
No related branches found
No related tags found
No related merge requests found
Pipeline #
package controller; package controller;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Iterator;
import java.util.Random;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import model.Board; import model.Board;
public class GameController implements Initializable { public class GameController implements Initializable {
...@@ -24,7 +27,7 @@ public class GameController implements Initializable { ...@@ -24,7 +27,7 @@ public class GameController implements Initializable {
private GridPane board; private GridPane board;
@FXML @FXML
private Label levelNumber; private Label labelLevelNumber;
/* ----- ATTRIBUTES ----- */ /* ----- ATTRIBUTES ----- */
...@@ -32,27 +35,78 @@ public class GameController implements Initializable { ...@@ -32,27 +35,78 @@ public class GameController implements Initializable {
/** /**
* *
*/ */
private static final String LEVEL_FILE = "data/level"; private static final String LEVEL_FILENAME = "data/level";
/** /**
* The model of the controller. *
*/
private static final int HOW_MANY_LEVELS = 1;
/**
*
*/ */
private Board model; private Board model;
/**
*
*/
private Color[] colors;
/**
*
*/
private IntegerProperty levelNumber;
public GameController() {
levelNumber = new SimpleIntegerProperty();
}
public void nextLevel() {
levelNumber.set(levelNumber.get() + 1);
try {
model = new Board(LEVEL_FILENAME + levelNumber.get() + ".txt");
} catch (IOException e) {
System.out.println(LEVEL_FILENAME + levelNumber.get() + ".txt not found");
}
}
private void initColors() {
colors = new Color[model.getCarsNumber()];
colors[0] = Color.RED;
for (int i = 1; i < colors.length; i++) {
Random random = new Random();
colors[i] = Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
}
private void initBoard() {
for (int i = 0; i < model.getSize(); i++)
for (int j = 0; j < model.getSize(); j++)
setCaseColor(i, j, model.getValue(i, j));
}
private void setCaseColor(int i, int j, int value) {
Iterator<Node> itr = board.getChildren().iterator();
for (int k = 0; k < i * model.getSize() + j; k++)
itr.next();
((Rectangle) itr.next()).setFill(colors[value]);
}
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
board.setGridLinesVisible(true); nextLevel();
Rectangle cell;
double size = 300.0 / model.getSize(); double size = 300.0 / model.getSize();
Pane cell;
boolean tmp = true;
for (int i = 0; i < model.getSize(); i++) for (int i = 0; i < model.getSize(); i++)
for (int j = 0; j < model.getSize(); j++) { for (int j = 0; j < model.getSize(); j++)
cell = new Pane(); board.add(new Rectangle(size, size), i, j);
cell.setPrefSize(size, size);
board.add(cell, i, j); labelLevelNumber.textProperty().bind(levelNumber.asString());
} initColors();
initBoard();
} }
} }
\ No newline at end of file
...@@ -60,7 +60,7 @@ public class Board { ...@@ -60,7 +60,7 @@ public class Board {
Car car = new Car(i, pos1, pos2, Car.Orientation.valueOf(splitl[4])); Car car = new Car(i, pos1, pos2, Car.Orientation.valueOf(splitl[4]));
cars[i] = car; cars[i] = car;
if(car.getOrientation() == Car.Orientation.HORIZONTAL) if(car.getOrientation() == Car.Orientation.HORIZONTAL)
for(int x = car.getBack().getX(); x <= car.getFront().getX(); x++) for(int x = car.getBack().getX(); x <= car.getFront().getX(); x++)
board[x][car.getFront().getY()] = car.getNumber(); board[x][car.getFront().getY()] = car.getNumber();
...@@ -100,7 +100,7 @@ public class Board { ...@@ -100,7 +100,7 @@ public class Board {
} }
return sb.toString(); return sb.toString();
} }
public int[][] getBoard() public int[][] getBoard()
{ {
return board; return board;
...@@ -110,7 +110,7 @@ public class Board { ...@@ -110,7 +110,7 @@ public class Board {
{ {
this.board = board; this.board = board;
} }
public static void main(String[]args) throws IOException{ public static void main(String[]args) throws IOException{
Board test = new Board("testlevel.txt"); Board test = new Board("testlevel.txt");
System.out.println(test.toString()); System.out.println(test.toString());
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="controller.GameController" stylesheets="style.css"> <BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="controller.GameController" stylesheets="style.css">
<center> <center>
<GridPane fx:id="board" alignment="CENTER"> <GridPane fx:id="board" alignment="CENTER">
...@@ -18,7 +17,7 @@ ...@@ -18,7 +17,7 @@
<bottom> <bottom>
<HBox alignment="CENTER"> <HBox alignment="CENTER">
<Label text="Level : " /> <Label text="Level : " />
<Label fx:id="levelNumber" /> <Label fx:id="labelLevelNumber" />
<padding> <padding>
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</padding> </padding>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment