private bool _ChessResult(Chess chess, PlayChess result) { bool over = false; //操作成功 result.Suc = true; switch (chess) { case Chess.Null: //操作失败 result.Suc = false; break; case Chess.None: result.Challenger = Chess.None; break; case Chess.Black: over = true; result.Challenger = Chess.Black; break; case Chess.White: over = true; result.Challenger = Chess.White; break; case Chess.Draw: over = true; result.Challenger = Chess.Draw; break; } return(over); }
private void _PlayChess(byte[] data) { PlayChess result = NetworkUtils.Deserialize <PlayChess>(data); if (!result.Suc) { Info.Instance.Print("下棋操作失败"); return; } switch (result.Challenger) { case Chess.None: break; case Chess.Black: NetworkPlayer.Instance.OnPlayingChange(false); Info.Instance.Print("黑棋胜利"); break; case Chess.White: NetworkPlayer.Instance.OnPlayingChange(false); Info.Instance.Print("白棋胜利"); break; case Chess.Draw: NetworkPlayer.Instance.OnPlayingChange(false); Info.Instance.Print("平局"); break; } //实例化棋子 NetworkGameplay.Instance.InstChess(result.Chess, new Vec2(result.X, result.Y)); }
/// <summary> /// 下棋请求 /// </summary> public void PlayChessRequest(int roomId) { //进行棋盘操作检测 Vec2 pos = NetworkGameplay.Instance.PlayChess(); if (pos.X == -1) { return; } PlayChess request = new PlayChess(); request.RoomId = roomId; request.Chess = NetworkPlayer.Instance.Chess; request.X = pos.X; request.Y = pos.Y; byte[] data = NetworkUtils.Serialize(request); NetworkClient.Enqueue(MessageType.PlayChess, data); }
private void _PlayChess(Player player, byte[] data) { //结果 PlayChess result = new PlayChess(); PlayChess receive = NetworkUtils.Deserialize <PlayChess>(data); //逻辑检测(有该房间) if (Server.Rooms.ContainsKey(receive.RoomId)) { //该房间中的玩家有资格下棋 if (Server.Rooms[receive.RoomId].Players.Contains(player) && Server.Rooms[receive.RoomId].State == Room.RoomState.Gaming && receive.Chess == Server.Rooms[receive.RoomId].GamePlay.Turn) { //判断结果 Chess chess = Server.Rooms[receive.RoomId].GamePlay.Calculate(receive.X, receive.Y); //检测操作:如果游戏结束 bool over = _ChessResult(chess, result); if (result.Suc) { result.Chess = receive.Chess; result.X = receive.X; result.Y = receive.Y; Console.WriteLine($"玩家:{player.Name}下棋成功"); //向该房间中玩家与观察者广播结果 data = NetworkUtils.Serialize(result); foreach (var each in Server.Rooms[receive.RoomId].Players) { each.Send(MessageType.PlayChess, data); } foreach (var each in Server.Rooms[receive.RoomId].OBs) { each.Send(MessageType.PlayChess, data); } if (over) { Console.WriteLine("游戏结束,房间关闭"); Server.Rooms[receive.RoomId].Close(); } } else { Console.WriteLine($"玩家:{player.Name}下棋失败"); //向玩家发送失败操作结果 data = NetworkUtils.Serialize(result); player.Send(MessageType.PlayChess, data); } } else { Console.WriteLine($"玩家:{player.Name}下棋失败"); //向玩家发送失败操作结果 data = NetworkUtils.Serialize(result); player.Send(MessageType.PlayChess, data); } } else { Console.WriteLine($"玩家:{player.Name}下棋失败"); //向玩家发送失败操作结果 data = NetworkUtils.Serialize(result); player.Send(MessageType.PlayChess, data); } }