Java:做一个自动生成24点游戏,并能自主解决的程序

我们首先利用random函数,随机从1~13中挑选数字,挑选四次。

 public static void main(String[] args) {
        Random r = new Random();
        int r1 = r.nextInt(13) + 1;
        int r2 = r.nextInt(13) + 1;
        int r3 = r.nextInt(13) + 1;
        int r4 = r.nextInt(13) + 1;
        int[] num = new int[]{r1, r2, r3, r4};
    }

为了让它更像卡牌,我们将1变换成A,将11,12,13变换为J、Q、K。

可以创建一个changeNum方法。

public static String changeNum(int num) {
        if (num == 1) {
            return "A";
        } else if (num == 11) {
            return "J";
        } else if (num == 12) {
            return "Q";
        } else if (num == 13) {
            return "K";
        } else {
            return "" + num;
        }
    }

这样一个随机生成的24点游戏就做好了,只需要在开头psvm处输出四个结果就好了。
比如

System.out.println(changeNum(r1) + " " + changeNum(r2) + " " + changeNum(r3) + " " + changeNum(r4));

J 8 8 Q (随机生成的)

然后我们要想办法制作一个能够自主解决的程序。计算机优势在于它的运算速度,因此我们可以使用穷举法。

首先创建一个未知数,用finalNum命名。最后我们要让它为24时输出整个式子。

然后我们需要创建三个变量,分别是math1math2math3,用以代表加减乘除的符号。

我试用了多层for循环嵌套,目的也是为了实现加减乘除的穷举法。但是当我写完代码后,我发现它只能按顺序来算。

比如

J 8 8 Q

会算成

-> ((11+8)/8)*12
即为: -> ((J+8)/8)*Q

注意,java的运算符中,如果使用int变量计算,除法是无法得到小数的。5/2=2而非2.5。所以我们可以在算式前加上(double)将int转换为double(灵感来自英英)。

它不能调换一下顺序,算成诸如:

-> ((11* 12)/8)+8
即为: -> ((J*Q)/8)+8

因此如何解决这个问题,成了接下来程序设计的重点。

我在原有嵌套中又多加了两层for循环嵌套,代码如下:

for (int l = 0; l < 4; l++) {
        for (int m = 1; m < 4 - l; m++) {

        //first

        if (i == 0) {
        finalNum = num[l] + num[l + m];
        math1 = "+";
        } else if (i == 1) {
        finalNum = num[l] - num[l + m];
        math1 = "-";
        } else if (i == 2) {
        finalNum = num[l] * num[l + m];
        math1 = "*";
        } else {
        finalNum = num[l] / num[l + m];
        math1 = "/";
        }

这样,我们只需要让后面运算的两个数字不要和num[l]以及num[l+m]撞车就好啦。

我采用了这样的写法:

if (n != l && n != l + m)

总体代码再经过大体细节雕琢,全部如下:

package S1;

import java.util.Random;

public class S1 {
    public static void main(String[] args) {
        Random r = new Random();
        int r1 = r.nextInt(13) + 1;
        int r2 = r.nextInt(13) + 1;
        int r3 = r.nextInt(13) + 1;
        int r4 = r.nextInt(13) + 1;
        int[] num = new int[]{r1, r2, r3, r4};
        System.out.println(changeNum(r1) + " " + changeNum(r2) + " " + changeNum(r3) + " " + changeNum(r4));
        calculator(num);
    }

    public static String changeNum(int num) {
        if (num == 1) {
            return "A";
        } else if (num == 11) {
            return "J";
        } else if (num == 12) {
            return "Q";
        } else if (num == 13) {
            return "K";
        } else {
            return "" + num;
        }
    }

    public static void calculator(int[] num) {
        double finalNum = 0;
        String math1 = "", math2 = "", math3 = "";
        int times = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                for (int k = 0; k < 4; k++) {
                    for (int l = 0; l < 4; l++) {
                        for (int m = 1; m < 4 - l; m++) {

                            //first

                            if (i == 0) {
                                finalNum = num[l] + num[l + m];
                                math1 = "+";
                            } else if (i == 1) {
                                finalNum = num[l] - num[l + m];
                                math1 = "-";
                            } else if (i == 2) {
                                finalNum = num[l] * num[l + m];
                                math1 = "*";
                            } else {
                                finalNum = (double)num[l] / num[l + m];
                                math1 = "/";
                            }

                            //second
                            int num1;
                            int num2;
                            for (int n = 0; n < 4; n++) {
                                if (n != l && n != l + m) {
                                    num1 = num[n];
                                    if (j == 0) {
                                        finalNum += num1;
                                        math2 = "+";
                                    } else if (j == 1) {
                                        finalNum -= num1;
                                        math2 = "-";
                                    } else if (j == 2) {
                                        finalNum *= num1;
                                        math2 = "*";
                                    } else {
                                        finalNum /= num1;
                                        math2 = "/";
                                    }
                                    for (int o = 3; o > n; o--) {
                                        if (o != l && o != l + m) {
                                            num2 = num[o];
                                            //third

                                            if (k == 0) {
                                                finalNum += num2;
                                                math3 = "+";
                                            } else if (k == 1) {
                                                finalNum -= num2;
                                                math3 = "-";
                                            } else if (k == 2) {
                                                finalNum *= num2;
                                                math3 = "*";
                                            } else {
                                                finalNum /= num2;
                                                math3 = "/";
                                            }
                                            if (finalNum == 24) {
                                                times += 1;
                                                System.out.println("找到第" + times + "个解决方法:");
                                                System.out.println(" -> ((" + num[l] + "" + math1 + num[l + m] + ")" + math2 + num1 + ")" + math3 + num2);
                                                System.out.println("即为:" + " -> ((" + changeNum(num[l]) + "" + math1 + changeNum(num[l + m]) + ")" + math2 + changeNum(num1) + ")" + math3 + changeNum(num2));
                                                System.out.println();
                                            }
                                        }
                                    }
                                }


                            }


                        }

                    }
                }
            }
        }
    }
}


