From bd7a0d19abb8fd158a37deb49250d8a2b553ee04 Mon Sep 17 00:00:00 2001 From: Philipp Dormeier Date: Sat, 3 Mar 2018 14:50:42 +0100 Subject: [PATCH] Version 1.1 Last bug of the base game fixed --- 2048/src/de/dormeier/philipp/Board.java | 44 +++++++++++++++----- 2048/src/de/dormeier/philipp/GameWindow.java | 11 ++--- 2048/src/de/dormeier/philipp/NumberTile.java | 16 +++++-- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/2048/src/de/dormeier/philipp/Board.java b/2048/src/de/dormeier/philipp/Board.java index 2c6c1eb..dbae2bf 100644 --- a/2048/src/de/dormeier/philipp/Board.java +++ b/2048/src/de/dormeier/philipp/Board.java @@ -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 * */ @@ -72,15 +73,15 @@ 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); @@ -88,7 +89,6 @@ private void initializeUI() { 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); @@ -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]; @@ -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 */ @@ -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 */ @@ -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); @@ -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); @@ -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; @@ -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) @@ -407,5 +431,5 @@ public void keyPressed(KeyEvent k) { } } } - } + } } diff --git a/2048/src/de/dormeier/philipp/GameWindow.java b/2048/src/de/dormeier/philipp/GameWindow.java index 4ea8ce8..c9d7ce6 100644 --- a/2048/src/de/dormeier/philipp/GameWindow.java +++ b/2048/src/de/dormeier/philipp/GameWindow.java @@ -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) { @@ -25,5 +27,4 @@ public void run() { } }); } - } diff --git a/2048/src/de/dormeier/philipp/NumberTile.java b/2048/src/de/dormeier/philipp/NumberTile.java index 019653a..9e0da9b 100644 --- a/2048/src/de/dormeier/philipp/NumberTile.java +++ b/2048/src/de/dormeier/philipp/NumberTile.java @@ -1,6 +1,7 @@ package de.dormeier.philipp; import java.awt.Color; +import java.awt.Font; import javax.swing.JButton; @@ -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); } @@ -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)]); } }