**本文是用C语言写的N子棋小游戏———一个C语言前期寓教于乐的小游戏。
ps.本文实例用三子棋来演示,需要可自行修改变量row,cow.
目录
一.游戏菜单的创建
在我们进入游戏时,首先出现的就应该是我们的菜单界面。
我们选择使用“菜单函数”menu()来实现这一功能,提高程序的可重复性,确保代码块的函数分块使用。
游戏菜单应该实现这几个功能:
- 游戏的进入
- 游戏的退出
接下来是我们实现这一功能的代码:
/* 菜单*/ void menu() { printf("******欢迎来到三子棋游戏******\n"); printf("****** 1·play ***********\n"); printf("******* 0·exit ********** \n"); printf("****************************\n"); }
二.游戏实现的思路
测试的逻辑——test.c
游戏的实现——game.h
游戏函数的声明——game.c
1.初始化棋盘
#define ROW 3 #define COL 3 char board[ROW][COL];
注:可通过改变变量增加游戏可玩性(3子->5子)
void init_board(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } }
2.打印棋盘
void print_board(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { //printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]); int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { //printf("---|---|---\n"); for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } }
3.玩家通过输入坐标(row,col)进行落子
void player_move(char board[ROW][COL], int row, int col) { printf("玩家下棋\n"); while (1) { printf("请输入要下棋的坐标:>"); int x = 0; int y = 0; 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"); } } }
4.电脑随机落子
在实现随机落子,我们需要创建随机数使用rand()函数,这就需要调用以下头文件:
void computer_move(char board[ROW][COL], int row, int col) { printf("电脑下棋:\n"); while (1) { int x = rand() % row; int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
5.判断棋盘是否为满
int is_full(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; }
6.判断输赢
char is_win(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][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[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 (is_full(board, row, col) == 1) { return 'Q'; } //继续 //没有玩家或者电脑赢,也没有平局,游戏继续 return 'C'; }
完整代码
/*主函数*/ int main() { int i = 0; do { menu(); printf("请输入:>"); scanf("%d", &i); switch (input) { case 1: printf("三子棋\n"); break; case 2: printf("退出游戏\n"); break; default: printf("输入错误,请重新选择:\n"); break; } } while (i); return 0; }
/* 头文件 */ #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h>
1.game.c
#include "game.h" void init_board(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } } void print_board(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { //printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]); int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { //printf("---|---|---\n"); for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } } void player_move(char board[ROW][COL], int row, int col) { printf("玩家下棋\n"); while (1) { printf("请输入要下棋的坐标:>"); int x = 0; int y = 0; 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 computer_move(char board[ROW][COL], int row, int col) { printf("电脑下棋:\n"); while (1) { int x = rand() % row; int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } //我希望is_full这个函数只是为了支持is_win函数的,只是在is_win函数内部使用 //那我们就没必要在头文件中声明 // //判断棋盘是否满了 static int is_full(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; } char is_win(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][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[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 (is_full(board, row, col) == 1) { return 'Q'; } //继续 //没有玩家或者电脑赢,也没有平局,游戏继续 return 'C'; }
2.game.h
#pragma once #define ROW 3 #define COL 3 //头文件中声明函数 //初始化棋盘 void init_board(char board[ROW][COL], int row, int col); //打印棋盘 void print_board(char board[ROW][COL], int row, int col); //玩家下棋 void player_move(char board[ROW][COL], int row, int col); //电脑下棋 void computer_move(char board[ROW][COL], int row, int col);
3.text.h
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" #include<stdio.h> void menu() /* 菜单*/ { printf("******欢迎来到三子棋游戏******\n"); printf("****** 1.play ***********\n"); printf("******* 0.exit ********** \n"); printf("****************************\n"); } void game() { char ret = 0; char board[ROW][COL]; //初始化一个ROW*COL的空棋盘 init_board(board, ROW, COL); print_board(board, ROW, COL); while (1) { player_move(board, ROW, COL); print_board(board, ROW, COL); //判断输赢 ret=is_win(board, ROW, COL); if (ret != 'C') { break; } computer_move(board, ROW, COL); print_board(board, ROW, COL); //判断输赢 if (ret != 'C') { break; } } if (ret == '*') { printf("玩家win\n"); } if (ret == '#') { printf("电脑win\n"); } if (ret == 'Q') { printf("平局\n"); } } //判断输赢 //电脑win:# //玩家win:* //平局:Q //游戏继续:C void test() { srand((unsigned int)time(NULL)); int input = 0; 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); } int main() { test(); return 0; }
本篇文章到此结束,请各位佬多多支持!😘😘