日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

北京工业大学 大一C语言课程设计--四子棋(Bingo)(连子游戏)in TurboC 3.0

發(fā)布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 北京工业大学 大一C语言课程设计--四子棋(Bingo)(连子游戏)in TurboC 3.0 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

?? ??

連子游戲設(shè)計報告書

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?


200912

?

目錄頁

?

?

目錄

1 需求分析... 3

1.1功能與數(shù)據(jù)需求... 3

1.1.1題目要求的功能... 3

1.1.2 擴展功能... 3

1.2界面需求... 4

1.3 開發(fā)環(huán)境與運行需求... 6

2 概要設(shè)計... 6

2.1主要數(shù)據(jù)結(jié)構(gòu)... 6

2.2程序總體結(jié)構(gòu)... 6

2.3 各模塊函數(shù)說明... 7

3 詳細(xì)設(shè)計... 9

3.1主函數(shù)部分... 9

3.2 游戲繪圖部分... 9

3.3 游戲部分... 10

3.4 判斷輸贏部分... 11

3.5 游戲存檔讀取 排名部分... 11

3.6 游戲AI部分... 12

4 測試... 13

5 用戶手冊... 13

5.1應(yīng)用程序功能的詳細(xì)說明... 13

5.2應(yīng)用程序運行環(huán)境要求... 13

5.5輸入數(shù)據(jù)類型、格式和內(nèi)容限制... 13

6總結(jié)提高... 13

6.1課程設(shè)計總結(jié)... 13

6.2開發(fā)中遇到的問題和解決方法... 14

6.3 對自己完成課設(shè)完成情況的評價... 15

6.4對于《高級語言程序設(shè)計Ⅲ》與《高級語言程序設(shè)計課程設(shè)計》兩門課程的意見與建議???? 15

附錄:程序源代碼... 16

?

?

?

需求分析

?? 功能與數(shù)據(jù)需求

???? 11題目要求的功能

?

1.?? 游戲畫面分為ABC三部分,分別代表菜單,棋盤和記分牌。

2.?? 進入游戲后玩家建立/讀取存檔,選擇難度,開始游戲。

3.?? B區(qū)畫棋盤,玩家與計算機交替部子(棋子顏色不同)。

4.?? 玩家通過上下左右移動鍵盤光標(biāo)回車鍵下棋(鼠標(biāo)亦可)。

5.?? 棋盤豎直放立,因此棋子會在該列由上往下落。

6.?? 游戲的任意一方橫向,縱向或者斜向棋子連成4子即為勝利,于是本局游戲結(jié)束并積1分。若棋盤布滿未分勝負(fù),本局結(jié)束,雙方各記一分。

7.?? 在存檔中存儲玩家勝負(fù)信息及游戲棋盤。

8.?? 游戲中途按Esc退出游戲。

9.?? 游戲設(shè)有排行榜,玩家可以看到自己的勝負(fù),排名,勝率。

10.? 玩家保存后將棋局錄入存檔。

11.? A區(qū)是菜單欄,設(shè):Level,New,Load/SaveRankHelp

12.? B區(qū)是玩家欄,顯示當(dāng)前玩家與電腦的比分。玩家每次輸贏均錄入存檔。

?

擴展功能

1.?? 設(shè)計游戲封面。

2.?? 調(diào)整坐標(biāo)設(shè)計游戲友好UI

3.?? 插入游戲聲音。

4.?? 設(shè)計下棋動畫,使畫面更為流暢酷炫。

5.?? 在退出時候再次請求用戶確認(rèn)以防誤操作。

6.?? 完善AI

?

?

?? 界面需求

?

?? 開發(fā)環(huán)境與運行需求

????? 硬件:Duo CORE2 T6500 ,4G Ram ,320G HDD ,nVidia 105M Video Clip.

??? ??? 軟件:Windows Vista 的安全模式 在EVGA(640*480)的顯示模式下運行???????????????????????????????????? Turbo C++ IDE 3.0.

概要設(shè)計

?? 主要數(shù)據(jù)結(jié)構(gòu)

1.結(jié)構(gòu)體:

?

Typedef struct{

???????? Player P1;

???????? Player P2;

???????? board[2][8][9];

}Info;

typedef struct {

???????? char name[8];

???????? int score;

}Player;

?

???????? 其中 Info用來儲存每個玩家的游戲數(shù)據(jù),其中,巧妙地使用一個三維數(shù)組來實現(xiàn)兩個棋盤的讀取。

?

2. 整形數(shù)據(jù)

int imax,jmax,level,i;

???????? 其中,imax jmax用來定義棋盤邊界,level定義本局游戲級數(shù)(有12)來選擇棋盤。i是下子的位置(即棋盤的橫坐標(biāo))。

?

?? 程序總體結(jié)構(gòu)

?

?

??? judge()判斷

??? judge()判斷

?

?

?

?

?

?

各模塊函數(shù)說明

?

/*===========================函數(shù)聲明=========================*/

//---------------------------繪畫函數(shù)--------------------------

void startGraphics();?? 繪制顯卡

void coverage();??????? 繪制封面

void drawTittle();????? 繪制封面文字

void drawCoverPicture1();繪制封面圖片1

void drawCoverPicture2();繪制封面圖片2

int? all_key();???????? 鍵盤接受函數(shù),返回鍵盤編碼

void drawGameUI();????? 繪制游戲界面

void drawStepXY(int i,int j,int level,int player);

繪制棋子,接收棋子所在棋盤坐標(biāo) I,J 棋盤登記 Level 棋子顏色Player等變量

void drawChessBoard(int level);???? 繪制棋盤,接收等級level 變量

void drawPlayerInfo(char name[]);?? 繪制玩家信息 接受玩家姓名字符串 name[]

void drawMenu();??????????? ??????? 繪制控制菜單

void drawScoreP1(int score);??????? 繪制玩家1分?jǐn)?shù)

void drawScoreP2(int score);??????? 繪制玩家2分?jǐn)?shù)

void repaintBoard();??????????????? 刷新棋盤

void repaint();???????????????????? 刷新游戲界面

void clearStepXY(int i,int j,int level);清除棋子,接收坐標(biāo)(I,j)登記level

void moveChess();?????????????????? 移動棋子函數(shù)

