在写程序之前,我们首先要理清思路。首先要有菜单来选择是否玩游戏,其次就是玩游戏时棋盘的打印,然后就是下棋,玩家下,电脑下,最后判断输赢。大致的思路就是这样,具体实现过程为:
1. 创建头文件game.h
,源文件test.c
和game.c
game.h
用来定义宏和函数
test.c
用来调用函数,逻辑实现三子棋游戏
game.c
用来实现函数
2.打印菜单。菜单的打印很简单,只需要几个printf()
语句就可以了。
void menu()
{
printf("#################################\n");
printf("######## 1. play ########\n");
printf("######## 0. exit ########\n");
printf("#################################\n");
}
菜单打印完需要选择,这时就要用到switch()
语句,而无论玩不玩游戏至少要打印一次菜单,所以用do while
循环来执行。代码如下:
void test()
{
int input = 0;
do
{
menu();
printf("请选择> \n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出程序\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
} while (input);
}
3.棋盘的打印,三子棋是一个3x3的棋盘,所以要创建一个二维数组,打印棋盘前要把数组初始化为空格,然后进行打印。代码如下:
初始化数组
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(board, ' ', row*col*sizeof board[0][0]);
}
打印棋盘
void PrintBoard(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 < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
4.玩家走,用X代替玩家的棋。代码如下:
void player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>\n");
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] = 'X';
break;
}
else
{
printf("该坐标被占,请重新输入坐标!\n");
}
}
else
printf("坐标非法!\n");
}
}
5.电脑走,用O代替电脑的棋。rand()
函数是获取随机值,调用该函数前要先调用srand()
函数,该函数是随机数的生成器。模3是为了控制坐标的合法性。代码如下:
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = 'O';
break;
}
}
}
6.判断输赢。赢有四种可能,一行下满,一列下满,两条对角线下满。
static char 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;
//int m = IsFull(board, ROW, COL);
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];
}
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];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
if (IsFull(board, ROW, COL) == 1)
return 'p';
return ' ';
}
最后,附上源码:
game.h
#ifndef __GAME_H__
#define __GAME_H__
#define ROW 3//这里定义为宏是为了便于修改
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void InitBoard(char board[ROW][COL], int row, int col);
void PrintBoard(char board[ROW][COL], int row, int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
#endif //__GAME_H__
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game()
{
char board[ROW][COL] = { 0 };
int ret = 0;
InitBoard(board, ROW, COL);//初始化数组为' '
PrintBoard(board, ROW, COL);//打印棋盘
while (1)
{
player_move(board, ROW, COL);//玩家走
ret = IsWin(board, ROW, COL);//每走一次都要判断输赢
if (ret!=' ')
break;
computer_move(board, ROW, COL);//电脑走
PrintBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != ' ')
break;
}
ret = IsWin(board, ROW, COL);
if (ret == 'X')
{
printf("玩家赢!\n");
}
if (ret == 'O')
{
printf("电脑赢!\n");
}
if (ret == 'p')
{
printf("平局!\n");
}
PrintBoard(board, ROW, COL);
}
void menu()
{
printf("#################################\n");
printf("######## 1. play ########\n");
printf("######## 0. exit ########\n");
printf("#################################\n");
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择> \n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出程序\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
} while (input);
}
int main()
{
test();
srand((unsigned int)time(NULL));
system("pause");
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(board, ' ', row*col*sizeof board[0][0]);
}
void PrintBoard(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 < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
void player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>\n");
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] = 'X';
break;
}
else
{
printf("该坐标被占,请重新输入坐标!\n");
}
}
else
printf("坐标非法!\n");
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = 'O';
break;
}
}
}
static char 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 < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
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];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
if (IsFull(board, ROW, COL) == 1)
return 'p';
return ' ';
}
效果展示
博主还在学习阶段,有不足的地方,请大家多多指教!