基本信息
源码名称:C# 网络对战 五子棋游戏wpf源码(支持聊天,亲测可用)
源码大小:1.09M
文件格式:.zip
开发语言:C#
更新时间:2018-06-24
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
wpf wcf实现
wpf wcf实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Service
{
/// <summary>处理游戏大厅中每个房间的玩家</summary>
public class GameTables
{
private const int max = 16; //棋盘网格最大的行列数
public const int None = -1; //无棋子
public const int Black = 0; //黑棋
public const int White = 1; //白棋
/// <summary>保存同一房间的两个玩家</summary>
public User[] players { get; set; }
/// <summary>棋盘,-1:无棋子,0:黑棋,1:白棋</summary>
private int[,] grid = new int[max, max];
/// <summary>下一步棋子颜色号(0:黑棋,1:白棋)</summary>
private int nextColor = 0;
public GameTables()
{
players = new User[2];
ResetGrid();
}
/// <summary>重置棋盘</summary>
public void ResetGrid()
{
for (int i = 0; i < max; i )
{
for (int j = 0; j < max; j )
{
grid[i, j] = None;
}
}
}
/// <summary>
/// 获取棋子落下后是否获胜
/// </summary>
public bool IsWin(int row, int col)
{
int x = 0, y = 0;
//与方格的第i,j交叉点向四个方向的连子数,依次是水平,垂直,左上右下,左下右上
int[] n = new int[4];
#region 检查水平同色棋子个数
n[0] = 1; //连子个数
x = row 1; //向右检查,前方棋子与row,col点不同时跳出循环
while (x < max)
{
if (grid[x, col] == grid[row, col]) { n[0] ; x ; } else { break; }
}
x = row - 1; //向左检查,前方棋子与row,col点不同时跳出循环
while (x >= 0)
{
if (grid[x, col] == grid[row, col]) { n[0] ; x--; } else { break; }
}
#endregion
#region 检查垂直同色棋子个数
n[1] = 1; //连子个数
y = col 1; //向右检查,前方棋子与row,col点不同时跳出循环
while (y < max)
{
if (grid[row, y] == grid[row, col]) { n[1] ; y ; } else { break; }
}
y = col - 1; //向上检查,前方棋子与row,col点不同时跳出循环
while (y >= 0)
{
if (grid[row, y] == grid[row, col]) { n[1] ; y--; } else { break; }
}
#endregion
#region 检查左上到右下同色棋子个数
n[2] = 1; //连子个数
x = row 1;
y = col 1;
while (x<max && y < max)
{
if (grid[x, y] == grid[row, col]) { n[2] ; x ; y ; } else { break; }
}
x = row - 1;
y = col - 1;
while (x>=0 && y >= 0)
{
if (grid[x, y] == grid[row, col]) { n[2] ; x--; y--; } else { break; }
}
#endregion
#region 检查左下到右上同色棋子个数
n[3] = 1; //连子个数
x = row - 1;
y = col 1;
while (x >=0 && y < max)
{
if (grid[x, y] == grid[row, col]) { n[3] ; x--; y ; } else { break; }
}
x = row 1;
y = col - 1;
while (x < max && y >= 0)
{
if (grid[x, y] == grid[row, col]) { n[3] ; x ; y--; } else { break; }
}
#endregion
//检查是否获胜
for (int k = 0; k < n.Length; k )
{
if (Math.Abs(n[k]) == 5) return true;
}
return false;
}
/// <summary>放置棋子。参数:行,列</summary>
public void SetGridDot(int i, int j)
{
grid[i, j] = nextColor;
players[0].callback.ShowSetDot(i, j, nextColor);
players[1].callback.ShowSetDot(i, j, nextColor);
if (IsWin(i, j))
{
players[0].IsStarted = false;
players[1].IsStarted = false;
string message = nextColor == 0 ? "黑方胜" : "白方胜";
players[0].callback.GameWin(message);
players[1].callback.GameWin(message);
this.ResetGrid();
}
else
{
nextColor = (nextColor 1) % 2;
}
}
}
}