用C写的扫雷小游戏
1.头文件
#ifndef MINESWEEPER_GAME_H #define MINESWEEPER_GAME_H #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define ROWS (ROW+2) #define COLS (COL+2) #define EASY_COUNT 10 //游戏地图初始化 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); //打印游戏地图 void DisplayBoard(char board[ROWS][COLS], int row, int col); //布置雷 void SetMine(char board[ROWS][COLS], int row, int col); //排查雷 void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col); #endif //MINESWEEPER_GAME_Hz
2.主函数
#include "game.h" //初始化棋盘函数 void InitBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } } //打印棋盘 void DisplayBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { //打印数据 // printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][3]); for (int j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); // 打印分割行 // printf("___|___|___\n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } } //玩家下棋 void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋(请输入坐标,用空格隔开):"); while (1) { 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] = '*'; break; } else { printf("该位置已占用,请重新输入!\n"); } } else { printf("坐标非法,请重新输入!\n"); } } } //电脑下棋 void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑下棋(#):\n"); while (1) { int x = rand() % row; // 0 — 2 int y = rand() % col; // 0 — 2 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } //如果满了就返回1,不满返回0 int IsFull(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } //判断输赢 char IsWin(char board[ROW][COL], int row, int col) { //行 for (int i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ') { return board[i][1]; } } //列 for (int j = 0; j < row; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ') { return board[1][j]; } } //对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') { return board[1][1]; } //平局 if (IsFull(board, row, col)) { return '!'; } else { //继续 return '&'; } }
3. main函数
#include "game.h" // 菜单打印 void menu() { printf("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n"); printf(" ☆☆☆☆☆☆ 1.play 0.exit ☆☆☆☆☆☆ \n"); printf("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n"); } // 进入游戏 void game() { char ret = 0; char board[ROW][COL] = {0}; // 初始化棋盘 InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); // 开始下棋 while (1) { PlayerMove(board, ROW, COL); // 判断输赢 ret = IsWin(board, ROW, COL); if (ret != '&') { break; } DisplayBoard(board, ROW, COL); ComputerMove(board, ROW, COL); // 判断输赢 ret = IsWin(board, ROW, COL); if (ret != '&') { break; } DisplayBoard(board, ROW, COL); } if (ret == '*') { printf("玩家赢!\n"); } else if (ret == '#') { printf("电脑赢!\n"); } else { printf("平局!\n"); } DisplayBoard(board, ROW, COL); } int main() { srand((unsigned int) time(NULL)); // 设置随机数的生成起点数 int input = 0; do { menu(); printf("开始游戏(1)退出游戏(0):"); scanf("%d", &input); switch (input) { case 1: printf("开始游戏!\n"); game(); break; case 0: printf("退出游戏!\n"); break; default: printf("选择错误!\n"); break; } } while (input); }