分别用Vue和Java来实现的风靡一时的2048 游戏

简介: @[TOC](目录)2048 游戏是一个基于网格的数字益智游戏,玩家需要通过滑动相同的数字来合并它们,并最终得到一个值为 2048 的方块。以下是分别用Vue和Java来实现的 2048 游戏,包含运行效果。# 1、Vue实现首先,创建一个名为`Game.vue`的 Vue 单文件组件,代码如下:```html <template> <div class="game-container"> <div class="grid"> <div v-for="(row, rowIndex) in board" :key="rowIndex" class="c

@TOC
2048 游戏是一个基于网格的数字益智游戏,玩家需要通过滑动相同的数字来合并它们,并最终得到一个值为 2048 的方块。以下是分别用Vue和Java来实现的 2048 游戏,包含运行效果。

1、Vue实现

首先,创建一个名为Game.vue的 Vue 单文件组件,代码如下:

<template>  
 <div class="game-container">  
   <div class="grid">  
     <div v-for="(row, rowIndex) in board" :key="rowIndex" class="cell">  
       <div v-if="row.length">  
         <div v-for="(cell, colIndex) in row" :key="colIndex" :class="{ 'highlight': cell === current }">  
           {
  { cell }}  
         </div>  
       </div>  
     </div>  
   </div>  
   <div class="score">  
     <p>得分:{
  { score }}</p>  
   </div>  
   <button @click="newGame">重新开始</button>  
 </div>  
</template>
<script>  
export default {
      
 data() {
      
   return {
      
     board: [  
       [1, 1, 2, 2],  
       [3, 3],  
       [4, 4],  
       [4, 4],  
       [2, 2],  
       [1, 1, 3, 3],  
       [2, 2],  
       [4, 4],  
     ],  
     current: null,  
     score: 0,  
   };  
 },  
 methods: {
      
   move(direction) {
      
     if (direction === 'left' && this.current && this.current.leftCell) {
      
       this.current.leftCell = this.current.leftCell.left;  
       if (!this.current.leftCell) {
      
         this.current = null;  
       }  
     } else if (direction === 'right' && this.current && this.current.rightCell) {
      
       this.current.rightCell = this.current.rightCell.right;  
       if (!this.current.rightCell) {
      
         this.current = null;  
       }  
     }  
   },  
   newGame() {
      
     this.board = [  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
     ];  
     this.score = 0;  
     this.current = null;  
   },  
   slide() {
      
     if (this.current) {
      
       if (this.current.leftCell) {
      
         this.move('left');  
       } else if (this.current.rightCell) {
      
         this.move('right');  
       }  
     }  
   },  
 },  
};  
</script>
<style scoped>  
.game-container {
      
 width: 100%;  
 max-width: 800px;  
 margin: 0 auto;  
 padding: 20px;  
 border: 1px solid #ccc;  
 border-radius: 5px;  
}
.grid {
      
 display: flex;  
 flex-wrap: wrap;  
}
.cell {
      
 width: 40px;  
 height: 40px;  
 background-color: #f2f2f2;  
 display: flex;  
 justify-content: center;  
 align-items: center;  
 border-radius: 5px;  
 margin: 10px;  
}
.cell:hover {
      
 background-color: #ddd;  
}
.highlight {
      
 background-color: #ffc107;  
}
.score {
      
 margin-top: 20px;  
 font-size: 24px;  
 font-weight: bold;  
}
</style>

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

相关文章
|
3天前
|
前端开发 JavaScript Java
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
|
4天前
|
存储 数据可视化 前端开发
Echarts+vue+java+mysql实现数据可视化
Echarts+vue+java+mysql实现数据可视化
|
5天前
|
JavaScript 前端开发 Java
java项目的打包将vue放到.jar里面部署
java项目的打包将vue放到.jar里面部署
|
5天前
|
数据库连接
java+ssm+vue代码视频学习讲解
java+ssm+vue代码视频学习讲解
11 0
|
5天前
|
网络协议 Ubuntu Java
如何使用MCSM搭建我的世界Java版服务器并实现远程联机游戏
如何使用MCSM搭建我的世界Java版服务器并实现远程联机游戏
40 0
|
5天前
|
Java
手把手教你用java OOP实现猜拳游戏!好玩有用!
手把手教你用java OOP实现猜拳游戏!好玩有用!
20 3
手把手教你用java OOP实现猜拳游戏!好玩有用!
|
5天前
|
JavaScript Java 测试技术
基于Java的游戏虚拟道具交易网站的设计与实现(源码+lw+部署文档+讲解等)
基于Java的游戏虚拟道具交易网站的设计与实现(源码+lw+部署文档+讲解等)
31 0
|
5天前
|
网络协议 Java Windows
打造个人的Minecraft服务器:Java+cpolar实现我的世界联机游戏
打造个人的Minecraft服务器:Java+cpolar实现我的世界联机游戏
124 0
|
5天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
36 0
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
5天前
|
人工智能 监控 安全
Java+Spring Cloud +Vue+UniApp微服务智慧工地云平台源码
视频监控系统、人员实名制与分账制管理系统、车辆管理系统、环境监测系统、大型设备监测(龙门吊、塔吊、升降机、卸料平台等)、用电监测系统、基坑监测系统、AI算法分析(安全帽佩戴、火焰识别、周界报警、人员聚众报警、升降机超载报警)、安全培训、设备监测。
31 4