Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- public class Game extends JPanel {
- static JFrame frame;
- Field field;
- boolean inGame = true;
- boolean theme = false;
- public Game() {
- setPreferredSize(new Dimension(342, 364));
- setFocusable(true);
- addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
- init();
- } else {
- if (inGame) {
- if (e.getKeyCode() == KeyEvent.VK_LEFT) {
- field.shuffle(1);
- check();
- } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- field.shuffle(2);
- check();
- } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
- field.shuffle(3);
- check();
- } else if (e.getKeyCode() == KeyEvent.VK_UP) {
- field.shuffle(0);
- check();
- }
- }
- inGame = field.check();
- if (inGame && !field.isFull()) {
- field.add();
- }
- }
- }
- });
- init();
- }
- void check() {
- inGame = field.check();
- if (inGame && !field.isFull())
- field.add();
- repaint();
- }
- void init() {
- field = new Field();
- inGame = true;
- repaint();
- }
- int getTileColor(int i) {
- switch (i) {
- case 2:
- return theme ? 0xad08aa : 0xeee4da;
- case 4:
- return theme ? 0xcc041e : 0xede0c8;
- case 8:
- return theme ? 0xf7de00 : 0xf2b179;
- case 16:
- return theme ? 0x00ad05 : 0xf59563;
- case 32:
- return theme ? 0x00967d : 0xf67c5f;
- case 64:
- return theme ? 0x910065 : 0xf65e3b;
- case 128:
- return theme ? 0xff7b0f : 0xedcf72;
- case 256:
- return theme ? 0x00ce71 : 0xedcc61;
- case 512:
- return theme ? 0x00005b : 0xedc850;
- case 1024:
- return theme ? 0xff2600 : 0xedc53f;
- case 2048:
- return theme ? 0x050030 : 0xedc22e;
- }
- return theme ? 0x510f66 : 0xcdc1b4;
- }
- int getTextColor(int i) {
- return theme ? 0xffffff : (i < 16 ? 0x776e65 : 0xf9f6f2);
- }
- int getTextSize(int i) {
- if (i < 100)
- return 36;
- if (i < 1000)
- return 32;
- if (i < 10000)
- return 28;
- return 22;
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(new Color(theme ? 0x262626 : 0xbbada0));
- g.fillRect(0, 0, this.getSize().width, this.getSize().height);
- frame.setTitle("Puzzle 2048. Score: " + field.getScore());
- for (int x = 0; x < 4; x++) {
- int i = x * 80 + 16;
- for (int y = 0; y < 4; y++) {
- int j = y * 80 + 16;
- draw((Graphics2D) g, field.get(x, y), i, j);
- }
- }
- if (!inGame)
- fin((Graphics2D) g);
- }
- void fin(Graphics2D g) {
- frame.setTitle("Puzzle 2048");
- g.setColor(new Color(255, 255, 255, 30));
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(new Color(0x000000));
- Font font = new Font("Cambria", Font.BOLD, 48);
- g.setFont(font);
- g.drawString("Game over!", 43, 104);
- g.drawString("Your score:", 44, 184);
- String s = String.valueOf(field.getScore());
- FontMetrics fm = getFontMetrics(font);
- int w = fm.stringWidth(s);
- g.drawString(s, (this.getSize().width - w) / 2, 264);
- }
- void draw(Graphics2D g, int val, int x, int y) {
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
- g.setColor(new Color(getTileColor(val)));
- if (theme) {
- g.fillRect(x, y, 64, 64);
- } else {
- g.fillRoundRect(x, y, 64, 64, 14, 14);
- }
- if (val > 0) {
- g.setColor(new Color(getTextColor(val)));
- Font font = new Font("Cambria", Font.BOLD, getTextSize(val));
- String s = String.valueOf(val);
- g.setFont(font);
- FontMetrics fm = getFontMetrics(font);
- int w = fm.stringWidth(s);
- int h = -(int) fm.getLineMetrics(s, g).getBaselineOffsets()[2];
- g.drawString(s, x + (64 - w) / 2, y + 64 - (64 - h) / 2 - 2);
- }
- }
- static class Field {
- Random random = new Random();
- int[][] field;
- int score = 0;
- public Field() {
- field = new int[4][4];
- add();
- add();
- }
- public int getScore() {
- return score;
- }
- public int get(int x, int y) {
- return field[x][y];
- }
- // Upward orientation
- public void shuffle(int times) {
- if (times != 0)
- rotate(times);
- for (int i = 0; i < 4; i++) {
- int[] in1 = new int[4];
- for (int j = 0; j < 4; j++)
- in1[j] = 0;
- int[] in2 = field[i];
- int j = 0;
- for (int k = 0; k < 4; k++) {
- if (in2[k] != 0) {
- in1[j] = in2[k];
- j++;
- }
- }
- if (in1[0] != 0) {
- for (int k = 0; k < 3; k++) {
- if (in1[k] == in1[k + 1]) {
- in1[k] *= 2;
- score += in1[k];
- in1[k + 1] = 0;
- }
- }
- j = 0;
- for (int k = 0; k < 4; k++) {
- if (in1[k] != 0) {
- in2[j] = in1[k];
- j++;
- }
- }
- while (j < 4) {
- in2[j] = 0;
- j++;
- }
- field[i] = in2;
- }
- }
- if (times != 0)
- rotate(4 - times);
- }
- // Clockwise
- private void rotate(int times) {
- for (int i = 0; i < times; i++) {
- int temp;
- temp = field[1][1];
- field[1][1] = field[1][2];
- field[1][2] = field[2][2];
- field[2][2] = field[2][1];
- field[2][1] = temp;
- temp = field[1][0];
- field[1][0] = field[0][2];
- field[0][2] = field[2][3];
- field[2][3] = field[3][1];
- field[3][1] = temp;
- temp = field[2][0];
- field[2][0] = field[0][1];
- field[0][1] = field[1][3];
- field[1][3] = field[3][2];
- field[3][2] = temp;
- temp = field[0][0];
- field[0][0] = field[0][3];
- field[0][3] = field[3][3];
- field[3][3] = field[3][0];
- field[3][0] = temp;
- }
- }
- public void add() {
- if (!isFull())
- while (true) {
- int x = random.nextInt(4);
- int y = random.nextInt(4);
- if (field[x][y] == 0) {
- field[x][y] = random.nextInt(100) > 24 ? 2 : 4;
- return;
- }
- }
- }
- public boolean isFull() {
- for (int x = 0; x < 4; x++)
- for (int y = 0; y < 4; y++)
- if (field[x][y] == 0)
- return false;
- return true;
- }
- public boolean check() {
- if (!isFull())
- return true;
- for (int x = 0; x < 4; x++)
- for (int y = 0; y < 4; y++)
- if ((x < 3 && field[x][y] == field[x + 1][y]) | (y < 3 && field[x][y] == field[x][y + 1]))
- return true;
- return false;
- }
- }
- public static void main(String[] args) {
- frame = new JFrame();
- frame.setTitle("Puzzle 2048");
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setSize(342, 364);
- frame.setResizable(false);
- frame.add(new Game());
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment