当前位置: 首页 > news >正文

网站建设深圳赶集网东莞网络推广

网站建设深圳赶集网,东莞网络推广,电子印章手机在线制作软件,php网站开发用什么php目录 1、Vue实现2、Java实现 2048 游戏是一个基于网格的数字益智游戏,玩家需要通过滑动相同的数字来合并它们,并最终得到一个值为 2048 的方块。以下是分别用Vue和Java来实现的 2048 游戏,包含运行效果。 1、Vue实现 首先,创建一…

目录

  • 1、Vue实现
  • 2、Java实现

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
http://www.yayakq.cn/news/444397/

相关文章:

  • 小型网站二手房中介网站模板
  • 报电子商务( 网站建设与运营)网站设计师大学学什么专业
  • 如何制作互联网网站网站开发项目运营经理岗位职责
  • 网站注册界面响应式网页需要设计几张图
  • 潜江市网站网站维护和建设工作范围
  • 个人网站如何在工信部备案如何查询网站备案时间
  • 做直播导航网站有哪些网站规划建设方案
  • 传奇发布网站排行wordpress去掉模板登录
  • openwrt做网站网站建设领导讲话稿
  • 网站类型定义公众号开发价格多少
  • 网站开发的就业前景如何网站研发进度表下载
  • 做酒水网站陕西有哪些合肥网站建设哪家好
  • 邯郸的互联网公司太原网站优化技术
  • 网站建设首选定制开发下载做网站ftp具体步骤
  • 莱芜金点子信息港官网宁波seo整站优化软件
  • 网站建设找宙斯站长工具html家乡网站设计
  • 网站跳出率太高2017wordpress整站源码
  • 建设英文网站南谯区城乡建设局网站
  • 网站开发配置管理计划sqlite 网站开发
  • 语文建设网站idmd设计
  • 青岛网站运营推广Dw制作个人网站
  • 宁德市住房和城乡建设局网站简历制作网站免费
  • 寻找做网站的公司云系统网站建设合同
  • 网站建设如何定价wordpress博客模板安装失败
  • 淘客优惠券推广网站怎么做漳州 网站建设公司
  • 福州 网站设计手机网站建设公司联系电话
  • 梧州推广网站服务商网络产品服务的提供者不得设置
  • php网站建设流程图专业企专业企业网站设计
  • itc 做市场分析的网站怎么建设淘宝客网站
  • 诸城网站制作网站建设需要些什么