用c 编写五子棋数据库设计游戏程序如何和数据库文件连接

C语言五子棋程序 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 14时,
今天闲来无聊,就用C写了个五子棋程序,基本功能已实现。想想不能就这么简单的完了,想想加一个电脑AI岂不更好,但是实现起来太复杂,目前还在构思,就先把基本功能代码贴上来。如果哪位大侠有好的电脑AI建议,不妨提出来,大家一起交流学习下哈
代码片段(3)
1.&[图片] QQ截图10.png&&&&
五子棋.rar&~&1KB&&&&
3.&[代码][C/C++]代码&&&&
#include "windows.h"
#include "stdio.h"
#define up 'w'
#define left 'a'
#define down 's'
#define right 'd'
#define lz 'p'
#define cls 'q'
struct stu
//定义位置记录结构体
int player=1;
int Q[20][20]={0}; //定义数组以记录落子情况
void gotoxy(int x, int y) //建立坐标函数
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c); //修改当前光标的位置
void drawqipan() //绘制棋盘及数据初始化
system("cls"); //清除屏幕
for(i=0;i&20;i++)
for(j=0;j&20;j++)
Q[i][j]=0;
printf("十");
printf("\n");
weizhi.x=0;weizhi.y=0; //程序数据初始化
gotoxy(0,0);
void jilu() //记录落子情况
Q[weizhi.x][weizhi.y]=player+1;
if(player)
//玩家变换
int cluozi(int x,int y) //由电脑落子时调用
weizhi.x=x;weizhi.y=y;
gotoxy(weizhi.x,weizhi.y);
if(Q[weizhi.x][weizhi.y]==0) //判断当前位置是否已经落子
printf("○");
gotoxy(weizhi.x,weizhi.y);
void luozi() //玩家落子
if(Q[weizhi.x][weizhi.y]==0) //判断当前位置是否已经落子
if(player)
printf("●");
printf("○");
gotoxy(weizhi.x,weizhi.y);
int checkWin() //检查游戏是否有输赢
int r,c,rr,cc,count=0;
p=player==0?2:1;
for(c=0;c&20;c++)
for(r=0;r&20;r++)
if(Q[r][c]!=p)
rr=r;cc=c;
while(--cc&=0 &&Q[rr][cc]==p)count++;
while(++cc&20 &&Q[rr][cc]==p)count++;
if(count+1&=5)
while(--rr&=0 &&Q[rr][cc]==p)count++;
while(++rr&20 &&Q[rr][cc]==p)count++;
if(count+1&=5)
//检查反斜边
cc--;rr--;
while((cc&=0||rr&=0) &&Q[rr][cc]==p){count++;cc--;rr--;}
rr=r;cc=c;
cc++;rr++;
while((cc&20||rr&20) &&Q[rr][cc]==p){count++;cc++;rr++;}
rr=r;cc=c;
if(count+1&=5)
//检查正斜边
cc++;rr--;
while((cc&20||rr&=0) &&Q[rr][cc]==p){count++;cc++;rr--;}
rr=r;cc=c;
cc--;rr++;
while((cc&=0||rr&20) &&Q[rr][cc]==p){count++;cc--;rr++;}
rr=r;cc=c;
if(count+1&=5)
void Keypress(char n) //光标位置移动
case up:if(weizhi.y&=0)weizhi.y=19;else weizhi.y--;gotoxy(weizhi.x,weizhi.y);
//向上移动光标
case left:if(weizhi.x&=0)weizhi.x=19;else weizhi.x--;gotoxy(weizhi.x,weizhi.y);
//向左移动光标
case right:if(weizhi.x&=19)weizhi.x=0;else weizhi.x++;gotoxy(weizhi.x,weizhi.y);
//向右移动光标
case down:if(weizhi.y&=19)weizhi.y=0;else weizhi.y++;gotoxy(weizhi.x,weizhi.y);
//向下移动光标
case lz:luozi();
//开始落子操作
case cls:drawqipan();
//重新开始
int winer=0;
system("color 2f");
l: drawqipan();
press=getch();
Keypress(press);
if(press=='p')
winer=checkWin();
if(winer!=0)
gotoxy(0,20);
if(winer==2)
printf("the side of ● wins\n");
else if(winer==1)
printf("the side of ○ wins\n");
// if(player==0)
//cluozi(1,2);
//此处落子由电脑控制
press=getch();
printf("do you want to play again?(y/n):");
press=getch();
if(press=='n')
else if(press=='y')
开源中国-程序员在线工具:
刚好这里也有一篇C语言五子棋的,你俩可以切磋下
2楼:Serq 发表于
判断游戏是否有输赢的算法循环次数太多了
3楼:逆尘 发表于
引用来自“Serq”的评论 判断游戏是否有输赢的算法循环次数太多了 是的,后来想想只需要对当前落子位置进行检查就够了,不需要全盘检查,是我考虑不周到。。。
开源从代码分享开始
逆尘的其它代码当前位置: →
作者:不详&&来源:本站整理&&发布时间: 07:38:52
一个用C++编写的五子棋游戏(3)
////////////////////////////////////////////////////////& Luffar.H& by Yuheng Zhao //////////////////////////////////////////////////////
#ifndef _LUFFAR_H_#define _LUFFAR_H_#include "shell.h"
//& Visa upp informationenclass CMessagePad{private:&int x0,y0,x1,y1;&int m_nS
&//& ruta dr texten ska visas&int mx0,my0,mx1,my1;&int m_nLineSpace, m_nLines, m_nCurrentLpublic:&CMessagePad();&void ScrollMessages();&void Draw();&void Message(char* msg);};
class CPlayer{private:&BOOL m_bC //& Om det r dator som kr.&int m_nP&//& Vilken spelare det rpublic:&CPlayer(int p)&{m_bComputer = FALSE; m_nPlayer=p;}&void ChangePlayer(BOOL com)&{m_bComputer =}&int WhichPlayer()&&{return m_nP}&BOOL IsComputer()&{return m_bC}};
class CBoard{private:&//& Vrdet 0 om platsen r tom, 1 fr spelaren 1, 2, fr spelare 2&int m_nBoard[MAX_X][MAX_Y];
&//& Var schackbrde ligger p?skrmen.&int x0,y0,x1,y1;&//& Positionen p?brdet&int m_nM // Hur stor Marginal det brdet ska ha&int m_nS // Hur lngt skuggan det ska vara&int m_nCellX;&//& Storleken p?en cell p?brdet&int m_nCellY;&CPlayer* m_pPlayer1;&CPlayer* m_pPlayer2;&int m_nWhoB&CPlayer* m_pCurrentP&BOOL m_bIsE
&//& Skapa bilder i minnet och anvnda PutImage() sedan&void CreateImages();&void *m_pImage1, *m_pImage2;&CPoint m_lastPt;&CPoint m_nextlPt; //& Nst sista punkten
&//& Kalkylera ut hur mnga i rad det finns i ett visst hll&int Calculate(int x,int y,Direction d,int player=-1);&CPoint Analyse(int x, int y, int count,int param=0);
&char msg[30];&CPoint RandomPoint();&CPoint Think();&CPoint GetEndPoint(int x, int y, Direction d, BOOL& closed); //& Returnera punkten efter den sista punkten i en viss rad&CPoint FindDangerPt(int player);
&BOOL m_bSearchA&CPoint SearchAll(int,int, int param=0);public:&CBoard(CPlayer* p1,CPlayer* p2);&~CBoard();&void ResetBoard();
&void Draw();&int Go();&//& Lt current player gra nsta drag, returnera vinnare om det finns&int GetWinner();&void ShowWinner(int x,int y,Direction start,int player=-1);};#endif
------------------------------------------------------------------------
////////////////////////////////////////////////////////& Mouse.h& by Yuheng Zhao //////////////////////////////////////////////////////
#ifndef _MOUSE_H_#define _MOUSE_H_
void InitMouse();void ShowPoint();void SetPoint(unsigned int x,unsigned int y);void HidePoint();void HidePointXY(unsigned int cordx,unsigned int cordy,unsigned int x,unsigned int y);void ReleaseXY(int &xcordi,int &ycordi,unsigned int bbutt);void PressXY(int &xcordi,int &ycordi,unsigned int bbutt);void ReadMouse(int &x,int &y,int &b);void Limits(unsigned int minx,unsigned maxX,unsigned miny,unsigned maxY);int MouseSize();
--------------------------------------------------------------------------------
////////////////////////////////////////////////////////& Shell.H& by Yuheng Zhao //////////////////////////////////////////////////////
#ifndef _SHELL_H_#define _SHELL_H_#include "luffar.h"
void InitGraphics();void Cls();
void DrawBackground();char ReadKey();void Rectangle(int,int,int,int,int,BOOL shadow=FALSE,int deep=0);
void WaitDlg(int nMode=0, int nDelay=0);int ShowWinDlg(CPlayer* winner);
void IntToChar(int, char*);void Message(char*);#endif
-----------------------------------------------------------
////////////////////////////////////////////////////////& Types.H& by Yuheng Zhao //////////////////////////////////////////////////////
#ifndef _TYPES_H_#define _TYPES_H_
#include &iostream.h&#include &graphics.h& // c++ grafik,Inte sjlv gjorda. ( ska nd?inte gra ngot grafisk avancerat sak )#include &stdlib.h&#include &process.h&#include &string.h&#include &alloc.h&#include &.h&
#define MAX_X&19#define MAX_Y&19
#define SCR_MAX_X&639#define SCR_MAX_Y 479
#define NOCOLOR -1#define BLACK &0#define BLUE&1#define GREEN&2#define CYAN&3#define RED&&4#define MAGENTA 5#define BROWN&6#define LGRAY&7#define DGRAY&8#define LBLUE&9#define LGREEN&10#define LCYAN&11#define LRED&12#define LMAGENTA 13#define YELLOW&14#define WHE&15
enum BOOL{TRUE=1, FALSE=0};enum Direction {LEFT,UPLEFT,UP,UPRIGHT,RIGHT,DOWNRIGHT,DOWN,DOWNLEFT};
class CPoint{public:&int x,y;&CPoint()&{x=-1;y=-1;}&CPoint(int xx,int yy)&{x= y=}&CPoint(const CPoint* pt) {x=pt-&x; y=pt-&y;}
&BOOL operator== (const CPoint& pt)&const CPoint& operator= (const CPoint& pt);};
class CRect{public:&int x0,y0,x1,y1;&CRect(int xx0,int yy0,int xx1,int yy1)&{x0=xx0; y0=yy0; x1=xx1; y1=yy1;}&CRect(const CRect* r)&&{x0=r-&x0; y0=r-&y0; x1=r-&x1; y1=r-&y1;}&const CRect& operator= (const CRect& pt);&
&BOOL PtInRect(const CPoint& pt);&void Draw(int,int lineColor=NOCOLOR,BOOL shadow=FALSE,int deep=0);};
class CButton{public:&CRect m_R&char* m_pchN&BOOL m_bP
&CButton(const CRect& rect,char* ch);&~CButton()&{delete m_pchN}&void Draw(); om ngon har klickat p?knappen&BOOL Clicked(int,int,int);&void SetWindowText(char*);};
----------------------------------------------
下& 载:已编译好的五子棋程序
注意:程序运行时还需要加载MOUSE驱动程序。
?上一篇教程:
?下一篇教程:
& &评论摘要(共 1 条,得分 85 分,平均 85 分)
[] 1 楼 yuvjtzn   打分:85 分  发表时间:
? Y9dB3k&&&a&href=&/&&ncuamqxffxqa&/a&,&nb
按字母检索
按声母检索
版权所有:& 备案编号:豫ICP备号 “法律声明”
Copyright (C)2004 - 2009
Port. All rights reserved.求用C编写的五子棋源代码,能运行成功_编程吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:143,249贴子:
求用C编写的五子棋源代码,能运行成功收藏
快试试吧,可以对自己使用挽尊卡咯~◆◆
求用C编写的五子棋源代码,能运行成功!
请各位高手帮帮忙!希望是能够运行成功的!
和张大佛爷、二月红一起去探秘矿洞墓穴!
我以前编写过一个,好象说不能打开bios.h
graphics.h
所以我就不拿出来献丑了!估计我那源程序换个版本的TC就能运行成功吧!
快试试吧,可以对自己使用挽尊卡咯~◆◆
收购废品油,高价。
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
VC++五子棋游戏程序设计(源代码及全套资料)
下载积分:1000
内容提示:VC++五子棋游戏程序设计(源代码及全套资料)
文档格式:DOC|
浏览次数:665|
上传日期: 16:47:38|
文档星级:
该用户还上传了这些文档
VC++五子棋游戏程序设计(源代码及全套资料)
官方公共微信

我要回帖

更多关于 c 编写五子棋 的文章

 

随机推荐