用C语言实现简单的三子棋程序,主要是用二维数组来进行实现,我们用两个源文件(test.c和game.c)和一个头文件(game.h)来实现。?
test.c | 主要包含主函数、函数调用 | game.c | 主要是函数的定义 | game.h | 主要是函数的声明、宏定义? |
思路:
一、菜单的创建
void menu()
{
printf("**********************************\n");
printf("***********欢迎来到三子棋*********\n");
printf("*********** 1. play *********\n");
printf("*********** 0. exit *********\n");
printf("**********************************\n");
}
二、 对game()函数实现
- 创建棋盘并初始化,初始化完成后打印棋盘(详情见下文代码)
char board[ROW][COL];//棋盘数组
//初始化棋盘 - board的元素都给成空格
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
? ? ? 2. 开始下棋
玩家先走,玩家走的字符为' *?',? ? ? 然后电脑走,电脑走的字符为'#'。
电脑和玩家每走一次都要判断输赢
char ret = 0;//定义变量接收判断输赢的函数返回值
while (1)
{
PlayerMove(board, ROW, COL);//玩家下棋
DisplayBoard(board, ROW, COL);//展示玩家下完棋的棋盘样子
ret = IsWin(board, ROW, COL);//判断游戏输赢
//要返回4种不同的状态
//玩家赢,返回 '*'
//电脑赢,返回 '#'
//平局,返回 'Q'
//继续,返回 'C'
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("恭喜你,你和电脑打成平手!\n");
}
}
确定思路后,我们完成程序的流程部分
代码如下:
game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
//打印菜单
void menu();
//初始化棋盘
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);
//判断游戏输赢
//要返回4种不同的状态
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - ‘Q’
//继续 - 'C'
char IsWin(char board[ROW][COL], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("**********************************\n");
printf("***********欢迎来到三子棋*********\n");
printf("*********** 1. play *********\n");
printf("*********** 0. exit *********\n");
printf("**********************************\n");
}
void InitBoard(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++)
{
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)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j<col - 1)
printf("|");
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
printf("玩家走:>\n");
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);//2 1 -- > 1 0
if (x >= 1 && x <= row && y >= 1 && y <= col)//判断玩家有没有非法输入
{
if (board[x - 1][y - 1] == ' ')//x、y是玩家输入的坐标,在数组中的坐标是0开始所以需要-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;
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;//棋盘满
}
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';
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
//三子棋的过程
char board[ROW][COL] = {0};//棋盘数组
//初始化棋盘 - board的元素都给成空格
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);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("恭喜你, 你赢了!\n");
}
else if (ret == '#')
{
printf("很遗憾,电脑赢了!\n");
}
else
{
printf("恭喜你,你和电脑打成平手!\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//随机数发生器的初始化函数,常和rand函数一起使用
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}
以上就是一个简单的三子棋程序
|