用C编写的三子棋小游戏,有诸多不足,望过路大神多多指教!
头文件
#ifndef GAME_GAME_H #define GAME_GAME_H #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 3 #define COL 3 //初始化棋盘函数 void InitBoard(char board[ROW][COL], int row, int col); //打印棋盘 void DisplayBoard(char board[ROW][COL], int row, int col); //玩家下棋 void PlayerMove(char board[ROW][COL], int row, int col); //电脑下棋 void ComputerMove(char board[ROW][COL], int row, int col); //玩家赢输出'*'; 电脑赢输出'#'; 平局输出'!'; 继续输出'&' char IsWin(char board[ROW][COL], int row, int col); //如果满了就返回1,不满返回0 int IsFull(char board[ROW][COL], int row, int col); #endif //GAME_GAME_H
主函数
#include "game.h" //游戏地图初始化 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { board[i][j] = set; } } } //打印游戏地图 void DisplayBoard(char board[ROWS][COLS], int row, int col) { printf("☆☆☆☆☆☆ 扫雷游戏 ☆☆☆☆☆☆\n"); printf("%c ", ' '); for (int j = 1; j <= col; j++) { printf("%d ", j); } printf("\n"); for (int i = 1; i <= row; i++) { printf("%d ", i); for (int j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("☆☆☆☆☆☆ 扫雷游戏 ☆☆☆☆☆☆\n"); } //布置雷 void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } //统计x,y坐标中有几个雷 int get_mine_count(char board[ROWS][COLS], int x, int y) { return ((board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1] + board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1]) - (8 * '0')); } //排查雷 void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; //找非雷的个数 while (win < row * col - EASY_COUNT) { printf("请输入排查的坐标:"); scanf_s("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (show[x][y] != '*') { printf("该位置已扫雷!\n"); } else { //若是雷 if (mine[x][y] == '1') { printf("很遗憾,被炸死了!\n"); DisplayBoard(mine, ROW, COL); break; } //若不是雷 else { win++; //统计mine数组中x,y坐标周围有几个雷 int count = get_mine_count(mine, x, y); //转换成数字字符 show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); } } } else { printf("坐标非法,重新输入!\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功!\n"); DisplayBoard(mine, ROW, COL); } }
main函数
#include "game.h" //菜单打印 void menu() { printf("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n"); printf(" ☆☆☆☆☆☆ 1.play 0.exit ☆☆☆☆☆☆ \n"); printf("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n"); } //开始游戏 void game() { char mine[ROWS][COLS] = {0}; // 存放布置好的雷的信息 char show[ROWS][COLS] = {0}; // 存放排查出的雷的信息 //游戏地图初始化 //mine数组在没有布置雷的时候,都为'0' InitBoard(mine, ROWS, COLS, '0'); //show数组在没有布置雷的时候,都为'*' InitBoard(show, ROWS, COLS, '*'); DisplayBoard(mine, ROW, COL); //设置雷 SetMine(mine, ROW, COL); DisplayBoard(show, ROW, COL); DisplayBoard(mine, ROW, COL); //排查雷 FindMine(mine, show, ROW, COL); } int main() { int input = 0; //设置随机数的生成起点 srand((unsigned int) time(NULL)); do { menu(); printf("开始游戏(1),退出游戏(0):"); scanf_s("%d", &input); switch (input) { case 1: printf("游戏开始:\n"); game(); break; case 0: printf("退出游戏!\n"); break; default: printf("选择错误!\n"); break; } } while (input); return 0; }