测试几个结果如下:

K 3 K 8
找到第1个解决方法:
-> ((13 + 3) - 13) * 8
即为: -> ((K + 3) - K ) * 8
找到第2个解决方法:
-> ((3 + 13) - 13) * 8
即为: -> ((3 + K) - K ) * 8
找到第3个解决方法:
-> ((13 + 8) - 13) * 3
即为: -> ((K + 8) - K ) * 3
找到第4个解决方法:
-> ((13 - 13) + 3) * 8
即为: -> ((K - K) + 3 ) * 8
找到第5个解决方法:
-> ((3 - 13) + 13) * 8
即为: -> ((3 - K) + K ) * 8
找到第6个解决方法:
-> ((3 * 8) + 13) - 13
即为: -> ((3 * 8) + K ) - K
找到第7个解决方法:
-> ((3 * 8) - 13) + 13
即为: -> ((3 * 8) - K ) + K
找到第8个解决方法:
-> ((13 * 8) * 3) / 13
即为: -> ((K * 8) * 3 ) / K
找到第9个解决方法:
-> ((3 * 8) * 13) / 13
即为: -> ((3 * 8) * K ) / K
找到第10个解决方法:
-> ((13 * 3) / 13) * 8
即为: -> ((K * 3) / K ) * 8
找到第11个解决方法:
-> ((3 * 13) / 13) * 8
即为: -> ((3 * K) / K ) * 8
找到第12个解决方法:
-> ((3 * 8) / 13) * 13
即为: -> ((3 * 8) / K ) * K
找到第13个解决方法:
-> ((13 * 8) / 13) * 3
即为: -> ((K * 8) / K ) * 3
找到第14个解决方法:
-> ((13 / 13) * 3) * 8
即为: -> ((K / K) * 3 ) * 8
找到第15个解决方法:
-> ((3 / 13) * 13) * 8
即为: -> ((3 / K) * K ) * 8

K 7 5 K
找到第1个解决方法:
-> ((5 + 13) + 13) - 7
即为: -> ((5 + K) + K) - 7
找到第2个解决方法:
-> ((13 + 5) - 7) + 13
即为: -> ((K + 5) - 7) + K
找到第3个解决方法:
-> ((13 + 13) - 7) + 5
即为: -> ((K + K) - 7) + 5
找到第4个解决方法:
-> ((13 - 7) + 5) + 13
即为: -> ((K - 7) + 5) + K

6 3 5 3
找到第1个解决方法:
-> ((6 + 5) - 3) * 3
即为: -> ((6 + 5) - 3) * 3
找到第2个解决方法:
-> ((6 - 3) + 5) * 3
即为: -> ((6 - 3) + 5) * 3
找到第3个解决方法:
-> ((5 - 3) + 6) * 3
即为: -> ((5 - 3) + 6) * 3
找到第4个解决方法:
-> ((3 * 5) + 6) + 3
即为: -> ((3 * 5) + 6) + 3
找到第5个解决方法:
-> ((5 * 3) + 6) + 3
即为: -> ((5 * 3) + 6) + 3
找到第6个解决方法:
-> ((6 * 5) - 3) - 3
即为: -> ((6 * 5) - 3) - 3

7 A 5 K
找到第1个解决方法:
-> ((5 + 13) + 7) - 1
即为: -> ((5 + K) + 7) - A
找到第2个解决方法:
-> ((7 + 5) - 1) + 13
即为: -> ((7 + 5) - A) + K
找到第3个解决方法:
-> ((7 + 13) - 1) + 5
即为: -> ((7 + K) - A) + 5
找到第4个解决方法:
-> ((7 - 1) + 5) + 13
即为: -> ((7 - A) + 5) + K


//(不知道为什么CSDN的乘号*有点bug,经常会显示错误,所以有一些我就在乘号两边加上空格了)

我还在学习之中,代码一定是不够优化的甚至可能存在bug。也希望大家对我批评纠正,互相学习。

==================== 2021.3.27更新,将:

 public static void main(String[] args) {
        Random r = new Random();
        int r1 = r.nextInt(13) + 1;
        int r2 = r.nextInt(13) + 1;
        int r3 = r.nextInt(13) + 1;
        int r4 = r.nextInt(13) + 1;
        int[] num = new int[]{r1, r2, r3, r4};
    }

改成:

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入第一个数字");
        int r1 = s.nextInt();
        System.out.println("请输入第二个数字");
        int r2 = s.nextInt();
        System.out.println("请输入第三个数字");
        int r3 = s.nextInt();
        System.out.println("请输入第四个数字");
        int r4 = s.nextInt();
        int[] num = new int[]{r1, r2, r3, r4};
        System.out.println(changeNum(r1) + " " + changeNum(r2) + " " + changeNum(r3) + " " + changeNum(r4));
        calculator(num);
    }

可以做题目了哈哈哈。

比如apple watch的24点游戏app:

在这里插入图片描述

我们输入:

在这里插入图片描述

