网页游戏五子棋(2) 五子棋实现
上一篇文章里写了三个基础的类,下面开始的是五子棋的实现,这里将会省略智能AI,留到最后来写
Dipper.WZQ.AI 机器人
Dipper.WZQ.Message 五子棋消息,相当于网络传输的数据包
![]()
Code
namespace Dipper.WZQ
{
public struct Message
{
private int _type;
//_type的参数 0 等待, 1 初始化, 2 开始, 3 胜利, 4 时间到, 5 离开, 6 打和, 9 下棋,
//数据包格式如[1,1,0,0] 前台根据此数据判定游戏状态
public int Type
{
get { return _type; }
}
private int _desktop;
public int Desktop
{
get { return _desktop; }
set { _desktop = value; }
}
private int _x;
public int X
{
get { return _x; }
}
private int _y;
public int Y
{
get { return _y; }
}
public Message(int type, int desktop, int x, int y)
{
this._type = type;
this._desktop = desktop;
this._x = x;
this._y = y;
}
public override string ToString()
{
return "[" + this._type + "," + this._desktop + "," + this._x + "," + this._y + "]";
}
}
}
Dipper.WZQ.Initialize 房间初始化类用来初始化游戏房间的,不过此类是可以大大改进的,我会列出现有方法和改进方法
namespace Dipper.WZQ
{
[Dipper.Games.DynamicList("在线五子棋", "/WZQ", 0)]
public class Initialize : Dipper.Games.iApplication //这是个空接口,用来定时清理相关资源用的
{
private int _roomId;
private Room _room;
private DateTime _startDateTime;
public DateTime StartDateTime
{
get { return _startDateTime; }
set { _startDateTime = value; }
}
public Room Room
{
get { return _room; }
}
public Initialize(int roomId)
{
if (roomId == -1)
{
this._room = null;
}
else
{
this._roomId = roomId;
if (HttpContext.Current.Application["WZQ_" + this._roomId + ""] == null)
{
this.Clear(); //这里判断application是否存在,如果不存在就初始化一个新房间
}
else
{
this._room = new Room((Room)HttpContext.Current.Application["WZQ_" + this._roomId + ""]);
}
}
}
public void Update()
{
HttpContext.Current.Application.UnLock();
HttpContext.Current.Application["WZQ_" + this._roomId + ""] = this._room;
HttpContext.Current.Application.Lock();
//这里用来把修改后的房间类保存到application
//由于我租的空间会定时清理内存,会删除application,可以考虑把房间类序列化之后保存到本地硬盘
//保存数据可以直接((room)application).RoomName="房间新名"的这种方式修改,不过可能不是很方便
}
public void Clear()
{
this._room = new Room(this._roomId);
this.Update();
}
}
}
Dipper.Games.Room
namespace Dipper.WZQ
{
public class Room : Dipper.Games.Room
{
private Message _roomMessage; //房间消息
private int[,] _roomBoard; //五子棋的棋盘,15*15的数组
private int _roomCount; //五子棋的棋子数,用来判定是否为平局
public int RoomCount
{
get { return _roomCount; }
set { _roomCount = value; }
}
public Message RoomMessage
{
get { return this._roomMessage; }
set { this._roomMessage = value; }
}
public int[,] RoomBoard
{
get { return _roomBoard; }
}
public Room(int roomId, string roomState, Message roomMessage, int[,] roomBoard, Player[] RoomPlayer)
: base(roomId, "五子棋 - 房间" + (roomId + 1), roomState, 2)
{
this._roomMessage = roomMessage;
this._roomBoard = roomBoard;
this._roomPlayer = RoomPlayer;
this._roomCount = 0;
}
public Room(int roomId)
: this(roomId, "开放中", new Message(0, 0, 0, 0), new int[15, 15], new Player[2])
{
this._roomBoard = this.BoardChess();
}
public Room(Room room)
: this(room.RoomId, room.RoomState, room.RoomMessage, room.RoomBoard, room.RoomPlayer)
{ }
public override int AddPlayer(Player player)
{
return base.AddPlayer(player);
}
public override void SubPlayer(int desktopId)
{
base.SubPlayer(desktopId);
}
public override void Team()
{
base.Team();
for (int i = 0; i < this._roomPlayer.Length; i++)
{
this._roomPlayer[i].UserTeam = i;
}
}
public void ReadyPlayer(int desktoppId)//玩家准备
{
this._roomPlayer[desktoppId].IsReady = true;
int first = this.First();
for (int i = 0; i < this._roomPlayer.Length; i++)
{
if (!this._roomPlayer[i].IsReady)//如果全部准备好,创建新的消息
{
this.RoomMessage = new Message(0, 0, 0, 0);
return;
}
else
{
if (i == first)//谁先开始
{
this._roomPlayer[i].IsFirst = true;
}
else
{
this._roomPlayer[i].IsFirst = false;
}
}
}
this.RoomMessage = new Message(1, first, 0, 0);
}
public void AutoPlayer(int desktopId)
{
Player player = new Player(0, "机器人", -1, true, false, 0, -1, -1, 60);
int _desktopId = this.AddPlayer(player);
this._roomPlayer[desktopId].MoneyAnte = 0;
this._roomPlayer[desktopId].IsReady = true;
this._roomPlayer[desktopId].IsAuto = true;
this._roomPlayer[_desktopId].IsFirst = true;
PlayChess(_desktopId, 7, 7);
this._roomMessage = new Dipper.WZQ.Message(2, _desktopId, 7, 7);
}
public void PlayChess(int desktopId, int x, int y)//下一步棋
{
this._roomBoard[x, y] = desktopId;
this._roomCount++;
}
public void WinChess(int desktopId)//胜局处理 {
if (this.RoomPlayer[desktopId].IsAuto)
{//如果是人机对战则删除机器人
this.SubPlayer(this.RoomPlayer[desktopId].UserNext);
this.RoomPlayer[desktopId].IsAuto = false;
}
else
{
try
{//加减赌注
this.RoomPlayer[this.RoomPlayer[desktopId].UserPrevious].SubMoney();
this.RoomPlayer[desktopId].AddMoney();
}
catch { }
}
for (int i = 0; i < this.RoomPlayer.Length; i++)
{//初始化玩家信息
if (this.RoomPlayer[i] != null)
{
this.RoomPlayer[i].IsReady = false;
this.RoomPlayer[i].UserTime = 60;
}
}
this._roomBoard = this.BoardChess();
}
public void DrawChess(int desktopId)//平局处理,类似与胜局处理
{
if (this.RoomPlayer[desktopId].IsAuto)
{
this.SubPlayer(this.RoomPlayer[desktopId].UserNext);
this.RoomPlayer[desktopId].IsAuto = false;
}
for (int i = 0; i < this.RoomPlayer.Length; i++)
{
if (this.RoomPlayer[i] != null)
{
this.RoomPlayer[i].IsReady = false;
this.RoomPlayer[i].UserTime = 60;
}
}
this._roomBoard = this.BoardChess();
}
public bool RuleChess(int desktopId)//判断玩家是否胜利
{
int[] count = new int[] { 0, 0, 0, 0 };
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
//y轴判断
if (this._roomBoard[i, j] == desktopId)
{ count[0]++; }
else { count[0] = 0; }
if (count[0] == 5) { return true; }
//x轴判断
if (i == 0)
{
for (int t = 0; t < 15; t++)
{
if (this._roomBoard[t, j] == desktopId) { count[1]++; }
else { count[1] = 0; }
if (count[1] >= 5) { return true; }
}
}
//斜下轴判断
if ((i == 0 && j <= 10) || (j == 0 && i <= 10))
{
for (int t = 0; t < 15; t++)
{
if (i + t < 15 && j + t < 15)
{
if (this._roomBoard[i + t, j + t] == desktopId) { count[2]++; }
else { count[2] = 0; }
if (count[2] >= 5) { return true; }
}
else
{ break; }
}
}
//斜上轴判断
if ((j == 0 && i >= 4) || (j <= 10 && i == 14))
{
for (var t = 0; t < 15; t++)
{
if (i - t > 0 && j + t < 15)
{
if (this._roomBoard[i - t, j + t] == desktopId) { count[3]++; }
else { count[3] = 0; }
if (count[3] >= 5) { return true; }
}
else
{ break; }
}
}
}
}
return false;
}
public int[,] BoardChess()//初始化棋盘
{
int[,] board = new int[15, 15];
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
board[i, j] = -1;
}
}
return board;
}
}
}
浙公网安备 33010602011771号