网页游戏五子棋(4) 机器人实现
失业了,在家学习学习,个人智慧有限,冥思苦想写了个五子棋的网页游戏。水平有限,请各位多多见谅。游戏网址:http://lumin.xc01.51ym.com/
namespace Dipper.WZQ
{
#region Chess
public struct Chess
{
private int _power;
public int Power
{
get { return _power; }
set { _power = value; }
}
private int _count;
public int Count
{
get { return _count; }
set { _count = value; }
}
public string _style;
public string Style
{
get { return _style; }
set { _style = value; }
}
public Chess(int power)
{
_power = power;
_count = 0;
_style = string.Empty;
}
#region 运算符重载
public static int operator +(Chess power1, Chess power2)
{
return power1.Power + power2.Power;
}
public static int operator +(int power1, Chess power2)
{
return power1 + power2.Power;
}
public static int operator +(Chess power1, int power2)
{
return power1.Power + power2;
}
public static bool operator >(Chess power1, Chess power2)
{
if (power1.Power > power2.Power)
return true;
else
return false;
}
public static bool operator <(Chess power1, Chess power2)
{
if (power2.Power > power1.Power)
return true;
else
return false;
}
public static bool operator ==(Chess power1, int power2)
{
if (power1.Power == power2)
return true;
else
return false;
}
public static bool operator !=(Chess power1, int power2)
{
if (power1.Power != power2)
return true;
else
return false;
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
#endregion
}
#endregion
public class AI
{
private int[,] _WZQBoard;
private int _rival;
private int _me;
public AI(int[,] WZQBoard, int me, int next)
{
this._WZQBoard = WZQBoard;
this._rival = next;
this._me = me;
}
public List<int[]> Calculate(int[,] WZQBoard, int me, int rival)
{
List<int[]> power = new List<int[]>();
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (WZQBoard[i, j] == -1)
{
int power1 = Total(new Chess[] { this.XTotal(WZQBoard, i, j, me, rival), this.YTotal(WZQBoard, i, j, me, rival), this.DTotal(WZQBoard, i, j, me, rival), this.UTotal(WZQBoard, i, j, me, rival) });
int power2 = Total(new Chess[] { this.XTotal(WZQBoard, i, j, rival, me), this.YTotal(WZQBoard, i, j, rival, me), this.DTotal(WZQBoard, i, j, rival, me), this.UTotal(WZQBoard, i, j, rival, me) });
if (power1 + power2 > 0) { power.Add(new int[] { (power1 + power2), i, j }); }
}
}
}
power.Sort(delegate(int[] a, int[] b)
{
if (a[0] == b[0]) { return 0; } else if (a[0] > b[0]) { return -1; } else { return 1; }
});
return power;
}
public int Total(Chess[] power)
{
int total = 0;
for (int i = 0; i < power.Length; i++)
{
total += power[i].Power;
}
return total;
}
#region Power
public int Power(Chess chess)
{
int power = 0;
if (chess.Count == 5) { power = 438; }
else if (chess.Count == 4)
{
switch (chess.Style)
{
case "-@@@@--":
case "--@@@@-":
case "-@@@@-*":
case "*-@@@@-":
power = 219;
break;
case "-@@@-@-":
case "-@@-@@-":
case "-@-@@@-":
power = 83;
break;
case "*@-@@@-":
case "*@@-@@-":
case "*@@@-@-":
case "*@@@@--":
case "-@@@-@*":
case "-@@-@@*":
case "-@-@@@*":
case "--@@@@*":
case "*-@@@@*":
case "*@-@@@*":
case "*@@-@@*":
case "*@@@-@*":
case "*@@@@-*":
power = 73;
break;
}
}
else if (chess.Count == 3)
{
switch (chess.Style)
{
case "--@@@--":
case "-@@@---":
case "---@@@-":
case "--@@@-*":
case "-@@@--*":
case "*-@@@--":
case "*--@@@-":
power = 73;
break;
case "*-@@@-*":
case "*-@-@@-":
case "-@-@@-*":
case "-@@-@-*":
case "*-@@-@-":
power = 59;
break;
case "*@@@--*":
case "*@@-@--":
case "*@@--@-":
case "*@-@@--":
case "*@--@@-":
case "---@@@*":
case "--@-@@*":
case "-@--@@*":
case "--@@-@*":
case "-@@--@*":
case "*@-@-@-":
case "-@-@-@*":
power = 46;
break;
case "*@-@@-*":
case "*@--@@*":
case "*-@@-@*":
case "*@@--@*":
case "*-@-@@*":
case "*@@-@-*":
case "*@-@-@*":
power = 33;
break;
}
}
else if (chess.Count == 2)
{
switch (chess.Style)
{
case "*-@@--*":
case "*--@@-*":
case "*-@-@-*":
case "-@@---*":
case "--@@--*":
case "---@@-*":
case "*---@@-":
case "*--@@--":
case "*-@@---":
case "*-@-@--":
case "*--@-@-":
case "*-@--@-":
case "--@-@-*":
case "-@-@--*":
case "-@--@-*":
case "---@-@-":
case "--@-@--":
case "-@-@---":
case "----@@-":
case "---@@--":
case "--@@---":
case "-@@----":
power = 32;
break;
case "*@@---*":
case "*@-@--*":
case "*@--@-*":
case "*---@@*":
case "*--@-@*":
case "*-@--@*":
case "*@---@*":
power = 22;
break;
}
}
else if (chess.Count == 1)
{
switch (chess.Style)
{
case "*-@---*":
case "*--@--*":
case "*---@-*":
case "--@---*":
case "---@--*":
case "----@-*":
case "*-@----":
case "*--@---":
case "*---@--":
power = 11;
break;
case "*@----*":
case "*----@*":
power = 1;
break;
}
}
return power;
}
#endregion
#region X轴
public Chess XTotal(int[,] WZQBoard, int x, int y, int me, int rival)
{
WZQBoard[x, y] = me;
int start = y, end = y;
bool isFirst = true, isEnd = true;
//判断本轴用户可用的位置数
for (int i = 1; i < 5; i++)
{
//开始位置
try
{
if (isFirst && WZQBoard[x, y - i] != rival) { start--; }
else { isFirst = false; }
}
catch
{ isFirst = false; }
//结束位置
try
{
if (isEnd && WZQBoard[x, y + i] != rival) { end++; }
else { isEnd = false; }
}
catch
{ isEnd = false; }
}
//建立本轴个位置数组,建立大小为5的滑动窗口,找出本轴以本点位置为中心的最大权值
Chess total = new Chess(0);
if ((end - start) >= 4)
{
Chess[] count = new Chess[end - start - 3];
for (int i = start; i <= end - 4; i++)
{
count[i - start] = new Chess(0);
if (i == start && WZQBoard[x, i] != -1)
{ count[i - start].Style += "*"; }
else
{ count[i - start].Style += "-"; }
for (int j = 0; j < 5; j++)
{
if (WZQBoard[x, i + j] == me)
{
count[i - start].Style += "@";
count[i - start].Count++;
}
else
{count[i - start].Style += "-";}
}
if (i == end - 4 && WZQBoard[x, i + 4] != -1)
{
count[i - start].Style += "*";
}
else
{ count[i - start].Style += "-"; }
count[i - start].Power = this.Power(count[i - start]);
if (count[i - start] > total) { total = count[i - start]; }
}
}
else
{ total = new Chess(0); }
WZQBoard[x, y] = -1;
return total;
}
#endregion
#region Y轴
public Chess YTotal(int[,] WZQBoard, int x, int y, int me, int rival)
{
WZQBoard[x, y] = me;
int start = x, end = x;
bool isStart = true, isEnd = true;
for (int i = 1; i < 5; i++)
{
try
{
if (isStart && WZQBoard[x - i, y] != rival) { start--; }
else { isStart = false; }
}
catch
{ isStart = false; }
try
{
if (isEnd && WZQBoard[x + i, y] != rival) { end++; }
else { isEnd = false; }
}
catch
{ isEnd = false; }
}
Chess total = new Chess(0);
if ((end - start) >= 4)
{
Chess[] count = new Chess[end - start - 3];
for (int i = start; i <= end - 4; i++)
{
count[i - start] = new Chess(0);
if (i == start && WZQBoard[i, y] != -1)
{ count[i - start].Style += "*"; }
else
{ count[i - start].Style += "-"; }
for (int j = 0; j < 5; j++)
{
if (WZQBoard[i + j, y] == me)
{
count[i - start].Style += "@";
count[i - start].Count++;
}
else
{ count[i - start].Style += "-"; }
}
if (i == end - 4 && WZQBoard[i + 4, y] != -1)
{ count[i - start].Style += "*"; }
else
{ count[i - start].Style += "-"; }
count[i - start].Power = this.Power(count[i - start]);
if (count[i - start] > total) { total = count[i - start]; }
}
}
else
{ total = new Chess(0); }
WZQBoard[x, y] = -1;
return total;
}
#endregion
#region D轴
public Chess DTotal(int[,] WZQBoard, int x, int y, int me, int rival)
{
WZQBoard[x, y] = me;
int start = 0, end = 0;
bool isStart = true, isEnd = true;
for (int i = 1; i < 5; i++)
{
try
{
if (isStart && WZQBoard[x - i, y - i] != rival) { start++; }
else { isStart = false; }
}
catch
{ isStart = false; }
try
{
if (isEnd && WZQBoard[x + i, y + i] != rival) { end++; }
else { isEnd = false; }
}
catch
{ isEnd = false; }
}
Chess total = new Chess(0);
if ((end + start) >= 4)
{
Chess[] count = new Chess[end + start - 3];
for (int i = 0; i <= (end + start) - 4; i++)
{
count[i] = new Chess(0);
if (i == 0 && WZQBoard[x - start + i, y - start + i] != -1)
{ count[i].Style += "*"; }
else
{ count[i].Style += "-"; }
for (int j = 0; j < 5; j++)
{
if (WZQBoard[x - start + i + j, y - start + i + j] == me)
{
count[i].Style += "@";
count[i].Count++;
}
else
{ count[i].Style += "-"; }
}
if (i == end - 4 && WZQBoard[x - start + i + 4, y - start + i + 4] != -1)
{ count[i].Style += "*"; }
else
{ count[i].Style += "-"; }
count[i].Power = this.Power(count[i]);
if (count[i] > total) { total = count[i]; }
}
}
else
{ total = new Chess(0); }
WZQBoard[x, y] = -1;
return total;
}
#endregion
#region U轴
public Chess UTotal(int[,] WZQBoard, int x, int y, int me, int rival)
{
WZQBoard[x, y] = me;
int start = 0, end = 0;
bool isStart = true, isEnd = true;
for (int i = 1; i < 5; i++)
{
try
{
if (isStart && WZQBoard[x + i, y - i] != rival) { start++; }
else { isStart = false; }
}
catch
{ isStart = false; }
try
{
if (isEnd && WZQBoard[x - i, y + i] != rival) { end++; }
else { isEnd = false; }
}
catch
{ isEnd = false; }
}
Chess total = new Chess(0);
if ((end + start) >= 4)
{
Chess[] count = new Chess[end + start - 3];
for (int i = 0; i <= (end + start) - 4; i++)
{
count[i] = new Chess(0);
if (i == 0 && WZQBoard[x + start - i, y - start + i] != -1)
{ count[i].Style += "*"; }
else
{ count[i].Style += "-"; }
for (int j = 0; j < 5; j++)
{
if (WZQBoard[x + start - i - j, y - start + i + j] == me)
{
count[i].Style += "@";
count[i].Count++;
}
else
{ count[i].Style += "-"; }
}
if (i == (end + start) - 4 && WZQBoard[x + start - i - 4, y - start + i + 4] != -1)
{ count[i].Style += "*"; }
else
{ count[i].Style += "-"; }
count[i].Power = this.Power(count[i]);
if (count[i] > total) { total = count[i]; }
}
}
else
{ total = new Chess(0); }
WZQBoard[x, y] = -1;
return total;
}
#endregion
public Message Chess()
{
List<int[]> power = Calculate(_WZQBoard, _me, _rival);
int[] total = power[0];
int count = 0;
foreach (int[] item in power) { if (item[0] == total[0]) { count++; } }
int index = new Random().Next(count);
return new Message(9, this._rival, power[index][1], power[index][2]);
}
}
}
浙公网安备 33010602011771号