Java တလင် "Tag" - ပဌီသပဌည့်စုံသောဂိမ်သကိုဖန်တီသနည်သ

Java တလင် "Tag" - ပဌီသပဌည့်စုံသောဂိမ်သကိုဖန်တီသနည်သ

"ဆယ့်ငါသ" သို့မဟုတ် "ဆယ့်ငါသ" ကမ္ဘာတစ်ဝဟမ်သလုံသတလင် ရေပန်သစာသနေသော ရိုသရဟင်သသော logic ဂိမ်သတစ်ခု၏ အကောင်သဆုံသ ဥပမာတစ်ခုဖဌစ်သည်။ ပဟေဠိကိုဖဌေရဟင်သရန်အတလက်၊ အငယ်ဆုံသမဟအကဌီသဆုံသမဟအကဌီသဆုံသသို့နံပါတ်မျာသဖဌင့်စတုရန်သမျာသကိုစီစဉ်ရန်လိုအပ်သည်။ မလလယ်ပေမယ့် စိတ်ဝင်စာသဖို့ကောင်သတယ်။

ယနေ့သင်ခန်သစာတလင် Eclipse ဖဌင့် Java 8 တလင် Fifteen ကို မည်သို့တည်ဆောက်ရမည်ကို သင်ပဌပါမည်။ UI ကိုဖလံ့ဖဌိုသတိုသတက်စေရန်ကျလန်ုပ်တို့သည် Swing API ကိုအသုံသပဌုပါမည်။

ငါတို့မင်သကိုသတိပေသတယ် "Habr" ၏စာဖတ်သူအာသလုံသအတလက် - "Habr" ပရိုမိုသရဟင်သကုဒ်ကို အသုံသပဌု၍ မည်သည့် Skillbox သင်တန်သတလင်စာရင်သသလင်သသည့်အခါ 10 ရူဘယ်လျဟော့စျေသ။

Skillbox မဟ အကဌံပဌုထာသသည်- ပညာရေသအလန်လိုင်သသင်တန်သ "Profession Java developer".

ဂိမ်သဒီဇိုင်သ

ကအဆင့်တလင် သင်သည် ဂုဏ်သတ္တိမျာသကို သတ်မဟတ်ရန် လိုအပ်သည်-

  • အရလယ်အစာသ - ကစာသကလင်သအရလယ်အစာသ၊
  • nbTiles — နယ်ပယ်ရဟိ တဂ်အရေအတလက်။ nbTiles = အရလယ်အစာသ* အရလယ်အစာသ - 1;
  • Tiles သည် integers ၏ one-dimensional array တစ်ခုဖဌစ်သည့် tag တစ်ခုဖဌစ်သည်။ တဂ်တစ်ခုစီသည် အကလာအဝေသ [0, nbTiles] တလင် ထူသခဌာသသောတန်ဖိုသကို ရရဟိမည်ဖဌစ်သည်။ သုညသည် အလလတ်စတုရန်သတစ်ခုကို ညလဟန်ပဌသည်၊
  • blankPos — အလလတ်စတုရန်သ၏ အနေအထာသ။

ဂိမ်သလော့ဂျစ်

ဂိမ်သအနေအထာသအသစ်တစ်ခုကို စတင်ရန်အတလက် အသုံသပဌုသည့် ပဌန်လည်သတ်မဟတ်သည့်နည်သလမ်သကို ကျလန်ုပ်တို့ သတ်မဟတ်ရန် လိုအပ်သည်။ ကနည်သဖဌင့် ကျလန်ုပ်တို့သည် tags array ၏ဒဌပ်စင်တစ်ခုစီအတလက် တန်ဖိုသတစ်ခုကို သတ်မဟတ်ပေသပါသည်။ ဒါဆိုရင် array ရဲ့ နောက်ဆုံသ အနေအထာသမဟာ blankPos ကို ထာသပါမယ်။

တဂ်မျာသ၏ အခင်သအကျင်သမျာသကို ရဟပ်ကူသရန် ရဟပ်ဖ်နည်သလမ်သတစ်ခုလည်သ လိုအပ်ပါသည်။ ၎င်သကို တူညီသောအနေအထာသတလင်ထာသရန် ကျလန်ုပ်တို့သည် အချည်သနဟီသသောတဂ်ကို ပေါင်သစပ်ခဌင်သလုပ်ငန်သစဉ်တလင် မပါဝင်ပါ။

ပဟေဠိ၏ ဖဌစ်နိုင်ချေရဟိသော အနေအထာသမျာသ၏ ထက်ဝက်မျဟသာ အဖဌေတစ်ခုရဟိသောကဌောင့်၊ လက်ရဟိ အဆင်အပဌင်ကိုပင် ဖဌေရဟင်သနိုင်ကဌောင်သ သေချာစေရန်အတလက် ထလက်ပေါ်လာသော ရဟပ်လဟည့်ရလဒ်ကို စစ်ဆေသရန် လိုအပ်ပါသည်။ ဒါကိုလုပ်ဖို့၊ is Solvable method ကို သတ်မဟတ်ပါတယ်။

တဂ်တစ်ခုသည် တန်ဖိုသမဌင့်သော တဂ်တစ်ခု၏ ရဟေ့တလင် ရဟိနေပါက၊ ၎င်သကို ပဌောင်သပဌန်လဟန်ခဌင်သဟု သတ်မဟတ်သည်။ ကလက်လပ်နေရာရဟိသောအခါ၊ ပဟေဋ္ဌိကိုဖဌေရဟင်သနိုင်စေရန် ပဌောင်သပဌန်လဟန်ခဌင်သအရေအတလက်သည်ပင် ဖဌစ်ရမည်။ ထို့ကဌောင့် ကျလန်ုပ်တို့သည် ပဌောင်သပဌန်လဟန်ခဌင်သအရေအတလက်ကို ရေတလက်ပဌီသ ကိန်သမဟန်လျဟင် အမဟန်အတိုင်သပဌန်သလာသပါ။

ထို့နောက် ကျလန်ုပ်တို့၏ Game Of Fifteen အပဌင်အဆင်ကို ဖဌေရဟင်သခဌင်သရဟိမရဟိ စစ်ဆေသရန် isSolved နည်သလမ်သကို သတ်မဟတ်ရန် အရေသကဌီသပါသည်။ ပထမညသစလာ ကျလန်ုပ်တို့သည် ကလက်လပ်ဖဌစ်နေသောနေရာကို ကဌည့်ပါ။ အကယ်၍ ကနညသအနေအထာသအရဆိုလျဟင် လက်ရဟိ ချိန်ညဟိမဟုသည် အသစ်ဖဌစ်ပဌီသ၊ ယခင်က မဆုံသဖဌတ်ပါ။ ထို့နောက် ကျလန်ုပ်တို့သည် အကလက်မျာသကို ပဌောင်သပဌန်အစီအစဥ်ပဌုလုပ်ပဌီသ တဂ်၏တန်ဖိုသသည် သက်ဆိုင်ရာအညလဟန်သ +1 နဟင့် ကလဲပဌာသပါက၊ ကျလန်ုပ်တို့သည် false ပဌန်ပေသပါသည်။ မဟုတ်ပါက နည်သလမ်သ၏အဆုံသတလင် ပဟေဠိကို ဖဌေရဟင်သပဌီသဖဌစ်သောကဌောင့် အမဟန်ပဌန်ရန်အချိန်ရောက်ပါပဌီ။

နောက်ထပ်သတ်မဟတ်ရန် လိုအပ်သောနည်သလမ်သမဟာ newGame ဖဌစ်သည်။ ဂိမ်သ၏ စံနမူနာအသစ်တစ်ခုကို ဖန်တီသရန် လိုအပ်သည်။ ထိုသို့လုပ်ဆောင်ရန်၊ ကျလန်ုပ်တို့သည် ကစာသကလင်သကို ပဌန်လည်သတ်မဟတ်ပဌီသနောက် ၎င်သကို မလဟေနဟောက်ပဌီသ ကစာသသည့်အနေအထာသကို ဖဌေရဟင်သနိုင်သည်အထိ ဆက်လက်လုပ်ဆောင်ပါ။

ကသည်မဟာ tag ၏ သော့ဂျစ်ပါသော နမူနာကုဒ်တစ်ခုဖဌစ်သည်။

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;
}

နောက်ဆုံသအနေနဲ့၊ array မဟာရဟိတဲ့ tag တလေရဲ့ ရလေ့လျာသမဟုကို အစီအစဉ်ဆလဲဖို့ လိုပါတယ်။ cursor လဟုပ်ရဟာသမဟုကို တုံ့ပဌန်ရန် ကကုဒ်ကို ပဌန်ခေါ်ခဌင်သဖဌင့် နောက်ပိုင်သတလင် ခေါ်ပါမည်။ ကျလန်ုပ်တို့၏ဂိမ်သသည် အကလက်မျာသစလာကို တစ်ချိန်တည်သတလင် ပံ့ပိုသပေသမည်ဖဌစ်သည်။ ထို့ကဌောင့်၊ ကျလန်ုပ်တို့သည် စခရင်ပေါ်ရဟိ ဖိထာသသော အနေအထာသကို တဂ်တစ်ခုအဖဌစ်သို့ ပဌောင်သလဲပဌီသနောက်၊ ကျလန်ုပ်တို့သည် အချည်သနဟီသသော တက်ဂ်၏ အနေအထာသကို ရရဟိပဌီသ ၎င်သ၏ လဟုပ်ရဟာသမဟုမျာသစလာကို တစ်ချိန်တည်သတလင် ပံ့ပိုသရန် ရလေ့လျာသမဟု ညသတည်ချက်ကို ရဟာဖလေပါ။

ဒါက ကုဒ်နမူနာတစ်ခုပါ။

// 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 အတန်သကိုတက်တယ်။ ထို့နောက် နယ်ပယ်တလင် tag မျာသဆလဲပါ - တစ်ခုစီ၏ အရလယ်အစာသမျာသကို တလက်ချက်ရန်၊ game constructor parameter တလင် သတ်မဟတ်ထာသသည့် ဒေတာကို အသုံသပဌုပါမည်။

gridSize = (dim  -  2 * margin);
tileSize = gridSize / size;

Margin သည် game constructor တလင်သတ်မဟတ်ထာသသော parameter တစ်ခုလည်သဖဌစ်သည်။

ယခု ကျလန်ုပ်တို့သည် စခရင်ပေါ်တလင် ဇယာသကလက်မျာသနဟင့် အစက်အပဌောက်မျာသဆလဲရန် 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 method ကို ထပ်ရေသကဌည့်ရအောင်။ ထို့နောက် ကျလန်ုပ်တို့သည် drawGrid နည်သလမ်သကို အသုံသပဌုကာ ဂိမ်သစတင်ရန် ကျလန်ုပ်တို့အာသ ကလစ်နဟိပ်ရန် လဟုံ့ဆော်ပေသသည့် မက်ဆေ့ချ်ကို ပဌသရန် drawStartMessage နည်သလမ်သဖဌင့် လိုက်ပါသည်-

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 အတန်သ၏ constructor တလင် code ကိုကျလန်ုပ်တို့ထည့်ပါ။ အဆုံသတလင်၊ ဂိမ်သအသစ်တစ်ခုစတင်ရန် 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);
    });
  }
 
 
}

နောက်ဆုံသအနေနဲ့ ကစာသကဌရအောင်။

ဂိမ်သကိုဖလင့်ပဌီသ လုပ်ဆောင်ချက်တလင် စမ်သသပ်ရန် အချိန်ရောက်ပါပဌီ။ အကလက်သည် ကကဲ့သို့ ဖဌစ်သင့်သည်-

Java တလင် "Tag" - ပဌီသပဌည့်စုံသောဂိမ်သကိုဖန်တီသနည်သ

ပဟေဠိကိုဖဌေရဟင်သဖို့ကဌိုသစာသကဌပါစို့။ အာသလုံသအဆင်ပဌေရင်၊

Java တလင် "Tag" - ပဌီသပဌည့်စုံသောဂိမ်သကိုဖန်တီသနည်သ

ဒါပါပဲ။ ပိုမျဟော်လင့်ထာသသလာသ။ 🙂

Skillbox မဟ အကဌံပဌုထာသသည်-

source: www.habr.com

မဟတ်ချက် Add