对于很多C语言学习者,做一个五子棋游戏是比较困难,因为没有经验,下面博主带着这位一起来开发这一块人人都想学习的五子棋大战游戏吧,当然想了解更过,学习更多C/C++编程开发教程、入门、大型项目视频教程可以加QQ群:569268376,好啦,废话不多说,直接上代码
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
//常量
#define CHECKERBOARDSIZE 15 ///棋盘大小
//全局变量
int g_count = 0; ///游戏回合数
int g_checkboard[CHECKERBOARDSIZE][CHECKERBOARDSIZE]; ///存储对局数据
int g_cursorX, g_cursorY; ///当前光标位置
int g_currentGamer; ///当前下子玩家 1:黑子;2:白子
//全局函数
int RunGame(); ///进行整个对局,返回赢家信息
void Initialize(); ///初始化一个对局
void DrawCheckerboard();///绘制棋盘
int Put(); ///在当前光标位置下子,如果非空,则表示下子失败,返回0
int Check(); ///判断胜负,即判断当前走子,五子连珠
char* GetStyle(int x,int y);///获取棋盘指定坐标点位置的字符
int main()
{
更多精彩教程,视频教学、源码分享请加入QQ群:569268376
SetConsoleTitle(L"五子棋人人对战"); //设置窗口标题
system("mode con cols=31 lines=17");//设置窗口大小
//控制台 color -help
system("color 80");//设置颜色
while (1)
{
//循环执行游戏
RunGame();
}
return 0;
}
///进行整个对局,返回赢家信息
int RunGame()
{
int nInput; //输入变量
int nWinner; //赢家信息
Initialize(); //初始化对局
//死循环,胜利跳出循环
while (1)
{
DrawCheckerboard();//绘制棋盘
nInput = getch();//等待键盘输入
switch (nInput)
{
case 27:///ESC
exit(0);
break;
case 32://空格 :开始下子
{
//下子,判断是否成功
if (Put())
{
//判断输赢
nWinner = Check();
g_currentGamer = 3 - g_currentGamer; //更新此时玩家
g_count++;