本文是大量参考b站课程鹏哥精讲的c语言里面实现的三字棋的内容,目的主要是为了复习和巩固自己c语言中函数与数组的短板,也希望各位大佬多多指教,非常感谢,话不多说现在开始。
首先搭建游戏我们需要拆分三个文件
game.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
//头文件的包含
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//符号的定义
#define ROW 20
#define COL 20
//函数的声明
//初始化棋盘的函数
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘的函数
void DisplayBoard(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);
//1.玩家赢了-*
//2.电脑赢了-#
//3.平局-Q
//4.游戏继续-C
//判断游戏是否有输赢
char IsWin(char board[ROW][COL], int row, int col);
自制头文件:声明了头文件的包含,符号的定义及函数的声明
优点:保持定义和声明的一致性,可以重复使用,具有一定的通用性
test.c
#include"game.h"
void menu()
{
printf("**********************************\n");
printf("****** 1.play *****\n");
printf("****** 0.exit *****\n");
printf("**********************************\n");
}
void game()
{
//存储数据-二维数组
char board[ROW][COL];
//初始化棋盘-初始化空格
InitBoard(board, ROW, COL);
//打印一下棋盘-本质是打印数值内容
DisplayBoard(board, ROW, COL);
char ret = 0;//接受游戏状态
while (1)
{
//玩家下棋
PlayerMove(board,ROW,COL);
DisplayBoard(board, ROW, COL);
//判断玩家是否赢的游戏
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}
DisplayBoard(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int) time(NULL));
do {
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
printf("三子棋游戏\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
1:制作游戏开始的菜单
2:输入1开始游戏,输入0退出游戏
3: 否则选择错误,重新选择
正式进入游戏后(进入game函数)
1:初始化棋盘-数组初始化
2:打印棋盘-打印初始化的数组
3:接受状态-后面判断输赢要用到
game.c
#include"game.h"
void InitBoard(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; j++)
{
board[i][j] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
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)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走:>\n");
while (1)
{
printf("请输入下棋的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][x - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑走:>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
//判断占用
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(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; j++)
if (board[i][j] == ' ')
{
return 0;//棋盘没满
}
}
return 1;
}
1:调用InitBoard函数,用嵌套的for循环把数组初始化
2:调用DisplayBoard函数循环遍历实现棋盘
游戏开始
玩家下棋:输入落棋的坐标(坐标默认减一)
先判断落棋的坐标是否被占用,再判断坐标合法性
电脑下棋: srand((unsigned int) time(NULL));
利用当前时间随机生成落棋坐标
膜上当前横竖坐标,然后判断坐标有无占用及合法性
(避免坐标超模)
判断输赢
横线连成:
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++)
{
if (board[i][i] == board[i + 1][i + 1]
&& board[i + 1][i + 1] == board[i + 2][i + 2]
&& board[i + 2][i + 2] == board[i + 3][i + 3]
&& board[i + 3][i + 3] == board[i + 4][i + 4]
&& board[i][i] != ' ')
{
return board[i][i];
}
if (board[i][i + 4] == board[i + 1][i + 3]
&& board[i + 1][i + 3] == board[i + 2][i + 2]
&& board[i + 2][i + 2] == board[i + 3][i + 1]
&& board[i + 3][i + 1] == board[i + 4][i]
&& board[i][i + 4] != ' ')
{
return board[i][i + 4];
}
}
判断坐标连成五子的算法还有待优化,欢迎大佬指教0.0
返回*玩家胜利
返回#电脑胜利
棋盘满 平局返回Q
返回C 游戏继续
构建IsFull函数,判断棋盘是否满
int IsFull(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; j++)
if (board[i][j] == ' ')
{
return 0;//棋盘没满
}
}
return 1;
}
遍历数组每个元素,有空格返回0,游戏继续
否则返回1,游戏结束,
//游戏平局
//如果棋盘满了返回1,不满返回0
int ret = IsFull(board, row, col);
if(ret==1)
{
return 'Q';
}
return 'C';
}