void PlayerTurn(int player);??????? 玩家12輪控制函數(shù),接收玩家player變量

void dropChess(int i,int player);?? 下子函數(shù),接收下子列I,玩家Player變量

void drawArrow(int i);????????? ??? 繪制選擇箭頭,接收箭頭所指列i

void clearArrow(int i);???????????? 清除選擇箭頭,接收箭頭所指列i

?

//---------------------------游戲函數(shù)------------------------------

?

void newGame();???????????????????? 新的游戲函數(shù)

void playingGame(int player);?????? 玩家局,接收玩家player變量

void SureToQuit();????????????????? 再次確認(rèn)退出游戲界面

void endDraw(char *buf);??????????? 一盤結(jié)束代碼 輸出字符串buf (輸贏或平)

void save();??????????????????????? 儲存游戲函數(shù)

void load();??????????????????????? 讀取游戲函數(shù)

void rank();??????????????????????? 排名界面

void cpyInfo(Info *p1,Info *p2);??? 復(fù)制游戲信息結(jié)構(gòu)體函數(shù)p1 to p2

void help();??????????????????????? 幫助界面

int judge();??????????????????????? 判斷輸贏 返回0繼續(xù) 1玩家贏 2 3

void AITurn(int player);??????????? 電腦下棋函數(shù) 接受下子顏色player

int ThinkNextStep(int player);????? 電腦計算下棋函數(shù)

?

//---------------------------設(shè)置函數(shù)----------------------------

void setimaxjmax();???????????????? 設(shè)置棋盤邊界 改變?nèi)肿兞?span lang="EN-US">imax jmax?

void setlevel();??????????????????? 設(shè)計等級 (直接改變?nèi)肿兞?span lang="EN-US">level)

void setPlayerName(Info *p);??????? 設(shè)置玩家姓名,形參指針指向貯存游戲信息的結(jié)構(gòu)體

?

???????????????

?

詳細(xì)設(shè)計

? 主函數(shù)部分

????? 本程序主函數(shù)短小精悍,意思簡潔明了!

void main(){

??? ??? ??? coverage();//繪制封面圖片

??????? ??? setlevel();//設(shè)置游戲等級

??????? ??? newGame(); //開始新的游戲

}

游戲繪圖部分

游戲程序中,設(shè)計了多個繪圖移動圖塊的函數(shù),比如繪制棋子的drawStepXY() ,clearStepXY(),drawArrow(),clearArrow()

所有繪制函數(shù)參使用整形變量 i,j,player。分別代表所在棋盤的橫縱坐標(biāo),哪個玩家。這樣在多次使用繪圖函數(shù)中,直觀方便,簡潔明了,增強了函數(shù)的易讀性和維護性。

為了在游戲中跳出后返回,設(shè)置了一個repaint()函數(shù),此函數(shù)可以重新繪制棋盤,十分方便! 代碼:

void repaint(){

???? clearviewport();

???? drawGameUI();

???? drawChessBoard(level);

???? drawPlayerInfo(InfoNow.P1.name);

???? drawScoreP1(InfoNow.P1.score);

???? drawScoreP2(InfoNow.P2.score);

???? if(level==1){

???????? ?drawStepXY(8,1,level,1);

???????? ?drawStepXY(8,4,level,2);

???? }

???? if(level==2){

???????? ?drawStepXY(11,1,level,1);

???? ???? ?drawStepXY(11,5,level,2);

???? }

???? drawArrow(i);

???? repaintBoard();

}

?? 游戲部分

??? newGame()函數(shù)為新開始游戲,次函數(shù)可以供游戲中菜單NewGame呼出后遞??? 歸套用,實現(xiàn)新開始游戲功能。

?

??????? nextGame()可以由下一局功能調(diào)用模塊,不會重置分?jǐn)?shù)。

?

??????? playingGame()函數(shù)為游戲進行函數(shù),是一個循環(huán)函數(shù)。

當(dāng)PlayerTurn()時,接受鍵盤輸入,此時玩家可以按F1F6開啟菜單功能,按Esc退出游戲,左右回車控制下子。其中使用repaint()函數(shù)來重繪界面,返回游戲。代碼:

???? void PlayerTurn(int player){

???? //set the Movement Limit

???? //move

???? while(1){

???????? keycode=all_key();

???????? if (keycode==RIGHT_ARROW && i>0 && i<imax ) {

????????????? clearArrow(i);

????????????? i++;

????????????? drawArrow(i);

???????? }

???????? if (keycode==LEFT_ARROW && i>1 && i<=imax ){

????????????? clearArrow(i);

????????????? i--;

????????????? drawArrow(i);

???????? }

???????? if(keycode==ENTER){

????????????? if(InfoNow.board[level-1][0][i-1]==0){

?????????????????? dropChess(i,player);

?????????????????? break;

????????????? }

???????? }

???????? if(keycode==ESC){

????????????? SureToQuit();

???????? }

???????? if(keycode==F1){newGame();

???????? }

???????? if(keycode==F2){nextGame();

???????? }

???????? if(keycode==F3){load()repain();;

???????? }

???????? if(keycode==F4){save();repaint();

???????? }

???????? if(keycode==F5){rank();repaint();

???????? }

???????? if(keycode==F6){setlevel();repaint();

???????? }

???????? if(keycode==F7){ help();repaint();

???????? }?????????????????????????????????????????????

???? ??? }

? }

?

?? 判斷輸贏部分?

??? 判斷輸贏或者平局函數(shù)judge,使用多重循環(huán)對棋盤進行掃描,遍歷所有可能情況。

??? 代碼較長,取一小段示意:

