
"පහළහ" හෝ "පහළොස්" ලොව පුරා ජනප්රිය සරල තර්ක ක්රීඩාවක් සඳහා විශිෂ්ට උදාහරණයකි. ප්රහේලිකාව විසඳීම සඳහා, ඔබ කුඩාම සිට විශාලතම දක්වා අංක සහිත කොටු පිළිවෙලට සකස් කළ යුතුය. එය පහසු නැත, නමුත් එය සිත්ගන්නා සුළුය.
අද පාඩම් මාලාවෙන් අපි පෙන්වන්නේ Eclipse සමඟ ජාවා 8 හි පහළොවක් දියුණු කරන ආකාරයයි. UI සංවර්ධනය කිරීමට අපි Swing API භාවිතා කරමු.
අපි ඔබට මතක් කරමු: "Habr" හි සියලුම පාඨකයින් සඳහා - "Habr" ප්රවර්ධන කේතය භාවිතයෙන් ඕනෑම Skillbox පාඨමාලාවකට ලියාපදිංචි වන විට රූබල් 10 ක වට්ටමක්.
Skillbox නිර්දේශ කරයි: අධ්යාපනික මාර්ගගත පාඨමාලාව .
ක්රීඩා නිර්මාණය
මෙම අදියරේදී ඔබ ගුණාංග නිර්වචනය කළ යුතුය:
- ප්රමාණය - ක්රීඩා පිටියේ ප්රමාණය;
- nbTiles - ක්ෂේත්රයේ ඇති ටැග් ගණන. nbTiles = ප්රමාණය * ප්රමාණය - 1;
- ටයිල් යනු නිඛිලවල ඒකමාන අරාවක් වන ටැග් එකකි. සෑම ටැග් එකකටම [0, nbTiles] පරාසය තුළ අනන්ය අගයක් ලැබෙනු ඇත. ශුන්යය හිස් චතුරස්රයක් පෙන්නුම් කරයි;
- blankPos - හිස් චතුරස්රයේ පිහිටීම.
ක්රීඩා තර්කනය
නව ක්රීඩා ස්ථානයක් ආරම්භ කිරීමට භාවිතා කරන යළි පිහිටුවීමේ ක්රමයක් අපට අර්ථ දැක්විය යුතුය. මේ ආකාරයට අපි ටැග් අරාවේ එක් එක් මූලද්රව්ය සඳහා අගයක් සකසමු. හොඳයි, එහෙනම් අපි array එකේ අවසාන ස්ථානයේ blankPos තබමු.
ටැග් අරාව කලවම් කිරීමට ද අපට කලවම් කිරීමේ ක්රමයක් අවශ්ය වේ. අපි හිස් ටැගය එකම ස්ථානයේ තැබීම සඳහා මාරු කිරීමේ ක්රියාවලියට ඇතුළත් නොකරමු.
ප්රහේලිකාවේ විය හැකි ආරම්භක ස්ථානවලින් අඩකට පමණක් විසඳුමක් ඇති බැවින්, වත්මන් පිරිසැලසුම පවා විසඳිය හැකි බවට වග බලා ගැනීම සඳහා ඔබට ලැබෙන කලවම් ප්රතිඵලය පරීක්ෂා කළ යුතුය. මෙය සිදු කිරීම සඳහා, අපි isSolvable ක්රමය නිර්වචනය කරමු.
යම් ටැග් එකකට පෙර ඉහළ අගයක් සහිත ටැගයක් ඇත්නම්, එය ප්රතිලෝමයක් ලෙස සැලකේ. හිස් ස්ථානය ඇති විට, ප්රහේලිකාව විසඳිය හැකි වීම සඳහා ප්රතිලෝම ගණන සමාන විය යුතුය. එබැවින් අපි ප්රතිලෝම ගණන ගණන් කර එම සංඛ්යාව ඉරට්ටේ නම් සත්ය ලබා දෙන්නෙමු.
එවිට අපගේ Game Of Fifteen පිරිසැලසුම විසඳී ඇත්දැයි පරීක්ෂා කිරීමට isSolved ක්රමය නිර්වචනය කිරීම වැදගත් වේ. මුලින්ම අපි බලමු හිස් තැන කොහෙද කියලා. ආරම්භක ස්ථානයේ නම්, වත්මන් පෙළගැස්ම නව, කලින් තීරණය කර නැත. ඉන්පසුව අපි ටයිල් හරහා ප්රතිලෝම අනුපිළිවෙලින් පුනරුච්චාරණය කරන්නෙමු, සහ ටැගයේ අගය අනුරූප දර්ශකය +1 ට වඩා වෙනස් නම්, අපි අසත්යය ලබා දෙන්නෙමු. එසේ නොමැතිනම්, ක්රමය අවසානයේ එය සත්ය බවට ආපසු යාමට කාලයයි, මන්ද ප්රහේලිකාව දැනටමත් විසඳා ඇත.
නිර්වචනය කළ යුතු තවත් ක්රමයක් වන්නේ newGame ය. ක්රීඩාවේ නව අවස්ථාවක් නිර්මාණය කිරීම අවශ්ය වේ. මෙය සිදු කිරීම සඳහා, අපි ක්රීඩා පිටිය නැවත සකසන්න, පසුව එය කලවම් කර ක්රීඩා කරන ස්ථානය විසඳන තෙක් දිගටම කරගෙන යන්නෙමු.
ටැගයේ ප්රධාන තර්කය සහිත උදාහරණ කේතයක් මෙන්න:
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;අපි Swing API භාවිතයෙන් UI සංවර්ධනය කරමු
අතුරු මුහුණත මත වැඩ කිරීමට කාලයයි. මුලින්ම අපි ගන්නේ Jpanel පන්තිය. ඉන්පසු අපි ක්ෂේත්රයේ ටැග් අඳින්නෙමු - එක් එක් ප්රමාණය ගණනය කිරීම සඳහා, අපි ක්රීඩා නිර්මාපක පරාමිතියෙහි දක්වා ඇති දත්ත භාවිතා කරන්නෙමු:
gridSize = (dim - 2 * margin);
tileSize = gridSize / size;Margin යනු ගේම් කන්ස්ට්රක්ටරය තුළ සැකසූ පරාමිතියකි.
දැන් අපි තිරය මත ජාලකය සහ ලප ඇඳීම සඳහා drawGrid ක්රමය අර්ථ දැක්විය යුතුය. අපි ටැග් අරාව විශ්ලේෂණය කර ඛණ්ඩාංක පරිශීලක අතුරුමුහුණත් ඛණ්ඩාංක බවට පරිවර්තනය කරමු. ඉන්පසු මධ්යයේ ඇති අනුරූප අංකය සමඟ එක් එක් ස්ථාන අඳින්න:
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 පන්තියෙන් ව්යුත්පන්න වන paintComponent ක්රමය අභිබවා යමු. ක්රීඩාව ආරම්භ කිරීමට ක්ලික් කිරීමට අපව පොළඹවන පණිවිඩයක් ප්රදර්ශනය කිරීම සඳහා අපි පසුව drawGrid ක්රමය භාවිතා කරමු.
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 පන්තියේ ඉදිකිරීම්කරු තුළ කේතය තබමු. අවසානයේදී, අපි නව ක්රීඩාවක් ආරම්භ කිරීමට newGame ක්රමයට කියමු.
සම්පූර්ණ ක්රීඩා කේතය
ක්රීඩාව ක්රියාත්මක වීමට පෙර අවසාන පියවර වන්නේ සියලුම කේත මූලද්රව්ය එකට තැබීමයි. මෙන්න මෙහෙමයි වෙන්නේ:
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);
});
}
}අවසාන වශයෙන්, අපි සෙල්ලම් කරමු!
ක්රීඩාව දියත් කිරීමට සහ එය ක්රියාවෙන් පරීක්ෂා කිරීමට කාලයයි. ක්ෂේත්රය මේ වගේ විය යුතුය:

අපි ප්රහේලිකාව විසඳීමට උත්සාහ කරමු. සෑම දෙයක්ම හොඳින් සිදු වූවා නම්, අපට මෙය ලැබේ:

එච්චරයි. ඔබ තවත් බලාපොරොත්තු වූවාද? 🙂
Skillbox නිර්දේශ කරයි:
- ප්රායෝගික පාඨමාලාව .
- මාර්ගගත පාඨමාලාවට අයදුම් කර ඇත .
- වසර දෙකක ප්රායෝගික පාඨමාලාව .
මූලාශ්රය: www.habr.com
