| 使用C#编写Windows Forms应用程序(下) |
|
| http://www.sina.com.cn 2001/10/29 17:50 新浪科技 |
|
|
文/George Shepherd 清单 1
public struct BoardSpace {
public BoardSpace(Mark mark,
int left,
int top,
int right,
int bottom) {
// Initialize internal state?
}
public void SetMark(Player player) {
// if the space is blank, mark it using
// the player enumeration
}
public void Render(Graphics g) {
Pen pen =
new Pen(Color.FromARGB(170, Color.Black), 3);
switch(m_mark) {
case Mark.XMark:
g.DrawLine(pen, m_left, m_top, m_right,
m_bottom);
g.DrawLine(pen, m_left, m_bottom, m_right,
m_top);
break;
case Mark.OMark:
int cx = m_right - m_left;
int cy = m_bottom - m_top;
g.DrawEllipse(pen, m_left, m_top, cx, cy);
break;
default:
break;
}
}
public Mark m_mark;
public int m_top, m_left, m_right, m_bottom;
};
清单 2
public struct TicTacToeBoard {
BoardSpace[,] m_BoardSpaces;
public void Initialize() {
m_BoardSpaces = new BoardSpace[3,3];
// Initialize each space with a location on the screen and a
// blank mark.
// Here's the first space:
m_BoardSpaces[0, 0] = new BoardSpace(Mark.Blank, 1,
1, 50, 50);
// Do the rest like that?
}
public void ClearBoard() {
// loop through the spaces clearing them
}
public Player EvaluateGame() {
// Check adjacent marks and see who won.
}
public Positions HitTest(int x, int y, Player player) {
// Test the incoming Coords and mark the right space
// using the player enumeration
}
public void Render(Graphics g) {
Pen pen = new Pen(Color.FromARGB(170,
Color.Black), 5);
g.DrawLine(pen, 1, 50, 150, 50);
g.DrawLine(pen, 50, 1, 50, 150);
g.DrawLine(pen, 1, 100, 150, 100);
g.DrawLine(pen, 100, 1, 100, 150);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
m_BoardSpaces[i, j].Render(g);
}
}
}
};
清单 3
public class CSharpTicTacToe : Form {
public Player m_Player = Player.XPlayer;
TicTacToeBoard m_board = new TicTacToeBoard();
public CSharpTicTacToe() {
SetStyle(ControlStyles.Opaque, true);
Size = new Size(500, 500);
Text = "CSharp Tic Tac Toe";
m_board.Initialize();
//Finally add a button so that we can render to a bitmap
Button buttonRestart = new Button();
buttonRestart.Size=new Size(100,50);
buttonRestart.Location=new Point(300,100);
buttonRestart.Text="Restart";
buttonRestart.AddOnClick(new EventHandler(Restart));
this.Controls.Add(buttonRestart);
}
//Fired when the restart button is pressed
private void Restart(object sender, EventArgs e) {
m_Player = Player.XPlayer;
m_board.ClearBoard();
this.Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e) {
base.OnMouseDown(e);
Positions position = m_board.HitTest(e.X, e.Y, m_Player);
if(position == Positions.Unknown) {
return;
}
if(m_Player == Player.XPlayer) {
m_Player = Player.OPlayer;
} else {
m_Player = Player.XPlayer;
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
e.Graphics.SmoothingMode =
SmoothingMode.AntiAlias;
g.FillRectangle(new
SolidBrush(Color.FromARGB(250,
Color.White)), ClientRectangle);
m_board.Render(g);
}
public static void Main() {
Application.Run(new CSharpTicTacToe());
}
}
}
|
|
|
|