C语言实现控制台五子棋小游戏
时间:2022-03-29 09:13:45|栏目:C代码|点击: 次
这是一个用C语言实现的控制台小游戏―-五子棋,棋盘大小和获胜棋子数目是用宏定义的,可以自定义,所以可以轻松改为三子棋、六子棋等等。此游戏设定为人机对战(PVE),电脑走棋是随机的。
编译环境:VS2015
此程序采用多文件编译,共有三个文件,一个game.h头文件,一个game.c文件和一个main.c文件,代码分别如下:
game.h
这里是一些数据和函数的声明
#ifndef _GAME_H_ #define _GAME_H_ #define _BOARD_ 10 //棋盘大小 #define _PIECE_ 5 //获胜所需要连起来的棋子个数 typedef struct Board{ char show[_BOARD_][_BOARD_]; //棋盘字符数组 char Pc; //电脑使用的棋子 char Player; //棋手使用的棋子 }Brd ,*pBrd; //Brd:五子棋 pBrd:五子棋指针 void init(pBrd B); void PrintBrd(const pBrd B); void Pcrand(pBrd B); int Player(pBrd B); int judge(pBrd B, int n); void menu(pBrd B); #endif // ! _GAME_H_
game.c
这里是函数的具体实现
#include<stdio.h> #include<stdlib.h> #include<time.h> #include "game.h" void init(pBrd B) { int i, j; for (i = 0; i < _BOARD_; i++) { for (j = 0; j < _BOARD_; j++) { B->show[i][j] = ' '; } } B->Pc = '@'; B->Player = '*'; } void PrintBrd(const pBrd B) { int i, j; printf(" "); for (i = 0; i < _BOARD_; i++) { printf(" %d", i + 1); } printf("\n"); for (i = 0; i < _BOARD_; i++) { for (j = 0; j < _BOARD_; j++) { if (j == 0)printf(" %d|", i + 1); printf(" %c |", B->show[i][j]); } if (i == 0)printf(" Player: *"); if (i == 1)printf(" AI: @"); printf("\n\n"); } } void Pcrand(pBrd B) { int i, j; srand((unsigned int)time((NULL))); int n = rand()%(_BOARD_*_BOARD_); for (i = 0; i < _BOARD_; i++) { for (j = 0; j < _BOARD_; j++) { if (n > 0)n--; else if (n == 0 && B->show[i][j] == ' ') { B->show[i][j] = B->Pc; return; } } } } int Player(pBrd B) { int i, j; printf("请输入落子位置:\n"); printf("行:>"); scanf("%d", &i); printf("列:>"); scanf("%d", &j); i--; j--; if (B->show[i][j] != ' ' || i >= _BOARD_ || j >= _BOARD_ || i < 0 || j < 0) { printf("请输入正确的位置!\n"); system("pause"); return 0; } B->show[i][j] = B->Player; return 1; } int judge(pBrd B, int n) { char s; int c = 0; int i, j, sum = 0; if (n == 1)s = B->Player; else if (n = 2) s = B->Pc; for (i = 0; i < _BOARD_; i++) { sum = 0; for (j = 0; j < _BOARD_; j++) { if (B->show[i][j] == s)sum++; else sum = 0; if (sum == _PIECE_)return 1; } } for (j = 0; j < _BOARD_; j++) { sum = 0; for (i = 0; i < _BOARD_; i++) { if (B->show[i][j] == s)sum++; else sum = 0; if (sum == _PIECE_)return 1; } } sum = 0; for (i = 0, j = 0; i < _BOARD_; i++, j++) { if (B->show[i][j] == s)sum++; else sum = 0; if (sum == _PIECE_)return 1; } sum = 0; for (i = 0, j = _BOARD_ - 1; i < _BOARD_; i++, j--) { if (B->show[i][j] == s)sum++; else sum = 0; if (sum == _PIECE_)return 1; } return 0; } void menu(pBrd B) { int c,n = 1; printf("****************************************\n"); printf("********* 1.play ** 0.exit *********\n"); printf("****************************************\n"); printf("请输入选项:>"); scanf("%d", &c); while (c) { system("cls"); if (n == 1) { PrintBrd(B); if(Player(B))n=2; else continue; } else { Pcrand(B); n = 1; } if (judge(B, n)) { PrintBrd(B); if (n == 1)printf("Old Fe 666!\n"); else printf("请大侠重新来过\n"); break; } } }
main.c
程序入口
#include"game.h" int main() { Brd B; init(&B); menu(&B); printf("Thanks for playing my game.\n"); return 0; }
运行截图