C# 在数组中保留数组
我想将kingarray[x1+1,y1-1]、king array[x1+1,y1]等保存在一个数组中(king在国际象棋游戏中可以使用的方式)。我怎么做?或者如果不可能,您建议我保留king可以使用的方式?谢谢C# 在数组中保留数组,c#,arrays,chess,C#,Arrays,Chess,我想将kingarray[x1+1,y1-1]、king array[x1+1,y1]等保存在一个数组中(king在国际象棋游戏中可以使用的方式)。我怎么做?或者如果不可能,您建议我保留king可以使用的方式?谢谢 int[,] kingarray = new int[8, 8]; for (i = 0; i < 1; i++) { return kingarray[x1 + 1, y1]; } for (i = 1; i > 0; i--) { retur
int[,] kingarray = new int[8, 8];
for (i = 0; i < 1; i++)
{
return kingarray[x1 + 1, y1];
}
for (i = 1; i > 0; i--)
{
return kingarray[x1 - 1, y1];
}
for (j = 0; j < 1; j++)
{
return kingarray[x1, y1 + 1];
}
for (j = 1; j > 0; j--)
{
return kingarray[x1, y1 - 1];
}
for (i = 0; i < 1; i++)
for (j = 1; j > 0; j--)
{
return kingarray[x1 + 1, y1 - 1];
}
for (i = 1; i > 0; i--)
for (j = 0; j < 1; j++)
{
return kingarray[x1 - 1, y1 + 1];
}
for (i = 0; i < 1; i++)
for (j = 0; j < 1; j++)
{
return kingarray[x1 + 1, y1 + 1];
}
for (i = 1; i > 0; i--)
for (j = 1; j > 0; j--)
{
return kingarray[x1 - 1, y1 - 1];
}
int[,]kingarray=newint[8,8];
对于(i=0;i<1;i++)
{
返回数组[x1+1,y1];
}
对于(i=1;i>0;i--)
{
返回数组[x1-1,y1];
}
对于(j=0;j<1;j++)
{
返回数组[x1,y1+1];
}
对于(j=1;j>0;j--)
{
返回数组[x1,y1-1];
}
对于(i=0;i<1;i++)
对于(j=1;j>0;j--)
{
返回数组[x1+1,y1-1];
}
对于(i=1;i>0;i--)
对于(j=0;j<1;j++)
{
返回数组[x1-1,y1+1];
}
对于(i=0;i<1;i++)
对于(j=0;j<1;j++)
{
返回数组[x1+1,y1+1];
}
对于(i=1;i>0;i--)
对于(j=1;j>0;j--)
{
返回数组[x1-1,y1-1];
}
int[,][]
声明一个包含int的2D数组的1D数组。这就是您想要的吗
而kingmoves可以简单地计算为:
IEnumerable<Position> ValidKingTargets(Position p)
{
int top=Math.Max(0,y-1);
int left=Math.Max(0,x-1);
int bottom=Math.Min(8,y+2);
int right=Math.Min(8,x+2);
for(int y=top;y<bottom;y++)
for(int x=left;x<right;x++)
if(x!=p.X || y!=p.Y)
yield return new Position(x,y);
}
IEnumerable有效目标(位置p)
{
int top=数学最大值(0,y-1);
int left=数学最大值(0,x-1);
int bottom=数学最小值(8,y+2);
int right=Math.Min(8,x+2);
for(int y=top;yint[,][]
声明一个1D数组,其中包含一个int的2D数组。这就是您想要的吗
而kingmoves可以简单地计算为:
IEnumerable<Position> ValidKingTargets(Position p)
{
int top=Math.Max(0,y-1);
int left=Math.Max(0,x-1);
int bottom=Math.Min(8,y+2);
int right=Math.Min(8,x+2);
for(int y=top;y<bottom;y++)
for(int x=left;x<right;x++)
if(x!=p.X || y!=p.Y)
yield return new Position(x,y);
}
IEnumerable有效目标(位置p)
{
int top=数学最大值(0,y-1);
int left=数学最大值(0,x-1);
int bottom=数学最小值(8,y+2);
int right=Math.Min(8,x+2);
对于(int y=top;y数组
确定国王可能的行动相当容易
class position { public int x, y }
...
public ArrayList<position> KingPossibleMove(position current)
{
var list = new ArrayList();
if (current.x>0) {
list.add(new position() { x= current.x - 1, y = current.y });
if (current.x<8) {
list.add(new position() { x= current.x + 1, y = current.y });
// The rest follows, try to determine if the move is within bound
// you can also look for if the move will cause immediate checkmate.
return list;
}
类位置{public int x,y}
...
公共阵列列表KingPossibleMove(当前位置)
{
var list=new ArrayList();
如果(当前.x>0){
add(新位置(){x=current.x-1,y=current.y});
if(current.x数组
确定国王可能的行动相当容易
class position { public int x, y }
...
public ArrayList<position> KingPossibleMove(position current)
{
var list = new ArrayList();
if (current.x>0) {
list.add(new position() { x= current.x - 1, y = current.y });
if (current.x<8) {
list.add(new position() { x= current.x + 1, y = current.y });
// The rest follows, try to determine if the move is within bound
// you can also look for if the move will cause immediate checkmate.
return list;
}
类位置{public int x,y}
...
公共阵列列表KingPossibleMove(当前位置)
{
var list=new ArrayList();
如果(当前.x>0){
add(新位置(){x=current.x-1,y=current.y});
如果(当前的.x
你有什么建议让我保持金可以走的路
这可能无法直接回答您的问题,并且您已经标记了正确的答案。但只是为了回答上述问题
与其保留国王可以走的所有位置,我还不如保留国王可以走的位置,并在运行时计算可能的路线
对于任何棋子(国王、棋子等),有8个位置可以移动。左、右、上、下、左上、右上、左下、右下。根据棋子类型,可以控制移动
class position { public int x, y }
...
public ArrayList<position> KingPossibleMove(position current)
{
var list = new ArrayList();
if (current.x>0) {
list.add(new position() { x= current.x - 1, y = current.y });
if (current.x<8) {
list.add(new position() { x= current.x + 1, y = current.y });
// The rest follows, try to determine if the move is within bound
// you can also look for if the move will cause immediate checkmate.
return list;
}
因为你可以创建一个ChessPiece
类。声明8个位置标志,bool
标志可能会定义一个棋子可以移动的可能位置
声明一个棋子可以跳过的块数,例如方向(;和从棋子中驱动类型并允许
--编辑--
例如:
//Class that contains the position of the Piece over the Tile
class PiecePosition
{
//Set the bounds an image/vector can move.
public int X, Y;
public PiecePosition(int x, int y) { this.X = x; this.Y = y; }
public PiecePosition(int x, int y, int width, int height) { this.X = x; this.Y = y; }
}
//Base ChessPeice class that shall be used to drive all types of chess pieces.
//Sixteen pieces: one king, one queen, two rooks, two knights, two bishops, and eight pawns
//http://en.wikipedia.org/wiki/Chess
abstract class ChessPiece
{
public object Image;//This is an "optional" object that contains the Picture of the Peice,
//alternatively, it may contain vector of the image that you want
//to draw. Right now this is object is here just for the sake of
//understanding that you can use this object here to Draw() it
//based upon its position.
//Possible movements of the unhindered piece=8
protected const int MaxDirectionsCount = 8;
public enum PieceType { King, Pawn, SomeOtherType }//Types of chess peice.
public enum Moves { Up, Down, Left, Right, TopLeft, Etc }//Possible positions a piece can move
protected PieceType Type; //Contains type of piece
protected Moves MoveableDirections;//Shall contain the allowable directions
public List<PiecePosition> listPositions;//List of possible positions to be calculated during runtime
//Defines a piece can skip
protected int SkippableBlocks;
public abstract void PossiblePositions(PiecePosition CurrentPosition);//Calculates possible positions
public abstract void Draw();//Draws the piece
}
//The King Chess piece
//http://en.wikipedia.org/wiki/King_%28chess%29
class King : ChessPiece
{
//Constructor that sets the type of piece
public King()
{
//Set the directions a King can move.
base.MoveableDirections = Moves.Down | Moves.Left | Moves.Right;
base.Type = PieceType.King;
SkippableBlocks = 1; //Max a king can move is one block in the base.Directions set above.
}
//Calculates possible available positions to move to, during runtime; based upon current position.
public override void PossiblePositions(PiecePosition CurrentPosition)
{
//Calculate position
//Since you know this is king piece, you can calculate the possible positions
//And add that the list of possible positions.
//For instance, a King can move
int X = 0; int Y = 0;
for (int i = 0; i < MaxDirectionsCount; i++)
{
//Calculate directions.
if (base.MoveableDirections == Moves.Down) { X = CurrentPosition.X - 1; Y = CurrentPosition.Y; }
if (base.MoveableDirections == Moves.Up) { X = CurrentPosition.X + 1; Y = CurrentPosition.Y; }
//Rest of the directions go here...
//...Btw, what would you do for cross directions?
//One way could be to pass a Rectangle in the ChessTile(x,y,width,height) constructor
//Add to list of possible directions.
listPositions.Add(new PiecePosition(X, Y));
}
}
public override void Draw()
{
//You can actually draw/redraw using the Image object
//based upon the current/moved position.
}
}
//类,该类包含块在平铺上的位置
类工件位置
{
//设置图像/向量可以移动的边界。
公共整数X,Y;
公共片段位置(intx,inty){this.x=x;this.y=y;}
公共工件位置(intx,inty,intwidth,intheight){this.x=x;this.y=y;}
}
//基本棋类,用于驱动所有类型的棋子。
//十六件:一位国王,一位王后,两辆车,两位骑士,两位主教和八名兵
//http://en.wikipedia.org/wiki/Chess
抽象类棋子
{
public object Image;//这是一个“可选”对象,包含Peice的图片,
//或者,它可能包含所需图像的矢量
//画画。现在这是物体,只是为了
//了解您可以在此处使用此对象来绘制它
//根据它的位置。
//无阻碍工件的可能移动=8
受保护常量int MaxDirectionsCount=8;
公共枚举片段类型{King,Pawn,SomeOtherType}//国际象棋的类型。
公共枚举移动{上、下、左、右、左上等}//工件可以移动的可能位置
受保护的工件类型;//包含工件的类型
受保护的移动移动方向;//应包含允许的方向
public List listPositions;//运行时要计算的可能位置列表
//定义可以跳过的工件
受保护的int-skipableblocks;
公共抽象无效可能位置(PiecePosition CurrentPosition);//计算可能的位置
public abstract void Draw();//绘制工件
}
//王牌棋子
//http://en.wikipedia.org/wiki/King_%28chess%29
班王:棋子
{
//设置工件类型的构造函数
公共国王()
{
//设定国王可以移动的方向。
base.MoveableDirections=Moves.Down | Moves.Left | Moves.Right;
base.Type=PieceType.King;
SkippableBlocks=1;//国王可以移动的最大值是底部的一个块。上面设置的方向。
}
//根据当前位置计算运行时可能要移动到的可用位置。
公共覆盖无效可能位置(PiecePosition CurrentPosition)
{
//计算位置
//既然你知道这是王牌,你可以计算出可能的位置
//并将其添加到可能的位置列表中。
//例如,国王可以移动
整数X=0;整数Y=0;
对于(int i=0;i