目录
前言
一、实现的思路
二、代码实现
2.定义游戏函数
3.主函数
4.棋盘初始化
5.打印棋盘
6.玩家下棋 电脑下棋
7.判断输赢
IsFull函数
三、模块化代码的实现
1.game.h
2.game.c
3.test.h
四、游戏运行结果
三子棋游戏是一款益智小游戏相信大家都玩过吧,本篇文章将通过c语言来实现这款游戏
游戏的实现思路其实很简单就是
1.初始化棋盘
2.打印棋盘
3.玩家下棋
4.判断输赢
5.电脑下棋
6.判断输赢
既然已经有了游戏的实现思路那么现在可以根据思路来实现代码了
首先我们需要一个进入游戏的菜单
void menu() {
printf("****************\n");
printf("*****1.piay*****\n");
printf("*****0.exit*****\n");
printf("****************\n");
}
三子棋游戏的实现
void game() {
char ret = '0';
char board[Row][Col];
InitBoard(board,Row,Col);
DisplayBoard(board, Row, Col);
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);
//判断输赢
ret = Iswin(board, Row, Col);
if (ret != 'c') {
break;
}
}
if (ret == '*') {
printf("玩家赢\n");
}
else if (ret == '#') {
printf("电脑赢\n");
}
else {
printf("平局");
}
}
通过do while语句和switch语句来实现这个游戏整体逻辑
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入");
break;
}
} while (input);
return 0;
}
初始化棋盘将棋盘全部初始化为“空格”
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 < row; j++) {
board[i][j] = ' ';
}
}
}
将棋盘打印出来
void DisplayBoard(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++) {
printf(" %c ", board[i][j]);
if(j
棋盘初始化和打印完成后玩家和电脑就可以下棋了
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][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else {
printf("位置已被占用,请输入其他坐标");
}
}
else
{
printf("输入错误,请重新输入");
}
}
}
void ComputerMove(char board[Row][Col], int row, int col) {
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1) {
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
玩家赢时返回----“*”
电脑赢时返回----“#”
平局时返回----“Q”
游戏继续返回----“c”
char Iswin(char board[Row][Col], int row, int col) {
int i = 0;
for (i = 0; i < row; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
return board[i][0];
}
}
for (i = 0; i < col; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
return board[1][1];
}
if (IsFull(board, row, col))
{
return 'Q';
}
return 'c';
}
判断游戏是否继续(判断平局)
static 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;
}
模块化编程的思想是:根据功能将工程划分为不同模块。主函数只调用函数,而不定义函数。在各模块文件中定义功能函数,并将要用到的函数利用同名头文件申明外部函数供其他文件调用
头文件:关于游戏包含的函数声明,符号声明头文件的包含以及宏定义
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include
#include
#include
#define Row 3
#define Col 3
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);
char Iswin(char board[Row][Col], int row, int col);
游戏相关的函数定义
#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 < row; j++) {
board[i][j] = ' ';
}
}
}
void DisplayBoard(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++) {
printf(" %c ", board[i][j]);
if(j= 1 && x <= row && y >= 1 && y <= col) {
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else {
printf("位置已被占用,请输入其他坐标");
}
}
else
{
printf("输入错误,请重新输入");
}
}
}
void ComputerMove(char board[Row][Col], int row, int col) {
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1) {
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
static 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;
}
char Iswin(char board[Row][Col], int row, int col) {
int i = 0;
for (i = 0; i < row; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
return board[i][0];
}
}
for (i = 0; i < col; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
return board[1][1];
}
if (IsFull(board, row, col))
{
return 'Q';
}
return 'c';
}
三子棋游戏的整体逻辑实现
#include "game.h"
void game() {
char ret = '0';
char board[Row][Col];
InitBoard(board,Row,Col);
DisplayBoard(board, Row, Col);
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);
//判断输赢
ret = Iswin(board, Row, Col);
if (ret != 'c') {
break;
}
}
if (ret == '*') {
printf("玩家赢\n");
}
else if (ret == '#') {
printf("电脑赢\n");
}
else {
printf("平局");
}
}
void menu() {
printf("****************\n");
printf("*****1.piay*****\n");
printf("*****0.exit*****\n");
printf("****************\n");
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入");
break;
}
} while (input);
return 0;
}