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