?????? int judge(){ //0:continue; 1:P1 win; 2:P2 win; 3:draw.

?????? /*-------set who?-------*/

?????? int who,code=1;

?????? int im,jm,k;

?????? /*------------"-"check------------*///橫向檢查

?????? for (jm=1;jm<=jmax;jm++)

????????????? for (im=1;im<=imax-3;im++)

???????????????????? if((who=InfoNow.board[level-1][jm-1][im-1])!=0){

??????????????????????????? for(k=1;k<=3;k++)

?????????????????????????????????? if(InfoNow.board[level-1][jm-1][im-1+k]==who) code++;

??????????????????????????? if (code==4) return(who);

??????????????????????????? else code=1;

???????????????????? }

?????? ……

?????? ……

?????? ……

?

?? 游戲存檔讀取 排名部分

結(jié)構(gòu)體Info用來存儲玩家名字,排名,局勢,棋盤, 玩家密碼。

讀寫過程中需要用戶名和密碼的輸入。

游戲存讀取通過鏈表組織,通過dos的文件讀寫完成存檔的讀寫。讀檔流程圖:

?

存檔流程圖:

?

排名是在文件讀取出鏈表,然后根據(jù)鏈表內(nèi)容進行冒泡排序,輸出實現(xiàn)的。關(guān)鍵代碼:

??? _node=_temp=head;

for(;_temp->next;_temp=_temp->next)

??????? for(_node=p;_node->next;_node=_node->next){

??????????? if(_node->P1.score < _node->next->P1.score){

??????????? cpyInfo(&InfoTemp,_node);

??????????? cpyInfo(_node,_node->next);

??????????? cpyInfo(_node->next,&InfoTemp);

??????????? }

??????? }

?

?? 游戲AI部分

游戲AI是本課設(shè)的難點,也是最后花費最多時間完成的內(nèi)容。思路如下

?

????????

測試

用戶手冊

應(yīng)用程序功能的詳細(xì)說明

?

本游戲為北京工業(yè)大學(xué)09級計算機學(xué)院劉某編寫的高級語言程序課程設(shè)計作業(yè)。

??? ??? 游戲簡單易上手,游戲過程中,按F6可以查詢幫助。

規(guī)則:連子游戲,游戲兩方交替部子,棋盤垂直,棋子受重力吸引。任意一方棋子橫縱斜連成4子即可獲勝。

操作:按左右方向鍵移動光標(biāo),按回車(Enter)進行部子。F1重新開始;F2讀取上局游戲;F3 存取當(dāng)前游戲; F4查看排行榜; F5設(shè)置等級; F6查詢幫助; 游戲途中按Esc鍵隨時退出!(退出前會詢問是否確認(rèn)退出以防誤操作。是Enter 取消Esc。)

?

應(yīng)用程序運行環(huán)境要求

?????????????????? 本游戲代碼在TurboC 3.0 Dos 6.22 編譯成功運行。

???????? 所得二進制程序需在Dos或兼容Dos的操作系統(tǒng)內(nèi)運行,如WinXP及以下。建議以全屏模式運行。

?

輸入數(shù)據(jù)類型、格式和內(nèi)容限制

?????????????????? 要求輸入玩家名時,需輸入ASCII碼的可顯示字符。限八字符內(nèi)。

?

總結(jié)提高

? 課程設(shè)計總結(jié)

這次課設(shè)作業(yè),是我所做過的印象最深,收獲最大的作業(yè)之一!二到十二周學(xué)習(xí)的理論知識,一直停留在僅僅能完成簡單的課后作業(yè)的水平而已。自從學(xué)習(xí)高級語言程序設(shè)計以來,就特別有獨立完成一個使用程序的強烈愿望。而這次課程設(shè)計作業(yè)給了我一個很好實踐理論知識的機會。也許是應(yīng)為需要完成一個富有趣味性但是開發(fā)起來有一定難度游戲的緣故吧,在開發(fā)過程中,體會到了很多課上學(xué)不到的東西。

首先,面對一個待構(gòu)建的龐大程序,應(yīng)該先組織好一個清晰的思路,用科學(xué)統(tǒng)籌的方法去做好規(guī)劃。而我在開始的時候沒有這樣的覺悟,毫無進展地度過了一個星期。

其次,我學(xué)會了用以致學(xué)。計算機科學(xué)是一門博大精深的學(xué)科,里面有相當(dāng)多的知識,工具。但是,為了更好的提高我們的工作效率,我們不可能全面了解某些領(lǐng)域的知識。這個時候,就需要我們選擇性的學(xué)習(xí),以提高我們的效率。

第三,學(xué)會利用圖書館。沒進入大學(xué)之前,我的知識主要從互聯(lián)網(wǎng)得來。以前認(rèn)為,凡事都可以在互聯(lián)網(wǎng)上搜索得到,現(xiàn)在已經(jīng)不需要圖書館了。然而,開始做課設(shè)時發(fā)現(xiàn),應(yīng)為TurboC是很久以前的東西,互聯(lián)網(wǎng)上關(guān)于它的資料不多,于是我開始檢索圖書館。在圖書館里,我發(fā)現(xiàn)了許多有用的書籍,比如當(dāng)我對指針模棱兩可的時候,閱讀幾本書,我就基本明白了指針的原理和應(yīng)用。當(dāng)我對鏈表不太熟悉的時候,幾本書的實例又讓我長了知識。我恍然大悟,圖書館真的是一個知識的寶庫,我們應(yīng)該好好利用它!

第四,讓我學(xué)會直面困難。初學(xué)者設(shè)計程序,錯誤,Bug在所難免。在一次次讓人沮喪的Compiling Error里頭,我不斷地鼓勵自己,不斷探索,思考,分析,修改,調(diào)試,直至一個個模塊被我順利調(diào)試通過,那難以言表的喜悅之情,重新點燃了我的斗志!

第五,我學(xué)會了如何更加有效率地Debug。無數(shù)次報錯,讓我我學(xué)會了巧妙設(shè)置斷點,巧妙地注釋掉代碼來排除Bug,提高了我的開發(fā)效率。

最后,也是最重要的一點,在完成課設(shè)的過程中,培養(yǎng)了我專注,堅持不懈的品質(zhì)。

好幾個周末,我都沉醉的藍(lán)色的海洋,從早上直到深夜,幾乎一直專注地在思索,考慮,調(diào)試……

?? 開發(fā)中遇到的問題和解決方法

???????? ???????? 開發(fā)中遇到的問題可以說是無以計數(shù)的吧,下面就印象最深的談?wù)劇?/p>

??????????????????

在開始的一個星期里頭,只最沒有頭緒的時候。首先要解決的,也是最能看出問題的就是繪制圖形界面。其中設(shè)計是比較耗費時間的,為了日后調(diào)用方便,我定義了許多繪圖函數(shù),同時做了大量的計算工作。

?

