原题:
一共分两步。
Now write a method called CheckPosition that is passed the row number and column number of the chesspiece and returns true if this is on the board or false if not, i.e. it will check if either value is less than 1 or greater than 8.
Modify your Open File... menu event method so that values from the csv array are extracted and the piece is drawn only if the csv array has the correct number of elements. If it doesn't have the correct number of elements or is not on the board, write the raw line read from the file to the console window.
Before drawing the piece, call your CheckPosition method and only draw the piece if the method returns true. If the method returns false then write the raw line read from the file to the console window.————————————————————华丽丽的分割线————————————————————我现在自己的代码: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");
}
}
}请问,是要写一个类似public bool CheckPisition(int row, int column)的method吗?之后如何写?
一共分两步。
Now write a method called CheckPosition that is passed the row number and column number of the chesspiece and returns true if this is on the board or false if not, i.e. it will check if either value is less than 1 or greater than 8.
Modify your Open File... menu event method so that values from the csv array are extracted and the piece is drawn only if the csv array has the correct number of elements. If it doesn't have the correct number of elements or is not on the board, write the raw line read from the file to the console window.
Before drawing the piece, call your CheckPosition method and only draw the piece if the method returns true. If the method returns false then write the raw line read from the file to the console window.————————————————————华丽丽的分割线————————————————————我现在自己的代码: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");
}
}
}请问,是要写一个类似public bool CheckPisition(int row, int column)的method吗?之后如何写?
bool CheckPosition(x,y){
Func<int,bool> isInBoard = (n)=> n > 0 && n < 9;
return isInBoard (x) && isInBoard(y);
}顺便一说西洋棋那么多子...
每一种写一个Drawxxx跟上一大串的if else 或 switch case 会很蛋疼的...
我都说了,我已经翻译了!!!!!!!!!
http://topic.csdn.net/u/20110926/04/fb9d956a-15ef-402b-9ffd-1a4df2fdad3a.html