C++语言五子棋源程序 (利用坐标确定棋子位置)

python 经典实践教程:五子棋如何获得鼠标点击坐标 @本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府 这篇文章主要介绍了python 五子棋如何获得鼠标点击坐标,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 点坐标的取自: from tkinter import * root=Tk() #创建一个框架,在这个框架中响应事件 frame=Frame(root,width=200,height=200) def callBackLeft(event): print("相对于应 阅读详情

http://blog.csdn.net/waterboy/archive/2004/08/24/83316.aspx

************************************************************** C++语言五子棋源程序
* 南阳理工学院计算机系01612班大鹏课程设计作品
* 运行环境:TC3.0 (vc6.0不支持画图函数,无法画出棋盘和棋子)
* Email:ypsky@mail2.nyist.net
************************************************************* #include
#include
#include
#include
#define backcolor CYAN
#define defaultcolor BLACK
#define linecolor MAGENTA
#define player1_color RED
#define player2_color WHITE
#define error_color RED
#define winner_color RED

const int left=40;
const int top=390;
const int d=30;
const int line_num=9;
const int turn=0;
const int r=d/3;
const int j=10;
int x,y,k=1,step=(line_num+1)*(line_num+1);
union REGS regs1,regs2;
class player1;
class player2;
class qipan{
public:
qipan();
~qipan(){};
void init_qipan();
friend void fall(player1 &num1,player2 &num2,qipan &num);
friend void input(player1 &num1,player2 &num2,qipan &num);

private:
int point[line_num+1][line_num+1];

};
class player1{
public:
player1();
~player1(){};
friend void fall(player1 &num1,player2 &num2,qipan &num);
friend void input(player1 &num1,player2 &num2);
friend int judge_winner(player1 &num1,player2 &num2);
private:
int point1[line_num+1][line_num+1];
};
class player2{
public:
player2();
~player2(){};
friend void fall(player1 &num1,player2 &num2,qipan &num);
friend void input(player1 &num1,player2 &num2,qipan &num);
friend int judge_winner(player1 &num1,player2 &num2);
private:
int point2[line_num+1][line_num+1];
};
void input(player1 &num1,player2 &num2);
void fall(player1 &num1,player2 &num2,qipan &num);
int judge_winner(qipan &num,player1 &num1,player2 &num2);
void inputerror();
void display_winner(int);


void main()
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"e:/tc30/bgi");

qipan num;
player1 num1;
player2 num2;
while(step--)
{
input(num1,num2,num);
fall(num1,num2,num);
if(judge_winner(num1,num2))
{
display_winner(k);
}
}

// getchar();
}
qipan::qipan(void)
{ int j,i;
char ch[2]="0";
setbkcolor(backcolor);
setcolor(linecolor);
for(i=0;i<=line_num;i++)
{
line(left,top-i*d,left+line_num*d,top-i*d);
}
for(i=0;i<=line_num;i++)
{
line(left+i*d,top,left+i*d,top-line_num*d);
}
for(i=0;i<=line_num;i++)
{ if(*ch=='9'+1) *ch='a';
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+i*d-2,top+r+3,ch);
(*ch)=(*ch)+1;
}
*ch='0';
for(i=0;i<=line_num;i++)
{if(*ch=='9'+1) *ch='a';
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left-r-10,top-d*i-3,ch);
(*ch)=(*ch)+1;
}

setcolor(defaultcolor);

for(i=0;i<=line_num;i++)
{
for(j=0;j<=line_num;j++)
point[i][j]=0;
}
}

void fall(player1 &num1,player2 &num2,qipan &num)
{
int flag=k%2;
if(flag)
{ setcolor(player2_color);
num2.point2[x][y]=1;
num.point[x][y]=2;
circle(left+d*x,top-d*y,r);
setfillstyle(1,player2_color);
floodfill(left+d*x,top-d*y,player2_color);
}
else
{ num1.point1[x][y]=1;
num.point[x][y]=1;
setcolor(player1_color);
circle(left+d*x,top-d*y,r);
setfillstyle(1,player1_color);
floodfill(left+d*x,top-d*y,player1_color);
}

setcolor(defaultcolor);
}

void input(player1 &num1,player2 &num2,qipan &num)
{ char xx,yy;

k++;
while(1)
{
regs1.h.ah=0;
xx=int86(22,®s1,®s1)-'0';
if(xx==('q'-'0')||xx==('Q'-'0'))
{ step=0;
return;
}
regs1.h.ah=0;
yy=int86(22,®s1,®s1)-'0';

if(yy==('q'-'0')||yy==('Q'-'0'))
{
step=0;
return ;
}
if(xx<0||xx>line_num)
{ inputerror();
continue;
}
if(yy<0||yy>line_num)
{inputerror();
continue;
}

if(num.point[xx][yy]==0)
{
break;
}
else
{
inputerror();
continue;
}
}

x=(int)xx;
y=(int)yy;

setcolor(backcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,"Input error");
setcolor(defaultcolor);
}



player1::player1()
{
int i,j;
for(i=0;i<=line_num;i++)
{
for(j=0;j<=line_num;j++)
point1[i][j]=0;
}
}

player2::player2()
{ int i,j;
for(i=0;i<=line_num;i++)
{
for(j=0;j<=line_num;j++)
point2[i][j]=0;
}
}
void inputerror(void)
{ setcolor(error_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,"Input error");
setcolor(defaultcolor);
}

int judge_winner(player1 &num1,player2 &num2)
{
int count=0,m=0,n=0,a=0,b=0,xx0,yy0;
int flag=k%2;
xx0=x; yy0=y;
if(!flag)
{ //left <-------> right
while(xx0>=1&&m<4) {xx0--;m++;}
while(n<9&&xx0<=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up <------> down
count=0; xx0=x; m=0; n=0;
while(yy0>=1&&m<4){yy0--;m++;}
while(n<9&&yy0<=line_num)
{
if(num1.point1[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up ----- right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0>=1&&m<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0--;
n++;
}
//right up <-----> left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0 <4){ xx0++; m++; a++;} m="0;"
while(yy0 <4){ m++;
if(a<=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0>=0&&yy0>=0&&n<9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0--;
yy0--;
n++;
}
//no winer
return 0;
}
else
{
//left <-------> right
while(xx0>=1&&m<4) {xx0--;m++;}
while(n<9&&xx0<=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up <------> down
count=0; xx0=x; m=0; n=0;
while(yy0>=1&&m<4){yy0--;m++;}
while(n<9&&yy0<=line_num)
{
if(num2.point2[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up ----- right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0>=1&&m<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0--;
n++;
}
//right up <-----> left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0 <4){ xx0++; m++; a++;} m="0;"
while(yy0 <4){ m++;
if(a<=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0>=0&&yy0>=0&&n<9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0--;
yy0--;
n++;
}
//no winer
return 0;

}
}

void display_winner(int k)
{
int flag=k%2;
if(!flag)
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+d*2,top+40,"Red is winner");
setcolor(defaultcolor);
step=0;
getchar();
}
else
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+2*d,top+40,"White is winner");
setcolor(defaultcolor);
step=0;

}
}
C++输入坐标五子棋小游戏 通过控制台输入x,y坐标,来达到五子棋的效果。通过控制台输入x,y坐标,来达到五子棋的效果。 立即下载

相关推荐

使用C++开发的双人五子棋游戏(附源代码)

这是一款使用Qt和C++开发的中国象棋游戏Windows应用程序,里面有一个打包好的可执行程序WeGo,整个项目的源代码在压缩包W_GO里。

五子棋 输赢判断算法

我们以一个二位数组表示五子棋,通过x、y坐标,表示一个棋子的位置。 1、基本原理: 五子棋判断输赢,其实就是一个二维坐标上,判断上下、左右、两个45度直线,是否有五个相同的直连棋子。 2、判断时机: 一般情况下,我们可以通过遍历棋盘的每个点,从这个点出发,判断是否有五连子。但是实际上,我们并不需要这样做,因为我们在落子时,肯定当前其他地方没有五连子。我们要做的,就是从当前落子的位置判断,4...

windchen1000的专栏 2万+

九宫棋游戏加源码

九宫棋游戏加源码九宫棋游戏加源码九宫棋游戏加源码九宫棋游戏加源码九宫棋游戏加源码九宫棋游戏加源码

C++_五子棋(注释版)

//main.cpp #include<cstdio> #include"ChessBoard.h" //包含入需要的各类头文件,除了需要的标准库头文件,还需要我们自定义的两个类头文件,使得两个类对全段程序可见 #include"Player.h" int main() { ChessBoard board; //定义了棋盘对象board,思考该对象的构造过程 Player playA("aaa", '*'); //玩家aaa的棋子形状是'*' play

qq_45908742的博客 956

九宫棋源码

九宫棋源码 九宫棋源码 九宫棋源码 九宫棋源码 九宫棋源码 九宫棋源码

QT实现的五子棋

使用QT5编写的五子棋,可以选择游戏模式(人机或者人人)以及棋子颜色,AI是参考别人的

c语言实现五子棋游戏,C语言实现五子棋小游戏

C语言实现五子棋小游戏# include# include# include# include//插入输入输出头文件# include//字符数组的函数定义的头文件# include//stdlib 头文件即standard library标准库头文件stdlib 头文件里包含了C、C++语言的最常用的系统函数该 ,文件包含了C语言标准库函数的定义.# define SPA 0//在C或C++语言...

weixin_42499882的博客 552

C++实现简单的五子棋

只是简单的C++课程作业,写一个简单的五子棋,没有界面,在VC上就能运行。

C语言实现五子棋对弈(简单输入坐标下棋,可设置悔棋次数版)

一、 设计题目、任务及要求 题目:五子棋对弈 对弈规则如下: 主要功能是实现两人之间的对弈,在画好的棋盘上,两个玩家轮流选择自己的落子坐标,然后由五子棋系统自动识别判断游戏的进展,知道一方的五子连成一条线或者棋盘已经无法落子时游戏结束。 选定五子棋的棋盘大小为19*19,玩家可以在这个棋盘上选择落子坐标位置,通过在棋盘上显示不同的符号来代替不同玩家所下的棋子,“o”代表A玩家,“*”代...

yuukai的博客 1万+

五子棋大作业(C++实现)

1、有两种对弈模式:人人对弈模式和人机对弈模式 2、采用黑屏字符输入输出实现UI 3、采用打分方法实现AI 4、棋盘大小为15*15,棋盘将显示相应的行列号(A-O) 5、棋子有两种颜色黑和白,代表着对弈的双方,棋子放在棋盘行列交叉处,惯例黑棋先下。 6、黑白任一方先连成5个棋子形成的直线(横线、竖线、对角线),则该方赢对方输 7、任一方都没连成直线,且棋盘已满时,为和局 8、最后一步下的位置有特殊标记

C++实现五子棋

使用C++实现五子棋功能,并不完善,不过可以借鉴其整体结构和流程。

C++实现简单的五子棋程序

//全部代码12 .26.2017 #include #include #include     //格式化输出 using namespace std; const int N = 9;       //修饰N为常量 static int A=0, B=0; char command; class jiemian { public: void Jiemai

weixin_39807914的博客 2万+

C++小游戏-五子棋(人机对战增强版).cpp

电脑已经不再死板,可以与你一起下五子棋,棋艺不佳者可移步“五子棋(菜鸟版)”,下载后用C++方可打开,玩的时候一定记住,全屏!全屏!!全屏!!!

c++实现五子棋

思路:先用用system(“color 70”)改变控制台的背景色为灰白色,前景色为黑色,然后用“■”打印棋盘,然后用SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a)改变输出颜色分别为白色和黑色,用字符“●”打印黑棋和白棋。 整个棋盘用一个char类型的二维数组保存,空的地方用‘ ’标识,玩家一下棋的地方用‘x’标识,玩家二下棋...

liumanghu的博客 3612

局部,全局(外部),static等变量详解

首先,必须明白一个程序是包含若干个源文件,每个源文件又是包含若干个函数,每个源文件、函数中又定义了若干个变量。但是每个变量都有自己的作用范围,也就是自己的作用域。只有在作用域内才可以访问变量。 变量的有效范围称为变量的作用域( scope)。归纳起来,变量有4种不同的作用域:文件作用域( file scope)、函数作用域( function scope)、块作用域(block sc...

归根结底不优秀的博客 1329

测试人必看!正交表与Allpairs工具对比:如何选择最适合你的组合测试方案?

本文深入对比了正交表、PICT与Allpairs三种主流组合测试方案,帮助测试人员根据项目场景做出最佳选择。文章分析了各自的核心原理、优劣势及适用场景,特别强调了在涉及复杂约束时PICT工具的强大功能,并提供了从原理学习到CI/CD集成的实战选型指南。

weixin_29224309的博客 271

c++语言设计五子棋游戏,C++实现五子棋游戏

搜索热词三子棋、五子棋之类的游戏,非常简单,对于初学者来说是一个不错的练手的小项目,以前用C语言写过三子棋游戏。最近在看C++,所以就想到在三子棋的基础上利用C++语言实现五子棋游戏。主要功能:有3个模式:0表示退出、1表示电脑vs玩家、2表示玩家vs玩家。当一局完成之后选择'y'则又会进入选择模式。源代码(VS2013编译器下写的):#include#include#include#includ...

weixin_33754535的博客 4027

基于知识图谱的电影推荐系统——Neo4j&Python

也可直接从这里下载:链接: https://pan.baidu.com/s/1l6wjwcUzy5G_dIlVDbCkpw 提取码: pkq6。修改main.py中的driver,输入自己数据库的用户名与密码。系统会询问是否需要重新加载并创建知识图谱,在第一次时输入1。

weixin_46351593的博客 6359
上一篇: PHP程序加速探索
下一篇: 尽可能使用堆栈变量
oxware
博客等级 码龄24年 3粉丝 61原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值