x01.Weiqi.2 保存棋谱
高手对决:上官金虹环在心,小李飞刀刀已发。牵强到编程:设计,时时刻刻环在心;实现,勇往直前刀已发。虽然最终小李飞刀战胜了上官金虹,但这并不表示实践高于理论。
—— 实践,理论,再实践 ... 这是个无穷递归!
此中有深意,欲辨已忘言。
保存棋谱的功能,相对提子算法,要简单得多。
首先,添加一个数据库文件:DbStep.mdf;建立表:Steps;添加两个字段:ID,StepContent。ID 为自动加一,StepContent 为 text 类型。需要说明的是,在程序中,Steps 表示所有的棋步,即一局棋。而在数据库中,Steps却表示所有的棋局。单独一局棋的内容,保存在 StepContent 字段中。这是冲突的地方,但将表名改为 Stepses,又显得不伦不类。使用 Entity Framework 技术,剩下的不值一提,Skip!
将保存的棋谱加载并显示运行,需用到 x01.Weiqi.1 中的功能。所以,到了重构的时候。
重构后的关键类图如下:
![](https://pic002.cnblogs.com/images/2011/128702/2011051401121095.png)
主要功能都在 BoardBase 中实现。加载并运行的逻辑,单独写入 StepBoard 类。
代码文件 StepBoard.cs 的内容如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace x01.Weiqi.Board
{
class StepBoard : BoardBase
{
List<StepContent> m_StepContent =new List<StepContent>();
int m_count =1;
struct StepContent
{
publicint Col;
publicint Row;
publicint Count;
}
public StepBoard(int chessSize =38)
: base(chessSize)
{
SettingsWindow settingsWin =new SettingsWindow();
settingsWin.ShowDialog();
int id = settingsWin.ID;
GetStepContent(id);
}
publicvoid GetStepContent(int id)
{
string steps = StepService.GetSteps(id);
if (string.IsNullOrEmpty(steps))
{
MessageBox.Show("Cannot get step content!");
return;
}
m_StepContent.Clear();
StepContent stepContent =new StepContent();
string[] step = steps.Split(',');
for (int i =0; i < step.Length; i++)
{
if (i %3==0)
{
stepContent =new StepContent();
stepContent.Col = Convert.ToInt32(step[i]);
}
elseif (i %3==1)
{
stepContent.Row = Convert.ToInt32(step[i]);
}
elseif (i %3==2)
{
stepContent.Count = Convert.ToInt32(step[i]);
m_StepContent.Add(stepContent);
}
}
}
publicvoid NextOne()
{
if (m_count >= m_StepContent.Count)
{
return;
}
foreach (var item in m_StepContent)
{
// If 内为复制,但并不以为不好
if (item.Count == m_count)
{
int col = item.Col;
int row = item.Row;
if (Steps[col, row].Color != ChessColor.Empty)
{
return;
}
if (NotInPos.X == col && NotInPos.Y == row)
{
return;
}
else
{
// If Pos(struct) is property, must use new.
NotInPos =new Pos(-1, -1);
}
DrawChess(col, row);
Eat(col, row);
}
}
m_count++;
}
protectedoverridevoid OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
NextOne();
}
protectedoverridevoid OnMouseRightButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
if (m_count <=1)
{
return;
}
BackOne();
m_count--;
}
}
}
{
class StepBoard : BoardBase
{
List<StepContent> m_StepContent =new List<StepContent>();
int m_count =1;
struct StepContent
{
publicint Col;
publicint Row;
publicint Count;
}
public StepBoard(int chessSize =38)
: base(chessSize)
{
SettingsWindow settingsWin =new SettingsWindow();
settingsWin.ShowDialog();
int id = settingsWin.ID;
GetStepContent(id);
}
publicvoid GetStepContent(int id)
{
string steps = StepService.GetSteps(id);
if (string.IsNullOrEmpty(steps))
{
MessageBox.Show("Cannot get step content!");
return;
}
m_StepContent.Clear();
StepContent stepContent =new StepContent();
string[] step = steps.Split(',');
for (int i =0; i < step.Length; i++)
{
if (i %3==0)
{
stepContent =new StepContent();
stepContent.Col = Convert.ToInt32(step[i]);
}
elseif (i %3==1)
{
stepContent.Row = Convert.ToInt32(step[i]);
}
elseif (i %3==2)
{
stepContent.Count = Convert.ToInt32(step[i]);
m_StepContent.Add(stepContent);
}
}
}
publicvoid NextOne()
{
if (m_count >= m_StepContent.Count)
{
return;
}
foreach (var item in m_StepContent)
{
// If 内为复制,但并不以为不好
if (item.Count == m_count)
{
int col = item.Col;
int row = item.Row;
if (Steps[col, row].Color != ChessColor.Empty)
{
return;
}
if (NotInPos.X == col && NotInPos.Y == row)
{
return;
}
else
{
// If Pos(struct) is property, must use new.
NotInPos =new Pos(-1, -1);
}
DrawChess(col, row);
Eat(col, row);
}
}
m_count++;
}
protectedoverridevoid OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
NextOne();
}
protectedoverridevoid OnMouseRightButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
if (m_count <=1)
{
return;
}
BackOne();
m_count--;
}
}
}
源代码可从 http://www.cnblogs.com/china_x01 的 download\code\x01.Weiqi 获取。
Copyright (c) 2011 by x01(china_x01@qq.com),未经许可,请勿转载。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步