NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import java.awt.BorderLayout;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JPanel;

public class Game extends JPanel {

private int size;// Size of our Game of Fifteen instance
private int nbTiles;// Number of tiles
private int dimension;// Grid UI Dimension
private static final Color FOREGROUND_COLOR = new Color(35, 55, 132);// Foreground Color
private static final Random RANDOM = new Random();// Random object to shuffle tiles
private int[] tiles; // Storing the tiles in a 1D Array of integers
private int tileSize; // Size of tile on UI
private int blankPos;// Position of the blank tile
private int margin;// Margin for the grid on the frame
private int gridSize;// Grid UI Size
private boolean gameOver; // true if game over, false otherwise

public Game(int size, int dim, int mar) {
this.size = size;
dimension = dim;
margin = mar;

nbTiles =(size * size) - 1; // -1 because we do not count blank tile
tiles = new int[size * size];

// calculate grid size and tile size
gridSize = (dim - 2 * margin);
tileSize = gridSize / size;

setPreferredSize(new Dimension(dimension, dimension + margin));
setBackground(Color.WHITE);
setForeground(FOREGROUND_COLOR);
setFont(new Font("SansSerif", Font.BOLD, 60));

gameOver = true;

addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// used to let users to interact on the grid by clicking
if (gameOver) {
newGame();
} else {
// get position of the click
int ex = e.getX() - margin;
int ey = e.getY() - margin;

// click in the grid ?
if (ex < 0 || ex > gridSize || ey < 0 || ey > gridSize)
return;

// get position in the grid
int c1 = ex / tileSize;
int r1 = ey / tileSize;

// get position of the blank cell
int c2 = blankPos % size;
int r2 = blankPos / size;

// we convert in the 1D coord
int clickPos = r1 * size + c1;

int dir = 0;

// we search direction for multiple tile moves at once
if (c1 == c2 && Math.abs(r1 - r2) > 0)
dir = (r1 - r2) > 0 ? size : -size;
else if (r1 == r2 && Math.abs(c1 - c2) > 0)
dir = (c1 - c2) > 0 ? 1 : -1;

if (dir != 0) {
// we move tiles in the direction
do {
int newBlankPos = blankPos + dir;
tiles[blankPos] = tiles[newBlankPos];
blankPos = newBlankPos;
} while(blankPos != clickPos);

tiles[blankPos] = 0;
}

// we check if game is solved
gameOver = isSolved();
}

// we repaint panel
repaint();
}
});

newGame();
}

private void newGame() {
do {
reset(); // reset in intial state
shuffle(); // shuffle
} while(!isSolvable()); // make it until grid be solvable

gameOver = false;
}

private void reset() {
for (int i = 0; i < tiles.length; i++) {
tiles[i] = (i + 1) % tiles.length;
}

// we set blank cell at the last
blankPos = tiles.length - 1;
}

private void shuffle() {
// don't include the blank tile in the shuffle, leave in the solved position
int n = nbTiles;

while (n > 1) {
int r = RANDOM.nextInt(n--);
int tmp = tiles[r];
tiles[r] = tiles[n];
tiles[n] = tmp;
}
}

// the number of inversions must be even for the puzzle to be solvable
private boolean isSolvable() {
int countInversions = 0;

for (int i = 0; i < nbTiles; i++) {
for (int j = 0; j < i; j++) {
if (tiles[j] > tiles[i])
countInversions++;
}
}

return countInversions % 2 == 0;
}

private boolean isSolved() {
if (tiles[tiles.length - 1] != 0) // if blank tile is not in the solved position ==> not solved
return false;

for (int i = nbTiles - 1; i >= 0; i--) {
if (tiles[i] != i + 1)
return false;
}

return true;
}

private void drawGrid(Graphics2D g) {
for (int i = 0; i < tiles.length; i++) {
//convert 1D coords to 2D coords given the size of the 2D Array
int r = i / size;
int c = i % size;
//convert in coords on the UI
int x = margin + c * tileSize;
int y = margin + r * tileSize;

//check special case for blank tile
if(tiles[i] == 0) {
if (gameOver) {
g.setColor(FOREGROUND_COLOR);
drawCenteredString(g, "u2713", x, y);
}

continue;
}

// for other tiles
g.setColor(getForeground());
g.fillRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.BLACK);
g.drawRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.WHITE);

drawCenteredString(g, String.valueOf(tiles[i]), x , y);
}
}

private void drawStartMessage(Graphics2D g) {
if (gameOver) {
g.setFont(getFont().deriveFont(Font.BOLD, 18));
g.setColor(FOREGROUND_COLOR);
String s = "Click to start new game";
g.drawString(s, (getWidth() - g.getFontMetrics().stringWidth(s)) / 2,
getHeight() - margin);
}
}

private void drawCenteredString(Graphics2D g, String s, int x, int y) {
// center string s for the given tile (x,y)
FontMetrics fm = g.getFontMetrics();
int asc = fm.getAscent();
int desc = fm.getDescent();
g.drawString(s, x + (tileSize - fm.stringWidth(s)) / 2,
y + (asc + (tileSize - (asc + desc)) / 2));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawGrid(g2D);
drawStartMessage(g2D);
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.