完成繪畫函數(shù)后,接下來就在調(diào)用鍵盤疑惑了,后來查看了兩三個課設(shè)的源代碼,知道了可以用一個無條件循環(huán)來運行程序, 在按下按鍵后跳出,長了見識。

?

在后期的開發(fā)中,輸贏的判斷和電腦下棋是一大難點。因為這兩種錯誤都不是顯而易見的。為了應(yīng)對這種情況,我在Debug的過程中學(xué)會了合理地設(shè)置斷點,逐步或跳步運行,監(jiān)視變量等技巧,大大的提高了開發(fā)效率。

?

最后,存讀檔是另一重點。課設(shè)讓我了解到先前我對鏈表,文件的應(yīng)用掌握的不是很充分。于是,我查找了許多書籍,閱讀了許多案例,最終把鏈表,文件讀取的應(yīng)用模塊編出來了。因為鏈表部分容易出隱藏錯誤,所以在老師的提示下,我將存讀取檔單獨編譯通過之后,在加入主函數(shù)。

隨著Debug次數(shù)的增加,我也認(rèn)識了解了各種各樣的錯誤和處理方法。而遇到

然的問題但是總是報錯的時候,我也可以漸漸冷靜下來,仔細(xì)思考,逐步分析,排除錯誤。

??????????????????

????? 對自己完成課設(shè)完成情況的評價

在剛看到課設(shè)題目的時候,完全沒有頭緒。當(dāng)時對C開發(fā)程序還是,一竅不通,更不會將諸多函數(shù)靈活貫通了。但是隨著自己編寫的函數(shù)一點一點的積累,逐漸的學(xué)會利用幫助文檔學(xué)習(xí)利用新的函數(shù),學(xué)會利用參考書籍加深對不太明白的知識點的理解。眼看期末考檢查臨近,開發(fā)接近尾聲,突然想到對游戲功能再加強鞏固,可是時間不允許了。設(shè)計的程序離自己理想的目標(biāo)還差很遠(yuǎn),我也會利用寒假時間進一步完善和改進。

至此,我對電腦的運算速度再次驚嘆,了解了編程的精萃是算法。如何利用更加高效的算法運行在速度有限的設(shè)備上,為人類解決更加多的問題,是程序員們永無止境的課題。

同時,我對人工智能算法產(chǎn)生了濃厚的興趣。同時也查閱了許多資料,涉及到剪枝,Alpha-beta算法等尚未涉獵的知識。相對于象棋算法和五子棋算法,連子游戲可能情況還是較少的,這進一步激發(fā)了我對更大規(guī)模,更高效的棋牌算法的興趣。

?

對于《高級語言程序設(shè)計Ⅲ》與《高級語言程序設(shè)計課程設(shè)計》兩門課程的意見與建議

在這兩門課程即將結(jié)束之際,我受益匪淺。我深深得知在計算機領(lǐng)域,學(xué)習(xí)的道路是永無止境的。計算機科學(xué)是一門注重實用的學(xué)科,掌握的所有理論,是為實際應(yīng)用。應(yīng)此,學(xué)習(xí)本門課程需要進行大量實踐。而高級語言程序設(shè)計和課程設(shè)計,為學(xué)生提供了一個絕佳的小試牛刀的機會。在開發(fā)的過程中,為了達(dá)成目標(biāo),而更加牢固地掌握所需知識,達(dá)到了很好的促進學(xué)習(xí)作用。同時巧妙的課設(shè)題目安排,也大大激發(fā)了學(xué)生的興趣,而老師的辛勤工作,上機輔導(dǎo)對我們的啟發(fā)良多!

?

我覺得使用TurboC進行圖形界面開發(fā)有些困難,而我曾見一些同學(xué)把主要精力放在了開發(fā)華麗界面上,而不是程序算法的優(yōu)化完善,實在有些本末倒置了。而且TurboC圖形模式的運行環(huán)境要求比較苛刻,不是在所有的Windows虛擬Dos環(huán)境下都可以完美兼容,為此我曾用了許多時間調(diào)試DOS的模擬環(huán)境。因此我建議,課設(shè)作業(yè)不妨布置一些用TurboC在文本模式下進行一些注重算法的程序開發(fā),而類Windows UI程序的開發(fā),不妨準(zhǔn)許同學(xué)們使用圖形界面下的IDE,這樣也許會事半功倍!

?

最后,在這兩門課程的學(xué)習(xí)過程中,培養(yǎng)了對計算機科學(xué)與技術(shù)這門專業(yè)強烈的興趣,在氣氛融洽的學(xué)習(xí)環(huán)境中,受益匪淺,感謝老師在這一學(xué)期里的耐心指導(dǎo)!

?

?


?

?

in order you wanna cheat,You got to form up this code together all by yourself.

BTW,I use project in TC3.0.

?


?main.c:

