?? classes.cs
字號:
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 + -