【江鸟中原】——简单五子棋
  JmkLogDFQMdR 2023年12月12日 21 0

一、引言

大家好,我是江鸟大白,经历了一个学期鸿蒙得学习后,想自己创建一个项目检验自己的能力,并把它分享给大家,其中遇到了许多的困难,借鉴了不少大牛的思路,最终写出了简单五子棋。

二、五子棋介绍

1.五子棋历史背景:五子棋作为一种深受欢迎的棋类游戏,其简单的规则和丰富的历史背景使其成为了中华文化的一部分。通过了解五子棋的历史、原理和游戏规则,我们可以更好地理解这一游戏的魅力所在,并在游戏中体验到中国传统文化的独特韵味。

2.五子棋游戏原理:五子棋是一种两人对弈的棋类游戏,目的是在棋盘上先将自己的五个棋子连成一线,或是阻止对手完成这个目标。棋盘为 15 行 15 列,每个玩家轮流下一个棋子,棋子只能放在空位上,不能重复放在已经有棋子的位置上。游戏结束后,如果有一方完成了五子连线,或是棋盘上已经没有空位可下,则游戏结束。

三、编写代码

1.初始棋子

void draw_chessman(int type, char *tableline) {

if (type == WHITE)

 printf("●");

if (type == BLACK)

 printf("○");

if (type == BLANK)

 printf("%s", tableline);//此处传入另一个参数tableline,是为了绘制棋盘更加方便

 

}

2.初始棋盘

//棋盘可分为三部分,第一行,最后一行,中间行

//用i代表行,j代表列

void draw_chessboardn(int row, int col, int chessboard[][MAX_COL]) {

for (int i = 0; i < row ; i++) {

 if (i == 0) {

  for (int j = 0; j < col; j++) {

   if (j == 0)

    draw_chessman(chessboard[i][j], "┌ ");

   else if (j == 14)

    draw_chessman(chessboard[i][j], "┐");

   else

    draw_chessman(chessboard[i][j], "┬ ");

  }

  printf("\n");//第一行┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐

 } else if (i == 14) {

  for (int j = 0; j < col; j++) {

   if (j == 0)

    draw_chessman(chessboard[i][j], "└ ");

   else if (j == 14)

    draw_chessman(chessboard[i][j], "┘ ");

   else

    draw_chessman(chessboard[i][j], "┴ ");

  }

  printf("\n");//最后一行└┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘

 } else {

  for (int j = 0; j < col; j++) {

   if (j == 0)

    draw_chessman(chessboard[i][j], "├ ");

   else if (j == 14)

    draw_chessman(chessboard[i][j], "┤");

   else

    draw_chessman(chessboard[i][j], "┼ ");

  }

  printf("\n");//其他行├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤

 }

}

}

 3.判断胜负

//返回当前棋子的值,若值为1,代表黑棋胜,若值为-1,代表白棋胜

int is_win(int chessboard[][MAX_COL], int row, int col) {

int i, j;

for (i = 0; i < row; i++) {

 for (j = 0; j < col; j++) {

  if (chessboard[i][j] == BLANK)

   continue;

 

           //  (-)横着连成五子

  if (j < col - 4)

   if (chessboard[i][j] == chessboard[i][j + 1] && chessboard[i][j] ==      

                       chessboard[i][j + 2]

           && chessboard[i][j] == chessboard[i][j + 3] && chessboard[i][j]  

                       == chessboard[i][j + 4])

    return chessboard[i][j];

 

           //  (|)竖着连成五子

  if (i < row - 4)

   if (chessboard[i][j] == chessboard[i + 1][j] && chessboard[i][j] ==  

                       chessboard[i + 2][j]

           && chessboard[i][j] == chessboard[i + 3][j] && chessboard[i][j]  

                       == chessboard[i + 4][j])

    return chessboard[i][j];

 

           //  (\)左斜连成五子

  if (i < row - 4 && j < col - 4)

   if (chessboard[i][j] == chessboard[i + 1][j + 1] && chessboard[i][j] ==  

                       chessboard[i + 2][j + 2]

           && chessboard[i][j] == chessboard[i + 3][j + 3] && chessboard[i]  

                       [j] == chessboard[i + 4][j + 4])

    return chessboard[i][j];

 

           //  (/)右斜连成五子  

  if (i < row - 4 && j > 4)                  

   if (chessboard[i][j] == chessboard[i + 1][j - 1] && chessboard[i][j] ==  

                       chessboard[i + 2][j - 2]

           && chessboard[i][j] == chessboard[i + 3][j - 3] && chessboard[i]  

                       [j] == chessboard[i + 4][j - 4])

    return chessboard[i][j];

 }

}

return BLANK;

}

4.确认落子位置

 public int[] concluate() {

       int[] res = new int[2];

       int max = 0;

       for(int i = 0; i < Constant.ROW; i++) {

           for(int j = 0; j < Constant.COL; j++) {

               if(board[i][j] != 0) {

                   continue;

               }

               int num = getScore(i,j);

               if(num == 200000){

                   res[0] = i;

                   res[1] = j;

                   return res;

               }

               if(num > max) {

                   max = num;

                   res[0] = i;

                   res[1] = j;

               }

           }

       }

       return res;

   }、

实现结果

【江鸟中原】——简单五子棋_i++

四、总结

五子棋作为一种深受欢迎的棋类游戏,其简单的规则和丰富的历史背景使其成为了中华文化的一部分。通过了解五子棋的历史、原理和游戏规则,我们可以更好地理解这一游戏的魅力所在,并在游戏中体验到中国传统文化的独特韵味。


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月12日 0

暂无评论