C#版智能五子棋游戏(4)-主窗体
C#版智能五子棋游戏(4)-主窗体
MainForm 布局局部类,随便拉起来的,由VS生成
MainForm类
最后是程序入口点,ChessForm结束,框架也结束
MainForm 布局局部类,随便拉起来的,由VS生成
namespace QiuQiu.ChessForm
{
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.chessboard1 = new QiuQiu.ChessEngine.Chessboard();
this.label1 = new System.Windows.Forms.Label();
this.lbNowPlaer = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.lbBlackTime = new System.Windows.Forms.Label();
this.lbWhiteTime = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.player1ComboBox = new System.Windows.Forms.ComboBox();
this.player2ComboBox = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(457, 415);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(59, 23);
this.button1.TabIndex = 1;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// chessboard1
//
this.chessboard1.CanManChess = false;
this.chessboard1.Location = new System.Drawing.Point(0, -2);
this.chessboard1.Name = "chessboard1";
this.chessboard1.Size = new System.Drawing.Size(460, 460);
this.chessboard1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(466, 236);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 9;
this.label1.Text = "当前手:";
//
// lbNowPlaer
//
this.lbNowPlaer.AutoSize = true;
this.lbNowPlaer.Location = new System.Drawing.Point(524, 236);
this.lbNowPlaer.Name = "lbNowPlaer";
this.lbNowPlaer.Size = new System.Drawing.Size(29, 12);
this.lbNowPlaer.TabIndex = 10;
this.lbNowPlaer.Text = "黑棋";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(466, 277);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 12);
this.label3.TabIndex = 11;
this.label3.Text = "黑棋时间:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(466, 328);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(65, 12);
this.label4.TabIndex = 6;
this.label4.Text = "白棋时间:";
//
// lbBlackTime
//
this.lbBlackTime.AutoSize = true;
this.lbBlackTime.Location = new System.Drawing.Point(492, 300);
this.lbBlackTime.Name = "lbBlackTime";
this.lbBlackTime.Size = new System.Drawing.Size(53, 12);
this.lbBlackTime.TabIndex = 7;
this.lbBlackTime.Text = "00:00:00";
//
// lbWhiteTime
//
this.lbWhiteTime.AutoSize = true;
this.lbWhiteTime.Location = new System.Drawing.Point(492, 354);
this.lbWhiteTime.Name = "lbWhiteTime";
this.lbWhiteTime.Size = new System.Drawing.Size(53, 12);
this.lbWhiteTime.TabIndex = 8;
this.lbWhiteTime.Text = "00:00:00";
//
// button2
//
this.button2.Location = new System.Drawing.Point(522, 415);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 23);
this.button2.TabIndex = 1;
this.button2.Text = "停止";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(463, 19);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 9;
this.label2.Text = "黑棋:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(463, 97);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(41, 12);
this.label5.TabIndex = 9;
this.label5.Text = "白棋:";
//
// player1ComboBox
//
this.player1ComboBox.FormattingEnabled = true;
this.player1ComboBox.Location = new System.Drawing.Point(465, 52);
this.player1ComboBox.Name = "player1ComboBox";
this.player1ComboBox.Size = new System.Drawing.Size(121, 20);
this.player1ComboBox.TabIndex = 12;
//
// player2ComboBox
//
this.player2ComboBox.FormattingEnabled = true;
this.player2ComboBox.Location = new System.Drawing.Point(465, 131);
this.player2ComboBox.Name = "player2ComboBox";
this.player2ComboBox.Size = new System.Drawing.Size(121, 20);
this.player2ComboBox.TabIndex = 13;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(599, 467);
this.Controls.Add(this.player2ComboBox);
this.Controls.Add(this.player1ComboBox);
this.Controls.Add(this.label5);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lbNowPlaer);
this.Controls.Add(this.label3);
this.Controls.Add(this.label4);
this.Controls.Add(this.lbBlackTime);
this.Controls.Add(this.lbWhiteTime);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.chessboard1);
this.Name = "MainForm";
this.Text = "五子棋";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
internal QiuQiu.ChessEngine.Chessboard chessboard1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lbNowPlaer;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label lbBlackTime;
private System.Windows.Forms.Label lbWhiteTime;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ComboBox player1ComboBox;
private System.Windows.Forms.ComboBox player2ComboBox;
}
}
{
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.chessboard1 = new QiuQiu.ChessEngine.Chessboard();
this.label1 = new System.Windows.Forms.Label();
this.lbNowPlaer = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.lbBlackTime = new System.Windows.Forms.Label();
this.lbWhiteTime = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.player1ComboBox = new System.Windows.Forms.ComboBox();
this.player2ComboBox = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(457, 415);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(59, 23);
this.button1.TabIndex = 1;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// chessboard1
//
this.chessboard1.CanManChess = false;
this.chessboard1.Location = new System.Drawing.Point(0, -2);
this.chessboard1.Name = "chessboard1";
this.chessboard1.Size = new System.Drawing.Size(460, 460);
this.chessboard1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(466, 236);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 9;
this.label1.Text = "当前手:";
//
// lbNowPlaer
//
this.lbNowPlaer.AutoSize = true;
this.lbNowPlaer.Location = new System.Drawing.Point(524, 236);
this.lbNowPlaer.Name = "lbNowPlaer";
this.lbNowPlaer.Size = new System.Drawing.Size(29, 12);
this.lbNowPlaer.TabIndex = 10;
this.lbNowPlaer.Text = "黑棋";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(466, 277);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 12);
this.label3.TabIndex = 11;
this.label3.Text = "黑棋时间:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(466, 328);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(65, 12);
this.label4.TabIndex = 6;
this.label4.Text = "白棋时间:";
//
// lbBlackTime
//
this.lbBlackTime.AutoSize = true;
this.lbBlackTime.Location = new System.Drawing.Point(492, 300);
this.lbBlackTime.Name = "lbBlackTime";
this.lbBlackTime.Size = new System.Drawing.Size(53, 12);
this.lbBlackTime.TabIndex = 7;
this.lbBlackTime.Text = "00:00:00";
//
// lbWhiteTime
//
this.lbWhiteTime.AutoSize = true;
this.lbWhiteTime.Location = new System.Drawing.Point(492, 354);
this.lbWhiteTime.Name = "lbWhiteTime";
this.lbWhiteTime.Size = new System.Drawing.Size(53, 12);
this.lbWhiteTime.TabIndex = 8;
this.lbWhiteTime.Text = "00:00:00";
//
// button2
//
this.button2.Location = new System.Drawing.Point(522, 415);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 23);
this.button2.TabIndex = 1;
this.button2.Text = "停止";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(463, 19);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 9;
this.label2.Text = "黑棋:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(463, 97);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(41, 12);
this.label5.TabIndex = 9;
this.label5.Text = "白棋:";
//
// player1ComboBox
//
this.player1ComboBox.FormattingEnabled = true;
this.player1ComboBox.Location = new System.Drawing.Point(465, 52);
this.player1ComboBox.Name = "player1ComboBox";
this.player1ComboBox.Size = new System.Drawing.Size(121, 20);
this.player1ComboBox.TabIndex = 12;
//
// player2ComboBox
//
this.player2ComboBox.FormattingEnabled = true;
this.player2ComboBox.Location = new System.Drawing.Point(465, 131);
this.player2ComboBox.Name = "player2ComboBox";
this.player2ComboBox.Size = new System.Drawing.Size(121, 20);
this.player2ComboBox.TabIndex = 13;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(599, 467);
this.Controls.Add(this.player2ComboBox);
this.Controls.Add(this.player1ComboBox);
this.Controls.Add(this.label5);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lbNowPlaer);
this.Controls.Add(this.label3);
this.Controls.Add(this.label4);
this.Controls.Add(this.lbBlackTime);
this.Controls.Add(this.lbWhiteTime);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.chessboard1);
this.Name = "MainForm";
this.Text = "五子棋";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
internal QiuQiu.ChessEngine.Chessboard chessboard1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lbNowPlaer;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label lbBlackTime;
private System.Windows.Forms.Label lbWhiteTime;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ComboBox player1ComboBox;
private System.Windows.Forms.ComboBox player2ComboBox;
}
}
MainForm类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QiuQiu.ChessEngine;
using QiuQiu.QChessPlayer;
namespace QiuQiu.ChessForm
{
public partial class MainForm : Form
{
private IChessEngine _engine;
private ChessPlayers _chessPalyers;
public MainForm()
{
InitializeComponent();
_chessPalyers = new ChessPlayers();
InitEngine();
}
private void InitEngine()
{
if (_engine != null)
_engine.Dispose();
_engine = new ChessEngine.ChessEngine();
_engine.DataChange += new EventHandler(_engine_DataChange);
_engine.PlayerChange += new EventHandler(_engine_PlayerChange);
_engine.EventFire += new EngineEventHandle(_engine_EventFire);
_engine.Timer.Elapsed += new ChessTimerElapsed(Timer_Elapsed);
chessboard1.SetChessData(_engine.ChessData);
}
/// <summary>
/// 计时器每秒触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer_Elapsed(object sender, ChessTimerEventArgs e)
{
//时间
try
{
Invoke(new EventHandler(timerAction), sender, e);
}
catch { }
}
/// <summary>
/// 计时器触发动作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerAction(object sender, EventArgs e)
{
this.lbBlackTime.Text = ((ChessTimerEventArgs)e).BlackPlayer.ToString().Substring(0, 8);
this.lbWhiteTime.Text = ((ChessTimerEventArgs)e).WhitePlayer.ToString().Substring(0, 8);
}
void _engine_EventFire(IChessEngine sender, EngineEventArgs args)
{
switch (args.EventType)
{
case EventType.BlackWin:
MessageBox.Show("黑棋胜!" + args.Message);
break;
case EventType.WhiteWin:
MessageBox.Show("白棋胜!" + args.Message);
break;
case EventType.DataFull:
MessageBox.Show("平局!" + args.Message);
break;
case EventType.Illegality:
MessageBox.Show((sender.CurrentPlayer.ChessType == ChessType.Black?"黑棋":"白棋") + "犯规!" + args.Message);
break;
case EventType.Exceptions:
MessageBox.Show((sender.CurrentPlayer.ChessType == ChessType.Black ? "黑棋" : "白棋") + "异常!" + args.Message);
break;
}
}
private void _engine_DataChange(object sender, EventArgs e)
{
try
{
chessboard1.SetChessData(_engine.ChessData);
Invoke(new DeleGate(chessboard1.Refresh));
}
catch
{
}
}
void _engine_PlayerChange(object sender, EventArgs e)
{
Invoke(new DeleGate(PlayerChang));
}
private void PlayerChang()
{
lbNowPlaer.Text = _engine.CurrentPlayer.ChessType == ChessType.Black ? "黑棋" : "白棋";
}
private void MainForm_Load(object sender, EventArgs e)
{
_chessPalyers.LoadPlayer();
player1ComboBox.DataSource = _chessPalyers.GetPlayerNames();
player2ComboBox.DataSource = _chessPalyers.GetPlayerNames();
}
protected override void OnClosing(CancelEventArgs e)
{
_engine.Stop();
base.OnClosing(e);
}
private void button1_Click(object sender, EventArgs e)
{
InitEngine();
_engine.Plyer1 = _chessPalyers.GetPlayer(player1ComboBox.SelectedItem.ToString(), this);// new QiuQiu.QChessPlayer.Player();
_engine.Plyer2 = _chessPalyers.GetPlayer(player2ComboBox.SelectedItem.ToString(), this);// new ManPlayer(chessboard1);
if (_engine.Plyer1 == null)
{
MessageBox.Show("黑棋玩家加载失败!");
return;
}
if (_engine.Plyer2 == null)
{
MessageBox.Show("白棋玩家加载失败!");
return;
}
chessboard1.Refresh();
_engine.Start();
}
void button2_Click(object sender, System.EventArgs e)
{
_engine.Stop();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QiuQiu.ChessEngine;
using QiuQiu.QChessPlayer;
namespace QiuQiu.ChessForm
{
public partial class MainForm : Form
{
private IChessEngine _engine;
private ChessPlayers _chessPalyers;
public MainForm()
{
InitializeComponent();
_chessPalyers = new ChessPlayers();
InitEngine();
}
private void InitEngine()
{
if (_engine != null)
_engine.Dispose();
_engine = new ChessEngine.ChessEngine();
_engine.DataChange += new EventHandler(_engine_DataChange);
_engine.PlayerChange += new EventHandler(_engine_PlayerChange);
_engine.EventFire += new EngineEventHandle(_engine_EventFire);
_engine.Timer.Elapsed += new ChessTimerElapsed(Timer_Elapsed);
chessboard1.SetChessData(_engine.ChessData);
}
/// <summary>
/// 计时器每秒触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer_Elapsed(object sender, ChessTimerEventArgs e)
{
//时间
try
{
Invoke(new EventHandler(timerAction), sender, e);
}
catch { }
}
/// <summary>
/// 计时器触发动作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerAction(object sender, EventArgs e)
{
this.lbBlackTime.Text = ((ChessTimerEventArgs)e).BlackPlayer.ToString().Substring(0, 8);
this.lbWhiteTime.Text = ((ChessTimerEventArgs)e).WhitePlayer.ToString().Substring(0, 8);
}
void _engine_EventFire(IChessEngine sender, EngineEventArgs args)
{
switch (args.EventType)
{
case EventType.BlackWin:
MessageBox.Show("黑棋胜!" + args.Message);
break;
case EventType.WhiteWin:
MessageBox.Show("白棋胜!" + args.Message);
break;
case EventType.DataFull:
MessageBox.Show("平局!" + args.Message);
break;
case EventType.Illegality:
MessageBox.Show((sender.CurrentPlayer.ChessType == ChessType.Black?"黑棋":"白棋") + "犯规!" + args.Message);
break;
case EventType.Exceptions:
MessageBox.Show((sender.CurrentPlayer.ChessType == ChessType.Black ? "黑棋" : "白棋") + "异常!" + args.Message);
break;
}
}
private void _engine_DataChange(object sender, EventArgs e)
{
try
{
chessboard1.SetChessData(_engine.ChessData);
Invoke(new DeleGate(chessboard1.Refresh));
}
catch
{
}
}
void _engine_PlayerChange(object sender, EventArgs e)
{
Invoke(new DeleGate(PlayerChang));
}
private void PlayerChang()
{
lbNowPlaer.Text = _engine.CurrentPlayer.ChessType == ChessType.Black ? "黑棋" : "白棋";
}
private void MainForm_Load(object sender, EventArgs e)
{
_chessPalyers.LoadPlayer();
player1ComboBox.DataSource = _chessPalyers.GetPlayerNames();
player2ComboBox.DataSource = _chessPalyers.GetPlayerNames();
}
protected override void OnClosing(CancelEventArgs e)
{
_engine.Stop();
base.OnClosing(e);
}
private void button1_Click(object sender, EventArgs e)
{
InitEngine();
_engine.Plyer1 = _chessPalyers.GetPlayer(player1ComboBox.SelectedItem.ToString(), this);// new QiuQiu.QChessPlayer.Player();
_engine.Plyer2 = _chessPalyers.GetPlayer(player2ComboBox.SelectedItem.ToString(), this);// new ManPlayer(chessboard1);
if (_engine.Plyer1 == null)
{
MessageBox.Show("黑棋玩家加载失败!");
return;
}
if (_engine.Plyer2 == null)
{
MessageBox.Show("白棋玩家加载失败!");
return;
}
chessboard1.Refresh();
_engine.Start();
}
void button2_Click(object sender, System.EventArgs e)
{
_engine.Stop();
}
}
}
最后是程序入口点,ChessForm结束,框架也结束
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace QiuQiu.ChessForm
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
namespace QiuQiu.ChessForm
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
原帖CSDN: http://topic.csdn.net/u/20080425/12/4bbb0f82-f963-4743-ac0e-f806333ac5f9.html
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网