Skip to content
Snippets Groups Projects
Commit 6f1d9e77 authored by ayazb7's avatar ayazb7
Browse files

Adding Game Logic Changes

parent 0b20a7fc
Branches
No related tags found
No related merge requests found
Showing
with 169 additions and 15 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA">
<option name="configuration">
<map>
<entry key="checkstyle-version" value="9.2.1" />
<entry key="copy-libs" value="true" />
<entry key="location-0" value="BUNDLED:(bundled):Sun Checks" />
<entry key="location-1" value="BUNDLED:(bundled):Google Checks" />
<entry key="scan-before-checkin" value="false" />
<entry key="scanscope" value="JavaOnly" />
<entry key="suppress-errors" value="false" />
</map>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GoogleJavaFormatSettings">
<option name="enabled" value="false" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="CheckStyle-IDEA-Module">
<option name="configuration">
<map />
</option>
</component>
</module>
\ No newline at end of file
...@@ -4,6 +4,9 @@ import org.apache.logging.log4j.LogManager; ...@@ -4,6 +4,9 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import uk.ac.soton.comp1206.component.GameBlock; import uk.ac.soton.comp1206.component.GameBlock;
import java.util.ArrayList;
import java.util.Random;
/** /**
* The Game class handles the main logic, state and properties of the TetrECS game. Methods to manipulate the game state * The Game class handles the main logic, state and properties of the TetrECS game. Methods to manipulate the game state
* and to handle actions made by the player should take place inside this class. * and to handle actions made by the player should take place inside this class.
...@@ -27,6 +30,8 @@ public class Game { ...@@ -27,6 +30,8 @@ public class Game {
*/ */
protected final Grid grid; protected final Grid grid;
private GamePiece currentPiece;
/** /**
* Create a new game with the specified rows and columns. Creates a corresponding grid model. * Create a new game with the specified rows and columns. Creates a corresponding grid model.
* @param cols number of columns * @param cols number of columns
...@@ -53,6 +58,7 @@ public class Game { ...@@ -53,6 +58,7 @@ public class Game {
*/ */
public void initialiseGame() { public void initialiseGame() {
logger.info("Initialising game"); logger.info("Initialising game");
this.currentPiece = spawnPiece();
} }
/** /**
...@@ -65,14 +71,80 @@ public class Game { ...@@ -65,14 +71,80 @@ public class Game {
int y = gameBlock.getY(); int y = gameBlock.getY();
//Get the new value for this block //Get the new value for this block
int previousValue = grid.get(x,y); logger.info("Block clicked at x = {} y = {}",x,y);
int newValue = previousValue + 1; boolean placed = grid.playPiece(currentPiece, x, y);
if (newValue > GamePiece.PIECES) { if (placed) {
newValue = 0; this.afterPiece();
this.nextPiece();
}
}
public GamePiece spawnPiece() {
Random rand = new Random();
int value = rand.nextInt(15);
logger.info("Spawning piece of value " + (value + 1));
return GamePiece.createPiece(14);
} }
//Update the grid with the new value public void nextPiece() {
grid.set(x,y,newValue); this.currentPiece = spawnPiece();
}
public void afterPiece() {
ArrayList<Integer> fullCols = new ArrayList<>();
ArrayList<Integer> fullRows = new ArrayList<>();
/**
* Check each vertical column to see if it is full
*/
for (int x = 0; x < grid.getCols(); x++) {
boolean full = true;
for (int y = 0; y < grid.getRows(); y++) {
if (grid.get(x,y) == 0) {
full = false;
}
}
if (full) {
fullCols.add(x);
}
}
/**
* Check each horizontal row to see if it is full
*/
for (int y = 0; y < grid.getCols(); y++) {
boolean full = true;
for (int x = 0; x < grid.getRows(); x++) {
if (grid.get(x,y) == 0) {
full = false;
}
}
if (full) {
fullRows.add(y);
}
}
if (fullCols.size() != 0 || fullRows.size() != 0) {
logger.info("Removing rows/columns");
}
/**
* Removes all columns which are full
*/
for (int x : fullCols) {
for (int y = 0; y < grid.getCols(); y++) {
grid.set(x,y,0);
}
}
/**
* Removes all rows which are full
*/
for (int y : fullRows) {
for (int x = 0; x < grid.getCols(); x++) {
grid.set(x,y,0);
}
}
} }
/** /**
......
...@@ -2,6 +2,9 @@ package uk.ac.soton.comp1206.game; ...@@ -2,6 +2,9 @@ package uk.ac.soton.comp1206.game;
import javafx.beans.property.IntegerProperty; import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uk.ac.soton.comp1206.App;
/** /**
* The Grid is a model which holds the state of a game board. It is made up of a set of Integer values arranged in a 2D * The Grid is a model which holds the state of a game board. It is made up of a set of Integer values arranged in a 2D
...@@ -15,7 +18,7 @@ import javafx.beans.property.SimpleIntegerProperty; ...@@ -15,7 +18,7 @@ import javafx.beans.property.SimpleIntegerProperty;
* The Grid should be linked to a GameBoard for it's display. * The Grid should be linked to a GameBoard for it's display.
*/ */
public class Grid { public class Grid {
private static final Logger logger = LogManager.getLogger(App.class);
/** /**
* The number of columns in this grid * The number of columns in this grid
*/ */
...@@ -87,6 +90,42 @@ public class Grid { ...@@ -87,6 +90,42 @@ public class Grid {
} }
} }
public boolean canPlayPiece(GamePiece gamePiece, int placeX, int placeY) {
int[][] blocks = gamePiece.getBlocks();
placeX = placeX - 1;
placeY = placeY - 1;
for (int x = 0; x < blocks.length; x++) {
for (int y = 0; y < blocks[x].length; y++) {
int value = blocks[x][y];
if (value == 0) continue;
if (get(x + placeX, y + placeY) != 0) {
logger.info("Cannot place piece here");
return false;
}
}
}
return true;
}
public boolean playPiece(GamePiece gamePiece, int placeX, int placeY) {
if (!this.canPlayPiece(gamePiece, placeX, placeY)) return false;
logger.info("Placing block at x:{} y:{}",placeX,placeY);
int[][] blocks = gamePiece.getBlocks();
placeX = placeX - 1;
placeY = placeY - 1;
for (int x = 0; x < blocks.length; x++) {
for (int y = 0; y < blocks[x].length; y++) {
int value = blocks[x][y];
if (value == 0) continue;
set(x + placeX, y + placeY, value);
}
}
return true;
}
/** /**
* Get the number of columns in this game * Get the number of columns in this game
* @return number of columns * @return number of columns
......
package uk.ac.soton.comp1206.scene; package uk.ac.soton.comp1206.scene;
import javafx.event.ActionEvent; import javafx.geometry.Pos;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
...@@ -43,17 +44,27 @@ public class MenuScene extends BaseScene { ...@@ -43,17 +44,27 @@ public class MenuScene extends BaseScene {
var mainPane = new BorderPane(); var mainPane = new BorderPane();
menuPane.getChildren().add(mainPane); menuPane.getChildren().add(mainPane);
VBox menuWidgets = new VBox();
//Awful title //Awful title
var title = new Text("TetrECS"); var title = new Text("TetrECS");
title.getStyleClass().add("title"); title.getStyleClass().add("title");
mainPane.setTop(title); menuWidgets.getChildren().add(title);
VBox menuButtons = new VBox();
var singlePlay = new Text("Single Player");
singlePlay.getStyleClass().add("menuItem");
singlePlay.setOnMouseClicked(this::startGame);
menuButtons.getChildren().add(singlePlay);
menuButtons.setAlignment(Pos.CENTER);
menuWidgets.setAlignment(Pos.TOP_CENTER);
//For now, let us just add a button that starts the game. I'm sure you'll do something way better. mainPane.setTop(menuWidgets);
var button = new Button("Play"); mainPane.setCenter(menuButtons);
mainPane.setCenter(button);
//Bind the button action to the startGame method in the menu
button.setOnAction(this::startGame);
} }
/** /**
...@@ -68,7 +79,7 @@ public class MenuScene extends BaseScene { ...@@ -68,7 +79,7 @@ public class MenuScene extends BaseScene {
* Handle when the Start Game button is pressed * Handle when the Start Game button is pressed
* @param event event * @param event event
*/ */
private void startGame(ActionEvent event) { private void startGame(MouseEvent event) {
gameWindow.startChallenge(); gameWindow.startChallenge();
} }
......
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
-fx-effect: dropshadow(gaussian, black, 1, 1.0, 1, 1); -fx-effect: dropshadow(gaussian, black, 1, 1.0, 1, 1);
} }
.heading { .heading {
-fx-fill: white; -fx-fill: white;
-fx-font-family: 'Orbitron'; -fx-font-family: 'Orbitron';
......
No preview for this file type
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
-fx-effect: dropshadow(gaussian, black, 1, 1.0, 1, 1); -fx-effect: dropshadow(gaussian, black, 1, 1.0, 1, 1);
} }
.heading { .heading {
-fx-fill: white; -fx-fill: white;
-fx-font-family: 'Orbitron'; -fx-font-family: 'Orbitron';
......
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment