• 生成拼图游戏


    1. import javax.swing.*;
    2. import java.awt.*;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. import java.awt.event.KeyEvent;
    6. import java.awt.event.KeyListener;
    7. import java.util.Arrays;
    8. import java.util.Collections;
    9. import java.util.List;
    10. class PuzzlePanel extends JPanel implements KeyListener {
    11. private static final int TILE_SIZE = 80;
    12. private static final int BOARD_SIZE = 3;
    13. private String[][] puzzle;
    14. private String[][] solvedPuzzle;
    15. public PuzzlePanel() {
    16. setPreferredSize(new Dimension(TILE_SIZE * BOARD_SIZE, TILE_SIZE * BOARD_SIZE));
    17. setFocusable(true);
    18. addKeyListener(this);
    19. initializePuzzle();
    20. }
    21. private void initializePuzzle() {
    22. puzzle = new String[BOARD_SIZE][BOARD_SIZE];
    23. solvedPuzzle = new String[BOARD_SIZE][BOARD_SIZE];
    24. List<String> numbers = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", " ");
    25. Collections.shuffle(numbers);
    26. int index = 0;
    27. for (int i = 0; i < BOARD_SIZE; i++) {
    28. for (int j = 0; j < BOARD_SIZE; j++) {
    29. puzzle[i][j] = numbers.get(index);
    30. solvedPuzzle[i][j] = String.valueOf(index + 1);
    31. index++;
    32. }
    33. }
    34. }
    35. private void drawTile(Graphics g, String value, int row, int col) {
    36. int x = col * TILE_SIZE;
    37. int y = row * TILE_SIZE;
    38. g.setColor(Color.lightGray);
    39. g.fillRect(x, y, TILE_SIZE, TILE_SIZE);
    40. g.setColor(Color.black);
    41. g.drawRect(x, y, TILE_SIZE, TILE_SIZE);
    42. g.setColor(Color.black);
    43. Font font = new Font("Arial", Font.BOLD, 24);
    44. g.setFont(font);
    45. FontMetrics fm = g.getFontMetrics();
    46. int textWidth = fm.stringWidth(value);
    47. int textHeight = fm.getHeight();
    48. int centerX = x + (TILE_SIZE - textWidth) / 2;
    49. int centerY = y + (TILE_SIZE - textHeight) / 2 + fm.getAscent();
    50. g.drawString(value, centerX, centerY);
    51. }
    52. private void drawPuzzle(Graphics g) {
    53. for (int i = 0; i < BOARD_SIZE; i++) {
    54. for (int j = 0; j < BOARD_SIZE; j++) {
    55. drawTile(g, puzzle[i][j], i, j);
    56. }
    57. }
    58. }
    59. private void checkIfSolved() {
    60. if (Arrays.deepEquals(puzzle, solvedPuzzle)) {
    61. JOptionPane.showMessageDialog(this, "恭喜你,拼图完成!", "拼图完成", JOptionPane.INFORMATION_MESSAGE);
    62. initializePuzzle();
    63. }
    64. }
    65. private void moveTile(int row, int col) {
    66. if (isValidMove(row, col)) {
    67. String temp = puzzle[row][col];
    68. puzzle[row][col] = puzzle[row + 1][col];
    69. puzzle[row + 1][col] = temp;
    70. checkIfSolved();
    71. }
    72. }
    73. private boolean isValidMove(int row, int col) {
    74. return row < BOARD_SIZE - 1 && puzzle[row + 1][col].equals(" ");
    75. }
    76. @Override
    77. protected void paintComponent(Graphics g) {
    78. super.paintComponent(g);
    79. drawPuzzle(g);
    80. }
    81. @Override
    82. public void keyTyped(KeyEvent e) {
    83. // 不需要实现
    84. }
    85. @Override
    86. public void keyPressed(KeyEvent e) {
    87. if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    88. moveTile(getEmptyRow(), getEmptyCol());
    89. repaint();
    90. }
    91. }
    92. @Override
    93. public void keyReleased(KeyEvent e) {
    94. // 不需要实现
    95. }
    96. private int getEmptyRow() {
    97. for (int i = 0; i < BOARD_SIZE; i++) {
    98. for (int j = 0; j < BOARD_SIZE; j++) {
    99. if (puzzle[i][j].equals(" ")) {
    100. return i;
    101. }
    102. }
    103. }
    104. return -1;
    105. }
    106. private int getEmptyCol() {
    107. for (int i = 0; i < BOARD_SIZE; i++) {
    108. for (int j = 0; j < BOARD_SIZE; j++) {
    109. if (puzzle[i][j].equals(" ")) {
    110. return j;
    111. }
    112. }
    113. }
    114. return -1;
    115. }
    116. }
    117. public class PuzzelGameApp extends JFrame {
    118. public PuzzelGameApp() {
    119. setTitle("拼图游戏");
    120. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    121. setResizable(false);
    122. PuzzlePanel puzzlePanel = new PuzzlePanel();
    123. add(puzzlePanel);
    124. pack();
    125. setLocationRelativeTo(null);
    126. Timer timer = new Timer(1000, new ActionListener() {
    127. @Override
    128. public void actionPerformed(ActionEvent e) {
    129. puzzlePanel.repaint();
    130. }
    131. });
    132. timer.start();
    133. }
    134. public static void main(String[] args) {
    135. SwingUtilities.invokeLater(() -> {
    136. PuzzelGameApp puzzleGameApp = new PuzzelGameApp();
    137. puzzleGameApp.setVisible(true);
    138. });
    139. }
    140. }

    生成2

    1. import java.util.Arrays;
    2. import java.util.Collections;
    3. import java.util.List;
    4. import java.util.Scanner;
    5. public class PuzzleGame {
    6. private static final String[][] puzzle = {
    7. {"1", "2", "3"},
    8. {"4", "5", "6"},
    9. {"7", "8", " "}
    10. };
    11. private static final String[][] solvedPuzzle = {
    12. {"1", "2", "3"},
    13. {"4", "5", "6"},
    14. {"7", "8", " "}
    15. };
    16. public static void main(String[] args) {
    17. shufflePuzzle();
    18. while (!isPuzzleSolved()) {
    19. displayPuzzle();
    20. moveTile();
    21. }
    22. System.out.println("恭喜你,拼图完成!");
    23. }
    24. private static void shufflePuzzle() {
    25. List<String> flatPuzzle = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", " ");
    26. Collections.shuffle(flatPuzzle);
    27. int index = 0;
    28. for (int i = 0; i < puzzle.length; i++) {
    29. for (int j = 0; j < puzzle[i].length; j++) {
    30. puzzle[i][j] = flatPuzzle.get(index++);
    31. }
    32. }
    33. }
    34. private static void displayPuzzle() {
    35. for (String[] row : puzzle) {
    36. for (String tile : row) {
    37. System.out.print(tile + "\t");
    38. }
    39. System.out.println();
    40. }
    41. System.out.println();
    42. }
    43. private static void moveTile() {
    44. Scanner scanner = new Scanner(System.in);
    45. System.out.print("请输入要移动的数字(1-8): ");
    46. String input = scanner.nextLine();
    47. String tileToMove = input.trim();
    48. // 寻找空白格的位置
    49. int emptyRow = -1;
    50. int emptyCol = -1;
    51. for (int i = 0; i < puzzle.length; i++) {
    52. for (int j = 0; j < puzzle[i].length; j++) {
    53. if (puzzle[i][j].equals(" ")) {
    54. emptyRow = i;
    55. emptyCol = j;
    56. break;
    57. }
    58. }
    59. }
    60. // 寻找要移动的数字的位置
    61. int tileRow = -1;
    62. int tileCol = -1;
    63. for (int i = 0; i < puzzle.length; i++) {
    64. for (int j = 0; j < puzzle[i].length; j++) {
    65. if (puzzle[i][j].equals(tileToMove)) {
    66. tileRow = i;
    67. tileCol = j;
    68. break;
    69. }
    70. }
    71. }
    72. // 判断是否可以移动
    73. if ((Math.abs(emptyRow - tileRow) == 1 && emptyCol == tileCol) ||
    74. (Math.abs(emptyCol - tileCol) == 1 && emptyRow == tileRow)) {
    75. // 交换空白格和要移动的数字的位置
    76. String temp = puzzle[emptyRow][emptyCol];
    77. puzzle[emptyRow][emptyCol] = puzzle[tileRow][tileCol];
    78. puzzle[tileRow][tileCol] = temp;
    79. } else {
    80. System.out.println("不能移动这个数字,请重新输入。");
    81. }
    82. }
    83. private static boolean isPuzzleSolved() {
    84. return Arrays.deepEquals(puzzle, solvedPuzzle);
    85. }
    86. }

     

     

  • 相关阅读:
    Ubuntu 18.04无网络连接的n种可能办法
    【Linux学习】05-1Linux上安装部署各类软件
    Linux编译FFmpeg
    书籍学习|Unsupervised Learning Algorithms(part1)
    LiveGBS流媒体平台GB/T28181常见问题-基础配置流媒体服务配置中本地|内网IP外网IP(可选)外网IP收流如何配置
    Servlet 基础知识(4)(利用Servlet实现文件上传功能)
    银河麒麟V10系统 syslog和kern.log文件过大问题解决,定时清理日志文件
    AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
    电子学会2023年6月青少年软件编程(图形化)等级考试试卷(二级)真题,含答案解析
    【python】python连接Oracle数据库
  • 原文地址:https://blog.csdn.net/GYyuandao/article/details/134546432