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

📄 classes.cs

📁 Visual studio 2005,C#开发 具有人工智能
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace GameWorld.ChineseChess
{
	internal class ChessBoard//由桃花岛的js象棋游戏改造而来,改造的过程比较生硬,感觉上写的不是很好,希望有人重写
	{

		//创建棋盘所在的层
		//int boardBase = document.createElement("<div class='chessBoard' id='boardLayer'>");
		//document.body.appendChild(boardBase); 
		//this.board = boardBase;
		private const int gridSize = 40;
		private const int ChessSize = 32;
		public ChessBoard()
		{
		}

		public void drawBoard(Graphics g)
		{
			int width = 8 * gridSize;
			int height = 9 * gridSize;

			int backwidth = 9 * gridSize;
			int backheight = 10 * gridSize;

			Matrix transformMatrix = new Matrix();
			transformMatrix.Translate(gridSize / 2.0F, gridSize / 2.0F);
			g.MultiplyTransform(transformMatrix);

			//绘制背景,把棋子完全覆盖
			g.FillRectangle(Brushes.White, -gridSize / 2, -gridSize / 2, backwidth, backheight);


			//绘制棋盘边框,粗线
			g.DrawRectangle(new Pen(Color.Black, 2), 0, 0, width, height);

			//绘制水平线
			for (int i = 0; i < 10; i++)
			{
				//int hLine = document.createElement("<v:line from='0,"+gridSize*i+"' to='"+width+","+gridSize*i+">");
				//boardBase.appendChild(hLine);

				g.DrawLine(Pens.Black, 0, gridSize * i, width, gridSize * i);
			}

			//绘制竖直线
			for (int i = 0; i < 9; i++)
			{
				//int vLine = document.createElement("<v:line from='"+gridSize*i+",0' to='"+gridSize*i+","+gridSize*4+">");
				//boardBase.appendChild(vLine);
				g.DrawLine(Pens.Black, gridSize * i, 0, gridSize * i, gridSize * 4);
			}

			for (int i = 0; i < 9; i++)
			{
				//int vLine = document.createElement("<v:line from='"+gridSize*i+","+gridSize*5+"' to='"+gridSize*i+","+this.height+">");
				//boardBase.appendChild(vLine);
				g.DrawLine(Pens.Black, gridSize * i, gridSize * 5, gridSize * i, height);
			}

			//绘制斜线
			//int upNW2SELine = document.createElement("<v:line from='"+3*gridSize+",0' to='"+5*gridSize+","+2*gridSize+"'>");
			//boardBase.appendChild(upNW2SELine);
			g.DrawLine(Pens.Black, 3 * gridSize, 0, 5 * gridSize, 2 * gridSize);


			//int upNE2SWLine = document.createElement("<v:line from='"+5*gridSize+",0' to='"+3*gridSize+","+2*gridSize+"'>");
			//boardBase.appendChild(upNE2SWLine);
			g.DrawLine(Pens.Black, 5 * gridSize, 0, 3 * gridSize, 2 * gridSize);

			//int downNW2SELine = document.createElement("<v:line from='"+3*gridSize+","+7*gridSize+"' to='"+5*gridSize+","+this.height+"'>");
			//boardBase.appendChild(downNW2SELine);
			g.DrawLine(Pens.Black, 3 * gridSize, 7 * gridSize, 5 * gridSize, height);

			//int downNE2SWLine = document.createElement("<v:line from='"+5*gridSize+","+7*gridSize+"' to='"+3*gridSize+","+this.height+"'>");
			//boardBase.appendChild(downNE2SWLine);
			g.DrawLine(Pens.Black, 5 * gridSize, 7 * gridSize, 3 * gridSize, height);

			//绘制炮位
			for (int i = 0; i < 2; i++)
			{
				for (int j = 0; j < 2; j++)
				{
					drawStar(1 + i * 6, 2 + j * 5,g);
				}
			}

			//绘制兵位
			for (int i = 0; i < 5; i++)
			{
				for (int j = 0; j < 2; j++)
				{
					drawStar(i * 2, 3 + j * 3,g);
				}
			}
		}

		//绘制兵位、炮位的方法
		void drawStar(int x, int y,Graphics g)
		{
			int centerX = x * gridSize;
			int centerY = y * gridSize;

			int distance = gridSize / 10;
			int length = gridSize / 4;

			//靠左边的位置不画左边一半
			if (x != 0)
			{
				int startX = centerX - distance;
				int startY = centerY - distance - length;
				int endX = centerX - distance - length;
				int endY = centerY - distance;

				//int vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
				//boardBase.appendChild(vLine);
				//int hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
				//boardBase.appendChild(hLine);
				g.DrawLine(Pens.Black, startX, startY, startX, endY);
				g.DrawLine(Pens.Black, startX, endY, endX, endY);


				startX = centerX - distance;
				startY = centerY + distance + length;
				endX = centerX - distance - length;
				endY = centerY + distance;

				//vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
				//boardBase.appendChild(vLine);
				//hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
				//boardBase.appendChild(hLine);
				g.DrawLine(Pens.Black, startX, startY, startX, endY);
				g.DrawLine(Pens.Black, startX, endY, endX, endY);

			}

			//靠右边的位置不画右边一半
			if (x != 8)
			{
				int startX = centerX + distance;
				int startY = centerY - distance - length;
				int endX = centerX + distance + length;
				int endY = centerY - distance;

				//int vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
				//boardBase.appendChild(vLine);
				//int hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
				//boardBase.appendChild(hLine);
				g.DrawLine(Pens.Black, startX, startY, startX, endY);
				g.DrawLine(Pens.Black, startX, endY, endX, endY);

				startX = centerX + distance;
				startY = centerY + distance + length;
				endX = centerX + distance + length;
				endY = centerY + distance;

				//vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
				//boardBase.appendChild(vLine);
				//hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
				//boardBase.appendChild(hLine);
				g.DrawLine(Pens.Black, startX, startY, startX, endY);
				g.DrawLine(Pens.Black, startX, endY, endX, endY);

			}
		}
	}


		public class General:ChessMan
		{
			public General(int color, int x, int y, int boardside) : base(color, GENERAL, x, y, boardside) { }
			public General(int color,int boardside):base(color, GENERAL, 0,boardside)
			{
			}

			bool isNormal(int posX, int posY)
			{

				if ((posY > 2 && posY < 7) || posX < 3 || posX > 5)//双方九宫皆可,为见面处理做准备
				{
					return false;
				}

				return true;
			}

			public override bool canGo(int toX, int toY)
			{
	
				if (    isNormal(toX, toY)
					&& (situation[toX,toY] != null)
					&& (situation[toX,toY].kind == GENERAL)
					&& (toX == this.x) && (situation[toX, toY].color != this.color))//老帅见面的判断
				{
					int ymin;
					int ymax;
			
					if(toY<this.y) 
					{
						ymin=toY;
						ymax=this.y;
					}
					else
					{
						ymax=toY;
						ymin=this.y;
					}
					int count=0;
					for(int i=ymin+1;i<ymax;i++) 
					{
						if ((situation[toX,i] != null)&& (situation[toX,i].color != GREY))//该位置有棋子
						{
							count++;
						}
					}
			
					if (count==0) return true;//老帅见面
				}

	
				if (isNormal(toX, toY)
					&& ((situation[toX,toY] == null) 
					|| ((situation[toX,toY] != null)
					&&(situation[toX,toY].color != this.color))))
				{
					if (Math.Abs(this.y-toY)+Math.Abs(toX-this.x) != 1)
					{
						return false;
					}
					else
					{
						return true;
					}
				}
				return false;
			}

		}

		public class Guard:ChessMan
		{
			public Guard(int color, int x, int y, int boardside) : base(color, GUARD, x, y, boardside) { }
			public Guard(int color,int number,int boardside):base(color, GUARD, number,boardside)
			{
			}

			public bool isNormal(int posX,int posY)
			{
				if (this.color == RED)
				{
					if (((posX==3) && (posY==7))
						|| ((posX==3) && (posY==9))
						|| ((posX==5) && (posY==7))
						|| ((posX==5) && (posY==9))
						|| ((posX==4) && (posY==8)))
					{
						return true;
					}
				}

				if (this.color == BLACK)
				{
					if (((posX==3) && (posY==0))
						|| ((posX==3) && (posY==2))
						|| ((posX==5) && (posY==0))
						|| ((posX==5) && (posY==2))
						|| ((posX==4) && (posY==1)))
					{
						return true;
					}
				}

				return false;
			}

			public override bool canGo(int toX, int toY)
			{
				if (isNormal(toX, toY)
					&& ((situation[toX,toY] == null) 
					|| ((situation[toX,toY] != null)
					&&(situation[toX,toY].color != this.color))))
				{
					if ((Math.Abs(this.x-toX) > 1)
						||(Math.Abs(this.y-toY) > 1)
						||((Math.Abs(this.x-toX) == 0)
						&&(Math.Abs(this.y-toY) == 0)))
					{
						return false;
					}
					else
					{
						return true;
					}
				}
				return false;
			}
		}

		class Staff:ChessMan
		{
			public Staff(int color, int x, int y, int boardside) : base(color, STAFF, x, y, boardside) { }
			public Staff(int color,int number,int boardside):base(color, STAFF, number,boardside)
			{
			}
			public bool isNormal(int posX,int posY)
			{
				if (this.color == BLACK)
				{
					if (((posX==0) && (posY==2))
						|| ((posX==2) && (posY==0))
						|| ((posX==2) && (posY==4))
						|| ((posX==4) && (posY==2))
						|| ((posX==6) && (posY==0))
						|| ((posX==6) && (posY==4))
						|| ((posX==8) && (posY==2)))
					{
						return true;
					}
				}
		
				if (this.color == RED)
				{
					if (((posX==0) && (posY==7))
						|| ((posX==2) && (posY==5))
						|| ((posX==2) && (posY==9))
						|| ((posX==4) && (posY==7))
						|| ((posX==6) && (posY==5))
						|| ((posX==6) && (posY==9))
						|| ((posX==8) && (posY==7)))
					{
						return true;
					}
				}

				return false;
			}

			public override bool canGo(int toX, int toY)
			{
				if (isNormal(toX, toY)
					&& ((situation[toX,toY] == null) 
					|| ((situation[toX,toY] != null)
					&&(situation[toX,toY].color != this.color))))
				{
					if ((Math.Abs(this.x-toX) != 2)
						||(Math.Abs(this.y-toY) != 2))
					{
						return false;
					}
					else
					{
						int i = (this.x + toX) / 2;
						int j = (this.y + toY) / 2;
						if (situation[i,j] == null)
						{
							return true;
						}
						else
						{
							return false;
						}
					}
				}
				return false;
			}
		}

		class Horse:ChessMan
		{
			public Horse(int color, int x, int y, int boardside) : base(color, HORSE, x, y, boardside) { }
			public Horse(int color,int number,int boardside):base(color, HORSE, number,boardside)
			{
			}
			public bool isNormal(int posX,int posY)
			{
				return true;
			}

			public override bool canGo(int toX, int toY)
			{
				if (isNormal(toX, toY)

⌨️ 快捷键说明

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