1 1 rook player1
2 1 pawn player1
7 1 pawn player1
8 1 rook player2
....................国家象棋
上面是一个CSV文件,从左到右分别是 row(行)、column(列)、type(象棋名称)、player(玩家)。题目要求:Write a suitable struct to store all the information about a chess piece and it’s position. Create a class scope variable that will store all chess piece positions in a list.(请写一个struct,用来存储每个棋子的所有信息和它的位置。 同时请创建一个class scope 变量,存储所有棋子的位置于一列表中)以下是我目前的代码,不知对各位看官有用与否?
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
Graphics paper = pictureBoxBoard.CreateGraphics();
DrawBoard(paper);
openFileDialog1.Filter = "CSV Files|*.CSV|ALL Files|*.*";
openFileDialog1.ShowDialog();
string[] csvArray;
string line;
//use try/catch stucture
try
{
StreamReader Readfile = File.OpenText(openFileDialog1.FileName);
while (Readfile.Peek() != -1)
{
int row, column;
string type;
string player;
line = Readfile.ReadLine();
csvArray = line.Split(',');
row = Convert.ToInt32(csvArray[0]);
column = Convert.ToInt32(csvArray[1]);
type = Convert.ToString(csvArray[2]);
player = Convert.ToString(csvArray[3]);
//show the content in the listBox
listBoxOutput.Items.Add(row + " " + column + " " + type.PadRight(6) + " " + player); if (csvArray[2] == "rook")
{
DrawRook(paper, (column - 1) * PieceSize, (row - 1) * PieceSize, GetSeatColor(player));
}
else if (csvArray[2] == "pawn")
{
DrawPawn(paper, (column - 1) * PieceSize, (row - 1) * PieceSize, GetSeatColor(player));
}
else if (csvArray[2] == "rook")
{
DrawRook(paper, (column - 1) * PieceSize, (row - 1) * PieceSize, GetSeatColor(player));
}
else if (csvArray[2] == "knight")
{
DrawKnight(paper, (column - 1) * PieceSize, (row - 1) * PieceSize, GetSeatColor(player));
}
else if (csvArray[2] == "bishop")
{
DrawBishop(paper, (column - 1) * PieceSize, (row - 1) * PieceSize, GetSeatColor(player));
}
else if (csvArray[2] == "queen")
{
DrawQueen(paper, (column - 1) * PieceSize, (row - 1) * PieceSize, GetSeatColor(player));
}
else if (csvArray[2] == "king")
{
DrawKing(paper, (column - 1) * PieceSize, (row - 1) * PieceSize, GetSeatColor(player));
}
} Readfile.Close();
}
catch (Exception)
{
}
}
private Color GetSeatColor(string typeofplayer)
{ if (typeofplayer == "player1")
{
return Color.White;
}
if (typeofplayer == "player2")
{
return Color.Black;
}
else throw new Exception("invaild type of player");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货