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

📄 chessman.cs

📁 Visual studio 2005,C#开发 具有人工智能
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GameWorld.ChineseChess
{
	public abstract partial class ChessMan : UserControl
	{
		public ChessMan()
		{
			InitializeComponent();
		}
		public const int RED = 2;
		public const int BLACK = 1;
		public const int GREY = 0;

		public const int GENERAL = 7;
		public const int GUARD = 6;
		public const int STAFF = 5;
		public const int HORSE = 4;
		public const int CHARIOT = 3;
		public const int CANNON = 2;
		public const int SOLDIER = 1;
		public const int BLANK = 0;

		public const int REDSIDE = 0;
		public const int BLACKSIDE = 1;
		private const int gridSize = 40;
		private const int ChessSize = 32;


		public int color;
		public int kind;
		public int number;
		public bool beAttack;
		public string fileName;
		public int x;
		public int y;
		public Graphics Graphics;
		//public int top;
		//public int left;
		public ChessMan[,] situation
		{
			get
			{
				if (this.Parent != null)
				{
					ChineseChessControl parent = this.Parent as ChineseChessControl;
					if (parent != null)
						return parent.situation;
				}
				return null;
			}
		}

		private Request Request = Request.CreateInstance();

		public ChessMan(int color, int kind, int number, int boardside)
		{
			InitializeComponent();

			this.DrawChessMan(color, kind, number, boardside);

		}
		public ChessMan(int color, int kind, int x, int y, int boardside)
		{
			InitializeComponent();

			this.DrawChessMan(color, kind, x, y, boardside);
		}

		public void DrawChessMan(int color, int kind,int x, int y,int boardside)
		{

			this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			this.BackColor = Color.Transparent;

			this.x = x;
			this.y = y;

			this.Width = ChessSize;
			this.Height = ChessSize;
			this.color = color;
			this.kind = kind;
			this.beAttack = false;
			this.fileName = @"ChineseChess\Image\";

			switch (this.color)
			{
				case RED:
					{
						this.fileName += "red";
						break;
					}
				case BLACK:
					{
						this.fileName += "black";
						break;
					}
			}

			switch (this.kind)
			{
				case GENERAL:
					{
						this.fileName += "general";
						break;
					}
				case GUARD:
					{
						this.fileName += "guard";
						break;
					}
				case STAFF:
					{
						this.fileName += "staff";
						break;
					}
				case HORSE:
					{
						this.fileName += "horse";
						break;
					}
				case CHARIOT:
					{
						this.fileName += "chariot";
						break;
					}
				case CANNON:
					{
						this.fileName += "cannon";
						break;
					}
				case SOLDIER:
					{
						this.fileName += "soldier";
						break;
					}
				case BLANK:
					{
						this.fileName += "blank";
						break;
					}
			}

			this.fileName += ".ico";

			this.Left = Math.Abs(8 * boardside - this.x) * gridSize - ChessSize / 2 + gridSize / 2;//gridSize/2是偏移
			this.Top = Math.Abs(9 * boardside - this.y) * gridSize - ChessSize / 2 + gridSize / 2;
		}

		public void DrawChessMan(int color, int kind, int number, int boardside)
		{

			//GraphicsPath graphicsPath = new GraphicsPath();
			//graphicsPath.AddEllipse(0, 0, ChessSize, ChessSize);
			//this.Region = new Region(graphicsPath);

			this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			this.BackColor = Color.Transparent;

			this.Width = ChessSize;
			this.Height = ChessSize;
			this.color = color;
			this.kind = kind;
			this.number = number;
			this.beAttack = false;
			this.fileName = @"ChineseChess\Image\";

			switch (this.color)
			{
				case RED:
					{
						this.fileName += "red";
						break;
					}
				case BLACK:
					{
						this.fileName += "black";
						break;
					}
			}

			switch (this.kind)
			{
				case GENERAL:
					{
						this.fileName += "general";
						this.x = 4;
						this.y = 9 * (this.color - 1);
						break;
					}
				case GUARD:
					{
						this.fileName += "guard";
						this.x = 3 + 2 * this.number;
						this.y = 9 * (this.color - 1);
						break;
					}
				case STAFF:
					{
						this.fileName += "staff";
						this.x = 2 + 4 * this.number;
						this.y = 9 * (this.color - 1);
						break;
					}
				case HORSE:
					{
						this.fileName += "horse";
						this.x = 1 + 6 * this.number;
						this.y = 9 * (this.color - 1);
						break;
					}
				case CHARIOT:
					{
						this.fileName += "chariot";
						this.x = 8 * this.number;
						this.y = 9 * (this.color - 1);
						break;
					}
				case CANNON:
					{
						this.fileName += "cannon";
						this.x = 1 + 6 * this.number;
						this.y = 2 + 5 * (this.color - 1);
						break;
					}
				case SOLDIER:
					{
						this.fileName += "soldier";
						this.x = 2 * this.number;
						this.y = 3 * this.color;
						break;
					}
				case BLANK:
					{
						this.fileName += "blank";
						break;
					}
			}

			this.fileName += ".ico";

			//int chessMan = document.createElement("<div class='chessMan'>");
			//int imgsrc = document.createElement("<img src='"+this.fileName+"'>");

			//chessMan.style.top = Math.Abs(9*boardside-this.y) * gridSize - 16;
			//chessMan.style.left = Math.Abs(8*boardside-this.x) * gridSize - 16;
			//chessMan.appendChild(imgsrc);
			//this.top = Math.Abs(9 * boardside - this.y) * gridSize - ChessSize / 2;
			//this.left = Math.Abs(8 * boardside - this.x) * gridSize - ChessSize / 2;

			this.Left = Math.Abs(8 * boardside - this.x) * gridSize - ChessSize / 2 + gridSize / 2;//gridSize/2是偏移
			this.Top = Math.Abs(9 * boardside - this.y) * gridSize - ChessSize / 2 + gridSize / 2;
		}

		//public void Draw(Graphics g)
		//{
		//    Icon ico = new Icon(this.fileName);
		//    g.DrawIcon(ico, this.left, this.top);
		//}

		private void ChessMan_MouseDown(object sender, MouseEventArgs e)
		{
			check();
		}

		private void ChessMan_Paint(object sender, PaintEventArgs e)
		{
			//Matrix transformMatrix = new Matrix();
			//transformMatrix.Scale(1.5F, 1.5F);
			//e.Graphics.MultiplyTransform(transformMatrix);

			Icon ico = new Icon(this.fileName);
			e.Graphics.DrawIcon(ico, new Rectangle(0, 0, ChessSize, ChessSize));
		}
		private void check()
		{
			ChineseChessControl control = this.Parent as ChineseChessControl;

			if (control.end == true)
			{
				MessageBox.Show("对局已经结束, 请重新开局!");
				return;
			}
			if (((control.BoardSide == REDSIDE) && (this.color == BLACK)) || ((control.BoardSide == BLACKSIDE) && (this.color == RED)))
			{
				if (this.beAttack == false)
				{
					MessageBox.Show("不能移动对方棋子!");
					return;
				}
			}
			if (control.selection == null)
			{
				if (this.color != control.side)
				{
					MessageBox.Show("不该你走棋!");
					return;
				}
			}
			else
			{
				if (this.color != control.selection.color)
				{
					if ((this.color != GREY)
					&& (this.beAttack == false))
					{
						MessageBox.Show("不该你走棋!");
						return;
					}
				}
			}


			if ((this.color == GREY)||(this.beAttack == true))
			{
				//坐标变换,本客户端采用左上角为原点,Web 服务采用左下角作原点
				int x1 = control.selection.x;
				int y1 = 9 - control.selection.y;
				int x2 = this.x;
				int y2 = 9 - this.y;

				control.ToPos = this;

				this.Request.Move(x1, y1, x2, y2);

				return;
			}


			control.clearBoard();
			control.selection = this;
			this.BackColor = Color.Blue;

			for (int i = 0; i < 9; i++)
			{
				for (int j = 0; j < 10; j++)
				{
					if (this.canGo(i, j))
					{
						control.addBlank(i, j);
					}
				}
			}
		}




		public abstract bool canGo(int i, int j);

		private void ChessMan_Load(object sender, EventArgs e)
		{


		}

	}
}

⌨️ 快捷键说明

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