get,set的基本用法,我知道,比如
public class aa
{
private int x;
public int X//但是这里变成this[]是什么意思??
{
get
{
return x;
}
set
{
x=value;
}
}
}using System;
using System.Collections.Generic;
using System.Text;namespace chess2
{
public class ChessBoard
{
private Chess[,] chesses = new Chess[10, 11];//存放棋子
public Chess currChess;
public bool move = false; //棋子索引器
public Chess this[ChessPoint p]//啥意思???????
{
get
{
return chesses[p.x, p.y];
}
set
{
chesses[p.x, p.y] = value;
}
} //按坐标返回棋子
public Chess this[int x, int y]//这个啥意思??????
{
get
{
return chesses[x, y];
}
set
{
chesses[x, y] = value;
}
}
//构造,(这块不用看)
public ChessBoard()
{
//初始化棋子
//黑棋子
chesses[1, 1] = new Rook(false, new ChessPoint(1, 1), this);//黑车
chesses[2, 1] = new Knights(false, new ChessPoint(2, 1), this);//黑马
chesses[2, 3] = new Cannons(false, new ChessPoint(2, 3), this);//黑炮-----
chesses[3, 1] = new Elephants(false, new ChessPoint(3, 1), this);//黑象
chesses[4, 1] = new Mandarins(false, new ChessPoint(4, 1), this);//黑士
chesses[5, 1] = new King(false, new ChessPoint(5, 1), this);//黑将 chesses[6, 1] = new Mandarins(false, new ChessPoint(6, 1), this);//黑士
chesses[7, 1] = new Elephants(false, new ChessPoint(7, 1), this);//黑象
chesses[8, 3] = new Cannons(false, new ChessPoint(8, 3), this);//黑炮-----
chesses[8, 1] = new Knights(false, new ChessPoint(8, 1), this);//黑马
chesses[9, 1] = new Rook(false, new ChessPoint(9, 1), this);//黑车 chesses[1, 4] = new Pawns(false, new ChessPoint(1, 4), this);//黑兵
chesses[3, 4] = new Pawns(false, new ChessPoint(3, 4), this);//黑兵
chesses[5, 4] = new Pawns(false, new ChessPoint(5, 4), this);//黑兵
chesses[7, 4] = new Pawns(false, new ChessPoint(7, 4), this);//黑兵
chesses[9, 4] = new Pawns(false, new ChessPoint(9, 4), this);//黑兵 //红旗子
chesses[1, 10] = new Rook(true, new ChessPoint(1, 10), this);//红车
chesses[2, 10] = new Knights(true, new ChessPoint(2, 10), this);//红马
chesses[2, 8] = new Cannons(true, new ChessPoint(2, 8), this);//红炮-----
chesses[3, 10] = new Elephants(true, new ChessPoint(3, 10), this);//红象
chesses[4, 10] = new Mandarins(true, new ChessPoint(4, 10), this);//红士
chesses[5, 10] = new King(true, new ChessPoint(5, 10), this);//红将 chesses[6, 10] = new Mandarins(true, new ChessPoint(6, 10), this);//红士
chesses[7, 10] = new Elephants(true, new ChessPoint(7, 10), this);//红象
chesses[8, 8] = new Cannons(true, new ChessPoint(8, 8), this);//红炮-----
chesses[8, 10] = new Knights(true, new ChessPoint(8, 10), this);//红马
chesses[9, 10] = new Rook(true, new ChessPoint(9, 10), this);//红车 chesses[1, 7] = new Pawns(true, new ChessPoint(1, 7), this);//红兵
chesses[3, 7] = new Pawns(true, new ChessPoint(3, 7), this);//红兵
chesses[5, 7] = new Pawns(true, new ChessPoint(5, 7), this);//红兵
chesses[7, 7] = new Pawns(true, new ChessPoint(7, 7), this);//红兵
chesses[9, 7] = new Pawns(true, new ChessPoint(9, 7), this);//红兵
}
}
}
//另一个类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;namespace chess2
{
public abstract class Chess:Label
{
//public bool isSelect = false;//是否为当前可移动棋子
public bool isRed;//true表示红方,false表示黑方
public ChessPoint currentPoint;//当前虚拟坐标点
public String chessText;
public ChessBoard chessBoard; //构造
public Chess(bool isRed ,ChessPoint targetpoint, ChessBoard chessBoard)
{
this.isRed = isRed;
this.currentPoint = targetpoint;
this.chessBoard = chessBoard;
//this.Image = image; realLocation(targetpoint);
this.AutoSize = false;
this.Size = new Size(53, 53); //改变标签的形状为圆形
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(this.ClientRectangle);
this.Region = new Region(path);
} //根据给定的虚拟坐标定位到真实的坐标
public void realLocation(ChessPoint targetpoint)
{
Point p = targetpoint.getRealPoint();
this.Location = p;
} //判断是否能够移动到目标点
public abstract bool canMove(ChessPoint targetPoint); //棋子的移动方法
public void moveTo(ChessPoint targetPoint)
{
if (targetPoint.x < 1 || targetPoint.x > 9 || targetPoint.y < 1 || targetPoint.y > 10)
return;
//获取目标点上的棋子
Chess targetChess = chessBoard[targetPoint.x, targetPoint.y];
//如果目标棋子的颜色与当前棋子的颜色相同,则不能移动;
if (targetChess != null && targetChess.isRed == this.isRed)
return; //是否能移动
if (!canMove(targetPoint)) return; //吃掉对方老王
if (chessBoard[targetPoint] is King)
throw new Exception(this.isRed ? "红方胜" : "黑方胜"); //移动
chessBoard[currentPoint] = null;
chessBoard[targetPoint] = this; currentPoint = targetPoint;
realLocation(targetPoint);
} //获取两点间的棋子数(直线)
public int getChessCount(ChessPoint start, ChessPoint end)
{
int count = 0;//记录棋子数
if (start.y == end.y)
{
int min = Math.Min(start.x, end.x);
int max = Math.Max(start.x, end.x);
for (int i = min + 1; i < max; i++)
{
if (chessBoard[i, start.y] != null)
count++;
}
}
else if(start.x == end.x)
{
int min = Math.Min(start.y, end.y);
int max = Math.Max(start.y, end.y);
for (int i = min + 1; i < max; i++)
{
if (chessBoard[start.x, i] != null)
count++;
}
}
return count;
}
}
}
public class aa
{
private int x;
public int X//但是这里变成this[]是什么意思??
{
get
{
return x;
}
set
{
x=value;
}
}
}using System;
using System.Collections.Generic;
using System.Text;namespace chess2
{
public class ChessBoard
{
private Chess[,] chesses = new Chess[10, 11];//存放棋子
public Chess currChess;
public bool move = false; //棋子索引器
public Chess this[ChessPoint p]//啥意思???????
{
get
{
return chesses[p.x, p.y];
}
set
{
chesses[p.x, p.y] = value;
}
} //按坐标返回棋子
public Chess this[int x, int y]//这个啥意思??????
{
get
{
return chesses[x, y];
}
set
{
chesses[x, y] = value;
}
}
//构造,(这块不用看)
public ChessBoard()
{
//初始化棋子
//黑棋子
chesses[1, 1] = new Rook(false, new ChessPoint(1, 1), this);//黑车
chesses[2, 1] = new Knights(false, new ChessPoint(2, 1), this);//黑马
chesses[2, 3] = new Cannons(false, new ChessPoint(2, 3), this);//黑炮-----
chesses[3, 1] = new Elephants(false, new ChessPoint(3, 1), this);//黑象
chesses[4, 1] = new Mandarins(false, new ChessPoint(4, 1), this);//黑士
chesses[5, 1] = new King(false, new ChessPoint(5, 1), this);//黑将 chesses[6, 1] = new Mandarins(false, new ChessPoint(6, 1), this);//黑士
chesses[7, 1] = new Elephants(false, new ChessPoint(7, 1), this);//黑象
chesses[8, 3] = new Cannons(false, new ChessPoint(8, 3), this);//黑炮-----
chesses[8, 1] = new Knights(false, new ChessPoint(8, 1), this);//黑马
chesses[9, 1] = new Rook(false, new ChessPoint(9, 1), this);//黑车 chesses[1, 4] = new Pawns(false, new ChessPoint(1, 4), this);//黑兵
chesses[3, 4] = new Pawns(false, new ChessPoint(3, 4), this);//黑兵
chesses[5, 4] = new Pawns(false, new ChessPoint(5, 4), this);//黑兵
chesses[7, 4] = new Pawns(false, new ChessPoint(7, 4), this);//黑兵
chesses[9, 4] = new Pawns(false, new ChessPoint(9, 4), this);//黑兵 //红旗子
chesses[1, 10] = new Rook(true, new ChessPoint(1, 10), this);//红车
chesses[2, 10] = new Knights(true, new ChessPoint(2, 10), this);//红马
chesses[2, 8] = new Cannons(true, new ChessPoint(2, 8), this);//红炮-----
chesses[3, 10] = new Elephants(true, new ChessPoint(3, 10), this);//红象
chesses[4, 10] = new Mandarins(true, new ChessPoint(4, 10), this);//红士
chesses[5, 10] = new King(true, new ChessPoint(5, 10), this);//红将 chesses[6, 10] = new Mandarins(true, new ChessPoint(6, 10), this);//红士
chesses[7, 10] = new Elephants(true, new ChessPoint(7, 10), this);//红象
chesses[8, 8] = new Cannons(true, new ChessPoint(8, 8), this);//红炮-----
chesses[8, 10] = new Knights(true, new ChessPoint(8, 10), this);//红马
chesses[9, 10] = new Rook(true, new ChessPoint(9, 10), this);//红车 chesses[1, 7] = new Pawns(true, new ChessPoint(1, 7), this);//红兵
chesses[3, 7] = new Pawns(true, new ChessPoint(3, 7), this);//红兵
chesses[5, 7] = new Pawns(true, new ChessPoint(5, 7), this);//红兵
chesses[7, 7] = new Pawns(true, new ChessPoint(7, 7), this);//红兵
chesses[9, 7] = new Pawns(true, new ChessPoint(9, 7), this);//红兵
}
}
}
//另一个类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;namespace chess2
{
public abstract class Chess:Label
{
//public bool isSelect = false;//是否为当前可移动棋子
public bool isRed;//true表示红方,false表示黑方
public ChessPoint currentPoint;//当前虚拟坐标点
public String chessText;
public ChessBoard chessBoard; //构造
public Chess(bool isRed ,ChessPoint targetpoint, ChessBoard chessBoard)
{
this.isRed = isRed;
this.currentPoint = targetpoint;
this.chessBoard = chessBoard;
//this.Image = image; realLocation(targetpoint);
this.AutoSize = false;
this.Size = new Size(53, 53); //改变标签的形状为圆形
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(this.ClientRectangle);
this.Region = new Region(path);
} //根据给定的虚拟坐标定位到真实的坐标
public void realLocation(ChessPoint targetpoint)
{
Point p = targetpoint.getRealPoint();
this.Location = p;
} //判断是否能够移动到目标点
public abstract bool canMove(ChessPoint targetPoint); //棋子的移动方法
public void moveTo(ChessPoint targetPoint)
{
if (targetPoint.x < 1 || targetPoint.x > 9 || targetPoint.y < 1 || targetPoint.y > 10)
return;
//获取目标点上的棋子
Chess targetChess = chessBoard[targetPoint.x, targetPoint.y];
//如果目标棋子的颜色与当前棋子的颜色相同,则不能移动;
if (targetChess != null && targetChess.isRed == this.isRed)
return; //是否能移动
if (!canMove(targetPoint)) return; //吃掉对方老王
if (chessBoard[targetPoint] is King)
throw new Exception(this.isRed ? "红方胜" : "黑方胜"); //移动
chessBoard[currentPoint] = null;
chessBoard[targetPoint] = this; currentPoint = targetPoint;
realLocation(targetPoint);
} //获取两点间的棋子数(直线)
public int getChessCount(ChessPoint start, ChessPoint end)
{
int count = 0;//记录棋子数
if (start.y == end.y)
{
int min = Math.Min(start.x, end.x);
int max = Math.Max(start.x, end.x);
for (int i = min + 1; i < max; i++)
{
if (chessBoard[i, start.y] != null)
count++;
}
}
else if(start.x == end.x)
{
int min = Math.Min(start.y, end.y);
int max = Math.Max(start.y, end.y);
for (int i = min + 1; i < max; i++)
{
if (chessBoard[start.x, i] != null)
count++;
}
}
return count;
}
}
}
表示索引器
在这里 只能索引两个整数
自己写的话可以
class A
{
this[int i]
{
get{}
set{}
}
this[string str]
{get{}
set{}
}
}
索引器可以有返回值的
另外索引器是属于类的
private Chess[,] chesses = new Chess[10, 11];//存放棋子
在这个类里尼定义了一个Chess的数组。。
可以通过 ChessBoard[x,y]坐标获取这个数组中对应的Chess极chesses[x,y];
有关索引器详细信息
请参照
http://msdn.microsoft.com/en-us/library/dk1507sz.aspx