#include "headfile.h" //=========================Data Settings========================== typedef struct{char name[8];int score; }PlayerInfo; //Player Infomation typedef struct info{int board[2][8][9];//set two chess board. board[0] for level 1;board[1] for level 2;PlayerInfo P1;//YouPlayerInfo P2;//PCchar psw[16];int player;//whose turnstruct info * next; }Info; Info InfoNow; board[2][8][9];// Creat two board for different level. 0 for nothing; 1 for player; 2 for PC. void *buf; //for sprintf use int level=1; int i=1,j=1; //Position of Chess int imax,jmax;//Set Limit Position unsigned int keycode;//receive key reaction code in all_key(); //data for game /*=========================Declare Statis=======================*/ //--------------------------Draw COVER------------------------- void startGraphics(); void coverage(); void drawTittle(); void drawCoverPicture1(); void drawCoverPicture2(); int all_key(); //Draw GameUI void drawGameUI(); void drawStepXY(int i,int j,int level,int player); void drawChessBoard(int level); void drawPlayerInfo(char name[]); void drawBar(); void drawScoreP1(int score); void drawScoreP2(int score); void repaintBoard(); void clearStepXY(int i,int j,int level); void moveChess(); void PlayerTurn(int player);//1 is you 2is pc void dropChess(int i,int player);// player=1 is You ; player=2 is PC void drawArrow(int i);// draw select arrow void clearArrow(int i);//clr select arrow //---------------------------Gaming------------------------------ void newGame(); void nextGame(); void playingGame(int player); void SureToQuit(); void endDraw(char *buf); void setPlayerName(char p[8]); void setPassWord(char p[16]); void save(); void load(); void rank(); void cpyInfo(Info *p1,Info *p2); void help(); void setlevel(); int judge();// 0 is gaming; 1 is YOU win; 2 is PC win; 3 is Draw. void AITurn(int player); int ThinkNextStep(int player); //---------------------------Settings---------------------------- void setimaxjmax(); void beginSet(); void repaint(); //----------------------------------------------------------------- /*================================================================*/ /*==========================Main Function=========================*/ /*================================================================*/ void main(){//Draw Beginning Picture,Press Any Key to Continue.Press Esc to exit.coverage();setlevel();newGame(); } /*---------------------------------------------------------------*/ /*==========================Functions=============================*/ void newGame(){/*---------------Draw Game UI----------------*///set structInfoNow.P1.score=0;InfoNow.P2.score=0;strcpy(InfoNow.P1.name,"YOU");InfoNow.player=1;//Set Player TurnsnextGame(); } void nextGame(){//Clear Chess Boardwhile(1){//-------------Clear Board-------------for(int i=0;i<2;i++)for(int j=0;j<8;j++)for(int k=0;k<9;k++)InfoNow.board[i][j][k]=0 ;i=1;//--------Playing Part-----------playingGame(InfoNow.player);} } void playingGame(int player){repaint();while(1){if(judge()==1){InfoNow.P1.score+=2;endDraw("Win");while(!bioskey(0)) break;break;}if(judge()==2){InfoNow.P2.score+=2;endDraw("Lose");while(!bioskey(0))break;break;}if(judge()==3){InfoNow.P1.score+=1;InfoNow.P2.score+=1;endDraw("Draw");while(!bioskey(0))break;break;}if(player==1) PlayerTurn(player);//Player=2 Change to AITurn() later!if(player==2) AITurn(player);player=(player)%2+1;//take turn to play} } void PlayerTurn(int player){//set the Movement Limit//movewhile(1){keycode=all_key();if (keycode==RIGHT_ARROW && i>0 && i<imax ) {clearArrow(i);i++;drawArrow(i);}if (keycode==LEFT_ARROW && i>1 && i<=imax ){clearArrow(i);i--;drawArrow(i);}if(keycode==ENTER){if(InfoNow.board[level-1][0][i-1]==0){dropChess(i,player);break;}}if(keycode==ESC){SureToQuit();}if(keycode==F1){newGame();}if(keycode==F2){nextGame();}if(keycode==F3){load();}if(keycode==F4){save();}if(keycode==F5){rank();}if(keycode==F6){setlevel();}if(keycode==F7){ help();//returnGame(player);}} } void AITurn(int player){int t=ThinkNextStep(player);while(t<1&&t>imax){//Adjust position;if(t>imax)t--;if(t<1)t++;} dropChess(t,player);} int ThinkNextStep(int player){//return iTemp to step to win; if no way to win,return -1.int boardTemp[2][8][9];for(int a=0;a<2;a++)//save boarfor(int b=0;b<8;b++)for(int c=0;c<9;c++)boardTemp[a][b][c]=InfoNow.board[a][b][c];int p2=player%2+1;//Try to drop use judge() to judge /*-------------Find my(AI) way to Win--------------*///Calculate how many times to drop & drop Datalyfor( int iTemp=1;iTemp<=imax;iTemp++){if(InfoNow.board[level-1][0][iTemp-1]==0){j=jmax;while (InfoNow.board[level-1][j-1][iTemp-1]!=0 )j--;InfoNow.board[level-1][j-1][iTemp-1]=player;//Judgeif(judge()==player){for(int a=0;a<2;a++)//restore boardfor(int b=0;b<8;b++)for(int c=0;c<9;c++)InfoNow.board[a][b][c]=boardTemp[a][b][c];return iTemp;}else{for( a=0;a<2;a++)//restore boardfor( b=0;b<8;b++)for( c=0;c<9;c++)InfoNow.board[a][b][c]=boardTemp[a][b][c];}}} /*-------------------------------------------------*/ /*------------Prevent oppponent to Win--------------*//*------Situation 1 (...oo.....)----*/for(int jA=1;jA<=jmax;jA++){for (int iA=2;iA<=imax-1;iA++){if(InfoNow.board[level-1][0][iA-1]==0){if (jA==1 && InfoNow.board[level-1][jmax-jA][iA-1-1]==0 &&InfoNow.board[level-1][jmax-jA][iA-1+0]==p2&&InfoNow.board[level-1][jmax-jA][iA-1+1]==p2&&InfoNow.board[level-1][jmax-jA][iA-1+2]==0)return (iA+2);if (jA>1 && InfoNow.board[level-1][jmax-jA][iA-1-1]==0 &&InfoNow.board[level-1][jmax-jA][iA-1+0]==p2&&InfoNow.board[level-1][jmax-jA][iA-1+1]==p2&&InfoNow.board[level-1][jmax-jA][iA-1+2]==0 &&InfoNow.board[level-1][jmax-jA+1][iA-1-1]!=0 &&InfoNow.board[level-1][jmax-jA+1][iA-1+2]!=0 )return (iA-1);}}}/*------Situation 2 (...ooo....)---*/for( iTemp=1;iTemp<=imax;iTemp++){if(InfoNow.board[level-1][0][iTemp-1]==0){j=jmax;while (InfoNow.board[level-1][j-1][iTemp-1]!=0 )j--;InfoNow.board[level-1][j-1][iTemp-1]=p2;//Judgeif(judge()==p2){for(int a=0;a<2;a++)//restore boardfor(int b=0;b<8;b++)for(int c=0;c<9;c++)InfoNow.board[a][b][c]=boardTemp[a][b][c];return iTemp;}else{for(int a=0;a<2;a++)//restore boardfor(int b=0;b<8;b++)for(int c=0;c<9;c++)InfoNow.board[a][b][c]=boardTemp[a][b][c];}}}/*---------Situation 3- ..o.o..-----*/for(jA=1;jA<=jmax;jA++){for (int iA=2;iA<=imax-3;iA++){if(InfoNow.board[level-1][0][iA-1]==0){if (jA==1&&// InfoNow.board[level-1][jmax-jA-1][iA-1-1]==0 &&InfoNow.board[level-1][jmax-jA][iA-1+0]==1&&InfoNow.board[level-1][jmax-jA][iA-1+1]==0 &&InfoNow.board[level-1][jmax-jA][iA-1+2]==1// InfoNow.board[level-1][jA-1][iA-1+3]==0)return (iA+1);if( jA!=1&& InfoNow.board[level-1][jmax-jA][iA-1+0]!=1 &&InfoNow.board[level-1][jmax-jA][iA-1+1]==0 &&InfoNow.board[level-1][jmax-jA-1][iA-1+1]==0&&InfoNow.board[level-1][jmax-jA+1][iA-1+1]!=0&&InfoNow.board[level-1][jmax-jA][iA-1+2]!=1 )return(iA+1);}}}/*-----------------------------------------------*//*----Find the way YOU won't win in next draw----*///Randomint iRandom;while(1){iRandom=random(imax)+1;if(InfoNow.board[level-1][0][iRandom-1]==0)return iRandom;} } void save(){closegraph();printf("/nNow Saving your Game ");for(int t=0;t<10;t++){printf(".");delay(100);}printf("/n");//----text----FILE *fp;Info *head,*p1,*p2;fp=fopen("d://tc30//BINGO//Savings.txt","r");if(fp==NULL){//No saving ,createsetPlayerName(InfoNow.P1.name);setPassWord(InfoNow.psw);fclose(fp);fp=fopen("d://tc30//BINGO//Savings.txt","w");InfoNow.next=NULL;fwrite(&InfoNow,sizeof(Info),1,fp);}else{//Have saving, Read & Add Now & Write//Readfclose(fp);fp=fopen("d://tc30//BINGO//Savings.txt","r");head=(Info *)malloc(sizeof(Info));p1=head;p2=head;fread(head,sizeof(Info),1,fp);while(p1->next!=NULL){p2=(Info *)malloc(sizeof(Info));fread(p2,sizeof(Info),1,fp);p1->next=p2;p1=p2;}p2->next=NULL;//Writeif(strcmp(InfoNow.P1.name,p2->P1.name)==0){//Same Name REFRESHcpyInfo(p2,&InfoNow);p2->next=NULL;//Writefclose(fp);fp=fopen("d://tc30//BINGO//Savings.txt","w");p1=head;while(p1!=NULL){fwrite(p1,sizeof(Info),1,fp);p1=p1->next;}printf("/n Saving Updated to new!");}else{//Not the Same Name Refreshif(!strcmp(InfoNow.P1.name,"YOU")){setPlayerName(InfoNow.P1.name);setPassWord(InfoNow.psw);}//Add InfoNow to the ENDp2->next=&InfoNow;InfoNow.next=NULL;//Writep1=head;fclose(fp);fp=fopen("d://tc30//BINGO//Savings.txt","w");while(p1!=NULL){fwrite(p1,sizeof(Info),1,fp);p1=p1->next;}printf("/n Saving Succeed!");}while(!bioskey(0)) break;}fclose(fp);startGraphics();repaint(); } void load(){closegraph();printf("/nEnter your name.If you have no savings,continue current game!/nName:");char tempname[8];char temppsw[16];scanf("%s",tempname);printf("/nNow Loading");for(int t=0;t<10;t++){printf(".");delay(100);}//----text----FILE *fp;Info *head,*p1,*p2;fp=fopen("d://tc30//BINGO//Savings.txt","r");if (fp==NULL){printf("/n No Saved files to load/n");fclose(fp);}else{rewind(fp);head=(Info*)malloc(sizeof(Info));p1=head;p2=head;fread(head,sizeof(Info),1,fp);while(p1->next!=NULL){p2=(Info*)malloc(sizeof(Info));fread(p2,sizeof(Info),1,fp);p1->next=p2;p1=p2;}p2->next=NULL;//Check If there's a save Named XXp2=head;while(strcmp(tempname,p2->P1.name)){if(p2==NULL)break;p2=p2->next;}//outputif(!strcmp(tempname,p2->P1.name)){//Name Right!printf(" %s's saving exist!/n",tempname);printf("/nPassword : ");scanf("%s",temppsw);if(!strcmp(temppsw,p2->psw)){//PassWord RIGHT!printf("/nHallo! %s !",tempname);cpyInfo(&InfoNow,p2);InfoNow.next=NULL;fclose(fp);}else{//name right psw wrongprintf("/nPassword Wrong!/n");int count=0;while(1){if(count>1) {printf("/nPassword Wrong!");break;}printf("/nTry Again:");scanf("%s",temppsw);delay(1000);if(!strcmp(temppsw,p2->psw)){//PassWord RIGHT!printf("/nHallo! %s !",tempname);cpyInfo(&InfoNow,p2);InfoNow.next=NULL;break;}else count ++;//PassWord Wrong}fclose(fp);}}/*else{//Name Wrong , cpy Lastgame to InfoNowwhile(p2!=NULL){p2=p2->next;}cpyInfo(&InfoNow,p2);printf("/n%s's Saving not found. Load Last Game!",tempname);}fclose(fp);*/} //End Loadingwhile(!bioskey(0))break;startGraphics();repaint(); } void rank (){//Read From LinkList and RankFILE *fp; Info *head,*p1,*p2,*p,*_temp,*_node;Info InfoTemp;int scoreTemp[11];char nameTemp[11][8];p=(Info *)malloc(sizeof(Info));fp=fopen("d://tc30//BINGO//Savings.txt","r");if(fp==NULL){fclose(fp);}else{//Load LinkListrewind(fp);head=(Info*)malloc(sizeof(Info));p1=head;p2=head;fread(head,sizeof(Info),1,fp);while(p1->next!=NULL){p2=(Info*)malloc(sizeof(Info));fread(p2,sizeof(Info),1,fp);p1->next=p2;p1=p2;}fclose(fp);p2->next=NULL;p1=head;_node=p1;_temp=p1;p=head;//Buble sortfor(;_temp->next;_temp=_temp->next)for(_node=p;_node->next;_node=_node->next){if(_node->P1.score < _node->next->P1.score){cpyInfo(&InfoTemp,_node);cpyInfo(_node,_node->next);cpyInfo(_node->next,&InfoTemp);}}}//out put i 0~9 Top 1~10clearviewport();settextstyle(GOTHIC_FONT,0,8);outtextxy(215,0,"Rank");settextstyle(SANS_SERIF_FONT,0,4);outtextxy(40,80,"No. Name Score");outtextxy(40,105,"-------------------------");settextstyle(3,0,4);char buf[100];p1=head;for(int m=0;m<10;m++){if(p1!=NULL){//if(p1->score>0) little color egg for me!sprintf(buf,"%2d %7s",(m+1),p1->P1.name);outtextxy(40,125+m*33,buf);sprintf(buf,"%-d",p1->P1.score);outtextxy(500,125+m*33,buf);p1=p1->next;}}//Press Anykey To Returnwhile(1){if ((keycode=all_key())!=ESC) {repaint();break;}if (keycode==ESC) SureToQuit();} } void help(){clearviewport();settextstyle(GOTHIC_FONT,0,1);outtextxy(220,5,"Help");settextstyle(6,0,3);outtextxy(40,100," This is a Game Create by LEWIS in 09070328.When one ");outtextxy(10,150,"side chess stick in a line ,it wins!");outtextxy(40,200," Press Left or Right to control your position . ");outtextxy(40,250," Press ENTER to drop .");outtextxy(40,300," Press Esc anytime if you want to quit .This game ");outtextxy(10,350,"will record your score and name.");outtextxy(40,400," Press anykey to continue your game ");while(1){if ((keycode=all_key())!=ESC){repaint();break;}if (keycode==ESC) SureToQuit();} } void SureToQuit(){closegraph();cprintf(" Quit Game ? Yes(Enter) / No(Esc) ");while(1){if((keycode=all_key())==ESC){break;}if(keycode==ENTER){printf("/n ----YES----/n");printf("/n Save Current Game ? Yes(Enter) / No(Esc) ");while(1){if((keycode=all_key())==ESC){exit(0);}if(keycode==ENTER){printf("/n ----YES----");save();exit(0);}}}}startGraphics();repaint(); } int judge(){ //0:continue; 1:P1 win; 2:P2 win; 3:draw./*-------set who?-------*/int who,code=1;int im,jm,k;/*------------"-"check------------*/for (jm=1;jm<=jmax;jm++)for (im=1;im<=imax-3;im++)if((who=InfoNow.board[level-1][jm-1][im-1])!=0){for(k=1;k<=3;k++)if(InfoNow.board[level-1][jm-1][im-1+k]==who) code++;if (code==4) return(who);else code=1;}/*-----------"|"checf-------------*/for (im=1;im<=imax;im++)for (jm=1;jm<=jmax-3;jm++)if((who=InfoNow.board[level-1][jm-1][im-1])!=0){for(k=1;k<=3;k++)if(InfoNow.board[level-1][jm-1+k][im-1]==who) code++;if (code==4) return(who);else code=1;}/*------------"/"check------------*/for (im=1;im<=imax-3;im++)for(jm=1;jm<=jmax-3;jm++)if((who=InfoNow.board[level-1][jm-1][im-1])!=0){for(k=1;k<=3;k++)if(InfoNow.board[level-1][jm-1+k][im-1+k]==who) code++;if (code==4) return(who);else code=1;}/*------------"/"check------------*/for( im=4;im<=imax;im++)for (jm=1;jm<=jmax-3;jm++)if((who=InfoNow.board[level-1][jm-1][im-1])!=0){for(k=1;k<=3;k++)if(InfoNow.board[level-1][jm-1+k][im-1-k]==who) code++;if (code==4) return(who);else code=1;}/*------------Draw Check----------*/for(int jj=0;jj<jmax;jj++){for(int ii=0;ii<imax;ii++)if(InfoNow.board[level-1][jj][ii]!=0) code++;}if (code==imax*jmax+1) return (3);else code=1;//--------------------------------//return 0; } void setPlayerName(char p[8]){//In test modeprintf("/nPlease Input Your Name: ");scanf("%s",p); } void setPassWord(char p[16]){printf("/nPlease set your PassWord: ");scanf("%s",p); } void setlevel(){clearviewport();closegraph();printf(" Choose Level :/n Level 1 (F1) / Level 2 (F2) " );while(1){keycode=all_key();if (keycode==F1){level=1;break;}if (keycode==F2){level=2;break;}}i=1;setimaxjmax();startGraphics();repaint(); } void setimaxjmax(){//set Limitif (level==1)imax=7,jmax=6;if (level==2)imax=9,jmax=8; } void cpyInfo(Info *p1,Info *p2){//Just DATA .No pointer.for(int i=0;i<2;i++)for(int j=0;j<8;j++)for(int k=0;k<9;k++)p1->board[i][j][k]=p2->board[i][j][k];strcpy(p1->P1.name,p2->P1.name);strcpy(p1->P2.name,p2->P2.name);strcpy(p1->psw,p2->psw);p1->P1.score=p2->P1.score;p1->P2.score=p2->P2.score;p1->player=p2->player;} void repaint(){clearviewport();drawGameUI();drawChessBoard(level);drawPlayerInfo(InfoNow.P1.name);drawScoreP1(InfoNow.P1.score);drawScoreP2(InfoNow.P2.score);if(level==1){drawStepXY(8,1,level,1);drawStepXY(8,4,level,2);}if(level==2){drawStepXY(11,1,level,1);drawStepXY(11,5,level,2);}drawArrow(i); } void drawGameUI(){settextstyle(6,0,1);outtextxy(8,2,"New(F1) Next(F2) Load(F3) Save(F4) rank(F5) Level(F6) help(F7) Quit(ESC)");drawBar(); } void drawBar(void){for(int i=0;i<8;i++)rectangle(2+80*i,4,2+80*(i+1)-10,28); } void drawChessBoard(int level){if (level==1){for(int i=0;i<=6;i++) //--line (0,47+i*72,504,47+i*72);for(int j=0;j<=7;j++) //||line (j*72,48,j*72,480);for( i=0;i<7;i++){for(int j=0;j<7;j++){circle(72*i+36,48+72*j+36,36);}}}if (level==2){for(int i=0;i<=8;i++)//--line(0,31+i*56,504,31+i*56);for(int j=0;j<=9;j++)//||line(j*56,32,j*56,480);for( i=0;i<9;i++){for(int j=0;j<8;j++){circle(56*i+28,31+56*j+28,28);}}}//draw chessfor(int i=1;i<=imax;i++)for(int j=1;j<=jmax;j++){if (InfoNow.board[level-1][j-1][i-1]==1) drawStepXY(i,j,level,1);if (InfoNow.board[level-1][j-1][i-1]==2) drawStepXY(i,j,level,2);} } void drawPlayerInfo(char name[]){setlinestyle(SOLID_LINE,1,1);//line(504,0,504,480); //|line(504,240,640,240);//---settextstyle(SANS_SERIF_FONT,0,4);outtextxy(520,160,name);outtextxy(520,400,"PC");settextstyle(SMALL_FONT,0,1);outtextxy(563,470,"Made By Lewis From 09070328");// draw01(572,60,level);// draw02(572,300,level); } void drawArrow(int i){int x, y,r;setfillstyle(SOLID_FILL,GREEN);if (level==1) y=48,r=36;if (level==2) y=32,r=28;floodfill(2*r*i-r*3/2,y,getmaxcolor());floodfill(2*r*i-r/2,y,getmaxcolor()); } void clearArrow(int i){int x,y,r;setfillstyle(SOLID_FILL,BLACK);if (level==1) y=48,r=36;if (level==2) y=32,r=28;floodfill(2*r*i-r*3/2,y,getmaxcolor());floodfill(2*r*i-r/2,y,getmaxcolor()); } void drawStepXY(int i,int j,int level,int player){if (player==1) setfillstyle(LTBKSLASH_FILL,LIGHTGREEN);if (player==2) setfillstyle(BKSLASH_FILL,LIGHTRED);if (level==1){i=72*i-36;j=48+72*j-36;circle(i,j,36);floodfill(i,j,getmaxcolor());}if (level==2){i=56*i-28;j=31+56*j-28;circle(i,j,28);floodfill(i,j,getmaxcolor());} } void clearStepXY(int i,int j,int level){setfillstyle(SOLID_FILL,BLACK);if (level==1){i=72*i-36;j=48+72*j-36;circle(i,j,36);floodfill(i,j,WHITE);}if (level==2){i=56*i-28;j=31+56*j-28;circle(i,j,28);floodfill(i,j,getmaxcolor());} } void dropChess(int i,int player){//Calculate how many times to dropj=jmax;while (InfoNow.board[level-1][j-1][i-1]!=0 ) j--;InfoNow.board[level-1][j-1][i-1]=player;//draw Stepif (j==1 ) drawStepXY(i,j,level,player);else{for(int m=1;m<j;m++){clearStepXY(i,m,level);drawStepXY(i,m+1,level,player);sound(300);delay(30);nosound();}} } void drawScoreP1(int score){setviewport(530,200,640,240,0);char *buf;settextstyle(3,0,3);sprintf(buf,"Score:%2d",score);outtextxy(530,200,buf); } void drawScoreP2(int score){setviewport(530,440,640,480,0);char *buf;settextstyle(3,0,3);sprintf(buf,"Score:%2d",score);outtextxy(530,440,buf); } void endDraw(char *buf){setcolor(13 );settextstyle(GOTHIC_FONT,0,10);outtextxy(200,140,buf);setcolor(getmaxcolor());delay(500);sound(800);delay(100);nosound();delay(100);sound(800);delay(100);sound(1200);delay(400);nosound(); }

