三子棋实现
1.思路
通过宏定义的方法规定行和列,这样既方便我们以后更改棋盘大小,也方便我们引用他们。先初始化棋盘,并把他们全部初始化为空,然后让电脑和玩家各自分步下棋,并在每一步都打印棋盘,从而让玩家能知道棋盘的状况,同时,在玩家和电脑下的每一步之后判断游戏是否结束(玩家或者电脑赢或者平局)。
2.代码
头文件game.h(函数的声明)
#define ROW 4 //棋盘行数
#define LINE 4 //棋盘列数
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
void menu(); // 打印菜单
void game(); //游戏开始
void board_init(char board[ROW][LINE]);//初始化棋盘
void board_print(char board[ROW][LINE]);//打印棋盘
void computer_play(char board[ROW][LINE]);//电脑下棋
void human_play(char board[ROW][LINE]);//玩家下棋
char iswin(char board[ROW][LINE]);//判断棋盘是否结束
game.c文件 (功能的实现)
#include"game.h"
void board_init(char board[ROW][LINE]) {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < LINE; j++) {
board[i][j] = ' ';
}
}
}//初始化棋盘
void board_print(char board[ROW][LINE]) {
for (int i = 0; i < ROW; i++) {
for(int j = 0; j < LINE; j++) {
printf(" %c ", board[i][j]);
if (j < LINE - 1) {
printf("|");
}
}
printf("\n");
if (i < ROW - 1) {
for (int j = 0; j < LINE; j++) {
if (j < LINE - 1)
printf("---|");
else printf("---\n");
}
}
}
printf("\n");
} //打印棋盘
void computer_play(char board[ROW][LINE]) {
while (1) {
int i = rand() % ROW;
int j = rand() % LINE;
if (board[i][j] == ' ') {
board[i][j] = '*';
break;
} //确保下棋的位置没有子
}
}
void human_play(char board[ROW][LINE]) {
while (1) {
int i = 0;
int j = 0;
printf("请输入你要选择的坐标\n");
scanf("%d%d", &i, &j);
i -= 1;
j -= 1;
if (board[i][j] == ' ') {
board[i][j] = '#';
break;
}
else {
printf("输入错误请重新输入\n");
}//确保下棋的位置没有子
}
}
char iswin(char board[ROW][LINE]) {
int flag = 0;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < LINE - 2; j++) {
if(board[i][j] == '*' && board[i][j+1] == '*' && board[i][j+2] == '*'|| board[i][j] == '#' && board[i][j + 1] == '#' && board[i][j + 2] == '#')
{
return board[i][j];
}
}
}
for (int j = 0; j < LINE; j++) {
for (int i = 0; i < ROW - 2; i++) {
if (board[i][j] == '*' && board[i+1][j] == '*' && board[i+2][j] == '*'|| board[i][j] == '#' && board[i + 1][j] == '#' && board[i + 2][j] == '#') {
return board[i][j];
}
}
}
for (int i = 0; i < ROW - 2; i++) {
for (int j = 0; j < LINE - 2; j++) {
if (board[i][j] =='*' && board[i + 1][j + 1] =='*' && board[i + 2][j + 2] == '*'|| board[i][j] == '#' && board[i + 1][j + 1] == '#' && board[i + 2][j + 2] == '#') {
return board[i][j];
}
}
}
for (int i = 0; i < ROW - 2;i++) {
for (int j = LINE - 1; j > 1; j--) {
if (board[i][j] == '*' && board[i + 1][j - 1] == '*' && board[i + 2][j - 2] == '*'|| board[i][j] == '#' && board[i + 1][j - 1] == '#' && board[i + 2][j - 2] == '#') {
return board[i][j];
}
}
}
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < LINE; j++) {
if (board[i][j] == ' ')
flag = 1;
}
}
if (flag == 0) {
return 'b';
}
return 'a';
} //判断横竖以及斜着是否有三子连在一起以及是否棋盘已经被占满 从而决定游戏是否结束
void menu() {
printf("*******************************\n");
printf("***********1.开始游戏**********\n");
printf("***********0.结束游戏**********\n");
printf("*******************************\n");
}
void game() {
char ret = 0;
printf("游戏开始\n");
char board[ROW][LINE] = { 0 };
board_init(board); //初始化棋盘
board_print(board);//打印棋盘
srand((unsigned int)time(NULL));
while (1) {
computer_play(board);
board_print(board);
ret = iswin(board);
if (ret != 'a') {
break;
}
human_play(board);
board_print(board);
ret = iswin(board);
if (ret != 'a') {
break;
}
}
if (ret == '*')
printf("电脑赢\n");
if (ret == '#')
printf("玩家赢\n");
if (ret == 'b')
printf("平局\n");
}
主函数
#include"game.h"
int main() {
int input = 0;
do {
menu();
printf("请输入:");
scanf("%d", &input);
switch (input) {
case 1:game();
case 0:break;
default:printf("输入错误请重新输入");
}
} while (input);
}
感谢聆听