收藏 分销(赏)

c++五子棋程序设计.pdf

上传人:曲**** 文档编号:826836 上传时间:2024-03-26 格式:PDF 页数:32 大小:265.82KB
下载 相关 举报
c++五子棋程序设计.pdf_第1页
第1页 / 共32页
c++五子棋程序设计.pdf_第2页
第2页 / 共32页
c++五子棋程序设计.pdf_第3页
第3页 / 共32页
c++五子棋程序设计.pdf_第4页
第4页 / 共32页
c++五子棋程序设计.pdf_第5页
第5页 / 共32页
点击查看更多>>
资源描述

1、C+五子棋C+五子棋/CManager.cpp 文件#include CManager.h#include iostreamusing namespace std;void CManager:GameRun()(while(1)/IsContinue 为否时退出CPIayer playerl,player2;CJudgejudge;CChessBoard board;CPiece piece;CShow show;COrganizer organizer;show.WelcomeQ;show.DisplayBoard();organizer.DecideGameMode(show);if(1=

2、organizer.ReturnMode()organizer.DecideWhoGoFirst(player1,player2);show.Welcome();/重新显示一遍棋盘show.DisplayBoard();while(1)(if(1=organizer.ReturnFlagTurn()/轮到 1 号玩家(do show.ShowlnputPosition(playerl);CPoint point=organizer.GetlnputPosition();playerl.GetPiece(piece,point);while(!judge.lsValid(board,piece)

3、;board.PlacePiece(piece);show.Welcome();show.DisplayBoard(board,piece);)else/轮至U 2号玩家(do show.ShowlnputPosition(player2);CPoint point=organizer.GetlnputPosition();player2.GetPiece(piece,point);while(!judge.lsValid(board5piece);board.PlacePiece(piece);show.Welcome();show.DisplayBoard(board,piece);)if

4、(judge.lsForbidden(board,piece)(if(1=organizer.ReturnFlagTurn()(show.ShowForbidden(player1);)elseshow.ShowForbidden(player2);break;)if(judge.lsWin(board5piece)if(1=organizer.ReturnFlagTurn()show.ShowWin(playerl);)elseshow.ShowWin(player2);break;)organizer.ExchangeFlagO;/while结束,退出游戏)else(CAIgorithm

5、algorithm;/算法类organizer.DecideWhoGoFirst(player1,player2);show.Welcome();/重新显示一遍棋盘show.DisplayBoard();while(1)(if(1=organizer.ReturnFlagTurn()/轮到 1 号玩家(do show.ShowlnputPosition(playerl);CPoint point=organizer.GetlnputPosition();playerl.GetPiece(piece,point);while(!judge.lsValid(board,piece);board.P

6、lacePiece(piece);show.Welcome();show.DisplayBoard(board,piece);)else/轮至U 2号玩家CPoint point=putePiecePosition(boardJplayer2);player2.GetPiece(piece,point);board.PlacePiece(piece);show.Welcome();show.DisplayBoard(board,piece);)if(judge.lsForbidden(board,piece)(if(1=organizer.ReturnFlagTurn()(show.ShowF

7、orbidden(player1);)elseshow.ShowForbidden(player2);break;)if(judge.lsWin(board,piece)(if(1=organizer.ReturnFlagTurn()(show.ShowWin(player1);elseshow.ShowWin(player2);break;)organizC+五子棋er.ExchangeFlag();/while结束,退出游戏 int choice=0;do show.ShowlsContinue();choice=organizer.GetlsContinueResult();while(

8、choice!=Y choice!=N);if(N=choice)break;)/COrganizer.cpp 文件include CManager.h#includeiostream#include string#include cctypeusing namespace std;void COrganizer:DecideGameMode(CShow show)(do show.ShowChoseGameMode();cinmode;while(mode!=1 mode!=2);)int COrganizer:ReturnMode()return mode;)string COrganiz

9、er:ReturnPlayer1 Name()(return playerlname;)string COrganizer:ReturnPlayer2Name()(return player2name;)void COrganizer:DecideWhoGoFirst(CPIayer p1,CPIayer p2)(if(1=mode)(p1.SetName(Player1);p2.SetName(Player2);)elsep1.SetName(Human);p2.SetName(Computer);)do coutendl;coutPlayer Listcoutp1.ReturnName()

10、,p2.ReturnName();coutendl;coutpl.ReturnNameO go first(1);coutendl;coutp2.ReturnNameO go first(2);coutendl;coutPlease decide who go first(1 or 2):cinflagturn;while(flagturn!=1 flagturn!=2);if(1=flagturn)(pl.SetColor(black);p2.SetColor(white);)elsepl.SetColor(white);p2.SetColor(black);)int COrganizer:

11、ReturnFlagTurn()(return flagturn;)CPoint COrganizer:GetlnputPosition()(string str;cinstr;/从屏幕中读取字符串int x=05y=0;if(str.at(0)=48 str.at(0)=57)/输入棋子坐标时数字在前面,字母在后面的情况(if(2=str.length()(x=int(toupper(str.at(1)-64;y=int(str.at(0)-48;return CPoint(x,y);)else(x=int(toupper(str.at(2)-64;y=int(str.at(0)-48;y=

12、y*10+int(str.at(1)-48;return CPoint(x,y);)else 输入棋子坐标时字母在前面,数字在后面的情况(if(2=str.length()(x=int(toupper(str.at(0)-64;y=int(str.at(1)-48;return CPoint(x,y);)elsex=int(toupper(str.at(0)-64;y=int(str.at(1)-48;y=y*10+int(str.at(2)-48;return CPoint(x,y);)void COrganizer:ExchangeFlag()if(1=flagturn)(flagturn

13、=2;)else(flagturn=1;char COrganizer:GetlsContinueResult()(char choice;cinchoice;choice=toupper(choice);return choice;)/CPiece.cpp 文件include CPiece.hvoid CPiece:SetColor(int col)(color=col;if(1=color)(shape=)else if(2=color)(shape=O;);)void CPiece:SetPosition(const CPoint p)(point=p;)int CPiece:Retur

14、nColor()constreturn color;)CPoint CPiece:ReturnPoint()const(return point;string CPiece:C+五子棋:ReturnShape()(return shape;/CPIayer.cpp 文件include CPIayer.hinclude stringusing namespace std;void CPIayer:SetColor(int col)color=col;)int CPIayer:ReturnColor()const(return color;)void CPIayer:GetPiece(CPiece

15、 piece,const CPoint pos)(piece.SetColor(color);piece.SetPosition(pos);)void CPIayer:SetName(string s)(name=s;)string CPIayer:ReturnName()const(return name;)/CPoint.cpp 文件#include CPoint.hCPoint:CPoint()CPoint:CPoint(int a,int b)(x=a;y=b;)void CPoint:SetPoint(int a,int b)x=a;y=b;void CPoint:operator=

16、(const CPoint p)(x=p.x;y=py;)bool CPoint:operator=(const CPoint p)if(x=p.x y=p.y)return true;elsereturn false;)/CShow.cpp 文件include CShow.h#include iostreamusing namespace std;void CShow:Welcome()(system(cls);/清屏coutendl;cout=Welcome to Five In Row Game=;coutendl;coutendl;)void CShow:ShowChoseGameMo

17、de()(/coutGame Modeendl;coutPeople VS People(1)endl;coutPeople VS Computed 2)endl;coutPlease Choose Game Mode(1 or 2):)void CShow:DisplayBoard()(int row=05col=0;if(SIZE10)(cout)coutSIZE;cout r;for(col=2;col=SIZE-1;col+)(cout-r;)cout|coutendl;for(row=SIZE-1;rowrow-)(if(row=10)/显示行数coutrow;)elsecout r

18、ow;coutM F;for(col=2;col=SIZE-1;col+)(cout)cout dcoutendl;cout 1;cout L;for(col=2;col=SIZE-1;col+)(cout L;coutcoutendl;以下显示ABCD.coutchar alpha=65;for(col=1;col=SIZE;col+)(coutalpha;coutalpha=alpha+1;)coutendl;coutendl;)void CShow:DisplayBoard(const CChessBoard board5const CPiece pie)(int row=05col=0

19、;CPiece piece;piece.SetColor(black);string blackshape=piece.ReturnShape();piece.SetColor(white);string whiteshape=piece.ReturnShape();string currentshape;if(black=pie.ReturnColor()currentshape=)else(currentshape=A;)CPoint pos=pie.ReturnPoint();/得到当前落子点if(SIZE10)(cout)coutSIZE;if(pos=CPoint(1,15)(cou

20、tcurrentshape;)elseswitch(board.ReturnPieceAt(CPoint(1,SIZE)case blank:cout r;break;case black:coutblackshape;break;case white:coutwhiteshape;)for(col=2;col=SIZE-1;col+)(if(pos=CPoint(col5SIZE)(coutcurrC+五子棋entshape;)elseswitch(board.ReturnPieceAt(CPoint(col5SIZE)case blank:cout r;break;case black:c

21、outblackshape;break;case white:coutwhiteshape;)if(pos=CPoint(SIZE,SIZE)(coutcurrentshape;)elseswitch(board.ReturnPieceAt(CPoint(SIZE5SIZE)case blank:cout-|break;case black:coutblackshape;break;case white:coutwhiteshape;)coutendl;for(row=SIZE-1 jrowrow-)(if(row=10)/显示行数(coutrow;)elsecout Yow;if(pos=C

22、Point(15row)coutcurrentshape;)else(switch(board.ReturnPieceAt(CPoint(1,row)(case blank:coutbreak;case black:coutblackshape;break;case white:coutwhiteshape;)for(col=2;col=SIZE-1;col+)(if(pos=CPoint(col5row)coutcurrentshape;)else(switch(board.ReturnPieceAt(CPoint(col5row)(case blank:cout+;break;case b

23、lack:coutblackshape;break;case white:coutwhiteshape;)if(pos=CPoint(SIZE,row)(coutcurrentshape;)elseswitch(board.ReturnPieceAt(CPoint(SIZE5row)case blank:cout Tbreak;case black:coutblackshape;break;case white:coutwhiteshape;)coutendl;)cout 1;if(pos=CPoint(151)(coutcurrentshape;)elseswitch(board.Retur

24、nPieceAt(CPoint(1,1)case blank:cout L;break;case black:coutblackshape;break;case white:coutwhiteshape;)for(col=2;col=SIZE-1;col+)(if(pos=CPoint(col51)(coutcurrentshape;)elseswitch(board.ReturnPieceAt(CPoint(col51)case blank:cout-L,;break;case black:coutblackshape;break;case white:coutwhiteshape;)if(

25、pos=CPoint(SIZE51)(coutcurrentshape;)elseswitch(board.ReturnPieceAt(CPoint(SIZE51)case blank:cout;break;case black:coutblackshape;break;case white:coutwhiteshape;)coutendl;以下显示ABC D.coutchar alpha=65;for(col=1;col=SIZE;col+)(coutalpha;coutalpha=alpha+1;)coutendl;coutendl;)void CShow:ShowlnputPositio

26、n(const CPIayer player)(CPiece pie;pie.SetColor(player.ReturnColor();coutplayer.ReturnName()(pie.ReturnShape()please input piece position A_A:)void CShow:ShowWin(const CPIayer player)(coutCongratuatC+五子棋ions!player.ReturnName()win!;coutendl;)void CShow:ShowForbidden(const CPIayer player)(cout,Sorry!,player.ReturnName()H forbidden!;coutendl;)void CShow:ShowlsContinue()coutOne more game?(Y/N)

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服