目录
一、实现目标
如下图:输入对应的坐标进行交替落子
直到有一方五子连珠时结束
二、代码逻辑
一、分文件处理
先创建game.c game.h main.c Makefile文件
二、InitBord 初始化棋盘
创建一个二维数组用来存放数据,进行内存初始化
// 初始化棋盘
int playbord[COW][COL];
memset(playbord, 0, sizeof(playbord));
三、PlayMove 玩家下棋
运用全局变量进行坐标的输入可以简化以后的需要使用的函数参数个数
int x,y; // 定义每次下棋输入的坐标
// 下棋
void BordMove(int bord[][COL], int cow, int col, int who)
{
printf("PLAYER[%d] is play:", who);
while (1)
{
scanf("%d %d", &x, &y);
// 检测合理
if (x > cow || x < 1 || y < 1 || y > col || bord[x - 1][y - 1] != 0)
{
printf("Please Input:");
}
else
{
bord[x - 1][y - 1] = who;
break;
}
}
}
四、Isover 输赢判断
每走一步就在那一步判断下棋的玩家是否赢,只要在下棋那么上一步就没有分出胜负
// 检测每一步落子的各个方向的连珠情况
int chesscount(int bord[][COL], int cow, int col, enum Dir d)
{
int _x = x - 1;
int _y = y - 1;
int count = 0;
while (1)
{
switch (d)
{
case LEFT:
_x++;
break;
case RIGHT:
_x--;
break;
case UP:
_y++;
break;
case DOWN:
_y--;
break;
case LEFT_UP:
_x++;
_y++;
break;
case LEFT_DOWN:
_x++;
_y--;
break;
case RIGHT_UP:
_x--;
_y++;
break;
case RIGHT_DOWN:
_x--;
_y--;
break;
default:
break;
}
// 合理检测
if (_x < 0 || _x >= cow || _y < 0 || _y >= col || bord[_x][_y] != bord[x - 1][y - 1])
{
break;
}
else
{
count++;
}
}
return count;
}
// 输赢判断函数
int isOver(int bord[][COL], int cow, int col)
{
int count1 = chesscount(bord, cow, col, LEFT) + chesscount(bord, cow, col, RIGHT) + 1;
int count2 = chesscount(bord, cow, col, UP) + chesscount(bord, cow, col, DOWN) + 1;
int count3 = chesscount(bord, cow, col, LEFT_UP) + chesscount(bord, cow, col, RIGHT_DOWN) + 1;
int count4 = chesscount(bord, cow, col, RIGHT_UP) + chesscount(bord, cow, col, LEFT_DOWN) + 1;
if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5)
{
return bord[x - 1][y - 1];// 落子的一方玩家获胜
}
int i = 0;
for (i = 0; i < cow; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (bord[i][j] == 0)
{
return NEXT;// 继续下棋
}
}
}
return DROW;// 平局
}
五、ShowBord 显示棋盘
// 显示棋盘
void ShowBord(int bord[][COL], int cow, int col)
{
printf("\x1b[H\x1b[2J");
int i = 0;
printf(" ");
for (i = 1; i <= col; i++)
{
printf("%3d", i);
}
printf("\n");
for (i = 0; i < cow; i++)
{
printf("%3d ", i + 1);
int j = 0;
for (;j < col; j++)
{
if (bord[i][j] == PLAYER1)
printf(" ● ");
else if (bord[i][j] == PLAYER2)
printf(" ○ ");
else
printf(" . ");
}
printf("\n");
}
}
三、代码实现
game.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COW 20 // 棋盘行
#define COL 20 // 棋盘列
#define NEXT 0 // 游戏继续的条件
#define PLAYER1 1 // 玩家1
#define PLAYER2 2 // 玩家2
#define PLAYER1_WIN 1 // 玩家1赢的判断
#define PLAYER2_WIN 2 // 玩家2赢的判断
#define DROW 3 // 平局的判断
enum Dir // 定义棋子周围的方向
{
LEFT,
RIGHT,
UP,
DOWN,
LEFT_UP,
LEFT_DOWN,
RIGHT_UP,
RIGHT_DOWN
};
void menu(); // 菜单
void game(); // 游戏实现
game.c
#include "game.h"
void menu()
{
printf("*******************************\n");
printf("**** 1.play 0.end ***********\n");
printf("*******************************\n");
printf("Please Select:");
}
int x,y; // 定义每次下棋输入的坐标
// 下棋
void BordMove(int bord[][COL], int cow, int col, int who)
{
printf("PLAYER[%d] is play:", who);
while (1)
{
scanf("%d %d", &x, &y);
// 检测合理
if (x > cow || x < 1 || y < 1 || y > col || bord[x - 1][y - 1] != 0)
{
printf("Please Input:");
}
else
{
bord[x - 1][y - 1] = who;
break;
}
}
}
int chesscount(int bord[][COL], int cow, int col, enum Dir d)
{
int _x = x - 1;
int _y = y - 1;
int count = 0;
while (1)
{
switch (d)
{
case LEFT:
_x++;
break;
case RIGHT:
_x--;
break;
case UP:
_y++;
break;
case DOWN:
_y--;
break;
case LEFT_UP:
_x++;
_y++;
break;
case LEFT_DOWN:
_x++;
_y--;
break;
case RIGHT_UP:
_x--;
_y++;
break;
case RIGHT_DOWN:
_x--;
_y--;
break;
default:
break;
}
if (_x < 0 || _x >= cow || _y < 0 || _y >= col || bord[_x][_y] != bord[x - 1][y - 1])
{
break;
}
else
{
count++;
}
}
return count;
}
int isOver(int bord[][COL], int cow, int col)
{
int count1 = chesscount(bord, cow, col, LEFT) + chesscount(bord, cow, col, RIGHT) + 1;
int count2 = chesscount(bord, cow, col, UP) + chesscount(bord, cow, col, DOWN) + 1;
int count3 = chesscount(bord, cow, col, LEFT_UP) + chesscount(bord, cow, col, RIGHT_DOWN) + 1;
int count4 = chesscount(bord, cow, col, RIGHT_UP) + chesscount(bord, cow, col, LEFT_DOWN) + 1;
if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5)
{
return bord[x - 1][y - 1];
}
int i = 0;
for (i = 0; i < cow; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (bord[i][j] == 0)
{
return NEXT;
}
}
}
return DROW;
}
void ShowBord(int bord[][COL], int cow, int col)
{
printf("\x1b[H\x1b[2J");
int i = 0;
printf(" ");
for (i = 1; i <= col; i++)
{
printf("%3d", i);
}
printf("\n");
for (i = 0; i < cow; i++)
{
printf("%3d ", i + 1);
int j = 0;
for (;j < col; j++)
{
if (bord[i][j] == PLAYER1)
printf(" ● ");
else if (bord[i][j] == PLAYER2)
printf(" ○ ");
else
printf(" . ");
}
printf("\n");
}
}
void game()
{
int result = 0;
// 初始化棋盘
int playbord[COW][COL];
memset(playbord, 0, sizeof(playbord));
while (1)
{
// 玩家1下棋
ShowBord(playbord, COW, COL);
BordMove(playbord, COW, COL, PLAYER1);
result = isOver(playbord, COW, COL);
if (result != NEXT)
{
break;
}
// 玩家2下棋
ShowBord(playbord, COW, COL);
BordMove(playbord, COW, COL, PLAYER2);
result = isOver(playbord, COW, COL);
if (result != NEXT)
{
break;
}
}
ShowBord(playbord, COW, COL);
if (result == PLAYER1_WIN)
printf("PLAYER1 is WIN!!!\n");
else
printf("PLAYER2 is WIN!!!\n");
}
main.c
#include "game.h"
int main()
{
int select = 0;
do
{
menu();
scanf("%d", &select);
switch (select)
{
case 1:
game();
break;
case 0:
break;
default:
break;
}
}while (select);
printf("game is over!!\n");
printf("game is over!!\n");
printf("game is over!!\n");
return 0;
}
Makefile
game:game.c main.c
gcc $^ -o $@
.PHONY:clean
clean:
rm -f game