keboard.c:

#include "headfile.h" int all_key(void); int all_key(){int keycode;keycode=bioskey(0);if(bioskey(2)&0x04){return keycode;}return keycode & 0xff? keycode & 0xff :keycode; }

?

headfile.h:

#include "stdio.h" #include "dos.h" #include "graphics.h" #include "stdlib.h" #include "conio.h" #include "bios.h" #include "alloc.h" #include "string.h" #include "ctype.h" #include "time.h" #define NULL 0 #define ESC 27 #define ENTER 0x0d #define F1 0x3b00 #define F2 0x3c00 #define F3 0x3d00 #define F4 0x3e00 #define F5 0x3f00 #define F6 0x4000 #define F7 0x4100 #define LEFT_ARROW 0x4b00 #define RIGHT_ARROW 0x4d00 #define UP_ARROW 0x4800 #define DOWN_ARROW 0x5000 ?

cover.c:

#include "headfile.h"" void startGraphics(); void drawTittle(); void drawCoverPicture1(); void drawCoverPicture2(); void coverage(); int all_key(); void coverage(){while(!bioskey(1)){setviewport(1,1,639,479,1);startGraphics ();drawTittle();drawCoverPicture1();drawCoverPicture2();if((all_key())!=ESC)break;else exit(0);}clearviewport(); } void startGraphics(){//Begin Graphics Mode/* request auto detection */int gdriver = DETECT, gmode, errorcode;/* initialize graphics mode */initgraph(&gdriver, &gmode, "d://tc30 //bgi"); } void drawCoverPicture1(){//Define the size of the two beginning picturint x=410,y=200,r=100;//Drawrectangle(x-r,y-r,x+r,y+r);circle(x,y,r);setfillstyle(SLASH_FILL,LIGHTRED);floodfill(x-r+1,y,WHITE); } void drawCoverPicture2(){//Define the size of the two beginning picturint x=410,y=200,r=100;x+=r,y+=r;//Drawsetfillstyle(XHATCH_FILL,LIGHTGREEN);rectangle(x-r,y-r,x+r,y+r);circle(x,y,r);floodfill(x-r/2,y-r/2,getmaxcolor());setfillstyle(LTBKSLASH_FILL,LIGHTBLUE);floodfill(x-1,y-1,getmaxcolor());floodfill(x+1,y+1,getmaxcolor()); } void drawTittle(){clearviewport();settextstyle(GOTHIC_FONT,0,10);outtext("Bingo!");settextstyle(SANS_SERIF_FONT,0,4);outtextxy(130,430,"Press anykey to continue!"); }?

總結(jié)

以上是生活随笔為你收集整理的北京工业大学 大一C语言课程设计--四子棋(Bingo)(连子游戏)in TurboC 3.0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。