⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chinesechesscontrol.cs

📁 Visual studio 2005,C#开发 具有人工智能
💻 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;//移动的目标位置
		public bool end;
		/// <summary>
		/// 当前走子的一方
		/// </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 };//服务器上的定义


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -