workman在线五子棋
一、下载安装workman,地址:https://github.com/walkor/workerman
composer require workerman/workerman
二、cd到workerman项目根目录下,在workerman根目录下新建game.php,然后编写五子棋服务端交互代码
<?php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
$global_uid = 0;
//棋盘大小,默认15行15列
$global_i = 15;
$global_j = 15;
// 当客户端连上来时分配uid
function handle_connection($connection)
{
global $text_worker, $global_uid, $global_i, $global_j;
// 为这个链接分配一个uid
$connection->uid = ++$global_uid;
$text_worker->uidConnections[$connection->uid] = array('cons' => $connection);
$text_worker->user_data[$connection->uid] = array('playing' => 0, 'name' => '玩家' . $connection->uid, 'qipan' => array(), 'type' => 0, 'move' => 0);
$json = array('status' => 1, 'msg' => '', 'data' => array());
$json['data']['name'] = $text_worker->user_data[$connection->uid]['name'];
$connection->send(json_encode($json)); //生成玩家昵称
echo "player {$connection->uid} connected!\n";
//分配对手
foreach ($text_worker->user_data as $k => $val) {
/*var_dump($val['playing']);*/
if ($val['playing'] == 0 && $k != $connection->uid) {
//初始化棋盘
$init_data = array();
for ($i = 0; $i <= $global_i; $i++) {
for ($j = 0; $j <= $global_j; $j++) {
$init_data[$i][$j] = 0;
}
}
$text_worker->user_data[$k]['qipan'] = $init_data;
$text_worker->user_data[$connection->uid]['qipan'] = $init_data;
$text_worker->user_data[$k]['playing'] = $connection->uid;
$text_worker->user_data[$connection->uid]['playing'] = $k;
//分配红黑方
$text_worker->user_data[$k]['type'] = 1;
$text_worker->user_data[$k]['move'] = 1;
$text_worker->user_data[$connection->uid]['type'] = 2;
$text_worker->user_data[$connection->uid]['move'] = 0;
$json = array('status' => 2, 'msg' => '初始化棋盘...', 'data' => array());
$json['data']['qipan'] = $init_data;
$text_worker->uidConnections[$k]['cons']->send("为你匹配到对手{$text_worker->user_data[$connection->uid]['name']}!");
$text_worker->uidConnections[$connection->uid]['cons']->send("为你匹配到对手{$val['name']}!");
break;
}
}
/*var_dump($text_worker->user_data);*/
}
// 当客户端发送消息过来时
function handle_message($connection, $data)
{
global $text_worker, $global_i, $global_j;
$data = json_decode($data, true);
if ($data['status'] == 2 && $text_worker->user_data[$connection->uid]['playing'] > 0 && $text_worker->user_data[$connection->uid]['move'] == 1) {
$my_uid = $connection->uid;
$your_uid = $text_worker->user_data[$my_uid]['playing'];
$qipan = $text_worker->user_data[$your_uid]['qipan'];
$press = explode('_', $data['data']);
array_shift($press);
if (!empty($press)) {
$press_val = $qipan[$press[0]][$press[1]];
if ($press_val != 0) {
return;
} else {
$qipan[$press[0]][$press[1]] = $text_worker->user_data[$my_uid]['type'];
$text_worker->user_data[$my_uid]['qipan'] = $qipan;
$text_worker->user_data[$your_uid]['qipan'] = $qipan;
$text_worker->user_data[$your_uid]['move'] = 1;
$text_worker->user_data[$my_uid]['move'] = 0;
$json = array('status' => 2, 'msg' => '', 'data' => array());
$json['data']['type'] = $text_worker->user_data[$my_uid]['type'];
$json['data']['press_i'] = $press[0];
$json['data']['press_j'] = $press[1];
$text_worker->uidConnections[$my_uid]['cons']->send(json_encode($json));
$text_worker->uidConnections[$your_uid]['cons']->send(json_encode($json));
$count = get_who_win($qipan, $press[0], $press[1], $text_worker->user_data[$my_uid]['type'], $global_i, $global_j);
file_put_contents('./qipan', json_encode($qipan));
if ($count >= 5) { //分出胜负
$json = array('status' => 3, 'msg' => $text_worker->user_data[$my_uid]['name'] . ' 获胜 !', 'data' => array());
$json['data']['type'] = $text_worker->user_data[$my_uid]['type'];
$text_worker->uidConnections[$my_uid]['cons']->send(json_encode($json));
$text_worker->uidConnections[$your_uid]['cons']->send(json_encode($json));
}
}
}
}
}
// 当客户端断开时,广播给所有客户端
function handle_close($connection)
{
global $text_worker;
/*foreach ($text_worker->connections as $conn) {
$conn->send("user[{$connection->uid}] logout");
}*/
}
/**
* 判断输赢
* $qipan 棋盘数据
* $i 下子时的横坐标
* $i 下子时的纵坐标
* $type 1黑方 2红方
* $global_i 棋盘边界值
* $global_j 棋盘边界值
*/
function get_who_win($qipan = array(), $i = -1, $j = -1, $type = 0, $global_i = 0, $global_j = 0)
{
$count = 0;
$temp_type = $type;
if (empty($qipan) || $i < 0 || $j < 0 || $type <= 0) {
return $count;
}
echo json_encode($qipan) . "\n";
echo 'i=' . $i . '|j=' . $j . '|type=' . $type . '|gi=' . $global_i . '|gj=' . $global_j . "\n";
/*左右上下的判断*/
$count = 1;
$a = array(
0 => array('index' => $j, 'border' => $global_j), //左右,
1 => array('index' => $i, 'border' => $global_i) //上下
);
for ($round = 0; $round <= 1; $round++) {
$mov1_num = 1;
$mov2_num = 1;
while (true) {
$mov1 = $a[$round]['index'] + $mov1_num;
$mov2 = $a[$round]['index'] - $mov2_num;
$temp_mov1 = $temp_mov2 = -1;
if ($mov1_num > 0) {
if ($round == 0 && $mov1 <= $global_j) {
$temp_mov1 = $qipan[$i][$mov1];
var_dump($i . ',' . $mov1 . ',' . $temp_mov1);
} elseif ($round == 1 && $mov1 <= $global_i) {
$temp_mov1 = $qipan[$mov1][$j];
}
if ($temp_mov1 == $temp_type) {
$count++;
var_dump('count=' . $count);
$mov1_num++;
} else {
$mov1_num = 0;
}
} else {
$mov1_num = 0;
}
if ($mov2 >= 0 && $mov2_num > 0) {
if ($round == 0) {
$temp_mov2 = $qipan[$i][$mov2];
var_dump($i . ',' . $mov2 . ',' . $temp_mov1);
} elseif ($round == 1) {
$temp_mov2 = $qipan[$mov2][$j];
}
if ($temp_mov2 == $temp_type) {
$count++;
$mov2_num++;
} else {
$mov2_num = 0;
}
} else {
$mov2_num = 0;
}
if ($count >= 5) {
return $count;
}
if (($mov1_num == 0 && $mov2_num == 0)) {
break;
}
}
}
/*斜角的判断*/
$count = 1;
for ($round = 0; $round <= 1; $round++) {
$mov1_num = 1;
$mov2_num = 1;
while (true) {
$temp_mov1 = $temp_mov2 = -1;
if (($i - $mov1_num) >= 0 && ($j - $mov1_num) >= 0 && ($j + $mov1_num) <= $global_j && $mov1_num > 0) {
if ($round == 0) {
$temp_mov1 = $qipan[$i - $mov1_num][$j + $mov1_num];
} elseif ($round == 1) {
$temp_mov1 = $qipan[$i - $mov1_num][$j - $mov1_num];
}
if ($temp_mov1 == $temp_type) {
$count++;
$mov1_num++;
} else {
$mov1_num = 0;
}
} else {
$mov1_num = 0;
}
if (($i + $mov2_num) <= $global_i && ($j - $mov2_num) >= 0 && ($j + $mov2_num) <= $global_j && $mov2_num > 0) {
if ($round == 0) {
$temp_mov2 = $qipan[$i + $mov2_num][$j - $mov2_num];
} elseif ($round == 1) {
$temp_mov2 = $qipan[$i + $mov2_num][$j + $mov2_num];
}
if ($temp_mov2 == $temp_type) {
$count++;
$mov2_num++;
} else {
$mov2_num = 0;
}
} else {
$mov2_num = 0;
}
if ($count >= 5) {
return $count;
}
if (($mov1_num == 0 && $mov2_num == 0)) {
break;
}
}
}
return $count;
}
// 创建一个文本协议的Worker监听2347接口
$text_worker = new Worker("websocket://0.0.0.0:2347");
// 只启动1个进程
$text_worker->count = 1;
$text_worker->uidConnections = array();
$text_worker->user_data = array();
$text_worker->onConnect = 'handle_connection';
$text_worker->onMessage = 'handle_message';
$text_worker->onClose = 'handle_close';
Worker::runAll();
三、编写前端代码,这里保存为index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<style type="text/css">
td{
width:30px;
height:30px;
border:1px solid #666;
}
table{
border-collapse: collapse;
border-spacing: 0;
}
</style>
</head>
<body>
<div class="player"></div>
<div id="demo"></div>
</body>
</html>
<script>
table = "<table>";
for (let i = 0; i <= 15; i++) {
table += "<tr>";
for (let j = 0; j <= 15; j++) {
table += "<td onclick='press_on(this)' id='div_" + i + "_" + j + "'>" + "</td>";
}
table += "</tr>";
}
table += "</table>";
$('#demo').html(table);
ws = new WebSocket("ws://127.0.0.1:2347");
ws.onopen = function () {
console.log("连接成功");
};
ws.onmessage = function (e) {
//alert("收到服务端的消息:" + e.data);
console.log(e.data);
var jsonobj = eval('(' + e.data + ')');
if (jsonobj.status == 1 && jsonobj.data.name != null) {//初始化名字
alert(jsonobj.data.name);
$('.player').html(jsonobj.data.name);
}
if (jsonobj.status == 2) {//
var type = jsonobj.data.type;
var press_i = jsonobj.data.press_i;
var press_j = jsonobj.data.press_j;
id = '#div_' + press_i + '_' + press_j;
$(id).unbind("click");
if (type == 1) {
$(id).html("<div style='background:#000000;width:30px;height:30px;border-radius:50%;'></div>");
}
if (type == 2) {
$(id).html("<div style='background:#ffbc00;width:30px;height:30px;border-radius:50%;'></div>");
}
}
if (jsonobj.status == 3) {//
var msg = jsonobj.msg;
var r = confirm(msg + ',是否从新开始?');
if (r) {
window.location.reload(true);
return;
} else {
return;
}
}
};
function press_on(value) {
var send = '{"status":2,"data":"' + value.id + '"}';
console.log(send);
ws.send(send);
}
</script>
四、运行
在命令行启动服务器php game.php
最后在浏览器地址访问index.html,进入游戏。
转载自https://blog.csdn.net/weixin_33971205/article/details/92266560
workman在线五子棋的更多相关文章
- jquery在线五子棋
在线五子棋试玩地址:http://keleyi.com/game/12/ 以下是完整代码,保存到html文件打开也可以玩: <!DOCTYPE html> <html> < ...
- js+html5双人五子棋(源码下载)
代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- aTool在线工具
在线HTTP POST/GET接口测试工具 - aTool在线工具 地址:http://www.atool.org/httptest.php在线接口测试工具接口测试是测试系统组件间接口的一种测试.接口 ...
- Web前端资源汇总
本文地址:http://www.cnblogs.com/jihua/p/webfront.html 网页特效库 2017新年快乐特效 CSS3+jQuery实现时钟插件 Html5入门实例" ...
- 仿腾讯QQ竖直滑动导航菜单
菜单就像qq软件的分组,鼠标经过自动显示相应组的内容. 效果体验网址:http://keleyi.com/a/bjad/nf86w2dv.htm 以下是源代码: <html> <he ...
- jQuery弹出窗口完整代码
jQuery弹出窗口完整代码 效果体验:http://keleyi.com/keleyi/phtml/jqtexiao/1.htm 1 <!DOCTYPE html PUBLIC "- ...
- WC 2018/CTSC 2018/APIO 2018 游记
(要写CTSC的时候才想起来没写WC2018,那就粗略回顾一下吧hhhhh) WC 2018(简略版): 大概和 一个宁夏和一个天津的大哥一个宿舍hhhh,字典序分宿舍真是奇妙. WC讲课真的不是人听 ...
- 从零开始完整开发基于websocket的在线对弈游戏【五子棋】,只用几十行代码完成全部逻辑。
五子棋是规则简单明了的策略型游戏,先形成五子连线者获胜.本课程习作采用两人在线对弈的方式进行比赛,拿着手机在上下班路上玩特别合适. 整个过程在众触低代码应用平台进行,使用表达式描述游戏逻辑(高度简化版 ...
- 自己写的HTML5 Canvas + Javascript五子棋
看到一些曾经只会灌水的网友,在学习了前端之后,已经能写出下载量几千几万的脚本.样式,帮助大众,成为受欢迎的人,感觉满羡慕的.我也想学会前端技术,变得受欢迎呀.于是心血来潮,开始学习前端知识,并写下了这 ...
- 完全自制的五子棋人机对战游戏(VC++实现)
五子棋工作文档 1说明: 这个程序在创建初期的时候是有一个写的比较乱的文档的,但是很可惜回学校的时候没有带回来……所以现在赶紧整理一下,不然再过一段时间就忘干净了. 最初这个程序是受老同学所托做的,一 ...
随机推荐
- AtCoder Beginner Contest 214 (D并查集,E反悔贪心,F公共子序列DP)
题目链接:Here ABC水题, D - Sum of Maximum Weights 上图中最大权 \(9\) 对答案的贡献是这条边两边的连通块的 size 的乘积再乘以 9 受到上面的启发,我们可 ...
- Django 对实体的增删改查样例
class UserInfo(models.Model): """ 人员信息 """ user_id = models.CharField( ...
- <vue 路由 1、路由的基本使用>
一. 项目创建 参考如下博客地址创建一个vue的项目 https://www.cnblogs.com/yclh/p/15356171.html vue学习笔记 二.环境搭建+项目创建 二. ...
- GDP折线图
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset=" ...
- Element UI Table合并行
Vue使用Element-ui Table 合并行,官方只是一个非常简单的合并例子,通常业务都是相同的某个字段进行合并. 效果图 代码实现 1.Table <el-table :data=&qu ...
- zookeeper 使用api 进行节点增删改查及实现简易的配置中心
本文为博主原创,未经允许不得转载: 目录: 1. 对 zookeeper 节点进行增删改查既配置acl 权限等 2.使用 zookeeper 实现一个简易的配置中心 1. 对 zookeeper 节 ...
- Java中内存四区
这里简要说明这四个区域通常用于存储的变量类型: 栈区(Stack): 存放局部变量.方法参数.返回地址等. 变量的生命周期与其所在的方法(函数)的调用周期一致. 堆区(Heap): 主要用于动态分配内 ...
- Linux-文件压缩-tar-gzip
- [转帖].NET Framework 中的传输层安全性 (TLS) 最佳做法
https://learn.microsoft.com/zh-cn/dotnet/framework/network-programming/tls 传输层安全性 (TLS) 协议是一个行业标准,旨在 ...
- [转帖]linux中批量多行缩进与添加空格
用vim打开修改python脚本的时候,将代码整体向后移动4个空格操作如下: ESC之后,ctrl+v进入多行行首选中模式 使用上下键进行上下移动,选中多行行首 shift+i,进入插入模式 连续敲击 ...