该项目实现了一个博弈类贪吃蛇的游戏,同时还完成了基于玩家战斗力匹配玩家的微服务系统,实现了玩家亲自玩或者玩家的AI代码匹配游戏的双重模式
整体实现逻辑:
项目演示:
1.玩家互殴
2.人机对战(比较经典)
3.机器大战(比较经典)
4.回放/排行榜(看录像!)
5.首先注册、登录
6.创建机器人
部署安装需要的软件: 1.idea 2.node.js 3.vscode 4.MyBatisPlus 5.mysql
将项目克隆到本地,通过git clone命令即可,后端通过idea打开对应的pom.xml即可
后端启动成功
启动vue 安装依赖
需要的依赖就这些,缺了就装
前端出现这个就成功了
表格式可以直接导入我这个,不用一点点建立
create table bot
(
id int auto_increment
primary key,
user_id int not null comment '作者',
title varchar(255) not null comment '标题',
description varchar(300) null comment '简介',
content text not null comment '代码',
createtime datetime not null comment '创建时间',
modifytime datetime not null comment '更新时间'
)
row_format = DYNAMIC;
create table record
(
id int auto_increment
primary key,
a_id int not null comment '左下角用户id',
a_sx int not null comment '左下角起始横坐标',
a_sy int not null comment '左下角起始纵坐标',
b_id int not null comment '右上角用户id',
b_sx int not null comment '右上角用户横坐标',
b_sy int not null comment '右上角用户纵坐标',
a_steps varchar(1000) not null comment '左下角用户移动记录',
b_steps varchar(1000) not null comment '右上角用户移动记录',
map varchar(1000) not null comment '地图',
loser varchar(10) not null comment '输的用户',
createtime datetime not null comment '对局时间'
)
comment '对局记录' row_format = DYNAMIC;
create table user
(
id int auto_increment
primary key,
username varchar(255) not null comment '用户名',
password varchar(500) not null comment '密码',
photo varchar(1000) not null comment '头像',
rating int default 1500 not null comment '天梯分'
)
row_format = DYNAMIC;
通过谷歌和本地浏览器登陆两个用户来测试功能
成功!
贴一个bot自动玩的代码,可以直接粘贴:
package com.kob.botrunningsystem.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Bot implements java.util.function.Supplier<Integer> {
static class Cell {
public int x, y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
}
private boolean check_tail_increasing(int step) { // 检验当前回合,蛇的长度是否增加
if (step <= 10) return true;
return step % 3 == 1;
}
public List<Cell> getCells(int sx, int sy, String steps) {
steps = steps.substring(1, steps.length() - 1);
List<Cell> res = new ArrayList<>();
int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
int x = sx, y = sy;
int step = 0;
res.add(new Cell(x, y));
for (int i = 0; i < steps.length(); i ++ ) {
int d = steps.charAt(i) - '0';
x += dx[d];
y += dy[d];
res.add(new Cell(x, y));
if (!check_tail_increasing( ++ step)) {
res.remove(0);
}
}
return res;
}
public Integer nextMove(String input) {
String[] strs = input.split("#");
int[][] g = new int[13][14];
for (int i = 0, k = 0; i < 13; i ++ ) {
for (int j = 0; j < 14; j ++, k ++ ) {
if (strs[0].charAt(k) == '1') {
g[i][j] = 1;
}
}
}
int aSx = Integer.parseInt(strs[1]), aSy = Integer.parseInt(strs[2]);
int bSx = Integer.parseInt(strs[4]), bSy = Integer.parseInt(strs[5]);
List<Cell> aCells = getCells(aSx, aSy, strs[3]);
List<Cell> bCells = getCells(bSx, bSy, strs[6]);
for (Cell c: aCells) g[c.x][c.y] = 1;
for (Cell c: bCells) g[c.x][c.y] = 1;
int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
for (int i = 0; i < 4; i ++ ) {
int x = aCells.get(aCells.size() - 1).x + dx[i];
int y = aCells.get(aCells.size() - 1).y + dy[i];
if (x >= 0 && x < 13 && y >= 0 && y < 14 && g[x][y] == 0) {
return i;
}
}
return 0;
}
@Override
public Integer get() {
File file = new File("input.txt");
try {
Scanner sc = new Scanner(file);
return nextMove(sc.next());
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
快捷部署是因为我把docker环境都配好了,直接镜像启动就行了。
*docker镜像地址:https://kobdocker-1302817640.cos.ap-nanjing.myqcloud.com/kob_final.tar.gz
*连接你的服务器,解压kob_final.jar.zip
*安装docker,参考下面的第3步。
*启动docker镜像:docker load -i kob_final.jar
*docker run -p 20000:22 -p 443:443 -p 80:80 -p 3000:3000 -p 3001:3001 -p 3002:3002 -itd --name kob_server kob_final:1.0
*docker start kob_server # 启动容器
*docker attach kob_server # 进入创建的docker容器
*启动mysql
sudo service mysql start
*启动Nginx
sudo /etc/init.d/nginx start
*启动三个后端
分别到对应文件jar包处
用tmux开3个分屏启动
具体命令 java -jar ***.jar
祝你成功!
服务器太贵了,学生党穷,本项目建议,4M带宽左右,要有2G内存,不然起不来的,亲测!
#这个很简单,可以先在发起端vim 一个config文件
Host server
HostName:ip
User:。。。
Port:默认22
#通过ssh-copy-id server server即可
sudo apt-get update
sudo apt install tmux
# 上传祖传文件
官网:https://docs.docker.com/engine/install/ubuntu/
到这步就可以了
这里为什么要用docker呢,因为docker更方便移植,脱离平台局限性,可以理解为一个小的独立环境
scp /var/lib/acwing/docker/images/django_lesson_1_0.tar server_name: # 将镜像上传到自己租的云端服务器
ssh server_name # 登录自己的云端服务器
docker load -i django_lesson_1_0.tar # 将镜像加载到本地
docker run -p 20000:22 -p 443:443 -p 80:80 -p 3000:3000 -p 3001:3001 -p 3002:3002 -itd --name kob_server django_lesson:1.0
docker start kob_server # 启动容器
docker attach kob_server # 进入创建的docker容器
#值得一提的是这个镜像只有一些基本的配置文件影响不大的
镜像链接:
这里需要配置本地到springboot服务器的免密和服务器server到springboot服务器的免密登录。
更新:
sudo apt-get update
安装:
sudo apt-get install mysql-server
启动:
sudo service mysql start
设置root用户的密码:
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
创建kob的database:
导入我们项目的数据库表:/home/tomas/create_kob.sql
create table kob.bot
(
id int auto_increment
primary key,
user_id int not null comment '作者',
title varchar(255) not null comment '标题',
description varchar(300) null comment '简介',
content text not null comment '代码',
createtime datetime not null comment '创建时间',
modifytime datetime not null comment '更新时间'
)
row_format = DYNAMIC;
create table kob.record
(
id int auto_increment
primary key,
a_id int not null comment '左下角用户id',
a_sx int not null comment '左下角起始横坐标',
a_sy int not null comment '左下角起始纵坐标',
b_id int not null comment '右上角用户id',
b_sx int not null comment '右上角用户横坐标',
b_sy int not null comment '右上角用户纵坐标',
a_steps varchar(1000) not null comment '左下角用户移动记录',
b_steps varchar(1000) not null comment '右上角用户移动记录',
map varchar(1000) not null comment '地图',
loser varchar(10) not null comment '输的用户',
createtime datetime not null comment '对局时间'
)
comment '对局记录' row_format = DYNAMIC;
create table kob.user
(
id int auto_increment
primary key,
username varchar(255) not null comment '用户名',
password varchar(500) not null comment '密码',
photo varchar(1000) not null comment '头像',
rating int default 1500 not null comment '天梯分'
)
row_format = DYNAMIC;
执行source /home/tomas/create_kob.sql命令导入这个文件
sudo apt-get install openjdk-8-jdk
三个后端都要这么改。
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--这里写上main方法所在类的路径-->
<configuration>
<mainClass>com.kob.backend.BackendApplication</mainClass> #改成对应这个后端的
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
全局的pom.xml,也添加build,但是不添加jar。
双击打包:
通过scp将target下的jar文件传到springboot的docker服务器中。
创建项目kob
# 通过java -jar 对应的.jar文件启动文件
这里启动了3个。
后端到此结束。
在/etc/nginx文件里面配置nginx.conf
文件内容:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name app3374.acapp.acwing.com.cn;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
listen 443 ssl;
server_name app3374.acapp.acwing.com.cn;
ssl_certificate cert/acapp.pem;
ssl_certificate_key cert/acapp.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
charset utf-8;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
client_max_body_size 10M;
location /api {
proxy_pass http://127.0.0.1:3000;
}
location /websocket {
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 36000s;
}
location / {
root /home/acs/kob/web;
index index.html;
try_files $uri $uri/ /index.html;
}
location /acapp {
alias /home/acs/kob/acapp;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
add_header 'Access-Control-Max-Age' 86400;
add_header 'Content-Type' 'text/html; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'PUT') {
add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
}
}
具体需要根据自己的服务器修改。
通过sudo /etc/init.d/nginx start启动,当出现start表示成功了。
前端build一下
然后将生成的dist文件传到springboot的web路径下。
# 照着网上学的
# 放在/etc/nginx/nginx.conf文件中
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name app3374.acapp.acwing.com.cn;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
listen 443 ssl;
server_name app3374.acapp.acwing.com.cn;
ssl_certificate cert/acapp.pem;
ssl_certificate_key cert/acapp.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
charset utf-8;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
client_max_body_size 10M;
location /api {
proxy_pass http://127.0.0.1:3000;
}
location /websocket {
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 36000s;
}
location / {
root /home/acs/kob/web;
index index.html;
try_files $uri $uri/ /index.html;
}
location /acapp {
alias /home/acs/kob/acapp;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
add_header 'Access-Control-Max-Age' 86400;
add_header 'Content-Type' 'text/html; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'PUT') {
add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' 'https://www.acwing.com';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Amz-Date';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
}
}
启动sudo /etc/init.d/nginx start
启动截图:
网址:https://app3374.acapp.acwing.com.cn
最后打开网址就能成功访问了!!
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。