Skip to content

Commit

Permalink
Version 1.1 Last bug of the base game fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
DerReparator committed Mar 3, 2018
1 parent 6e9afad commit bd7a0d1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
44 changes: 34 additions & 10 deletions 2048/src/de/dormeier/philipp/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/**
* @version 1.1 There may still be a bug when right-shifting which causes not all tiles that should merge to actually merge.
* @version 1.1.1 Fixed the last bug in the base game.
* @author Philipp
*
*/
Expand Down Expand Up @@ -72,23 +73,22 @@ private void startNewGame() {
private void initializeUI() {
this.removeAll();
gameboard = new JPanel();
gameboard.setPreferredSize(new Dimension(GameWindow.BLOCK_SIZE*4, GameWindow.BLOCK_SIZE*4));
gameboard.setPreferredSize(new Dimension(GameWindow.BLOCK_SIZE*board_size, GameWindow.BLOCK_SIZE*board_size));
gameboard.setLayout(new GridLayout(board_size,board_size,NumberTile.borderSize, NumberTile.borderSize));
gameboard.setBackground(Color.DARK_GRAY);
gameboard.setVisible(true);
gameboard.setEnabled(true);

the_score = new JLabel();
the_score.setForeground(Color.white);
the_score.setPreferredSize(new Dimension(GameWindow.BLOCK_SIZE*board_size, GameWindow.BLOCK_SIZE/4));
the_score.setForeground(Color.WHITE);
the_score.setVisible(true);

f_game_over = new Font("Helvetica", Font.BOLD, GameWindow.BLOCK_SIZE/3);

l_game_over = new JLabel(s_game_over);
l_game_over.setFont(f_game_over);
l_game_over.setBackground(this.getBackground());
l_game_over.setForeground(Color.WHITE);
l_game_over.setVisible(true);
l_game_over.setEnabled(false);

Expand Down Expand Up @@ -117,8 +117,8 @@ private void initializeNumbers() {
* Update the game's logic.
*/
private void updateBoard(int dx, int dy) {
int startX = 3; /* rightwards */
int startY = 3; /* downwards */
int startX = board_size-1; /* rightwards */
int startY = board_size-1; /* downwards */

boolean[] merged = new boolean[board_size];

Expand Down Expand Up @@ -165,7 +165,7 @@ else if (dy == 1) {
for(int b = 0; b < merged.length; b++) {
merged[b] = false;
}
startY = 2;
startY = board_size-2;
while(startY >= 0) /* for every tile except the first */
{
if(board[startY][x] > 1) /* if tile not empty */
Expand Down Expand Up @@ -198,7 +198,10 @@ else if (board[i][x] == board[i-dy][x]) /* suitable tile found -> MERGE */
else if(dx == 1) {
for(int y = 0; y < board_size; y++) /* for every row */
{
startX = 2;
for(int b = 0; b < merged.length; b++) {
merged[b] = false;
}
startX = board_size-2;
while(startX >= 0) /* for every tile except the first */
{
if(board[y][startX] > 1) /* if tile not empty */
Expand Down Expand Up @@ -326,9 +329,9 @@ private void doMove(int dX, int dY) {
}

/**
* Reset the board one step.
* Sets the board back one step.
*/
private void resetBoard() {
private void setbackBoard() {
board = Arrays.stream(prev_board).map(int[]::clone).toArray(int[][]::new);
l_game_over.setVisible(false);
l_game_over.setEnabled(false);
Expand All @@ -346,6 +349,21 @@ private void gameOver() {
GAME_RUNNING = false;
this.removeAll();
add(l_game_over, BorderLayout.CENTER);
l_game_over.setForeground(Color.WHITE);
l_game_over.setText(html1 + (this.getWidth()-GameWindow.BLOCK_SIZE) + html2 + String.format(l_game_over.getText(), score));
l_game_over.setEnabled(true);
l_game_over.setVisible(true);
}

/**
* Gets called when the current game is won. Constructs and displays
* the 'Game Won'-screen.
*/
private void gameWon() {
GAME_RUNNING = false;
this.removeAll();
add(l_game_over, BorderLayout.CENTER);
l_game_over.setForeground(Color.GREEN);
l_game_over.setText(html1 + (this.getWidth()-GameWindow.BLOCK_SIZE) + html2 + String.format(l_game_over.getText(), score));
l_game_over.setEnabled(true);
l_game_over.setVisible(true);
Expand All @@ -361,6 +379,9 @@ private boolean isGameOver() {
} else {
for(int y = 0; y < board_size; y++) {
for(int x = 0; x < board_size; x++) {
/* GAME WON */
if(2048 == board[y][x])
gameWon();
/* check above */
if(y-1 >= 0 && board[y-1][x] == board[y][x])
return false;
Expand Down Expand Up @@ -399,6 +420,9 @@ public void keyPressed(KeyEvent k) {
case KeyEvent.VK_LEFT:
doMove(-1, 0);
break;
default:
keyGotPressed = false;
break;
}
} else {
if(key == KeyEvent.VK_SPACE)
Expand All @@ -407,5 +431,5 @@ public void keyPressed(KeyEvent k) {
}
}
}
}
}
}
11 changes: 6 additions & 5 deletions 2048/src/de/dormeier/philipp/GameWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

public class GameWindow extends JFrame {

public static final int BLOCK_SIZE = 60;
public static final int BLOCK_SIZE = 96;
private Board b;

public GameWindow() {
setTitle("Fusion 2048");
setResizable(false);
setLocationRelativeTo(null);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(new Board(),BorderLayout.CENTER);
b = new Board();
this.add(b,BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}

public static void main(String[] args) {
Expand All @@ -25,5 +27,4 @@ public void run() {
}
});
}

}
16 changes: 12 additions & 4 deletions 2048/src/de/dormeier/philipp/NumberTile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.dormeier.philipp;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JButton;

Expand Down Expand Up @@ -29,8 +30,8 @@ public class NumberTile extends JButton {
public NumberTile() {
setSize(GameWindow.BLOCK_SIZE-2*borderSize, GameWindow.BLOCK_SIZE-2*borderSize);
setBackground(tileBg[0]);
setForeground(Color.BLACK);
setEnabled(false);
setVisible(true);
setText("");
setVisible(true);
}
Expand All @@ -43,10 +44,17 @@ public void updateTile(int value) {
if(value > 1 && value % 2 != 0) {
throw new IllegalArgumentException("Der Tile-Wert wurde nicht richtig berechnet");
} else {
if(value > 1)
if(value <= 1) {
setText("");
} else {
setText(""+value);
else
setText("");
if(value > 1000)
setFont(new Font("Helvetica", Font.PLAIN, GameWindow.BLOCK_SIZE/4));
else if(value > 100)
setFont(new Font("Helvetica", Font.PLAIN, GameWindow.BLOCK_SIZE/3));
else
setFont(new Font("Helvetica", Font.PLAIN, GameWindow.BLOCK_SIZE/2));
}
setBackground(tileBg[log2(value)]);
}
}
Expand Down

0 comments on commit bd7a0d1

Please sign in to comment.