使用C语言实现三子棋主要是运用到二维数组的知识,这里用一个头文件两个源文件来实现
头文件:game.h //包含函数各种声明,宏定义
源文件:main.c //主要负责测试整个代码
源文件:game.c //包含各种函数的定义
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<windows.h>
#include<time.h>
#pragma warning (disable:4996)
#define ROW 3//棋盘行
#define COL 3//棋盘列
#define PLAYER 'X'//玩家的棋子
#define COMPUTER 'O'//电脑的棋子
#define NEXT 'N'//未分胜负且棋盘未满 继续
#define FULL 'F'//棋盘下满,平局
void menu();//菜单
void play(int* quit);//开始游戏
void inti_board(char[][COL], int row, int col);//初始化棋盘
void show_board(char board[][COL], int row, int col);//显示棋盘
void playermove(char board[][COL], int row, int col);//玩家走棋
void computermove(char board[][COL], int row, int col);//电脑走棋
char judge(char board[][COL], int row, int col);//判断
int first_step();//谁先走
#endif
game.c
#include"game.h"
void menu() {
printf("***********************\n");
printf("*** 三子棋小游戏 ***\n");
printf("***********************\n");
printf("***** 1.Play *****\n");
printf("***** 2.Exit *****\n");
printf("***********************\n");
printf("请选择:->");
}
//初始棋盘全部置空(空格)
void init_board(char board[][COL], int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
board[j][i] = ' ';
}
}
}
void play(int* quit) {
do {
char board[ROW][COL];//棋盘
int first = first_step();
init_board(board, ROW, COL);//清空棋盘
char who_win = NEXT;//胜负标志
show_board(board, ROW, COL);//显示棋盘
do {
if (first) {
playermove(board, ROW, COL);//玩家走棋
show_board(board, ROW, COL);//显示棋盘
who_win = judge(board, ROW, COL);//判断
if (who_win == PLAYER) {//玩家赢
printf("恭喜!你赢了!\n");
break;
}
if (who_win == COMPUTER) {//电脑赢
printf("很遗憾,你输了!\n");
break;
}
if (who_win == FULL) {
printf("平局!\n");
break;
}
computermove(board, ROW, COL);//电脑走棋
show_board(board, ROW, COL);//显示棋盘
who_win = judge(board, ROW, COL);//判断
if (who_win == PLAYER) {
printf("恭喜!你赢了!\n");
break;
}
if (who_win == COMPUTER) {
printf("很遗憾,你输了!\n");
break;
}
if (who_win == FULL) {
printf("平局!\n");
break;
}
}
else {
computermove(board, ROW, COL);//电脑走棋
show_board(board, ROW, COL);//显示棋盘
who_win = judge(board, ROW, COL);//判断
if (who_win == PLAYER) {
printf("恭喜!你赢了!\n");
break;
}
if (who_win == COMPUTER) {
printf("很遗憾,你输了!\n");
break;
}
if (who_win == FULL) {
printf("平局!\n");
break;
}
playermove(board, ROW, COL);//玩家走棋
show_board(board, ROW, COL);//显示棋盘
who_win = judge(board, ROW, COL);//判断
if (who_win == PLAYER) {//玩家赢
printf("恭喜!你赢了!\n");
break;
}
if (who_win == COMPUTER) {//电脑赢
printf("很遗憾,你输了!\n");
break;
}
if (who_win == FULL) {
printf("平局!\n");
break;
}
}
} while (1);
printf("是否继续: 是:1 否:0\n");
scanf("%d", &(*quit));
} while (*quit);
}
void show_board(char board[][COL], int row, int col) {
printf(" 1 | 2 | 3 \n");
printf("----------------\n");
for (int i = 1; i <= row; i++) {
printf(" %d | ", i);
for (int j = 0; j < col; j++) {
printf("%c | ", board[i - 1][j]);
}
printf("\n----------------\n");
}
}
void playermove(char board[][COL], int row, int col) {
printf("轮到玩家走棋(输入坐标):");
int x = 0;
int y = 0;
do {
scanf("%d %d", &x, &y);
if (x < 1 || x > 3 || y < 1 || y > 3 || board[x - 1][y - 1] != ' ') {
printf("输入的坐标有误,请重新输入:");
continue;
}
else {
board[x - 1][y - 1] = PLAYER;
break;
}
} while (1);
}
void computermove(char board[][COL], int row, int col) {
printf("电脑思考中...\n");
Sleep(120);
if (board[1][1] == ' ') {
board[1][1] = COMPUTER;
}
else {
while (1) {
int x = rand() % 3;
int y = rand() % 3;
if (board[x][y] == ' ') {
board[x][y] = COMPUTER;
break;
}
}
}
}
char judge(char board[][COL], int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i][0] == board[i][1] &&\
board[i][2] == board[i][1]) {
return board[i][1];
}
}
for (int i = 0; i < col; i++) {
if (board[0][i] == board[1][i] && \
board[1][i] == board[2][i]) {
return board[1][i];
}
}
if ((board[1][1] == board[0][0] && board[1][1] == board[2][2]) ||
(board[1][1] == board[0][2] &&board[1][1] == board[2][0])) {
return board[1][1];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == ' ') {
return 'N';
}
}
}
return 'F';
}
int first_step() {
int select = 0;
printf(" 1.玩家先走 2.电脑先走 3.随机\n");
printf("请选择先后:\n");
scanf_s("%d", &select);
switch (select) {
case 1:
return 1;
break;
case 2:
return 0;
break;
case 3:
printf("随机结果为:");
int result = rand() % 2;
if (result) {
printf("玩家先\n");
}
else
printf("电脑先\n");
return result;
default:
break;
}
}
main.c
#include"game.h"
int main() {
srand((unsigned long)time(NULL));
int quit = 0;
do {
menu();//展示菜单主界面
int select = 0;
scanf_s("%d", &select);
switch (select) {
case 1:
play(&quit);
break;
case 2:
printf("ByeBye!\n");
quit = 1;
break;
default:
break;
}
} while (!quit);
}
程序有一个主函数和若干自定义函数。其分别为: 菜单打印函数、棋盘打印函数、初始化棋盘函数、谁先走函数、电脑下棋函数、玩家下棋函数、赢家判断函数。
整体思路
玩家的棋为字符 ‘x’,电脑的棋为字符 ‘0’。
1.先用二维数组构造一个棋盘,这个棋盘各个位置需要初始化为字符零,定义 init_borad()函数来实现:
2.初始化棋盘之后我们需要在很多地方打印棋盘,定义show_borad()函数来实现:
3.确定谁先走,用int first_step()函数实现:
4.玩家下棋,我们需要输入坐标来确定你要走的位置,定义 void playermove(char board[][COL], int row, int col)函数来实现:
5.电脑下棋,电脑的两个坐标值分别由产生随机数的函数rand()来生成,产生的坐标错误还得循环产生,直到正确为止,定义void computermove(char board[][COL], int row, int col))函数来实现:
6.玩家和电脑每次下完棋之后都要检测输赢,定义char judge(char board[][COL], int row, int col) 函数来实现,这个函数的原理是:通过检测每行,每列,和两个对角线的所有字符是否为三个相同的字符,如果是就返回那三个相同的字符,这个函数无非就四个结果,(1)玩家赢,(2)电脑赢,(3)棋盘没满可以继续,(4)棋盘满了,但未分出胜负,平局。各自的情况返回各自的符号用于之后的判断:
我们可以确定一个大的思路,将各个函数的执行放在游戏执行函数game函数中,将game函数放在循环中以便我们游戏结束后还可以玩,还要选择判断继续玩游戏还是退出游戏,然后将game函数放在一个main()钟函数实现即可
小白, 能力有限, 若有缺陷,请指教。