五子棋(完整源码)

news/2024/11/24 4:53:25/

单文件完整源码:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>#define ROW 9
#define COL 9
#define NUM 5//菜单
void menu()
{printf("*************  三子棋游戏  ************\n");printf("************1.play   0.exit************\n");
}//胜利打印
void Win()
{printf("*************  你赢啦  ************\n");
}//失败打印
void Defeat()
{printf("*************  你输啦  ************\n");
}//平局打印
void Draw()
{printf("*************  平局啦  ************\n");
}//初始化棋盘
void Initializeboard(char borad[ROW][COL], int row, int col)
{for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){borad[i][j] = ' ';}}
}//打印棋盘
void Dispiayboard(char borad[ROW][COL], int row, int col)
{system("cls");for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){printf(" %c ", borad[i][j]);if (j != col-1){printf("|");}}printf("\n");if (i != col-1){for (int n = 0; n < col; n++){printf("---");if (n != col-1){printf("|");}}printf("\n");}}
}//向左查同
int Cleft(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y-1 >= 0) && (borad[x][y-1] == type)){count ++;count += Cleft(borad, type, x, y-1);}return count;
}//向右查同
int Cright(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y+1 < COL) && (borad[x][y+1] == type)){count ++;count += Cright(borad, type, x, y+1);}return count;
}//向上查同
int Cover(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (borad[x-1][y] == type)){count ++;count += Cover(borad, type, x-1, y);}return count;
}//向下查同
int Cdown(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (borad[x+1][y] == type)){count ++;count += Cdown(borad, type, x+1, y);}return count;
}//向左斜左查同
int Cldial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y-1 >= 0) && (borad[x+1][y-1] == type)){count ++;count += Cldial(borad, type, x+1, y-1);}return count;
}//向左斜右查同
int Cldiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y+1 < COL) && (borad[x-1][y+1] == type)){count ++;count += Cldiar(borad, type, x-1, y+1);}return count;
}//向右斜左查同
int Crdial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y-1 >= 0) && (borad[x-1][y-1] == type)){count ++;count += Crdial(borad, type, x-1, y-1);}return count;
}//向右斜右查同
int Crdiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y+1 < COL) && (borad[x+1][y+1] == type)){count ++;count += Crdiar(borad, type, x+1, y+1);}return count;
}//平局判定
int Isfull(char borad[ROW][COL], int row, int col)
{for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){if (borad[i][j] == ' '){return 1;}}}return 2;
}//判断输赢
int Iswin(char borad[ROW][COL], char type, int x, int y)
{//横向判定“---”int temp1 = 1;temp1 += Cleft(borad, type, x, y);temp1 += Cright(borad, type, x, y);//竖向判定“|”int temp2 = 1;temp2 += Cover(borad, type, x, y);temp2 += Cdown(borad, type, x, y);//左斜判定“/”int temp3 = 1;temp3 += Cldial(borad, type, x, y);temp3 += Cldiar(borad, type, x, y);//右斜判定“\”int temp4 = 1;temp4 += Crdial(borad, type, x, y);temp4 += Crdiar(borad, type, x, y);if ((temp1==NUM) || (temp2==NUM) || (temp3==NUM) || (temp4==NUM)){return 0;}else{//判断平局函数return Isfull(borad, ROW, COL);}
}//玩家下棋
int Playermove(char borad[ROW][COL], int row, int col)
{int x, y, z;//利用do while循环和状态值z实现坐标不合法时重新输入do{	z = 1;printf("请输入要下的坐标:\n");scanf("%d%d", &x, &y);//验证当前是否被占用或者超出范围if (borad[x-1][y-1] == ' '){//坐标可用改变状态值z以实现跳出循环z = 0;}else{printf("坐标不合法(被占用或者不在范围),请重新输入!\n");}} while (z);//将玩家下棋位置标记为“*”borad[x-1][y-1] = '*';//返回输赢结果return Iswin(borad, '*', x-1, y-1);
}//电脑下棋
int Computermove(char borad[ROW][COL], int row, int col)
{int x, y;while (1){x = rand()%row;y = rand()%col;if (borad[x][y] == ' '){borad[x][y] = '#';return Iswin(borad, '#', x, y);}}
}void game()
{//创建棋盘char borad[ROW][COL] = {0};//初始化棋盘Initializeboard(borad, ROW, COL);//打印棋盘Dispiayboard(borad, ROW, COL);while (1){//玩家下棋int ret1 = Playermove(borad, ROW, COL);if (ret1 == 0){Dispiayboard(borad, ROW, COL);printf("\n");Win();printf("\n\n\n");break;}else if (ret1 == 2){Dispiayboard(borad, ROW, COL);printf("\n");Draw();printf("\n\n\n");break;}//电脑下棋int ret2 = Computermove(borad, ROW, COL);//打印结果Dispiayboard(borad, ROW, COL);if (ret2 == 0){printf("\n");Defeat();printf("\n\n\n");break;}else if (ret2 == 2){printf("\n");Draw();printf("\n\n\n");break;}}
}//测试代码
void test()
{int a;srand((unsigned int) time(NULL));do{menu();scanf("%d", &a);switch (a){case 0:{printf("退出游戏!\n");break;}case 1:{game();break;}default:{printf("请输入正确指令!\n");}}} while (a);
}int main()
{test();return 0;
}

标准化项目源码:

test.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "game.h"void menu()
{printf("*******************************************               三子棋游戏               *************************************");printf("************************************************************************************************************************");printf("*******************************************             1.Play  0.Exit             *************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void Win()
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("*********************************************               你赢啦               ***************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void Defeat()
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("*********************************************               你输啦               ***************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void Draw()
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("*********************************************               平局啦               ***************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void game()
{char borad[ROW][COL] = {0};//初始化棋盘Initializeboard(borad, ROW, COL);//打印棋盘Dispiayboard(borad, ROW, COL);while (1){//玩家走int ret = Playermove(borad, ROW, COL);//清屏system("cls");//打印结果Dispiayboard(borad, ROW, COL);if (ret == 0){Win();break;}else if (ret == 2){Draw();break;}Sleep(1000);//电脑走int ret2 = Computermove(borad, ROW, COL);//清屏system("cls");//打印结果Dispiayboard(borad, ROW, COL);if (ret2 == 0){Defeat();break;}else if (ret2 == 2){Draw();break;}}
}void test()
{int a;srand((unsigned int)time(NULL));do{printf("请根据菜单选择(1开始游戏,0退出游戏):\n");scanf("%d", &a);switch (a){case 1:{system("cls");game();break;}case 0:{printf("退出游戏!\n");break;}default:{printf("请输入正确选项!\n");}}} while (a);
}int main()
{//打印菜单menu();//运行测试test();return 0;
}

game.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"//初始化棋盘
void Initializeboard(char borad[ROW][COL], int row, int col)
{int i = 0;int j = 0;for (i = 0; i < row; i++){for (j = 0; j < col; j++){borad[i][j] = ' ';}}
}//打印棋盘
void Dispiayboard(char borad[ROW][COL], int row, int col)
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("*******************************************               三子棋游戏               *************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("\n");printf("\n");int i = 0;int j = 0;for (i = 0; i < row; i++){for (j = 0; j < col; j++){printf(" %c ", borad[i][j]);if (j == col-1){break;}printf("|");}if (i == row-1){break;}printf("\n");for (int i = 0; i < col; i++){printf("---");if (i == col-1){break;}printf("|");}printf("\n");}printf("\n");printf("\n");printf("************************************************************************************************************************");
}//玩家走
int Playermove(char borad[ROW][COL], int row, int col)
{int x, y;while (1){printf("请输入坐标:\n");scanf("%d%d", &x, &y);if (x>0 && x<=row && y>0 && y<=col ){if (borad[x-1][y-1] == ' '){borad[x-1][y-1] = '*';int ret = Iswin(borad, '*', x-1, y-1);return ret;}else{printf("坐标被占用,请重新输入!\n");}}else{printf("坐标不在范围,请重新输入:\n");}}
}//电脑走
int Computermove(char borad[ROW][COL], int row, int col)
{int x, y;while (1){x = rand()%row;y = rand()%col;if (borad[x][y] == ' '){borad[x][y] = '#';int ret = Iswin(borad, '#', x, y);return ret;break;}}
}//判断输赢
int Iswin(char borad[ROW][COL], char type, int x, int y)
{//横向判定“---”int temp1 = 1;temp1 += Cleft(borad, type, x, y);temp1 += Cright(borad, type, x, y);//竖向判定“|”int temp2 = 1;temp2 += Cover(borad, type, x, y);temp2 += Cdown(borad, type, x, y);//左斜判定“/”int temp3 = 1;temp3 += Cldial(borad, type, x, y);temp3 += Cldiar(borad, type, x, y);//右斜判定“\”int temp4 = 1;temp4 += Crdial(borad, type, x, y);temp4 += Crdiar(borad, type, x, y);if ((temp1==NUM) || (temp2==NUM) || (temp3==NUM) || (temp4==NUM)){return 0;}else{//判断平局函数return Isfull(borad, ROW, COL);}
}//向左查同
int Cleft(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y-1 >= 0) && (borad[x][y-1] == type)){count ++;count += Cleft(borad, type, x, y-1);}return count;
}//向右查同
int Cright(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y+1 < COL) && (borad[x][y+1] == type)){count ++;count += Cright(borad, type, x, y+1);}return count;
}//向上查同
int Cover(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (borad[x-1][y] == type)){count ++;count += Cover(borad, type, x-1, y);}return count;
}//向下查同
int Cdown(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (borad[x+1][y] == type)){count ++;count += Cdown(borad, type, x+1, y);}return count;
}//向左斜左查同
int Cldial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y-1 >= 0) && (borad[x+1][y-1] == type)){count ++;count += Cldial(borad, type, x+1, y-1);}return count;
}//向左斜右查同
int Cldiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y+1 < COL) && (borad[x-1][y+1] == type)){count ++;count += Cldiar(borad, type, x-1, y+1);}return count;
}//向右斜左查同
int Crdial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y-1 >= 0) && (borad[x-1][y-1] == type)){count ++;count += Crdial(borad, type, x-1, y-1);}return count;
}//向右斜右查同
int Crdiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y+1 < COL) && (borad[x+1][y+1] == type)){count ++;count += Crdiar(borad, type, x+1, y+1);}return count;
}//平局判定
int Isfull(char borad[ROW][COL], int row, int col)
{for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){if (borad[i][j] == ' '){return 1;}}}return 2;
}

 game.h

#define ROW 9
#define COL 9
#define NUM 5#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <Windows.h>void Initializeboard(char borad[ROW][COL], int row, int col);
void Dispiayboard(char borad[ROW][COL], int row, int col);
int Playermove(char borad[ROW][COL], int row, int col);
int Computermove(char borad[ROW][COL], int row, int col);
int Iswin(char borad[ROW][COL], char type, int x, int y);
int Isfull(char borad[ROW][COL], int row, int col);
int Cleft(char borad[ROW][COL], char type, int x, int y);
int Cright(char borad[ROW][COL], char type, int x, int y);
int Cover(char borad[ROW][COL], char type, int x, int y);
int Cdown(char borad[ROW][COL], char type, int x, int y);
int Cldial(char borad[ROW][COL], char type, int x, int y);
int Cldiar(char borad[ROW][COL], char type, int x, int y);
int Crdial(char borad[ROW][COL], char type, int x, int y);
int Crdiar(char borad[ROW][COL], char type, int x, int y);

http://www.ppmy.cn/news/881448.html

相关文章

联网版五子棋

下面的代码实现了一个非常简单的联网五子棋程序&#xff0c;写给学生的一个例子&#xff0c;很多功能都还没实现&#xff0c;用的也是民间规则&#xff0c;姑且一看吧&#xff01; 棋盘类&#xff1a; package com.accp;import java.awt.Color; import java.awt.Graphics;/***…

我的五子棋人工智能算法

近期完成了一个支持人机对战的五子棋游戏。虽然比较简单,而且对于常下五子棋的人来说,计算机的胜率往往低于50%,但无论如何,我对人工智能一块还是比较满意,所以发表出来,以期抛砖引玉,和大家共同探讨。 对于五子棋游戏来说,人工智能的核心思想是:分析棋盘上所有的空…

五子棋项目

目录 核心技术: 主要模块和功能: 基本思路: 注册、登录接口: 具体实现: 匹配功能接口: 具体实现: 用户对战接口: 具体实现: 项目源码Gitee地址: 网页版五子棋的基本思路及实现 核心技术: Spring/SpringBoot/SpringMVCWebSocketMySQLMyBatisHTML/CSS/JS/AJAX 主要模…

python五子棋ai棋力最高_棋力最强的五子棋 App 是什么?

插嘴说下这个五子棋大师 编程我不懂&#xff0c;这个五子棋大师的计算尚可&#xff0c;应该在五步以上&#xff0c;除去掌握比较熟练的棋型&#xff0c;大部分情况下我没有它算的远&#xff0c;如果正常和它下的话&#xff0c;想赢最高级&#xff0c;很吃力。 但是我还是可以轻…

简易人机对弈算法的五子棋程序

要求的五子棋游戏应达到以下几方面的要求: &#xff08;1&#xff09;运行程序后即刻出现棋盘并可以开始下棋&#xff1b; &#xff08;2&#xff09;人机对弈时&#xff0c;先手为黑棋&#xff0c;后手为白棋&#xff1b;人为先手&#xff0c;计算机为后手&#xff1b; &…

inventor2五子棋游戏apk_五子棋大师2中文版游戏

五子棋大师2中文版是一款经典好玩的五子棋游戏&#xff0c;计有世界比赛和区域比赛两种形态。无论何种比赛形态&#xff0c;玩家均必须和电脑控制的几位对手进行比赛&#xff0c;求取最佳名次&#xff0c;以打入排行榜。电脑控制的对手棋力和棋风各有不同&#xff0c;但整体而言…

五子棋 源码

JS 五子棋 带成绩排名 游戏&#xff1a;http://wx0725.top/index.php/181.html 主程序学习&#xff1a;https://blog.csdn.net/qq_44731019/article/details/109586730 改了几个 BUG添加了成绩截图功能去掉了悔棋 自己看着重新写吧&#xff0c;下面代码只是一个思路&#xff0c…

五子棋AI算法

在本次“五子棋“程序的编写中&#xff0c;只编写了人机对弈部分&#xff0c;运用了博弈树进行搜索&#xff0c;在选取最优的走步时使用极大极小分析法&#xff0c;考虑到搜索的时间复杂度和空间复杂度&#xff0c;在程序中只进行了2步搜索&#xff0c;即计算机在考虑下一步的走…