时间:2022-10-18 11:54:05 | 栏目:C代码 | 点击:次
本文实例为大家分享了C语言实现五子棋小游戏的具体代码,供大家参考,具体内容如下
我们需要一个二维数组去储存当前的棋盘状态,然后打印出来。
我们游戏的逻辑是初始化棋盘,打印棋盘,人下棋,打印棋盘,判断是否游戏结束,电脑下棋,打印棋盘,判断是否游戏结束,然后回到人下棋过程,结束游戏后,判断谁赢谁输还是平局。
判断输赢的逻辑是遍历行,遍历列,遍历主对角线,遍历反对角线,如果都判断不出输赢,那么就继续游戏。
game.h
#include <stdio.h> #include <stdlib.h> #include <time.h> //棋盘大小 #define ROW 10 #define COL 10 //初始化 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);
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #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; for (i = 0; i < row; i++) { //1.打印一行的数据 int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); //2.打印分割行 if (i < row - 1) { for (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("玩家下棋:>\n"); while (1) { printf("请输入要下的坐标:>"); scanf("%d%d", &x, &y); //判断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) { int x = 0; int y = 0; printf("电脑下棋:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } //返回1表示棋盘满了 //返回0表示棋盘没满 static 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;//满了 } char IsWin(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 - 4; j++) { if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j + 2] == board[i][j + 3] && board[i][j + 3] == board[i][j + 4] && board[i][j] != ' ') { return board[i][j]; } } } //竖 for (i = 0; i < row - 4; i++) { for (j = 0; j < col; j++) { if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i + 2][j] == board[i + 3][j] && board[i + 3][j] == board[i + 4][j] && board[i][j] != ' ') { return board[i][j]; } } } //两个对角线 for (i = 0; i < row - 4; i++) { for (j = 0; j < col - 4; j++) { if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i + 2][j + 2] == board[i + 3][j + 3] && board[i + 3][j + 3] == board[i + 4][j + 4] && board[i][j] != ' ') { return board[i][j]; } } } for (i = 0; i < row - 4; i++) { for (j = col-1; j >= 4; j--) { if (board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i + 2][j - 2] && board[i + 2][j - 2] == board[i + 3][j - 3] && board[i + 3][j - 3] == board[i + 4][j - 4] && board[i][j] != ' ') { return board[i][j]; } } } //判断是否平局 if (1 == IsFull(board, ROW, COL)) { return 'Q'; } return 'C'; }
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() { printf("***********************\n"); printf("*******五子棋游戏*******\n"); printf("**** 1.play 0.exit ***\n"); printf("***********************\n"); } // # | * | //---|---|--- // * | | //---|---|--- // | | // //告诉我们四种游戏的状态 //玩家赢-'*' //电脑赢-'#' //平局 -'Q' //继续 -'C' //游戏的整个算法实现 void game() { char ret = 0;//接收四种状态的返回值 //数组-存放走出的棋盘信息 char board[ROW][COL] = { 0 };//全部空格 InitBoard(board, ROW, COL);//初始化棋盘 //打印棋盘 DisplayBoard(board, ROW, COL); //下棋 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); //判断电脑是否赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else { printf("平局\n"); } } void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:>"); scanf("%d", &input); switch(input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择!\n"); break; } } while (input); } int main() { test(); return 0; }
功能测试