//主函数
#include "game.h"
int main() {
int input = 0;
srand(time(NULL)); //利用时间戳生成随机数
do {
menu(); //打印菜单
scanf("%d", &input); //输入1开始游戏,0退出游戏
if (input)
game();
else {
printf("退出游戏\n");
break;
}
} while (input);
return 0;
}
//game()函数
#include "game.h"
void menu() {
printf("*******************************\n");
printf("***1.start************0.exit***\n");
printf("*******************************\n");
printf("请选择\n");
}
void initboard(int board[ROW][COL]) {
//初始化board数组每个元素为空格(不初始化,会导致打印棋盘时出现问题)
int i = 0;
for (i = 0; i < ROW; i++) {
int j = 0;
for (j = 0; j < COL; j++) {
board[i][j] =' ';
}
}
}
void printboard(int board[ROW][COL]) {
int i = 0;
for (i = 0; i < ROW; i++) {
int j = 0;
for (j = 0; j < COL; j++) {
//打印1行
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(int board[ROW][COL]) {
int r = 0;
int c = 0;
printf("请输入坐标\n");
scanf("%d %d", &r, &c);
if (board[r - 1][c - 1] ==' ')
board[r - 1][c - 1] ='*';
else
printf("请重新输入\n");
}
char judge(int board[ROW][COL]) {
int i = 0;
int j = 0;
char tmp = '!'; //表示棋盘已经下满了
//判断是否有三个相同的在一行
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 (j = 0; j < COL; j++) {
if ((board[0][j] == board[1][j]) && (board[1][j] == board[2][j]) && (board[0][j] != ' '))
return board[0][j];
}
//判断对角线是否有三个相同的
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' '))
return board[1][1];
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
//遍历每个元素,若都不为空格,则为平局
if (board[i][j] == ' ')
tmp = '@'; //来表示棋盘没下满
}
}
return tmp;
}
void print_winner(char rst) {
if (rst == '*') {
printf("YOU WIN\n");
}
else if (rst == '#') {
printf("YOU LOSE\n");
}
else if (rst == '!') {
printf("平局\n");
}
}
void AI_move(int board[ROW][COL]) {
printf("电脑下棋\n");
while (1) {
int r = rand() % 3; //随机数取余3,得到0-2的数
int c = rand() % 3;
if (board[r][c] == ' ') {
board[r][c] = '#';
break;
}
}
}
void game() {
printf("开始游戏\n");
//初始化棋盘
int board[ROW][COL] = { 0 };
initboard(board);
//打印初始化棋盘
printboard(board);
while (1) {
//玩家下棋
player_move(board);
printboard(board);
//判断
char rst = judge(board);
print_winner(rst);
if ((rst == '*' )||(rst == '#')||(rst == '!'))
break;
//电脑下棋
AI_move(board);
printboard(board);
//判断
rst = judge(board);
print_winner(rst);
if ((rst == '*') || (rst == '#') || (rst == '!'))
break;
}
}
//声明函数的头文件
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#define ROW 3
#define COL 3
void menu();
void game();
void initboard(int board[ROW][COL]);
void printboard(int board[ROW][COL]);
void player_move(int board[ROW][COL]);
char judge(int board[ROW][COL]);
void print_winner(char ret);
void player_move(int board[ROW][COL]);
标签:实现,三子,void,C语言,int,board,printf,COL,ROW
From: https://blog.csdn.net/2402_83534870/article/details/144326645