#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#define ROW 3
#define COL 3
void menu();//菜单函数声明
void game();//游戏函数声明
void Init_board(char board[ROW][COL], int row, int col);//棋盘初始化函数声明
void Display_board(char board[ROW][COL], int row, int col);//棋盘展示函数声明
void player_board(char board[ROW][COL], int row, int col);//玩家下棋函数声明
void computer_board(char board[ROW][COL],int row,int col);//电脑下棋函数声明
char is_end(char board[ROW][COL], int row, int col);//判断是否结束函数声明
void is_win(char flag);//判断谁获胜函数声明
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:->");
scanf("%d", &input);
system("cls");
switch (input)
{
case 1:
game();
break;
case 0:
printf("已退出!\n");
break;
default:
break;
}
} while (input);
return 0;
}
void menu()
{
printf("*************************\n");
printf("******* 1.play ********\n");
printf("******* 0.exit ********\n");
printf("*************************\n");
}
//游戏函数
void game()
{
char board[ROW][COL] = { 0 };
Init_board(board, ROW, COL);//棋盘初始化
Display_board(board, ROW, COL);//棋盘展示
while (1)
{
player_board(board, ROW, COL);//玩家下棋
Display_board(board, ROW, COL);//棋盘展示
printf("\n");
if (is_end(board, ROW, COL) != 'c')
{
is_win(is_end(board, ROW, COL));
break;
}
computer_board(board, ROW, COL);//电脑下棋
Display_board(board, ROW, COL);//棋盘展示
printf("\n");
if (is_end(board, ROW, COL) != 'c')
{
is_win(is_end(board, ROW, COL));
break;
}
}
}
void Init_board(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++)
{
board[i][j] = ' ';
}
}
}
void Display_board(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (j < col - 1)
printf(" %c |", board[i][j]);
else
printf(" %c \n", board[i][j]);
}
if (i < row-1 )
{
for (int j = 0; j < col; j++)
{
if (j < col - 1)
printf("___|");
else
printf("___\n");
}
}
else
{
for (int j = 0; j < col; j++)
{
if (j < col - 1)
printf(" |");
else
printf(" \n");
}
}
}
}
void player_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
while (1)
{
int i = 0;
int j = 0;
printf("请输入下棋位置->");
scanf("%d %d", &i, &j);
if (i <= row && i >= 1 && j <= col && j >= 1)
{
if (board[i-1][j-1] == ' ')
{
board[i-1][j-1] = '*';
break;
}
else
{
printf("坐标已有棋,清重新输入!\n");
}
}
else
{
printf("坐标非法,清重新输入!\n");
}
}
}
void computer_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
while (1)
{
i = rand() % 3;
j = rand() % 3;
if (board[i][j] ==' ')
{
board[i][j] = '#';
break;
}
else
continue;
}
}
//判断是否结束
char is_end(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 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[0][0] != ' ')
return board[0][0];
//'/'对角线
if (board[2][0] == board[1][1] == board[0][2] && board[2][0] != ' ')
return board[2][0];
//未结束
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 'c';
}
}
//平局
return '-';
}
//判断谁获胜
void is_win(char flag)
{
if (flag == '*')
{
printf("玩家获胜!\n");
system("pause");
system("cls");
}
else if (flag == '#')
{
printf("电脑获胜!\n");
system("pause");
system("cls");
}
else if (flag == '-')
{
printf("平局!\n");
system("pause");
system("cls");
}
}
标签:无脑,int,col,C语言,小游戏,board,printf,COL,ROW
From: https://blog.csdn.net/A_Lv123/article/details/141229019