package com.zyx.server.utils;
import com.alibaba.fastjson2.JSONObject;
import com.zyx.pojo.Review;
import com.zyx.pojo.Robot;
import com.zyx.pojo.User;
import com.zyx.server.WebSocketServer;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author 张宇森
* @version 1.0
* 存储游戏流程
*/
public class Game extends Thread {
private final Integer rows;
private final Integer cols;
//地图刷新的随机障碍物数量
private final Integer ranWalls_count;
private final int[][] map;
private final static int[] dx = {-1,0,1,0}, dy = {0,1,0,-1};
private final Player playerA, playerB;
private Integer nextStepA = null;
private Integer nextStepB = null;
private ReentrantLock lock = new ReentrantLock();
private String status = "playing"; //playing -> finished
private String loser = ""; //AB A B;
private final static String addRobotUrl = "http://127.0.0.1:3003/robot/add";
public Game(Integer rows, Integer cols, Integer ranWalls_count, Integer idA, Robot aRobot, Integer idB, Robot bRobot) {
this.rows = rows;
this.cols = cols;
this.ranWalls_count = ranWalls_count;
this.map = new int[rows][cols];
Integer aRobotId = -1, bRobotId = -1;
String aRobotCode = "", bRobotCode = "";
if (aRobot != null) {
aRobotId = aRobot.getId();
aRobotCode = aRobot.getContent();
}
if (bRobot != null) {
bRobotId = bRobot.getId();
bRobotCode = bRobot.getContent();
}
playerA = new Player(idA,aRobotId,aRobotCode,this.rows-2,1,new ArrayList<>());
playerB = new Player(idB,bRobotId,bRobotCode,1,this.cols - 2, new ArrayList<>());
}
//返回地图
public int[][] getMap() {
return map;
}
public Player getPlayerA() {
return playerA;
}
public Player getPlayerB() {
return playerB;
}
public void setNextStepA(Integer nextStepA) {
lock.lock();
try {
this.nextStepA = nextStepA;
} finally {
lock.unlock();
}
}
public void setNextStepB(Integer nextStepB) {
lock.lock();
try {
this.nextStepB = nextStepB;
}finally {
lock.unlock();
}
}
private boolean is_connective(int sx, int sy, int tx, int ty) {
if (sx == tx && sy == ty) return true;
map[sx][sy] = 1;
for (int i = 0; i < 4; i ++){
int x = sx + dx[i], y = sy + dy[i];
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols && map[x][y] == 0){
if (is_connective(x,y,tx,ty)){
map[sx][sy] = 0;
return true;
}
}
}
map[sx][sy] = 0;
return false;
}
//画地图
private boolean draw() {
for (int i = 0; i < this.rows; i ++) {
for (int j = 0; j < this.cols; j ++) {
map[i][j] = 0;
}
}
//给地图增加墙障碍物
for (int r = 0; r < this.rows; r ++) {
map[r][0] = map[r][this.cols-1] = 1;
}
for (int c = 0; c < this.cols; c ++) {
map[0][c] = map[this.rows-1][c] = 1;
}
Random random = new Random();
for (int i = 0; i < this.ranWalls_count / 2; i ++){
for (int j = 0; j < 1000; j ++) {
int r = random.nextInt(this.rows);
int c = random.nextInt(this.cols);
if (map[r][c] == 1 || map[this.rows-1-r][this.cols-1-c] == 1)
continue;
if ( r == this.rows - 2 && c == 1 || r == 1 && c == this.cols - 2 )
continue;
map[r][c] = map[this.rows-1-r][this.cols-1-c] = 1;
break;
}
}
return is_connective(this.rows-2,1,1,this.cols-2);
}
public String getMapToString() {
StringBuilder res = new StringBuilder();
for (int i = 0; i < rows; i ++){
for (int j = 0; j < cols; j ++){
res.append(map[i][j]);
}
}
return res.toString();
}
public void createMap() {
for (int i = 0; i < 1000; i ++) {
if (draw()) break;
}
}
//将当前局面信息编码为字符串
private String getInput(Player player) {
// 字符串 => 地图 # me.sx # me.sy # me操作 # ta.sx # ta.sy # you操作
Player me = null, you = null;
if (playerA.getId().equals(player.getId())){
me = playerA;
you = playerB;
} else {
me = playerB;
you = playerA;
}
return getMapToString() + "#" +
me.getSx() + "#" +
me.getSy() + "#(" +
me.getStepsToString() + ")#" +
you.getSx() + "#" +
you.getSy() + "#(" +
you.getStepsToString() + ")" ;
}
//是否向botSystem 传代码
private void sendBotCode(Player player) {
if (player.getRobotId().equals(-1)) return; //人出场不需要传代码
MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
data.add("user_id",player.getId().toString());
data.add("robot_code",player.getRobotCode());
data.add("input",getInput(player));
WebSocketServer.restTemplate.postForObject(addRobotUrl,data,String.class);
}
//等待两名玩家的下一步操作
private boolean nextStep() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
sendBotCode(playerA);
sendBotCode(playerB);
for (int i = 0; i < 50; i ++) {
try {
Thread.sleep(100);
lock.lock();
try {
if (nextStepA != null && nextStepB != null) {
playerA.getSteps().add(nextStepA);
playerB.getSteps().add(nextStepB);
return true;
}
} finally {
lock.unlock();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return false;
}
//判断蛇身体是否合法
private boolean check_valid(List<SnakeBody> snakeA, List<SnakeBody> snakeB) {
int n = snakeA.size();
SnakeBody snakeBody = snakeA.get(n - 1);
if (map[snakeBody.x][snakeBody.y] == 1) return false;
for (int i = 0; i < n-1; i ++) {
if (snakeA.get(i).x == snakeBody.x && snakeA.get(i).y == snakeBody.y)
return false;
}
for (int i = 0; i < n-1; i ++) {
if (snakeB.get(i).x == snakeBody.x && snakeB.get(i).y == snakeBody.y)
return false;
}
return true;
}
//判断两名玩家下一步操作是否合法
private void judge() {
List<SnakeBody> snakeAs = playerA.getSnakeBody();
List<SnakeBody> snakeBs = playerB.getSnakeBody();
boolean checkA = check_valid(snakeAs, snakeBs);
boolean checkB = check_valid(snakeBs, snakeAs);
if (!checkA || !checkB) {
status = "finished";
if (!checkA && !checkB) {
loser = "AB";
}else if (!checkA) {
loser = "A";
}else {
loser = "B";
}
}
}
//向每一个人广播信息
private void sendAllMessage(String message) {
if (WebSocketServer.users.get(playerA.getId()) != null) {
没有合适的资源?快使用搜索试试~ 我知道了~
基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip
共589个文件
xml:230个
class:127个
java:123个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 166 浏览量
2023-12-15
14:38:50
上传
评论
收藏 77.96MB ZIP 举报
温馨提示
【项目介绍】 基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip 基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip 【备注】 1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载食用体验! 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈!
资源推荐
资源详情
资源评论
收起资源包目录
基于springboot+redis+mysql的人机对抗平台源码(可借鉴学习).zip (589个子文件)
last-build.bin 1B
Game.class 11KB
WebSocketServer.class 8KB
Game.class 8KB
QQWebServiceImpl.class 7KB
Review.class 7KB
Review.class 7KB
WebSocketServer.class 7KB
Player.class 6KB
Robot.class 5KB
MatchingPool.class 5KB
SecurityConfig.class 5KB
Robot.class 5KB
SecurityConfig.class 5KB
Player.class 5KB
Consumer.class 4KB
User.class 4KB
UpdateRobotServiceImpl.class 4KB
Robot.class 4KB
UpdateRobotServiceImpl.class 4KB
AddRobotServiceImpl.class 4KB
GetReviewListServiceImpl.class 4KB
JwtUtil.class 3KB
JwtUtil.class 3KB
RegisterServiceImpl.class 3KB
User.class 3KB
JwtAuthenticationTokenFilter.class 3KB
RegisterServiceImpl.class 3KB
JwtAuthenticationTokenFilter.class 3KB
AddRobotServiceImpl.class 3KB
UserController.class 3KB
SecurityConfig.class 3KB
SecurityConfig.class 3KB
Player.class 3KB
DeleteRobotServiceImpl.class 3KB
DeleteRobotServiceImpl.class 3KB
HttpClientUtil.class 3KB
Robot.class 3KB
GetPeakListServiceImpl.class 3KB
UserDetailsImpl.class 2KB
UserDetailsImpl.class 2KB
LoginServiceImpl.class 2KB
LoginServiceImpl.class 2KB
Main.class 2KB
RobotPool.class 2KB
GetListRobotServiceImpl.class 2KB
GetListRobotServiceImpl.class 2KB
MatchingController.class 2KB
AccessConfig.class 2KB
AccessConfig.class 2KB
InfoServiceImpl.class 2KB
InfoServiceImpl.class 2KB
StartGameController.class 2KB
User$UserBuilder.class 2KB
ReceiveRobotMoveServiceImpl.class 2KB
ReceiveRobotMoveController.class 2KB
RobotRunningController.class 2KB
LoginController.class 2KB
LoginController.class 2KB
UserDetailsServiceImpl.class 2KB
UserDetailsServiceImpl.class 2KB
QQWebController.class 2KB
SnakeBody.class 2KB
SnakeBody.class 2KB
MatchingServiceImpl.class 2KB
GetReviewListController.class 2KB
User$UserBuilder.class 2KB
RegisterController.class 2KB
RegisterController.class 2KB
GetPeakListController.class 1KB
RobotRunningServiceImpl.class 1KB
GameserverApplicationTests.class 1KB
StartGameServiceImpl.class 1KB
DeleteRobotController.class 1KB
UpdateRobotController.class 1KB
UpdateRobotController.class 1KB
DeleteRobotController.class 1KB
AddRobotController.class 1KB
AddRobotController.class 1KB
Solution.class 1KB
MybatisConfig.class 1KB
GameserverApplicationTests.class 1KB
JwtAuthentication.class 1KB
JwtAuthentication.class 1KB
RobotRunningSystemApplication.class 930B
GetListRobotController.class 919B
GetListRobotController.class 915B
MatchingSystemApplication.class 907B
InfoController.class 887B
InfoController.class 883B
GameserverApplication.class 733B
GameserverApplication.class 733B
WebSocketConfig.class 702B
WebSocketConfig.class 702B
RestTemplateConfig.class 652B
RestTemplateConfig.class 652B
RestTemplateConfig.class 652B
BattleController.class 610B
Robot$SnakeBody.class 488B
RegisterService.class 443B
共 589 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
北航程序员小C
- 粉丝: 2243
- 资源: 1824
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- HM2305B-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 基于52单片机、ADC0832、LCD1602、两个74HC393和一个74HC08的频率测量计 不能用,请私我
- HM2302-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- python实战项目-学生成绩管理系统(基础版)
- 微信小程序源码 实现查公交 滴滴公交 app 源码下载
- HM2302E-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 基于C#图片相似度比较,感知哈希算法
- VR开发的概要介绍与分析
- 自动驾驶定位系列教程七:点云畸变补偿.pdf
- HM2302D-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功