c语言的扫雷游戏

时间:2022-03-07 09:15:44

用c语言学习编写了一个简单的扫雷游戏,觉得编写程序也是很有趣的!

game.h

#ifndef __GAME_H__
#define __GAME_H__

#define ROWS 3
#define COLS 3


void init_board(char board[ROWS][COLS],int rows,int cols);
void display_board(char board[ROWS][COLS],int rows,int cols);
void player_move(char board[ROWS][COLS],int rows,int cols);
char check_win(char board[ROWS][COLS],int rows,int cols);
void computer_move(char board[ROWS][COLS],int rows,int cols);

#endif //__GAME_H__

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

void init_board(char board[ROWS][COLS],int rows,int cols)
{
memset(board,' ',sizeof(char)*cols*rows);
}
void display_board(char board[ROWS][COLS],int rows,int cols)
{
int i=0;
printf("_____________\n");
for(i=0;i<rows;i++)
{

printf("| %c | %c | %c |\n",board[i][0],board[i][1],board[i][2]);
if(i!=ROWS-1)
printf("|___|___|___|\n");

}
printf("|___|___|___|\n");

}
void player_move(char board[ROWS][COLS],int rows,int cols)
{
int x=0;
int y=0;
printf("玩家输入坐标:");
scanf("%d %d",&x,&y);
if(x>=0&&x<=rows&&y>=0&&y<=cols)
{
if(board[x][y]==' ')

{
board[x][y]='X';
}
}
else
{
printf("输入错误,请重新输入:");
}
}
void computer_move(char board[ROWS][COLS],int rows,int cols)
{
int x=0;
int y=0;
srand((unsigned int)time(NULL));
printf("电脑输入坐标:\n");
while(1)
{
x=rand()%3;
y=rand()%3;
if(board[x][y]==' ')
{
board[x][y]='O';
break;
}
}

}
static int is_full(char board[ROWS][COLS],int rows,int cols)
{
int i=0;
int j=0;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(board[i][j]==' ')
return 0;//没满
}
}
return 1;//已满
}
char check_win(char board[ROWS][COLS],int rows,int cols)
{
char ret=0;
int i=0;
for(i=0;i<rows;i++)
{
if(board[i][0]==board[i][1]&&board[i][1]==board[i][2]&&board[i][1]=='X')
return board[i][1];
if(board[0][i]==board[1][i]&&board[1][i]==board[2][i]&&board[1][i]=='X')
return board[1][i];
if(board[0][0]==board[1][1]&&board[1][1]==board[2][2]&&board[1][1]=='X')
return board[1][1];
if(board[0][2]==board[1][1]&&board[1][1]==board[2][0]&&board[1][1]=='X')
return board[1][1];
}

if(is_full(board, ROWS, COLS))
{
return 'q';//平局
}
return ' ';
}


test.c

#include"game.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

#define ROWS 3
#define CLOS 3
enum Choose
{
EXIT,
PLAY
};
void menu()
{
printf("*******************************************\n");
printf("*** 1.PLAY ***\n");
printf("*** 0.EXIT ***\n");
printf("*******************************************\n");
}
void game()
{
char board[ROWS][COLS];
char ret;
init_board(board, ROWS, COLS);
while(1)
{

player_move(board, ROWS, COLS);
display_board(board, ROWS, COLS);
ret=check_win(board, ROWS, COLS);
{
if(ret!=' ')
break;
}

computer_move(board,ROWS, COLS);
display_board(board, ROWS, COLS);
ret=check_win(board, ROWS, COLS);
{
if(ret!=' ')
break;
}

}
if(ret=='X')
printf("玩家赢\n");
else if(ret=='O')
printf("电脑赢\n");
else
printf("平局\n");
}
int main()
{
int input=0;
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch(input)
{
case PLAY:
game();
break;
case EXIT:
break;
default:
printf("选择错误,请重新选择.\n");
break;
}
}
while(input);
return 0;
}

程序调试结果:

c语言的扫雷游戏