- (1)打印游戏菜单
- (2)若选1,开始游戏:打印棋盘 >> 玩家下棋 >> 电脑下棋 >> 判断输赢 …
- (3)若选0,退出游戏
在头文件中,我们将 ROW 和 COL 都改为为9,定义一个9*9的棋盘
#define ROW 9
#define COL 9
这里我们打印一个9*9的棋盘,并且标清楚棋盘对应的行号和列号
//打印棋盘
void ShowBoard(char board[ROW][COL], int row, int col)
{
printf(" %d %d %d %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 6, 7, 8, 9); //列号
for (int i = 0; i < row; i++)
{
printf("%d ", i+1); //行号
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
printf(" ");
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
printf("\n");
}
这里的判断输赢用到了推移的思路!其实就是先判断棋盘大小为5×5时赢的情况,然后再根据自己设置的棋盘大小进行推移继续判断就可以了。
判断每一行:
先判断数组下标为(0,0)(0,1)(0,2)(0,3)(0,4)【即棋盘坐标为(1,1)(1,2)(1,3)(1,4)(1,5)】的位置。若是连续非空并相同的,则返回5个数组坐标中的任意一个数组坐标的内容return board[i][j]
。若不是连续非空并相同的,则+1向右推移后得到(0,1)(0,2)(0,3)(0,4)(0,5)【即棋盘坐标为(1,2)(1,3)(1,4)(1,5)(1,6)】继续判断。以此类推,直到第一个棋子坐标的列的下标等于 COL - 5【即(0,4)】此次判断完,停止该行的判断,进入下一行的判断。
判断每一列:
先判断数组下标为(0,0)(1,0)(2,0)(3,0)(4,0)【即棋盘坐标为(1,1)(2,1)(3,1)(4,1)(5,1)】的位置。若是连续非空并相同的,则返回5个数组坐标中的任意一个数组坐标的内容return board[i][j]
。若不是连续非空并相同的,则+1向下推移后得到(1,0)(2,0)(3,0)(4,0)(5,0)【即棋盘坐标为(2,1)(3,1)(4,1)(5,1)(6,1)】继续判断。以此类推,直到第一个棋子坐标的行的下标等于 ROW - 5【即(4,0)】此次判断完,停止该列的判断,进入下一列的判断。
从左向右判断对角线:
遍历红色方框内的数组坐标,只要以红色方框内的数组坐标为起始坐标,依次向右下方查5个连续的坐标并进行判断。若是连续非空并相同的,则返回5个数组坐标中的任意一个数组坐标的内容return board[i][j]
。若不是连续非空并相同的,遍历下一组坐标。
从右向左判断对角线:
遍历红色方框内的数组坐标,只要以红色方框内的数组坐标为起始坐标,依次向左下方查5个连续的坐标并进行判断。若是连续非空并相同的,则返回5个数组坐标中的任意一个数组坐标的内容return board[i][j]
。若不是连续非空并相同的,遍历下一组坐标。
//判断输赢
char IsWing(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++) //判断每一行
{
for (j = 0; j < col - 4; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j + 2] == board[i][j + 3] && board[i][j + 3] == board[i][j + 4] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
for (j = 0; j < col; j++) //判断每一列
{
for (i = 0; i < row - 4; i++)
{
if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i + 2][j] == board[i + 3][j] && board[i + 3][j] == board[i + 4][j] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//从左向右判断对角线
for (i = 0; i < row - 4; i++)
{
for (j = 0; j < col - 4; j++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i + 2][j + 2] == board[i + 3][j + 3] && board[i + 3][j + 3] == board[i + 4][j + 4] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//从右向左判断对角线
for (i = 0; i < row - 4; i++)
{
for (j = 4; j < col; j++)
{
if (board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i + 2][j - 2] && board[i + 2][j - 2] == board[i + 3][j - 3] && board[i + 3][j - 3] == board[i + 4][j - 4] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//判断是否平局
if (IsFull(board, row, col))
{
return 'P';
}
else
{
return 'C';
}
}
这里设置电脑自动关机时间为60s
printf("【请注意,若您60秒内未获得胜利,电脑将自动关机!!!】\n");
system("shutdown -s -t 60");
#pragma once
#define ROW 9
#define COL 9
//头文件
#include
#include //srand函数需要
#include //time函数需要
//函数声明
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void ShowBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判断输赢
char IsWing(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include "五子棋.h"
void menu()
{
printf("=====================\n");
printf("==== 1. play ====\n");
printf("==== 0. exit ====\n");
printf("=====================\n");
}
void game()
{
//存放一个9*9的数组
char board[ROW][COL] = { 0 };
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
ShowBoard(board, ROW, COL);
char ret = 0;
while (1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
//打印棋盘
ShowBoard(board, ROW, COL);
//判断输赢
ret = IsWing(board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑下棋
ComputerMove(board, ROW, COL);
//打印棋盘
system("cls"); //清屏操作
printf("电脑已下棋【#】:\n");
ShowBoard(board, ROW, COL);
//判断输赢
ret = IsWing(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if ('*' == ret)
{
printf("恭喜您获得获胜,电脑将取消自动关机!\n");
system("shutdown -a");
printf("\n");
}
else if ('#' == ret)
{
printf("很遗憾,电脑获胜!\n");
printf("\n");
}
else if ('P' == ret)
{
printf("平局啦,要不要再来一局!\n");
printf("\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏:\n");
printf("【请注意,若您60秒内未获得胜利,电脑将自动关机!!!】\n");
system("shutdown -s -t 60");
game();
break;
case 0:
printf("已为您退出游戏!\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}while (input); //只要输入的不是0,都会一直循环
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "五子棋.h"
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//打印棋盘
void ShowBoard(char board[ROW][COL], int row, int col)
{
printf(" %d %d %d %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 6, 7, 8, 9); //列号
for (int i = 0; i < row; i++)
{
printf("%d ", i+1); //行号
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
printf(" ");
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
printf("\n");
}
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
printf("请玩家下棋【*】:\n");
int x = 0;
int y = 0;
while (1)
{
printf("\n请输入想要落子的坐标(例如:1 1):");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已被占用,请重新输入!\n");
}
}
else
{
printf("坐标非法,请重新输入!\n");
}
}
}
//电脑随机下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
//printf("电脑下棋【#】:\n");
while (1)
{
x = rand() % row; //限制x不超过row
y = rand() % col; //限制y不超过col
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//玩家赢 - *
//电脑赢 - #
//平局 - P
//游戏继续 - C
//判断是否平局
int IsFull(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
//判断输赢
char IsWing(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++) //判断每一行
{
for (j = 0; j < col - 4; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j + 2] == board[i][j + 3] && board[i][j + 3] == board[i][j + 4] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
for (j = 0; j < col; j++) //判断每一列
{
for (i = 0; i < row - 4; i++)
{
if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i + 2][j] == board[i + 3][j] && board[i + 3][j] == board[i + 4][j] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//从左向右判断对角线
for (i = 0; i < row - 4; i++)
{
for (j = 0; j < col - 4; j++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i + 2][j + 2] == board[i + 3][j + 3] && board[i + 3][j + 3] == board[i + 4][j + 4] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//从右向左判断对角线
for (i = 0; i < row - 4; i++)
{
for (j = 4; j < col; j++)
{
if (board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i + 2][j - 2] && board[i + 2][j - 2] == board[i + 3][j - 3] && board[i + 3][j - 3] == board[i + 4][j - 4] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//判断是否平局
if (IsFull(board, row, col))
{
return 'P';
}
else
{
return 'C';
}
}