# 双人小游戏—五子棋(c语言)

2021-06-05 09:11:25游戏开发人已围观

# 如何实现

1.显示棋盘很简单，慢慢凑棋盘就好
2. 用户下子，注意两个条件：棋子在棋盘里，下子位置未被占用。
3.判断是最难的，

``````Showboard(board, ROW, COL)；//展示棋盘
Playermove(board, ROW, COL, cur);//玩家下注，cur表示哪个玩家下子
Judge(board, ROW, COL);//判断5子连线
Getcount(board[][COL], row, col, dir)//计算方向dir相同棋子数
``````

# 代码

``````#ifndef __FIVECHREE_H__
#define __FIVECHEE_H__

#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)

#define ROW 10//棋盘行数
#define COL 10//棋盘列数
#define INIT '*'//棋盘初始化

#define PLAYER1 1
#define PLAYER2 2
#define NEXT 3//继续往下下
#define DRAW 4//棋盘下满 平局

//8个方向
#define	UP 10
#define	RIGHT_UP 11
#define	RIGHT 12
#define	RIGHT_DOWN 13
#define	DOWN 14
#define	LEFT_DOWN 15
#define	LEFT 16
#define	LEFT_UP 17

extern void Game();

#endif
``````

main函数源文件

``````#include"fivechree.h"
int main(){
int quit = 0;
while (!quit){
int select = 0;
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 2:
quit = 1;
break;
default:
printf("Enter Error!\n");
break;

}

}
printf("Byebye\n");
system("pause");
return 0;
}
``````

``````#include"fivechree.h"

static int x = 0;
static int y = 0;

printf("+---------------------+\n");
printf("+- 1.Play    2.Exit  -+\n");
printf("+---------------------+\n");

}
static void Showboard(int board[][COL], int row, int col){//展示棋盘 o玩家1棋子，x玩家2棋子
system("cls");
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
switch (board[i][j]){
case PLAYER1:
board[i][j] = 'o';
break;
case PLAYER2:
board[i][j] = 'x';
break;
case 0:
board[i][j] = INIT;
break;
default:
break;
}
}
}
printf("  ");
for (int i =1; i <= row; i++){
printf("%2d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++){
printf("%-2d", i);
for (int j = 1; j <= col; j++){
printf(" %c ", board[i - 1][j - 1]);
}
printf("\n");
}
}

static void Playermove(int board[][COL], int row, int col, int who){//玩家下子，who 为哪个玩家下子
while (1){
scanf("%d %d", &x, &y);
if (x<1 || x>row || y<1 || y>col){  //超过棋盘范围
printf("Postion is error!\n");
continue;
}
if (board[x - 1][y - 1] == INIT){//判断位置是否已被下子
board[x - 1][y - 1] = who;
break;
}
printf("Postion is not empty\n");
}

}

static int Getcount(int board[][COL], int row, int col, int dir){//判断8个方向相同棋子的数目
int _x = x;//_x,_y变化，后面与x，y棋子相比较
int _y = y;
int count = 0;
while (1){
switch (dir){
case UP:
_x--;
break;
case DOWN:
_x++;
break;
case LEFT:
_y--;
break;
case RIGHT:
_y++;
break;
case RIGHT_UP:
_x--, _y++;
break;
case RIGHT_DOWN:
_x++, _y++;
break;
case LEFT_DOWN:
_x++, _y--;
break;
case LEFT_UP:
_x--, _y--;
break;
default:
break;
}
if (_x>=1 || _x<=row || _y>=1 || _y<=col){//棋子不能越界
if (board[x-1][y-1] == board[_x-1][_y-1]){
//printf("yes\n");
count++;
}
else{
//printf("no\n");
break;
}
}
else{
return count;
}

}
return count;
}
//如何判断：从下子位置的8个方向(上，下，左，右，右上，右下，左上，左下)
//计算相同棋子数目，然后将对角的棋子数相加,等于5说明有5子连线
static int Judge(int board[][COL], int row, int col){
int count1 = Getcount(board, row, col, UP)\
+ Getcount(board, row, col, DOWN);
//printf("%d\n", count1);
if (count1 >= 4){
return board[x-1][y-1];
}
count1 = Getcount(board, row, col, RIGHT_UP)\
+ Getcount(board, row, col, LEFT_DOWN);
//printf("%d\n", count1);
if (count1 >= 4){
return board[x-1][y-1];
}
count1 = Getcount(board, row, col, RIGHT)\
+ Getcount(board, row, col, LEFT);
//printf("%d\n", count1);
if (count1 >= 4){
return board[x-1][y-1];
}
count1 = Getcount(board, row, col, RIGHT_DOWN)\
+ Getcount(board, row, col, LEFT_UP);
if (count1 >= 4){
return board[x-1][y-1];
}

for (int i = 0; i < row; i++){//判断棋盘是否下满
for (int j = 0; j < col; j++){
if (board[i][j] == INIT){
return NEXT;
}
}
}
return DRAW;
}

void Game(){
int board[ROW][COL] = { 0 };
//memset(board, INIT, ROW*COL);
int result = 0;
int cur = PLAYER1;
Showboard(board, ROW, COL);//先展示棋盘
while (1){
//Showboard(board, ROW, COL);
Playermove(board, ROW, COL, cur);
Showboard(board, ROW, COL);//棋盘将board数组变化，所以要在判断前将数组变化
result = Judge(board, ROW, COL);
if (result != NEXT){
break;
}
cur = (cur == PLAYER1 ? PLAYER2 : PLAYER1);//三目表达式，注意不是 PLAYER1 ? PLAYER2 : PLAYER1
}

Showboard(board, ROW, COL);
switch (result){
case 'o':
printf("Player1 Win!\n");
break;
case 'x':
printf("Player2 Win!\n");
break;
case DRAW:
printf("Tie Game!\n");
break;
default:
//printf("%c\n", result);
printf("BUG\n");
break;
}
}
``````

Tags：游戏 五子棋 C语言

## 文章评论

 留言与评论（共有 0 条评论）

 验证码：

## 站点信息

• 文章统计247968篇文章
• 浏览统计18356次浏览
• 评论统计1个评论
• 标签管理标签云
• 统计数据：统计代码
• 微信公众号：扫描二维码，关注我们