//blog/tags/about/friends

xChess 叉棋 类五子棋小游戏

- code csharp gobang game

前言

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

轩然(左)我(右)

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

玩法

下载

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

GitHub: https://github.com/jsun969/xChess

点击 Release

下载最新版本的 exe 文件

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

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

设置

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

扩大棋盘

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

存档

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

代码

新建棋盘

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