php workman游戏,利用workerman+html开发简单的在线五子棋对战

一、workerman下载,地址:https://github.com/walkor/workerman

二、cd到workerman项目根目录下,在workerman根目录下新建game.php,然后编写五子棋服务端交互代码,最后命令启动已经写好的五子棋服务器

sudo php game.php  start

game.php的代码如下:

use Workerman\Worker;

require_once __DIR__ . '/Autoloader.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' => 'player' . $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'

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值