ಜಾವಾದಲ್ಲಿ "ಟ್ಯಾಗ್" - ಪೂರ್ಣ ಪ್ರಮಾಣದ ಆಟವನ್ನು ಹೇಗೆ ಅಭಿವೃದ್ಧಿಪಡಿಸುವುದು
"ಹದಿನೈದು" ಅಥವಾ "ಹದಿನೈದು" ಪ್ರಪಂಚದಾದ್ಯಂತ ಜನಪ್ರಿಯವಾಗಿರುವ ಸರಳ ಲಾಜಿಕ್ ಆಟದ ಅತ್ಯುತ್ತಮ ಉದಾಹರಣೆಯಾಗಿದೆ. ಒಗಟು ಪರಿಹರಿಸಲು, ನೀವು ಚಿಕ್ಕದರಿಂದ ದೊಡ್ಡದಕ್ಕೆ ಕ್ರಮವಾಗಿ ಸಂಖ್ಯೆಗಳೊಂದಿಗೆ ಚೌಕಗಳನ್ನು ಜೋಡಿಸಬೇಕು. ಇದು ಸುಲಭವಲ್ಲ, ಆದರೆ ಇದು ಆಸಕ್ತಿದಾಯಕವಾಗಿದೆ.
ಇಂದಿನ ಟ್ಯುಟೋರಿಯಲ್ ನಲ್ಲಿ ಎಕ್ಲಿಪ್ಸ್ನೊಂದಿಗೆ ಜಾವಾ 8 ರಲ್ಲಿ ಹದಿನೈದು ಅಭಿವೃದ್ಧಿಪಡಿಸುವುದು ಹೇಗೆ ಎಂದು ನಾವು ನಿಮಗೆ ತೋರಿಸುತ್ತೇವೆ. UI ಅನ್ನು ಅಭಿವೃದ್ಧಿಪಡಿಸಲು ನಾವು ಸ್ವಿಂಗ್ API ಅನ್ನು ಬಳಸುತ್ತೇವೆ.
ನಾವು ನೆನಪಿಸುತ್ತೇವೆ:ಎಲ್ಲಾ Habr ಓದುಗರಿಗೆ - Habr ಪ್ರೊಮೊ ಕೋಡ್ ಬಳಸಿಕೊಂಡು ಯಾವುದೇ ಸ್ಕಿಲ್ಬಾಕ್ಸ್ ಕೋರ್ಸ್ಗೆ ದಾಖಲಾಗುವಾಗ 10 ರೂಬಲ್ ರಿಯಾಯಿತಿ.
ಟೈಲ್ಸ್ ಒಂದು ಟ್ಯಾಗ್ ಆಗಿದ್ದು ಅದು ಪೂರ್ಣಾಂಕಗಳ ಒಂದು ಆಯಾಮದ ಶ್ರೇಣಿಯಾಗಿದೆ. ಪ್ರತಿಯೊಂದು ಟ್ಯಾಗ್ಗಳು [0, nbTiles] ಶ್ರೇಣಿಯಲ್ಲಿ ಅನನ್ಯ ಮೌಲ್ಯವನ್ನು ಪಡೆಯುತ್ತವೆ. ಶೂನ್ಯವು ಖಾಲಿ ಚೌಕವನ್ನು ಸೂಚಿಸುತ್ತದೆ;
blankPos - ಖಾಲಿ ಚೌಕದ ಸ್ಥಾನ.
ಆಟದ ತರ್ಕ
ಹೊಸ ಆಟದ ಸ್ಥಾನವನ್ನು ಪ್ರಾರಂಭಿಸಲು ಬಳಸಲಾಗುವ ಮರುಹೊಂದಿಸುವ ವಿಧಾನವನ್ನು ನಾವು ವ್ಯಾಖ್ಯಾನಿಸಬೇಕಾಗಿದೆ. ಈ ರೀತಿಯಾಗಿ ನಾವು ಟ್ಯಾಗ್ಗಳ ರಚನೆಯ ಪ್ರತಿಯೊಂದು ಅಂಶಕ್ಕೆ ಮೌಲ್ಯವನ್ನು ಹೊಂದಿಸುತ್ತೇವೆ. ಸರಿ, ನಂತರ ನಾವು blankPos ಅನ್ನು ರಚನೆಯ ಕೊನೆಯ ಸ್ಥಾನದಲ್ಲಿ ಇರಿಸುತ್ತೇವೆ.
ಟ್ಯಾಗ್ಗಳ ಶ್ರೇಣಿಯನ್ನು ಷಫಲ್ ಮಾಡಲು ನಮಗೆ ಷಫಲ್ ವಿಧಾನದ ಅಗತ್ಯವಿದೆ. ಖಾಲಿ ಟ್ಯಾಗ್ ಅನ್ನು ಅದೇ ಸ್ಥಾನದಲ್ಲಿ ಬಿಡಲು ನಾವು ಶಫಲಿಂಗ್ ಪ್ರಕ್ರಿಯೆಯಲ್ಲಿ ಸೇರಿಸುವುದಿಲ್ಲ.
ಪಝಲ್ನ ಸಂಭವನೀಯ ಆರಂಭಿಕ ಸ್ಥಾನಗಳಲ್ಲಿ ಅರ್ಧದಷ್ಟು ಮಾತ್ರ ಪರಿಹಾರವನ್ನು ಹೊಂದಿರುವುದರಿಂದ, ಪ್ರಸ್ತುತ ಲೇಔಟ್ ಸಹ ಪರಿಹರಿಸಬಲ್ಲದು ಎಂದು ಖಚಿತಪಡಿಸಿಕೊಳ್ಳಲು ನೀವು ಪರಿಣಾಮವಾಗಿ ಷಫಲ್ ಫಲಿತಾಂಶವನ್ನು ಪರಿಶೀಲಿಸಬೇಕು. ಇದನ್ನು ಮಾಡಲು, ನಾವು isSolvable ವಿಧಾನವನ್ನು ವ್ಯಾಖ್ಯಾನಿಸುತ್ತೇವೆ.
ನಿರ್ದಿಷ್ಟ ಟ್ಯಾಗ್ನ ಮುಂದೆ ಹೆಚ್ಚಿನ ಮೌಲ್ಯದೊಂದಿಗೆ ಟ್ಯಾಗ್ ಇದ್ದರೆ, ಅದನ್ನು ವಿಲೋಮ ಎಂದು ಪರಿಗಣಿಸಲಾಗುತ್ತದೆ. ಖಾಲಿ ಸ್ಥಳವು ಸ್ಥಳದಲ್ಲಿದ್ದಾಗ, ಒಗಟು ಪರಿಹರಿಸಲು ವಿಲೋಮಗಳ ಸಂಖ್ಯೆಯು ಸಮವಾಗಿರಬೇಕು. ಆದ್ದರಿಂದ ನಾವು ವಿಲೋಮಗಳ ಸಂಖ್ಯೆಯನ್ನು ಎಣಿಸುತ್ತೇವೆ ಮತ್ತು ಸಂಖ್ಯೆಯು ಸಮವಾಗಿದ್ದರೆ ನಿಜವನ್ನು ಹಿಂತಿರುಗಿಸುತ್ತೇವೆ.
ನಮ್ಮ ಹದಿನೈದು ವಿನ್ಯಾಸದ ಆಟವು ಪರಿಹಾರವಾಗಿದೆಯೇ ಎಂದು ಪರಿಶೀಲಿಸಲು isSolved ವಿಧಾನವನ್ನು ವ್ಯಾಖ್ಯಾನಿಸುವುದು ಮುಖ್ಯವಾಗಿದೆ. ಮೊದಲು ನಾವು ಖಾಲಿ ಸ್ಥಳ ಎಲ್ಲಿದೆ ಎಂದು ನೋಡುತ್ತೇವೆ. ಆರಂಭಿಕ ಸ್ಥಾನದಲ್ಲಿದ್ದರೆ, ಪ್ರಸ್ತುತ ಜೋಡಣೆ ಹೊಸದಾಗಿದೆ, ಹಿಂದೆ ನಿರ್ಧರಿಸಲಾಗಿಲ್ಲ. ನಂತರ ನಾವು ಹಿಮ್ಮುಖ ಕ್ರಮದಲ್ಲಿ ಅಂಚುಗಳ ಮೂಲಕ ಪುನರಾವರ್ತಿಸುತ್ತೇವೆ ಮತ್ತು ಟ್ಯಾಗ್ನ ಮೌಲ್ಯವು ಅನುಗುಣವಾದ ಸೂಚ್ಯಂಕ +1 ಗಿಂತ ಭಿನ್ನವಾಗಿದ್ದರೆ, ನಾವು ತಪ್ಪನ್ನು ಹಿಂತಿರುಗಿಸುತ್ತೇವೆ. ಇಲ್ಲದಿದ್ದರೆ, ವಿಧಾನದ ಕೊನೆಯಲ್ಲಿ ಅದು ನಿಜವಾಗಿ ಮರಳಲು ಸಮಯವಾಗಿದೆ ಏಕೆಂದರೆ ಒಗಟು ಈಗಾಗಲೇ ಪರಿಹರಿಸಲಾಗಿದೆ.
ವ್ಯಾಖ್ಯಾನಿಸಬೇಕಾದ ಇನ್ನೊಂದು ವಿಧಾನವೆಂದರೆ ಹೊಸ ಆಟ. ಆಟದ ಹೊಸ ನಿದರ್ಶನವನ್ನು ರಚಿಸಲು ಇದು ಅಗತ್ಯವಿದೆ. ಇದನ್ನು ಮಾಡಲು, ನಾವು ಆಟದ ಮೈದಾನವನ್ನು ಮರುಹೊಂದಿಸಿ, ನಂತರ ಅದನ್ನು ಷಫಲ್ ಮಾಡಿ ಮತ್ತು ಆಟದ ಸ್ಥಾನವನ್ನು ಪರಿಹರಿಸುವವರೆಗೆ ಮುಂದುವರಿಸಿ.
ಟ್ಯಾಗ್ನ ಪ್ರಮುಖ ತರ್ಕದೊಂದಿಗೆ ಉದಾಹರಣೆ ಕೋಡ್ ಇಲ್ಲಿದೆ:
private void newGame() {
do {
reset(); // reset in initial 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;
}
}
// Only half permutations of the puzzle are solvable/
// Whenever a tile is preceded by a tile with higher value it counts
// as an inversion. In our case, with the blank tile in the solved position,
// 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;
}
ಅಂತಿಮವಾಗಿ, ನೀವು ರಚನೆಯಲ್ಲಿನ ಟ್ಯಾಗ್ಗಳ ಚಲನೆಯನ್ನು ಪ್ರೋಗ್ರಾಂ ಮಾಡಬೇಕಾಗುತ್ತದೆ. ಕರ್ಸರ್ ಚಲನೆಗೆ ಪ್ರತಿಕ್ರಿಯಿಸಲು ಕಾಲ್ಬ್ಯಾಕ್ ಮೂಲಕ ಈ ಕೋಡ್ ಅನ್ನು ನಂತರ ಕರೆಯಲಾಗುವುದು. ನಮ್ಮ ಆಟವು ಒಂದೇ ಸಮಯದಲ್ಲಿ ಅನೇಕ ಟೈಲ್ ಚಲನೆಗಳನ್ನು ಬೆಂಬಲಿಸುತ್ತದೆ. ಹೀಗಾಗಿ, ನಾವು ಪರದೆಯ ಮೇಲೆ ಒತ್ತಿದ ಸ್ಥಾನವನ್ನು ಟ್ಯಾಗ್ ಆಗಿ ಪರಿವರ್ತಿಸಿದ ನಂತರ, ನಾವು ಖಾಲಿ ಟ್ಯಾಗ್ನ ಸ್ಥಾನವನ್ನು ಪಡೆಯುತ್ತೇವೆ ಮತ್ತು ಅದೇ ಸಮಯದಲ್ಲಿ ಅದರ ಹಲವಾರು ಚಲನೆಗಳನ್ನು ಬೆಂಬಲಿಸಲು ಚಲನೆಯ ದಿಕ್ಕನ್ನು ಹುಡುಕುತ್ತೇವೆ.
ಉದಾಹರಣೆ ಕೋಡ್ ಇಲ್ಲಿದೆ:
// 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;
ನಾವು ಸ್ವಿಂಗ್ API ಬಳಸಿಕೊಂಡು UI ಅನ್ನು ಅಭಿವೃದ್ಧಿಪಡಿಸುತ್ತೇವೆ
ಇಂಟರ್ಫೇಸ್ನಲ್ಲಿ ಕೆಲಸ ಮಾಡುವ ಸಮಯ ಇದು. ಮೊದಲು ನಾವು Jpanel ವರ್ಗವನ್ನು ತೆಗೆದುಕೊಳ್ಳುತ್ತೇವೆ. ನಂತರ ನಾವು ಮೈದಾನದಲ್ಲಿ ಟ್ಯಾಗ್ಗಳನ್ನು ಸೆಳೆಯುತ್ತೇವೆ - ಪ್ರತಿಯೊಂದರ ಗಾತ್ರವನ್ನು ಲೆಕ್ಕಾಚಾರ ಮಾಡಲು, ನಾವು ಆಟದ ಕನ್ಸ್ಟ್ರಕ್ಟರ್ ಪ್ಯಾರಾಮೀಟರ್ನಲ್ಲಿ ನಿರ್ದಿಷ್ಟಪಡಿಸಿದ ಡೇಟಾವನ್ನು ಬಳಸುತ್ತೇವೆ:
ಮಾರ್ಜಿನ್ ಸಹ ಆಟದ ಕನ್ಸ್ಟ್ರಕ್ಟರ್ನಲ್ಲಿ ಹೊಂದಿಸಲಾದ ಪ್ಯಾರಾಮೀಟರ್ ಆಗಿದೆ.
ಈಗ ನಾವು ಪರದೆಯ ಮೇಲೆ ಗ್ರಿಡ್ ಮತ್ತು ತಾಣಗಳನ್ನು ಸೆಳೆಯಲು ಡ್ರಾಗ್ರಿಡ್ ವಿಧಾನವನ್ನು ವ್ಯಾಖ್ಯಾನಿಸಬೇಕಾಗಿದೆ. ನಾವು ಟ್ಯಾಗ್ಗಳ ಶ್ರೇಣಿಯನ್ನು ವಿಶ್ಲೇಷಿಸುತ್ತೇವೆ ಮತ್ತು ನಿರ್ದೇಶಾಂಕಗಳನ್ನು ಬಳಕೆದಾರ ಇಂಟರ್ಫೇಸ್ ನಿರ್ದೇಶಾಂಕಗಳಾಗಿ ಪರಿವರ್ತಿಸುತ್ತೇವೆ. ನಂತರ ಪ್ರತಿ ಸ್ಥಳವನ್ನು ಕೇಂದ್ರದಲ್ಲಿ ಅನುಗುಣವಾದ ಸಂಖ್ಯೆಯೊಂದಿಗೆ ಸೆಳೆಯಿರಿ:
private void drawGrid(Graphics2D g) {
for (int i = 0; i < tiles.length; i++) {
// we convert 1D coords to 2D coords given the size of the 2D Array
int r = i / size;
int c = i % size;
// we 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);
}
}
ಅಂತಿಮವಾಗಿ, JPane ವರ್ಗದಿಂದ ಪಡೆದ ಪೇಂಟ್ ಕಾಂಪೊನೆಂಟ್ ವಿಧಾನವನ್ನು ಅತಿಕ್ರಮಿಸೋಣ. ನಂತರ ನಾವು ಡ್ರಾಗ್ರಿಡ್ ವಿಧಾನವನ್ನು ಬಳಸುತ್ತೇವೆ, ನಂತರ ಡ್ರಾಸ್ಟಾರ್ಟ್ ಮೆಸೇಜ್ ವಿಧಾನವನ್ನು ಅನುಸರಿಸಿ ಸಂದೇಶವನ್ನು ಪ್ರದರ್ಶಿಸಲು ಆಟವನ್ನು ಪ್ರಾರಂಭಿಸಲು ಕ್ಲಿಕ್ ಮಾಡಲು ನಮ್ಮನ್ನು ಪ್ರೇರೇಪಿಸುತ್ತದೆ:
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);
}
UI ನಲ್ಲಿ ಬಳಕೆದಾರರ ಕ್ರಿಯೆಗಳಿಗೆ ಪ್ರತಿಕ್ರಿಯಿಸುವುದು
ಆಟವು ತನ್ನ ಕೋರ್ಸ್ ಅನ್ನು ಚಲಾಯಿಸಲು, UI ನಲ್ಲಿ ಬಳಕೆದಾರರ ಕ್ರಿಯೆಗಳನ್ನು ಪ್ರಕ್ರಿಯೆಗೊಳಿಸುವುದು ಅವಶ್ಯಕ. ಇದನ್ನು ಮಾಡಲು, Jpanel ನಲ್ಲಿ MouseListener ನ ಅನುಷ್ಠಾನ ಮತ್ತು ಸ್ಥಳಗಳನ್ನು ಚಲಿಸುವ ಕೋಡ್ ಅನ್ನು ಸೇರಿಸಿ, ಈಗಾಗಲೇ ಮೇಲೆ ತೋರಿಸಲಾಗಿದೆ:
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// used to let users to interact on the grid by clicking
// it's time to implement interaction with users to move tiles to solve the game !
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();
}
});
GameOfFifteen ವರ್ಗದ ಕನ್ಸ್ಟ್ರಕ್ಟರ್ನಲ್ಲಿ ನಾವು ಕೋಡ್ ಅನ್ನು ಇರಿಸುತ್ತೇವೆ. ಕೊನೆಯಲ್ಲಿ, ಹೊಸ ಆಟವನ್ನು ಪ್ರಾರಂಭಿಸಲು ನಾವು ಹೊಸ ಗೇಮ್ ವಿಧಾನವನ್ನು ಕರೆಯುತ್ತೇವೆ.
ಪೂರ್ಣ ಆಟದ ಕೋಡ್
ಆಟದ ಕ್ರಿಯೆಯನ್ನು ನೋಡುವ ಮೊದಲು ಕೊನೆಯ ಹಂತವೆಂದರೆ ಎಲ್ಲಾ ಕೋಡ್ ಅಂಶಗಳನ್ನು ಒಟ್ಟಿಗೆ ಸೇರಿಸುವುದು. ಏನಾಗುತ್ತದೆ ಎಂಬುದು ಇಲ್ಲಿದೆ:
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.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
// We are going to create a Game of 15 Puzzle with Java 8 and Swing
// If you have some questions, feel free to read comments ;)
public class GameOfFifteen extends JPanel { // our grid will be drawn in a dedicated Panel
// Size of our Game of Fifteen instance
private int size;
// Number of tiles
private int nbTiles;
// Grid UI Dimension
private int dimension;
// Foreground Color
private static final Color FOREGROUND_COLOR = new Color(239, 83, 80); // we use arbitrary color
// Random object to shuffle tiles
private static final Random RANDOM = new Random();
// Storing the tiles in a 1D Array of integers
private int[] tiles;
// Size of tile on UI
private int tileSize;
// Position of the blank tile
private int blankPos;
// Margin for the grid on the frame
private int margin;
// Grid UI Size
private int gridSize;
private boolean gameOver; // true if game over, false otherwise
public GameOfFifteen(int size, int dim, int mar) {
this.size = size;
dimension = dim;
margin = mar;
// init tiles
nbTiles = size * size - 1; // -1 because we don't 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
// it's time to implement interaction with users to move tiles to solve the game !
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;
}
}
// Only half permutations of the puzzle are solvable.
// Whenever a tile is preceded by a tile with higher value it counts
// as an inversion. In our case, with the blank tile in the solved position,
// 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++) {
// we convert 1D coords to 2D coords given the size of the 2D Array
int r = i / size;
int c = i % size;
// we 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);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Game of Fifteen");
frame.setResizable(false);
frame.add(new GameOfFifteen(4, 550, 30), BorderLayout.CENTER);
frame.pack();
// center on the screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
ಅಂತಿಮವಾಗಿ, ನಾವು ಆಡೋಣ!
ಆಟವನ್ನು ಪ್ರಾರಂಭಿಸಲು ಮತ್ತು ಅದನ್ನು ಕ್ರಿಯೆಯಲ್ಲಿ ಪರೀಕ್ಷಿಸಲು ಇದು ಸಮಯ. ಕ್ಷೇತ್ರವು ಈ ರೀತಿ ಇರಬೇಕು:
ಒಗಟು ಪರಿಹರಿಸಲು ಪ್ರಯತ್ನಿಸೋಣ. ಎಲ್ಲವೂ ಸರಿಯಾಗಿ ನಡೆದರೆ, ನಾವು ಇದನ್ನು ಪಡೆಯುತ್ತೇವೆ: