{{ cell }}
分别用Vue和Java来实现的风靡一时的2048 游戏
短信预约 -IT技能 免费直播动态提醒
2048 游戏是一个基于网格的数字益智游戏,玩家需要通过滑动相同的数字来合并它们,并最终得到一个值为 2048 的方块。以下是分别用Vue和Java来实现的 2048 游戏,包含运行效果。
1、Vue实现
首先,创建一个名为Game.vue
的 Vue 单文件组件,代码如下:
得分:{{ score }}
2、Java实现
import java.util.*; import java.util.concurrent.ThreadLocal;public class 2048Game { private static int BOARD_SIZE = 4; private static int[][] board = new int[BOARD_SIZE][BOARD_SIZE]; private static int current = 0; private static int score = 0; public static void main(String[] args) { new ThreadLocal<2048Game>().set(new 2048Game()); } private 2048Game() { reset(); } public void reset() { board = new int[BOARD_SIZE][BOARD_SIZE]; generateBoard(); current = 0; score = 0; } private void generateBoard() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { board[i][j] = Math.floor(Math.random() * 4) + 1; } } } public void slide(int direction) { if (direction == 0 || direction == 1) { for (int i = 0; i < board.length; i++) { int[] temp = board[i]; int j = 0; for (int k = 0; k < temp.length; k++) { if (temp[k]!= 0) { while (j < temp.length - 1 && temp[j + 1] == temp[k]) { temp[j] += temp[j + 1]; j++; } } temp[j] = k; j++; } board[i] = temp; } } else if (direction == 2 || direction == 3) { for (int i = 0; i < board.length; i++) { int[] temp = board[i]; int k = 0; for (int j = 0; j < temp.length; j++) { if (temp[j]!= 0) { while (k < temp.length - 1 && temp[k + 1] == temp[j]) { temp[k] += temp[k + 1]; k++; } } temp[k] = j; k++; } board[i] = temp; } } } public void printBoard() { System.out.println("当前分数:" + score); for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } public void checkWin() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if (board[i][j] == 0) { return; } if (j < board[i].length - 1 && board[i][j] == board[i][j + 1]) { int sum = board[i][j] + board[i][j + 1]; board[i][j] = 0; board[i][j + 1] = 0; score += sum; System.out.println("恭喜你赢得了 " + sum + " 分!"); reset(); } } } } }
运行效果:
当前分数:0
来源地址:https://blog.csdn.net/superdangbo/article/details/132230690
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341