重新学习C语言day07

一身转战三千里,一剑曾当百万师。这篇文章主要讲述重新学习C语言day07相关的知识,希望能为你提供帮助。
数组实例:三子棋和扫雷游戏 1、三子棋1)分为3个部分:
test.c:测试游戏的逻辑

#include "game.h" void menu() { printf("***********************\\n"); printf("*********1.play********\\n"); printf("*********0.exit********\\n"); printf("***********************\\n"); } void game() { char board[ROW][COL]; //初始化棋盘 InitBoard(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); char ret = 0; //接收游戏状态 while (1) {//玩家下棋 PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); //判断输赢 ret = IsWin(board, ROW, COL); if (ret != \'C\') { break; }//电脑下棋 ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); //判断输赢 // char ret = IsWin(board, ROW, COL); if (ret != \'C\') { break; } } if (ret == \'*\') { printf("玩家赢!\\n"); } else if (ret == \'#\') { printf("电脑赢!\\n"); } else { printf("平局!\\n"); } DisplayBoard(board, ROW, COL); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:> "); scanf("%d", & input); switch (input) { case 1: printf("开始游戏\\n"); game(); break; case 0: printf("退出游戏\\n"); break; default: printf("输入错误\\n"); break; } } while (input); return 0; }

game.h:游戏相关的函数声明,符号声明和头文件的包含
#include "game.h"void InitBoard(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 DisplayBoard(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++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\\n"); if (i < row - 1) { int k = 0; for (k = 0; k < col; k++) { printf("---"); if (k < col - 1) { printf("|"); } } printf("\\n"); } } } void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("请下棋> \\n"); printf("请输入坐标:"); scanf("%d %d", & x, & y); if (x > = 1 & & x < = row & & y > = 1 & & y < = row) { if (board[x - 1][y - 1] == \' \') { board[x - 1][y - 1] = \'*\'; } else printf("坐标已被占用,请重新输入\\n"); break; } else printf("坐标非法,请重新输入!\\n"); } } void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑下棋> \\n"); int x = rand() % row; int y = rand() % col; while (1) { if (board[x][y] == \' \') { board[x][y] = \'#\'; break; } } } char IsWin(char board[ROW][COL], int row, int col) { int i = 0; //判断行相等 for (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 (i = 0; i < ROW; i++) { if (board[0][i] == board[1][i] & & board[1][i] == board[2][i] & & board[1][i] != \' \') { return board[1][i]; } } //判断对角线 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]; } //判断平局 int ret = IsFull(board, ROW, COL); if (ret == 1) { return \'E\'; } else return \'C\'; } int IsFull(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++) { if (board[i][j] == \' \') { return 0; } } } return 1; }

game.c:游戏相关函数的实现
#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); int IsFull(char board[ROW][COL], int row, int col);

2、扫雷游戏1)写两个数组:一个用来存放布置的雷的信息,另一个存放排查出来的雷的信息
2)如果想实现9*9的棋盘,数组设计成11*11的大小
test.c:测试游戏的逻辑
//扫雷游戏的测试 #include "game.h"int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu(); printf("请选择:> "); scanf("%d", & input); switch (input) { case 1: game(); break; case 0: printf("退出成功"); break; default: printf("选择错误,请重新选择!"); break; } } while (input); return 0; }

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 #define MID_COUNT 20 #define HARD_COUNT 30//游戏的函数声明 void menu(); void game(); void InitBoard(char board[ROWS][COLS], int rows, int cols,char tag); void DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char mine[ROWS][COLS],int row,int col); void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col); int GetMineCount(char mine[ROWS][COLS], int x, int y);

game.c:游戏相关函数的实现
//游戏的函数实现 #include "game.h" void menu() { printf("***********************\\n"); printf("*********1.play********\\n"); printf("*********0.exit********\\n"); printf("***********************\\n"); } void game() { char mine[ROWS][COLS] = {0}; //存放布置好的雷的信息 char show[ROWS][COLS] = {0}; //存放排查出的雷的信息 //初始化棋盘 InitBoard(mine, ROWS, COLS, \'0\'); InitBoard(show, ROWS, COLS, \'*\'); //打印棋盘 DisplayBoard(show, ROW, COL); //布置雷 SetMine(mine, ROW, COL); //打印棋盘 // DisplayBoard(mine, ROW, COL); //排查雷 FindMine(mine, show, ROW, COL); } //初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols, char tag) { int i = 0; int j = 0; for (i = 0; i < = rows; i++) { for (j = 0; j < = cols; j++) { board[i][j] = tag; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("-----扫雷游戏------\\n"); //打印列号 for (i = 1; i < = col; i++) { printf("%d", i); } printf("\\n"); for (i = 1; i < = row; i++) { printf("%d", i); //打印行号for (j = 1; j < = col; j++) { printf("%c", board[i][j]); } printf("\\n"); } } void SetMine(char mine[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = 0; int y = 0; //生产随机下标 x = rand() % row + 1; y = rand() % col + 1; if (mine[x][y] == \'0\') { mine[x][y] == \'1\'; count--; } } } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { //输入要排查的坐标 //检查坐标处是不是雷: //如果是--> 炸死,游戏结束 //如果不是--> 统计坐标周围雷的个数--> 存储到show数组中,游戏继续 int x = 0; int y = 0; int win=0; while (win< row+col-EASY_COUNT) { printf("请输入要排查的坐标:\\n"); scanf("%d %d", & x, & y); if (x > = 1 & & x < = row & & y > = 1 & & y < = col) { if (mine[x][y] == \'1\') { printf("很遗憾你被炸死了,游戏结束\\n"); DisplayBoard(mine, ROW, COL); break; } else { int minecount = GetMineCount(mine, x, y); show[x][y] = minecount + \'0\'; DisplayBoard(show, ROW, COL); win++; } } else { printf("坐标不合法!请重新输入!\\n"); } } if (win==row+col-EASY_COUNT) { printf("排雷成功!\\n"); DisplayBoard(mine, ROW, COL); }} int GetMineCount(char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * \'0\'; }

【重新学习C语言day07】

    推荐阅读