欢迎访问
这里是荆棘的私人博客
螓首蛾眉
xChess叉棋 类五子棋小游戏

前言

失踪人口回归,其实这个项目我两个星期之前就写完了。但是由于各种原因一直着没发出来。起因是我跟轩然某天无聊各自写了个cli五子棋

https://upyun.jsun.limecho.net/2020/12/image-10-1024x415.png!/format/webp/lossless/true
轩然(左)我(右)

放到学校里跟同学玩了一会就突然想做窗体

玩法

下载

温馨提示:本游戏在触摸屏上游玩体验更佳
首先你要下载这个软件,先点击下面这个链接

https://upyun.jsun.limecho.net/2020/12/image-1-1024x481.png!/format/webp/lossless/true
点击Release
https://upyun.jsun.limecho.net/2020/12/image-2-1024x408.png!/format/webp/lossless/true
下载最新版本的exe文件

下载完以后打开即可,绿色软件无需安装

https://upyun.jsun.limecho.net/2020/12/image-3.png!/format/webp/lossless/true

点击方格即可落子,首次打开默认为双人五子棋

设置

设置中可以设置一些基本游戏玩法滑块输入当然是为了方便触摸屏

https://upyun.jsun.limecho.net/2020/12/image-5.png!/format/webp/lossless/true

扩大棋盘

如果嫌棋盘太小拖动窗口点击新游戏即可最大化也行

https://upyun.jsun.limecho.net/2020/12/20201205.gif!/format/webp/lossless/true

存档

游戏支持导出为xChess存档文件,使用存档仅需导入拖入游戏界面即可

https://upyun.jsun.limecho.net/2020/12/image-6.png!/format/webp/lossless/true

代码

新建棋盘

new Button来自定义棋盘大小

for (int i = 1; i <= rowNum; i++)
{
    for (int j = 1; j <= columnNum; j++)
    {
        Btn[i, j] = new chessBtn
        {
            btnPosX = i,
            btnPosY = j,
            Location = new Point(12 + 36 * (j - 1), 12 + 36 * (i - 1)),
            Size = new Size(30, 30),
            Text = "",
            TabStop = false,
        };
        groupBox1.Controls.Add(Btn[i, j]);
        Btn[i, j].Click += new EventHandler(Btn_Click);
        playerNameTmp[i, j] = 0;
    }
}

输赢判断

轩然给的思路,还挺不错

int cou = 0;
int i, j;
//行 
for (i = chessBt.btnPosY + 1; playerNameTmp[chessBt.btnPosX, i] == playerNum; i++)
{
    cou++;
}
for (i = chessBt.btnPosY - 1; playerNameTmp[chessBt.btnPosX, i] == playerNum; i--)
{
    cou++;
}
if (cou == winChessNum - 1) ifPlayerWin();
//列 
cou = 0;
for (i = chessBt.btnPosX + 1; playerNameTmp[i, chessBt.btnPosY] == playerNum; i++)
{
    cou++;
}
for (i = chessBt.btnPosX - 1; playerNameTmp[i, chessBt.btnPosY] == playerNum; i--)
{
    cou++;
}
if (cou == winChessNum - 1) ifPlayerWin();
//右对角线
cou = 0;
i = chessBt.btnPosX + 1;
j = chessBt.btnPosY + 1;
while (playerNameTmp[i, j] == playerNum)
{
    cou++;
    i++;
    j++;
}
i = chessBt.btnPosX - 1;
j = chessBt.btnPosY - 1;
while (playerNameTmp[i, j] == playerNum)
{
    cou++;
    i--;
    j--;
}
if (cou == winChessNum - 1) ifPlayerWin();
//左对角线
cou = 0;
i = chessBt.btnPosX - 1;
j = chessBt.btnPosY + 1;
while (playerNameTmp[i, j] == playerNum)
{
    cou++;
    i--;
    j++;
}
i = chessBt.btnPosX + 1;
j = chessBt.btnPosY - 1;
while (playerNameTmp[i, j] == playerNum)
{
    cou++;
    i++;
    j--;
}
if (cou == winChessNum - 1) ifPlayerWin();

设置暂存

再次打开游戏时,棋盘大小和设置不会改变

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Properties.Settings.Default.playerCou = playerCnt;
    Properties.Settings.Default.winChessNum = winChessNum;
    Properties.Settings.Default.formWidth = Width;
    Properties.Settings.Default.formHeight = Height;
    if (WindowState == FormWindowState.Maximized) Properties.Settings.Default.ifMaximize = true;
    else Properties.Settings.Default.ifMaximize = false;
    Properties.Settings.Default.Save();
}

存档

采用Base64编码存档

string outputData = Height.ToString() + "\n" + Width.ToString() + "\n" + playerCnt.ToString() + "\n" + winChessNum.ToString() + "\n" + playerNum.ToString() + "\n" + playedChessCnt.ToString() + "\n";
if (WindowState == FormWindowState.Maximized) outputData += "true";
else outputData += "false";
for (int i = 1; i <= rowNum; i++)
{
    for (int j = 1; j <= columnNum; j++)
    {
        if (playerNameTmp[i, j] != 0)
        {
            outputData += "\n" + playerNameTmp[i, j].ToString();
            outputData += "\n" + i;
            outputData += "\n" + j;
        }
    }
}
File.WriteAllText(sfd.FileName, Convert.ToBase64String(Encoding.UTF8.GetBytes(outputData)));

后记

本来是作为班级娱乐活动放到学校大屏幕上玩的,但后来被班主任删了QwQ

赞赏

发表评论

textsms
account_circle
email

螓首蛾眉

xChess叉棋 类五子棋小游戏
前言 失踪人口回归,其实这个项目我两个星期之前就写完了。但是由于懒各种原因一直拖着没发出来。起因是我跟轩然某天无聊各自写了个cli的五子棋 轩然(左)我(右) 放到学校里跟同…
扫描二维码继续阅读
2020-12-05