?? chinesechesscontrol.cs
字號:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Thinker;
namespace GameWorld.ChineseChess//由桃花島的js象棋游戲改造而來,改造的過程比較生硬,感覺上寫的不是很好,希望有人重寫
{
public partial class ChineseChessControl : UserControl
{
Request Request = Request.CreateInstance();
int RED = 2;
int BLACK = 1;
int GREY = 0;
int GENERAL = 7;
int GUARD = 6;
int STAFF = 5;
int HORSE = 4;
int CHARIOT = 3;
int CANNON = 2;
int SOLDIER = 1;
int BLANK = 0;
int REDSIDE = 0;
int BLACKSIDE = 1;
public ChessMan[,] situation = new ChessMan[9, 10];
private ControlCollection board
{
get
{
return this.Controls;
}
}
ChessBoard chessBoard = new ChessBoard();
public ChessMan selection;
public ChessMan ToPos;//移動的目標(biāo)位置
public bool end;
/// <summary>
/// 當(dāng)前走子的一方
/// </summary>
public int side;
/// <summary>
/// 棋盤布局,有紅方在下、黑方在下兩種
/// </summary>
public int BoardSide;
public ChineseChessControl()
{
InitializeComponent();
}
private string InitSetMap = "";
public void Init(string setMap)
{
if (setMap == "")
return;
this.InitSetMap = setMap;
this.board.Clear();
selection = null;
end = false;
SideColor sideColor = (SideColor)Enum.Parse(typeof(SideColor), setMap.Substring(0, 1));
SideColor sideToRun = (SideColor)Enum.Parse(typeof(SideColor), setMap.Substring(1, 1));
if (sideToRun == SideColor.Red)
{
side = RED;//該紅方走棋
}
else
{
side = BLACK;
}
if (sideColor == SideColor.Red)
{
BoardSide = REDSIDE;//紅方在下的棋盤布局
}
else
{
BoardSide = BLACKSIDE;//黑方在下的棋盤布局
}
situation = new ChessMan[9, 10];
int index = 2;
while (index < setMap.Length)
{
string str = setMap.Substring(index, 4);
int x = int.Parse(str.Substring(0, 1));
int y = 9 - int.Parse(str.Substring(1, 1));
int color = 2 - int.Parse(str.Substring(2, 1));
ChessmanType chessmanType = (ChessmanType)Enum.Parse(typeof(ChessmanType), str.Substring(3, 1));
switch (chessmanType)
{
case ChessmanType.HeaderCapital:
situation[x, y] = new General(color, x, y, BoardSide);
break;
case ChessmanType.Minister:
situation[x, y] = new Staff(color, x, y, BoardSide);
break;
case ChessmanType.Guard:
situation[x, y] = new Guard(color, x, y, BoardSide);
break;
case ChessmanType.Horse:
situation[x, y] = new Horse(color, x, y, BoardSide);
break;
case ChessmanType.General:
situation[x, y] = new Chariot(color, x, y, BoardSide);
break;
case ChessmanType.Gun:
situation[x, y] = new Cannon(color, x, y, BoardSide);
break;
case ChessmanType.Soldier:
situation[x, y] = new Soldier(color, x, y, BoardSide);
break;
}
this.board.Add(situation[x, y]);
index = index + 4;
}
}
public void moveChess(ChessMan from, ChessMan to)
{
//下面這段,不得已出此下策,吃子
for (int i = 0; i < this.board.Count; i++)
{
ChessMan pos = this.board[i] as ChessMan;
if ((pos.x == situation[to.x, to.y].x)
&& (pos.y == situation[to.x, to.y].y))
{
this.board.Remove(pos);
if (pos.kind == GENERAL)
{
this.end = true;
}
}
}
situation[to.x, to.y] = from;
situation[from.x, from.y] = null;
int x = from.x;
int y = from.y;
from.x = to.x;
from.y = to.y;
//from.left = to.left;
//from.top = to.top;
from.Top = to.Top;
from.Left = to.Left;
this.clearBoard();
this.side = 3 - this.side;
this.selection = null;
this.ToPos = null;
}
public void PreMoveChess(int x1, int y1,int x2,int y2)
{
if (this.Controls.Count == 0)
{
this.Init((this.FindForm() as MatchForm).SetMap);
}
for (int i = 0; i < this.board.Count; i++)//選中棋子
{
if ((((ChessMan)this.board[i]).x == x1)
&& (((ChessMan)this.board[i]).y == y1))
{
this.selection=(ChessMan)this.board[i];
break;
}
}
if (this.selection.canGo(x2, y2))
{
this.ToPos=this.addBlank(x2, y2);
}
}
public void clearBoard()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10; j++)
{
if (situation[i, j] != null)
{
if (situation[i, j].color == GREY)
{
this.board.Remove(situation[i, j]);
situation[i, j] = null;
}
else
{
situation[i, j].BorderStyle = BorderStyle.None;
situation[i, j].BackColor = Color.White;
situation[i, j].beAttack = false;
}
}
}
}
}
public ChessMan addBlank(int x, int y)
{
if (situation[x, y] == null)
{
situation[x, y] = new Blank(x, y, this.BoardSide);
this.board.Add(situation[x, y]);
}
else
{
situation[x, y].BorderStyle = BorderStyle.FixedSingle;
situation[x, y].beAttack = true;
}
return situation[x, y];
}
private void ChineseChessControl_Paint(object sender, PaintEventArgs e)
{
chessBoard.drawBoard(e.Graphics);
}
private MessageCenter MessageCenter = MessageCenter.CreateInstance();
private void ChineseChessControl_Load(object sender, EventArgs e)
{
}
void MessageCenter_MoveEvent(object sender, MoveEventArgs e)
{
if ((e.MatchId != Global.LoginUser.MatchId) || (e.SetId != Global.LoginUser.SetId))
return;
switch (e.MoveResult)
{
case MoveResult.Ok:
if ((this.selection != null) && (this.ToPos != null))
{
this.moveChess(this.selection, this.ToPos);
this.selection = null;
this.ToPos = null;
}
break;
case MoveResult.OpponentMove:
int x1 = e.X1;
int y1 = 9 - e.Y1;
int x2 = e.X2;
int y2 = 9 - e.Y2;
PreMoveChess( x1, y1, x2, y2);
this.moveChess(this.selection, this.ToPos);
this.situation[x2, y2].BackColor = Color.Green;
break;
case MoveResult.RedWin:
this.end = true;
break;
case MoveResult.BlackWin:
this.end = true;
break;
case MoveResult.Draw:
this.end = true;
break;
case MoveResult.Error:
break;
}
}
public void OnMoveChessEvent(MoveEventArgs e)
{
if ((e.MatchId != Global.LoginUser.MatchId) || (e.SetId != Global.LoginUser.SetId))
return;
switch (e.MoveResult)
{
case MoveResult.Ok:
if ((this.selection != null) && (this.ToPos != null))
{
this.moveChess(this.selection, this.ToPos);
this.selection = null;
this.ToPos = null;
}
break;
case MoveResult.OpponentMove:
int x1 = e.X1;
int y1 = 9 - e.Y1;
int x2 = e.X2;
int y2 = 9 - e.Y2;
PreMoveChess(x1, y1, x2, y2);
this.moveChess(this.selection, this.ToPos);
this.situation[x2, y2].BackColor = Color.Green;
break;
case MoveResult.RedWin:
this.end = true;
break;
case MoveResult.BlackWin:
this.end = true;
break;
case MoveResult.Draw:
this.end = true;
break;
case MoveResult.Error:
break;
}
}
public string GetSetMap()
{
string setMap = (this.side == RED) ? "0" : "1";
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10; j++)
{
if (this.situation[i, j] != null)
{
int x = this.situation[i, j].x;
int y = 9 - this.situation[i, j].y;
int color = 2 - this.situation[i, j].color;
int type = 7 - this.situation[i, j].kind;
setMap += x.ToString() + y.ToString() + color.ToString() + type.ToString();
}
}
}
return setMap;
}
public void AutoMove()
{
if (this.end) return;
if (Global.ChessMode == ChessMode.Manual) return;
string setMap = this.GetSetMap();
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
bool b = ChineseChessThinker.Calculate(setMap,10000, ref x1, ref y1, ref x2, ref y2);
if (b)
{
if (Global.ChessMode == ChessMode.Auto)
{
this.PreMoveChess(x1, 9 - y1, x2, 9 - y2);
this.Request.Move(x1, y1, x2, y2);
}
else
{
//畫出推薦著法
this.situation[x1, 9 - y1].BackColor = Color.Blue;
if (this.situation[x2, 9 - y2] != null)
{
this.situation[x2, 9 - y2].BackColor = Color.Blue;
}
else
{
this.addBlank(x2, 9-y2);
this.situation[x2, 9 - y2].BackColor = Color.Blue;
}
}
}
}
}
public enum ChessmanType { HeaderCapital, Guard, Minister, Horse, General, Gun, Soldier };//服務(wù)器上的定義
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -