大家好,欢迎观看Mr.kanglong的CSDN博客,今天和大家讨论的是如何使用C语言来实现三子棋游戏。相信大家儿时或多或少玩过三子棋游戏,那么怎么用C语言实现这样一个游戏呢?且听亢龙娓娓道来~
目录
三子棋介绍
游戏效果
实现代码
game.h
game.c
test.c
三子棋介绍
三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉棋、一条龙、井字棋等。游戏分为双方对战,双方依次在9宫格棋盘上摆放棋子,率先将自己的三个棋子走成一条线就视为胜利,而对方就算输了,但是三子棋在很多时候会出现和棋的局面。
游戏效果
玩家通过输入要下棋的坐标下棋,玩家的棋子是*,电脑自动下棋,电脑的棋子是#,当某方的棋子率先连成一条线时,则取得胜利;若棋盘布满时还没有分出胜负,则视为平局。
实现代码
在三个文件中实现了三子棋游戏,分别是game.c game.h test.c,game.c中放置的是游戏用到的封装函数,game.h中放置的是game.c中函数的声明。
game.h
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//初始化棋盘
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
#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++){//打印数据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){for (j = 0; j < col; j++){printf("---");if (j < col - 1)printf("|");}printf("\n");}}
}
//玩家下棋
void Playermove(char board[ROW][COL], int row, int col)
{printf("玩家下棋>:\n");int x = 0;int y = 0;while (1){printf("请输入下棋的坐标>:");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;}elseprintf("该坐标已被占用。请输入其他坐标\n");}else{printf("坐标非法,请重新输入\n");}}
}//电脑随机下棋
void Computermove(char board[ROW][COL], int row, int col)
{printf("电脑下棋>:\n");while (1){int x = rand() % 3;int y = rand() % 3;if (board[x][y] == ' '){board[x][y] = '#';break;}}}
//判断棋盘是不是满了
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;}
//返回* -- 玩家赢
//返回# -- 电脑赢
//返回Q -- 平局
//返回C -- 继续
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][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[0][0] != ' '){return board[0][0];}//副对角线if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != ' '){return board[2][0];}//判断平局if (IsFull(board, ROW, COL))return 'Q';return 'C';
}
test.c
#include "game.h"void menu()
{printf("*******************************\n");printf("********** 1. play **********\n");printf("********** 0. exit **********\n");printf("*******************************\n");
}
void game()
{char ret = 0;//接受IsWin的返回值char board[ROW][COL];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");elseprintf("平局\n");
}
int main()
{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);return 0;
}
记得点赞哦