由于需要做一个C#的五子棋,在查阅了很多大神们的资料之后自己动手做了一个,实现了人机对战,人人对战,电脑对战.部分素材源自网络.
入门级程序员,欢迎各位同行批评指正!游戏模式切换判断如下:
private void btnSetting_Click(object sender, EventArgs e)
{
bool tmpBgM = Properties.Settings.Default.BgMusic;
string tmpbgmpath = Properties.Settings.Default.BgMusicPath;
bool tmpGM = Properties.Settings.Default.GameModeAI;
bool tmpPF = Properties.Settings.Default.AIBlackPiece;
timerAll.Stop();
if (seter.ShowDialog() == DialogResult.OK)
{
//如果背景音乐相关设置发生变化则进行相应处理
if ((Properties.Settings.Default.BgMusic && !tmpBgM) || (tmpbgmpath != Properties.Settings.Default.BgMusicPath) && Properties.Settings.Default.BgMusic)
{
Loger.WriteLine("Set Background Music:" + Properties.Settings.Default.BgMusicPath);
MusicBox.MusicLoad(Properties.Settings.Default.BgMusicPath);
}
if (!Properties.Settings.Default.BgMusic) { MusicBox.MusicStop(); Loger.WriteLine("Background Music Close."); }
if (isRunning)
{
timerAll.Start();
//游戏模式由玩家对战切换为人机(或者先手玩家切换为电脑先手时)
if ((Properties.Settings.Default.GameModeAI && !tmpGM) || (Properties.Settings.Default.GameModeAI && (Properties.Settings.Default.AIBlackPiece != tmpPF)))
{
//判断下一步棋子颜色并下子
bool tmpColor = Ruler.BlackNext(pieces.GetPiecesStatus());
if (tmpColor == Properties.Settings.Default.AIBlackPiece)
{
tmpPt = AI.Think(pieces.GetPiecesStatus(), Properties.Settings.Default.AIBlackPiece);
pieces.PutPiece(tmpPt.X, tmpPt.Y, tmpColor);
listHistory.Items.Add((tmpColor ? "黑" : "白") + "棋: " + tmpPt.X + "," + tmpPt.Y);
MusicBox.PiecePut();
if (Ruler.CheckWin(tmpPt.X, tmpPt.Y, pieces.GetPiecesStatus()))
{
timerAll.Stop();
MessageBox.Show((tmpColor ? "黑" : "白") + "棋获胜!很遗憾您输了.\n本场对局耗时" + lbTimeAll.Text);
gameStop();
}
}
}
//游戏模式由人机切换为玩家对战
if (tmpGM && !Properties.Settings.Default.GameModeAI)
{
if (Ruler.BlackNext(pieces.GetPiecesStatus()))
{
//设置下一落子方为黑棋
pieceColor = true;
}
else { pieceColor = false; }
}
}
}
}
完整项目下载地址
点击打开链接