Skip to content
Snippets Groups Projects
Commit 192fd853 authored by Nils Breuer's avatar Nils Breuer
Browse files

Add new Version of spiel2048

parent ec74e39e
No related merge requests found
package spiel2048.controller;
import spiel2048.model.Game2048;
import java.util.Arrays;
public class Controller implements IController {
private Game2048 model;
private IView view;
private GameState state;
public Controller(IView view) {
this.view = view;
this.model = new Game2048();
this.state = GameState.TITLE_SCREEN;
}
public void nextFrame() {
switch(state) {
case TITLE_SCREEN -> {
view.drawTitleScreen();
}
case GAME -> {
view.drawGame(model.grid);
if(model.is_game_over(model.grid)) state = GameState.GAME_OVER;
}
case GAME_OVER -> {
view.drawGameOver(model.score);
}
}
}
public void userInput(Direction direction) {
switch(state) {
case TITLE_SCREEN -> {
view.setupGame();
state = GameState.GAME;
return; //userInput bricht ab und es wird noch keine neue Tile erzeugt
}
case GAME -> {
int[] temp_grid = new int[model.grid.length];
//arrayCopy(model.grid, temp_grid);
System.arraycopy(model.grid, 0, temp_grid, 0, model.grid.length);
if(model.game) {
switch(direction) {
case LEFT:
model.score += model.move(model.grid);
break;
case RIGHT:
model.rotate(model.grid,2);
model.score += model.move(model.grid);
model.rotate(model.grid,2);
break;
case UP:
model.rotate(model.grid);
model.score += model.move(model.grid);
model.rotate(model.grid,3);
break;
case DOWN:
model.rotate(model.grid,3);
model.score += model.move(model.grid);
model.rotate(model.grid);
}
}
if(!Arrays.equals(model.grid,temp_grid)) {
model.random_tile(model.grid);
System.out.println("SCORE =" + model.score);
}
if(model.is_game_over(model.grid)) {
model.game = false;
System.out.println("GAME OVER. YOUR SCORE =" + model.score);
}
}
}
}
public void setupGrid() {
model.random_tile(model.grid);
model.random_tile(model.grid);
view.drawGame(model.grid);
}
}
\ No newline at end of file
package spiel2048.controller;
public enum Direction {
LEFT, RIGHT, UP, DOWN
}
package spiel2048.controller;
public enum GameState {
TITLE_SCREEN,
GAME,
GAME_OVER
}
package spiel2048.controller;
public interface IController {
public void nextFrame();
public void userInput(Direction direction);
public void setupGrid();
}
package spiel2048.controller;
public interface IView {
public void drawTitleScreen();
public void setupGame();
public void drawGame(int[] grid);
public void drawGameOver(int score);
}
package spiel2048.model;
import java.util.Arrays;
import java.util.Random;
public class Game2048 {
public int[] grid = new int[16]; //default values are 0
public int score = 0;
public boolean game = true;
public int free_slots(int[] grid) {
int i = 0;
for (int val : grid) {
if (val == 0) i++;
}
return i;
}
/* 0 1 2 3 3 7 11 15 +12 +7 +2 -3 -> -5
4 5 6 7 2 6 10 14 +9 +4 -1 -6 -> -5
8 9 10 11 1 5 9 13 +6 +1 -4 -9 -> -5
12 13 14 15 0 4 8 12 +3 -2 -7 -12 -> -5
| | | |
V V V V
-3 -3 -3 -3
*/
public void rotate(int[] grid) {
int[] temp_grid = new int[grid.length];
for (int i=0; i<grid.length; i++) {
temp_grid[i+12-(i%4)*5-(i/4)*3] = grid[i];
}
//arrayCopy(temp_grid,grid);
System.arraycopy(temp_grid, 0, grid, 0, temp_grid.length);
}
public void rotate(int[] grid, int n) {
for (int i=1; i<=(n%4); i++) { rotate(grid); }
}
public void shift(int[] grid) {
int offset=0;
for (int i=0; i<grid.length; i++) {
if (i%4 == 0) {
offset = 0;
}
if (grid[i] == 0) {
offset++;
} else if (offset > 0) {
grid[i-offset] = grid[i];
grid[i] = 0;
}
}
}
public int move(int[] grid) {
int score = 0;
shift(grid);
score = merge(grid);
shift(grid);
return score;
}
public boolean is_game_over(int[] grid) {
int[] temp_grid = new int[grid.length];
//arrayCopy(grid, temp_grid);
System.arraycopy(grid,0, temp_grid, 0, grid.length);
for (int i=1; i<=4; i++) {
move(temp_grid); rotate(temp_grid);
}
return Arrays.equals(temp_grid,grid);
}
public int merge(int[] grid) {
int score = 0;
for (int i=0; i<grid.length; i++) {
if (i%4 < 3) {
if (grid[i] > 0 && grid[i] == grid[i+1]) {
grid[i] += grid[i+1];
grid[i+1] = 0;
score += grid[i];
}
}
}
return score;
}
public void insert_tile(int[] grid, int n, int val) {
for (int i=0; i<grid.length; i++) {
if (grid[i] == 0) {
if (n == 0) {
grid[i] = val;
break;
}
n--;
}
}
}
public void random_tile(int[] grid) {
Random random = new Random();
int pos, val;
pos = (int)random.nextFloat(0, free_slots(grid)); //pos = int(random(0, free_slots(grid)));
val = random.nextFloat(0, 1) < 0.9 ? 2 : 4; //val = random(0, 1) < 0.9 ? 2 : 4;
insert_tile(grid, pos, val);
}
public String toString() {
String output = "";
for(int i = 0; i < grid.length; i++) {
if(i%4 == 0) output += "\n";
output += " " + grid[i];
}
return output;
}
}
\ No newline at end of file
package spiel2048.view;
import spiel2048.controller.Controller;
import spiel2048.controller.IController;
import spiel2048.controller.IView;
import processing.core.PApplet;
import spiel2048.controller.Direction;
public class ViewDark extends PApplet implements IView {
public static void main(String[] args) {
PApplet.main(ViewDark.class);
}
private IController controller;
private final int X_POS = 0;
private final int Y_POS = 0;
private final int X_OFFSET = 20;
private final int Y_OFFSET = 20;
private final int SIZE_TILE = 80;
private final int SIZE_BORDER = 10;
private final int X_SIZE = 2*X_POS+2*X_OFFSET+SIZE_BORDER+4*(SIZE_TILE+SIZE_BORDER);
private final int Y_SIZE = 2*Y_POS+2*Y_OFFSET+SIZE_BORDER+4*(SIZE_TILE+SIZE_BORDER);
public void settings() {
super.size(X_SIZE, Y_SIZE);
}
public void setup() {
controller = new Controller(this);
}
public void draw() {
controller.nextFrame();
}
public void keyPressed() {
if (key == CODED) {
switch(keyCode) {
case LEFT:
controller.userInput(Direction.LEFT);
break;
case RIGHT:
controller.userInput(Direction.RIGHT);
break;
case UP:
controller.userInput(Direction.UP);
break;
case DOWN:
controller.userInput(Direction.DOWN);
}
}
}
public void drawTitleScreen() {
super.background(color(0, 0, 0));
super.textAlign(CENTER, CENTER);
super.noStroke();
super.textSize(64);
super.text("Game Start", X_SIZE/2, Y_SIZE/2);
super.textSize(16);
super.text("Press any key to start", X_SIZE/2, Y_SIZE/2+45);
super.fill(255);
}
public void setupGame() {
super.textAlign(CENTER, CENTER);
super.textSize(27);
super.noStroke();
super.background(color(0, 0, 0));
super.colorMode(HSB, 360, 100, 100);
controller.setupGrid();
}
public void drawGame(int[] grid) {
//int edge_length = int(sqrt(grid.length));
int edge_length = (int)sqrt(grid.length);
int i = 0;
int X, Y;
for (int y=0; y<edge_length; y++) {
Y = Y_POS+Y_OFFSET+SIZE_BORDER+y*(SIZE_TILE+SIZE_BORDER);
for (int x=0; x<edge_length; x++) {
X = X_POS+X_OFFSET+SIZE_BORDER+x*(SIZE_TILE+SIZE_BORDER);
//fill(color(179, 189, 214));
super.fill(color(30+log(grid[i]+1)/log(2)*10, 100, 100));
super.rect(X, Y, SIZE_TILE, SIZE_TILE, 15);
if (grid[i] != 0) {
super.fill(color(271, 0, 1));
super.text(grid[i], X+SIZE_TILE/2+1, Y+SIZE_TILE/2+1);
}
i++;
}
}
}
public void drawGameOver(int score) {
super.colorMode(RGB, 255, 255, 255);
super.background(color(0, 0, 0));
super.textAlign(CENTER, CENTER);
super.noStroke();
super.textSize(64);
super.text("Gameover", X_SIZE/2, Y_SIZE/2);
super.textSize(16);
super.text("Score: " + score, X_SIZE/2, Y_SIZE/2+45);
super.fill(255);
}
}
package spiel2048.view;
import spiel2048.controller.*;
import processing.core.PApplet;
public class ViewLight extends PApplet implements IView {
public static void main(String[] args) {
PApplet.main(ViewLight.class);
}
private IController controller;
private final int X_POS = 0;
private final int Y_POS = 0;
private final int X_OFFSET = 20;
private final int Y_OFFSET = 20;
private final int SIZE_TILE = 80;
private final int SIZE_BORDER = 10;
private final int X_SIZE = 2*X_POS+2*X_OFFSET+SIZE_BORDER+4*(SIZE_TILE+SIZE_BORDER);
private final int Y_SIZE = 2*Y_POS+2*Y_OFFSET+SIZE_BORDER+4*(SIZE_TILE+SIZE_BORDER);
public void settings() {
super.size(X_SIZE, Y_SIZE);
}
public void setup() {
controller = new Controller(this);
}
public void draw() {
controller.nextFrame();
}
public void keyPressed() {
if (key == CODED) {
switch(keyCode) {
case LEFT:
controller.userInput(Direction.LEFT);
break;
case RIGHT:
controller.userInput(Direction.RIGHT);
break;
case UP:
controller.userInput(Direction.UP);
break;
case DOWN:
controller.userInput(Direction.DOWN);
}
}
}
public void drawTitleScreen() {
super.background(color(255, 255, 255));
super.textAlign(CENTER, CENTER);
super.noStroke();
super.textSize(64);
super.text("Game Start", X_SIZE/2, Y_SIZE/2);
super.textSize(16);
super.text("Press any key to start", X_SIZE/2, Y_SIZE/2+45);
super.fill(0);
}
public void setupGame() {
super.textAlign(CENTER, CENTER);
super.textSize(27);
super.noStroke();
super.background(color(255, 255, 255));
super.colorMode(HSB, 360, 100, 100);
controller.setupGrid();
}
public void drawGame(int[] grid) {
//int edge_length = int(sqrt(grid.length));
int edge_length = (int)sqrt(grid.length);
int i = 0;
int X, Y;
for (int y=0; y<edge_length; y++) {
Y = Y_POS+Y_OFFSET+SIZE_BORDER+y*(SIZE_TILE+SIZE_BORDER);
for (int x=0; x<edge_length; x++) {
X = X_POS+X_OFFSET+SIZE_BORDER+x*(SIZE_TILE+SIZE_BORDER);
//fill(color(179, 189, 214));
super.fill(color(30+log(grid[i]+1)/log(2)*10, 100, 100));
super.rect(X, Y, SIZE_TILE, SIZE_TILE, 15);
if (grid[i] != 0) {
super.fill(color(271, 0, 1));
super.text(grid[i], X+SIZE_TILE/2+1, Y+SIZE_TILE/2+1);
}
i++;
}
}
}
public void drawGameOver(int score) {
super.colorMode(RGB, 255, 255, 255);
super.background(color(255, 255, 255));
super.textAlign(CENTER, CENTER);
super.noStroke();
super.textSize(64);
super.text("Gameover", X_SIZE/2, Y_SIZE/2);
super.textSize(16);
super.text("Score: " + score, X_SIZE/2, Y_SIZE/2+45);
super.fill(0);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment