您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > 基于C语言的REVERSI的源代码
基于C语言的REVERSI游戏的源代码://Program9.9REVERSIAnOthellotypegame#define__STDC_WANT_LIB_EXT1__1#includestdio.h#includestdbool.h#includectype.h#includestring.h#defineSIZE6//YoucanchangeitifyoulikeconstcharcompC='@';constcharplayerC='O';//FunctionprototypesvoidDisplay(charboard[][SIZE]);voidResetBoard(charboard[][SIZE]);intValidMoves(charboard[][SIZE],boolmoves[][SIZE],charplayer);intPlayerCounters(charboard[][SIZE],charplayer);voidMakeMove(charboard[][SIZE],size_trow,size_tcol,charplayer);voidComputerMove(charboard[][SIZE],boolmoves[][SIZE],charplayer);intGetScore(charboard[][SIZE],charplayer);intBestMove(charboard[][SIZE],boolmoves[][SIZE],charplayer);intmain(void){charboard[SIZE][SIZE]={0};boolmoves[SIZE][SIZE]={false};intnoOfMoves=0;intinvalidMoves=0;charagain=0;chary=0;size_tx=0;//Playerindicator:trueforplayerandfalseforcomputerboolnextPlayer=true;//Promptforhowtoplayprintf(\nREVERSI\n\n);printf(Youcangofirstonthefirstgame,thenwewilltaketurns.\n);printf(Youwillbewhite-(%c)\nIwillbeblack-(%c).\n,playerC,compC);printf(Selectasquareforyourmovebytypingadigitfortherow\nandaletterforthecolumnwithnospacesbetween.\n);printf(\nGoodluck!PressEntertostart.\n);//Themaingameloopdo{ResetBoard(board);//Onevengamestheplayerstarts,onoddgamesthecomputerstartsnextPlayer=!nextPlayer;noOfMoves=4;//Thegameplayloopdo{Display(board);if(true==(nextPlayer=!nextPlayer)){if(ValidMoves(board,moves,playerC)){for(;;){printf(Pleaseenteryourmove(rowcolumn-nospace):);scanf(%zd%c,&x,&y);fflush(stdin);y=tolower(y)-'a';--x;if(y0||y=SIZE||x=SIZE||!moves[x][y]){printf(Notavalidmove,tryagain.\n);continue;}MakeMove(board,x,y,playerC);++noOfMoves;break;}}else{if(++invalidMoves2){printf(\nYouhavetopass,pressreturn);scanf(%c,&again);fflush(stdin);}elseprintf(\nNeitherofuscango,sothegameisover.\n);}}else{if(ValidMoves(board,moves,playerC)){invalidMoves=0;ComputerMove(board,moves,compC);++noOfMoves;}else{if(++invalidMoves2)printf(\nIhavetopass,yourgo);elseprintf(\nNeitherofuscango,sothegameisover.\n);}}}while(noOfMovesSIZE*SIZE&&invalidMoves2);//GameisoverDisplay(board);printf(Thefinalscoreis:\n);printf(Computer%d\nUser%d\n\n,PlayerCounters(board,compC),PlayerCounters(board,playerC));printf(Doyouwanttoplayagain(y/n):);scanf(%c,&again);fflush(stdin);}while(tolower(again)=='y');printf(\nGoodbye\n);return0;}//ResettheboardtostartingstatevoidResetBoard(charboard[][SIZE]){//Blankalltheboardsquaresfor(size_trow=0;rowSIZE;row++)for(size_tcol=0;colSIZE;col++)board[row][col]='';//Placetheinitatefourcountersinthecentersize_tmid=SIZE/2;board[mid-1][mid-1]=board[mid][mid]=playerC;board[mid-1][mid]=board[mid][mid-1]=compC;}//FunctiontodisplaytheboardinitscurrentstatevoidDisplay(charboard[][SIZE]){//DisplaythecolumnlabelscharcolLabel='a';printf(\n);for(size_tcol=0;colSIZE;col++)printf(%c,(char)(colLabel+col));printf(\n);//Displaytherow...for(size_trow=0;rowSIZE;row++){//Displaythetoplineforthecurrentrowprintf(+);for(size_tcol=0;colSIZE;col++)printf(---+);printf(\n%2zd|,row+1);//Displaythecountersincurrentrowfor(size_tcol=0;colSIZE;col++)printf(%c|,board[row][col]);printf(\n);}//Finallydisplaythebuttomlineoftheboardprintf(+);for(size_tcol=0;colSIZE;col++)printf(---+);printf(\n);}intValidMoves(charboard[][SIZE],boolmoves[][SIZE],charplayer){introwdelta=0;intcoldelta=0;intx=0;inty=0;intnoOfMoves=0;//Settheopponentcharopponent=(player==playerC)?compC:playerC;//Initializemovesarraytofalsefor(size_trow=0;rowSIZE;row++)for(size_tcol=0;colSIZE;col++)moves[row][col]=false;//Findsquaresforvalidmoves//Avalidmovemustbeonablanksquareandmustenclose//atleastoneopponentsquarebetweentwoplayersquaresfor(size_trow=0;rowSIZE;row++){for(size_tcol=0;colSIZE;col++){if(board[row][col]!='')continue;//Checkallthesquaresaroundtheblanksquareforopponentscounterfor(rowdelta=-1;rowdelta=1;rowdelta++){for(coldelta=-1;coldelta=1;coldelta++){//Don'tcheckoutsidethearray,orthecurrentsquareif((row==0&&rowdelta==-1)||row+rowdelta=SIZE||(col==0&&coldelta==-1)||col+coldelta=SIZE||(rowdelta==0&&coldelta==0))continue;//Nowcheckthesquareif(board[row+rowdelta][col+coldelta]==opponent){//Ifwefindtheopponent,moveinthedeltadirection//overopponentcounterssearchingforaplayercounterx=row+rowdelta;y=col+coldelta;//Lookforaplayersquareinthedeltadirectionfor(;;){x+=rowdelta;y+=coldelta;//Ifwemoveoutsidethearrayorit'sablanksquare,giveupif(x0||x=SIZE||y0||y=SIZE||board[x][y]=='')break;//Ifsquarehasaplayercounterthenwehaveavalidmoveif(board[x][y]==player){moves[row][col]=true;noOfMoves++;break;}}}}}}}returnnoOfMoves;}intPlayerCounters(charboard[][SIZE],charplayer){intcount=0;for(size_trow=0;rowSIZE;row++)for(size_tcol=0;colSIZE;col++)if(board[row][col]==player)++count;returncount;}voidMakeMove(charboard[][SIZE],size_trow,size_tcol,charplayer){introwdelta=0;intcoldelta=0;size_tx=0;size_ty=0;//Identifyopponentcharopponent=(player==playerC)?compC:playerC;board[row][col]=player;//Checkallsquaresaroundthissquareforopponentcounterfor(rowdelta=-1;rowdelta=1;rowdelta++){for(coldelta=-1;coldelta=1;
本文标题:基于C语言的REVERSI的源代码
链接地址:https://www.777doc.com/doc-5473261 .html