就可以作弊了~

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,以下是一个简单的JavaJavaFX实现电脑鼠走迷宫的程序。这个程序可以使用Prim算法生成迷宫,使用深度优先搜索来找到通路,使用Dijkstra算法来找到最短路径,并使用JavaFX设计合理的界面布局展示给用户。 ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.stage.Stage; import java.util.*; public class MazeSolver extends Application { private static final int MAZE_SIZE = 25; private static final int CELL_SIZE = 20; private static final int WALL_SIZE = 2; private static final int SCREEN_WIDTH = MAZE_SIZE * (CELL_SIZE + WALL_SIZE) + WALL_SIZE; private static final int SCREEN_HEIGHT = MAZE_SIZE * (CELL_SIZE + WALL_SIZE) + WALL_SIZE; private static final Color WALL_COLOR = Color.BLACK; private static final Color CELL_COLOR = Color.WHITE; private static final Color START_COLOR = Color.GREEN; private static final Color END_COLOR = Color.RED; private static final Color PATH_COLOR = Color.BLUE; private int[][] maze; private int startX, startY; private int endX, endY; private List<int[]> path; @Override public void start(Stage primaryStage) throws Exception { maze = generateMaze(); findPath(); BorderPane root = new BorderPane(); Canvas canvas = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT); GraphicsContext gc = canvas.getGraphicsContext2D(); drawMaze(gc); drawPath(gc); root.setCenter(canvas); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); } private int[][] generateMaze() { int[][] maze = new int[MAZE_SIZE][MAZE_SIZE]; Random random = new Random(); for (int i = 0; i < MAZE_SIZE; i++) { Arrays.fill(maze[i], 1); } int startX = random.nextInt(MAZE_SIZE); int startY = random.nextInt(MAZE_SIZE); maze[startX][startY] = 0; List<int[]> walls = new ArrayList<>(); addWalls(walls, startX, startY); while (!walls.isEmpty()) { int[] wall = walls.remove(random.nextInt(walls.size())); int x = wall[0]; int y = wall[1]; int nx = wall[2]; int ny = wall[3]; if (maze[x][y] == maze[nx][ny]) { continue; } if (maze[x][y] == 0) { maze[nx][ny] = 0; } else { maze[x][y] = 0; } addWalls(walls, nx, ny); } this.startX = startX; this.startY = startY; this.endX = random.nextInt(MAZE_SIZE); this.endY = random.nextInt(MAZE_SIZE); maze[endX][endY] = 0; return maze; } private void addWalls(List<int[]> walls, int x, int y) { if (x > 0) { walls.add(new int[]{x, y, x - 1, y}); } if (y > 0) { walls.add(new int[]{x, y, x, y - 1}); } if (x < MAZE_SIZE - 1) { walls.add(new int[]{x, y, x + 1, y}); } if (y < MAZE_SIZE - 1) { walls.add(new int[]{x, y, x, y + 1}); } } private void findPath() { boolean[][] visited = new boolean[MAZE_SIZE][MAZE_SIZE]; int[][] distance = new int[MAZE_SIZE][MAZE_SIZE]; int[][] prevX = new int[MAZE_SIZE][MAZE_SIZE]; int[][] prevY = new int[MAZE_SIZE][MAZE_SIZE]; for (int i = 0; i < MAZE_SIZE; i++) { Arrays.fill(visited[i], false); Arrays.fill(distance[i], Integer.MAX_VALUE); } distance[startX][startY] = 0; PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> distance[o[0]][o[1]])); queue.offer(new int[]{startX, startY}); while (!queue.isEmpty()) { int[] cell = queue.poll(); int x = cell[0]; int y = cell[1]; if (visited[x][y]) { continue; } visited[x][y] = true; if (x == endX && y == endY) { break; } if (x > 0 && maze[x - 1][y] == 0 && !visited[x - 1][y]) { int d = distance[x][y] + 1; if (d < distance[x - 1][y]) { distance[x - 1][y] = d; prevX[x - 1][y] = x; prevY[x - 1][y] = y; queue.offer(new int[]{x - 1, y}); } } if (y > 0 && maze[x][y - 1] == 0 && !visited[x][y - 1]) { int d = distance[x][y] + 1; if (d < distance[x][y - 1]) { distance[x][y - 1] = d; prevX[x][y - 1] = x; prevY[x][y - 1] = y; queue.offer(new int[]{x, y - 1}); } } if (x < MAZE_SIZE - 1 && maze[x + 1][y] == 0 && !visited[x + 1][y]) { int d = distance[x][y] + 1; if (d < distance[x + 1][y]) { distance[x + 1][y] = d; prevX[x + 1][y] = x; prevY[x + 1][y] = y; queue.offer(new int[]{x + 1, y}); } } if (y < MAZE_SIZE - 1 && maze[x][y + 1] == 0 && !visited[x][y + 1]) { int d = distance[x][y] + 1; if (d < distance[x][y + 1]) { distance[x][y + 1] = d; prevX[x][y + 1] = x; prevY[x][y + 1] = y; queue.offer(new int[]{x, y + 1}); } } } path = new ArrayList<>(); int x = endX; int y = endY; while (x != startX || y != startY) { path.add(new int[]{x, y}); int nx = prevX[x][y]; int ny = prevY[x][y]; x = nx; y = ny; } path.add(new int[]{startX, startY}); Collections.reverse(path); } private void drawMaze(GraphicsContext gc) { gc.setFill(WALL_COLOR); gc.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); gc.setFill(CELL_COLOR); for (int i = 0; i < MAZE_SIZE; i++) { for (int j = 0; j < MAZE_SIZE; j++) { if (maze[i][j] == 0) { gc.fillRect(j * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, i * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, CELL_SIZE, CELL_SIZE); } } } gc.setFill(START_COLOR); gc.fillOval(startY * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, startX * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, CELL_SIZE, CELL_SIZE); gc.setFill(END_COLOR); gc.fillOval(endY * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, endX * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, CELL_SIZE, CELL_SIZE); } private void drawPath(GraphicsContext gc) { gc.setFill(PATH_COLOR); for (int i = 0; i < path.size() - 1; i++) { int[] cell1 = path.get(i); int[] cell2 = path.get(i + 1); gc.strokeLine(cell1[1] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2, cell1[0] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2, cell2[1] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2, cell2[0] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2); } } public static void main(String[] args) { launch(args); } } ``` 这个程序中,我们使用generateMaze方法和Prim算法来生成迷宫,并使用findPath方法和Dijkstra算法来找到最短路径。我们使用JavaFX的Canvas来绘制迷宫和路径,并使用BorderPane来布局界面。在drawMaze方法中,我们首先使用黑色填充整个画布,然后使用白色填充迷宫的单元格。在drawPath方法中,我们使用蓝色绘制最短路径。 运行这个程序,你可以看到一个自动生成迷宫并找到最短路径的演示。你可以根据自己的需求,修改这个程序,例如修改迷宫大小、单元格大小、墙壁大小、颜色等,以及使用其他算法来生成迷宫和找到路径。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值