北京工业大学 大一C语言课程设计--四子棋(Bingo)(连子游戏)in TurboC 3.0
?
?? ??
連子游戲設計報告書
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
2009年12月
?
目錄頁
?
?
目錄
1 需求分析... 3
1.1功能與數據需求... 3
1.1.1題目要求的功能... 3
1.1.2 擴展功能... 3
1.2界面需求... 4
1.3 開發環境與運行需求... 6
2 概要設計... 6
2.1主要數據結構... 6
2.2程序總體結構... 6
2.3 各模塊函數說明... 7
3 詳細設計... 9
3.1主函數部分... 9
3.2 游戲繪圖部分... 9
3.3 游戲部分... 10
3.4 判斷輸贏部分... 11
3.5 游戲存檔讀取 排名部分... 11
3.6 游戲AI部分... 12
4 測試... 13
5 用戶手冊... 13
5.1應用程序功能的詳細說明... 13
5.2應用程序運行環境要求... 13
5.5輸入數據類型、格式和內容限制... 13
6總結提高... 13
6.1課程設計總結... 13
6.2開發中遇到的問題和解決方法... 14
6.3 對自己完成課設完成情況的評價... 15
6.4對于《高級語言程序設計Ⅲ》與《高級語言程序設計課程設計》兩門課程的意見與建議???? 15
附錄:程序源代碼... 16
?
?
?
需求分析
?? 功能與數據需求
???? .1.1題目要求的功能
?
1.?? 游戲畫面分為ABC三部分,分別代表菜單,棋盤和記分牌。
2.?? 進入游戲后玩家建立/讀取存檔,選擇難度,開始游戲。
3.?? 在B區畫棋盤,玩家與計算機交替部子(棋子顏色不同)。
4.?? 玩家通過上下左右移動鍵盤光標回車鍵下棋(鼠標亦可)。
5.?? 棋盤豎直放立,因此棋子會在該列由上往下落。
6.?? 游戲的任意一方橫向,縱向或者斜向棋子連成4子即為勝利,于是本局游戲結束并積1分。若棋盤布滿未分勝負,本局結束,雙方各記一分。
7.?? 在存檔中存儲玩家勝負信息及游戲棋盤。
8.?? 游戲中途按Esc退出游戲。
9.?? 游戲設有排行榜,玩家可以看到自己的勝負,排名,勝率。
10.? 玩家保存后將棋局錄入存檔。
11.? A區是菜單欄,設:Level,New,Load/Save,Rank,Help。
12.? B區是玩家欄,顯示當前玩家與電腦的比分。玩家每次輸贏均錄入存檔。
?
擴展功能
1.?? 設計游戲封面。
2.?? 調整坐標設計游戲友好UI。
3.?? 插入游戲聲音。
4.?? 設計下棋動畫,使畫面更為流暢酷炫。
5.?? 在退出時候再次請求用戶確認以防誤操作。
6.?? 完善AI。
?
?
?? 界面需求
?
?? 開發環境與運行需求
????? 硬件:Duo CORE2 T6500 ,4G Ram ,320G HDD ,nVidia 105M Video Clip.
??? ??? 軟件:Windows Vista 的安全模式 在EVGA(640*480)的顯示模式下運行???????????????????????????????????? Turbo C++ IDE 3.0.
概要設計
?? 主要數據結構
1.結構體:
?
Typedef struct{
???????? Player P1;
???????? Player P2;
???????? board[2][8][9];
}Info;
typedef struct {
???????? char name[8];
???????? int score;
}Player;
?
???????? 其中 Info用來儲存每個玩家的游戲數據,其中,巧妙地使用一個三維數組來實現兩個棋盤的讀取。
?
2. 整形數據
int imax,jmax,level,i;
???????? 其中,imax jmax用來定義棋盤邊界,level定義本局游戲級數(有1,2)來選擇棋盤。i是下子的位置(即棋盤的橫坐標)。
?
?? 程序總體結構
?
| ? ??? judge()判斷 |
| ??? judge()判斷 |
?
?
?
?
?
?
各模塊函數說明
?
/*===========================函數聲明=========================*/
//---------------------------繪畫函數--------------------------
void startGraphics();?? 繪制顯卡
void coverage();??????? 繪制封面
void drawTittle();????? 繪制封面文字
void drawCoverPicture1();繪制封面圖片1
void drawCoverPicture2();繪制封面圖片2
int? all_key();???????? 鍵盤接受函數,返回鍵盤編碼
void drawGameUI();????? 繪制游戲界面
void drawStepXY(int i,int j,int level,int player);
繪制棋子,接收棋子所在棋盤坐標 I,J 棋盤登記 Level 棋子顏色Player等變量
void drawChessBoard(int level);???? 繪制棋盤,接收等級level 變量
void drawPlayerInfo(char name[]);?? 繪制玩家信息 接受玩家姓名字符串 name[]
void drawMenu();??????????? ??????? 繪制控制菜單
void drawScoreP1(int score);??????? 繪制玩家1分數
void drawScoreP2(int score);??????? 繪制玩家2分數
void repaintBoard();??????????????? 刷新棋盤
void repaint();???????????????????? 刷新游戲界面
void clearStepXY(int i,int j,int level);清除棋子,接收坐標(I,j)登記level
void moveChess();?????????????????? 移動棋子函數
void PlayerTurn(int player);??????? 玩家1或2輪控制函數,接收玩家player變量
void dropChess(int i,int player);?? 下子函數,接收下子列I,玩家Player變量
void drawArrow(int i);????????? ??? 繪制選擇箭頭,接收箭頭所指列i
void clearArrow(int i);???????????? 清除選擇箭頭,接收箭頭所指列i
?
//---------------------------游戲函數------------------------------
?
void newGame();???????????????????? 新的游戲函數
void playingGame(int player);?????? 玩家局,接收玩家player變量
void SureToQuit();????????????????? 再次確認退出游戲界面
void endDraw(char *buf);??????????? 一盤結束代碼 輸出字符串buf (輸贏或平)
void save();??????????????????????? 儲存游戲函數
void load();??????????????????????? 讀取游戲函數
void rank();??????????????????????? 排名界面
void cpyInfo(Info *p1,Info *p2);??? 復制游戲信息結構體函數p1 to p2
void help();??????????????????????? 幫助界面
int judge();??????????????????????? 判斷輸贏 返回0繼續 1玩家贏 2輸 3平
void AITurn(int player);??????????? 電腦下棋函數 接受下子顏色player
int ThinkNextStep(int player);????? 電腦計算下棋函數
?
//---------------------------設置函數----------------------------
void setimaxjmax();???????????????? 設置棋盤邊界 改變全局變量imax jmax?
void setlevel();??????????????????? 設計等級 (直接改變全局變量level)
void setPlayerName(Info *p);??????? 設置玩家姓名,形參指針指向貯存游戲信息的結構體
?
???????????????
?
詳細設計
? 主函數部分
????? 本程序主函數短小精悍,意思簡潔明了!
void main(){
??? ??? ??? coverage();//繪制封面圖片
??????? ??? setlevel();//設置游戲等級
??????? ??? newGame(); //開始新的游戲
}
游戲繪圖部分
游戲程序中,設計了多個繪圖移動圖塊的函數,比如繪制棋子的drawStepXY() ,clearStepXY(),drawArrow(),clearArrow()。
所有繪制函數參使用整形變量 i,j,player。分別代表所在棋盤的橫縱坐標,哪個玩家。這樣在多次使用繪圖函數中,直觀方便,簡潔明了,增強了函數的易讀性和維護性。
為了在游戲中跳出后返回,設置了一個repaint()函數,此函數可以重新繪制棋盤,十分方便! 代碼:
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()函數為新開始游戲,次函數可以供游戲中菜單NewGame呼出后遞??? 歸套用,實現新開始游戲功能。
?
??????? nextGame()可以由”下一局”功能調用模塊,不會重置分數。
?
??????? playingGame()函數為游戲進行函數,是一個循環函數。
當PlayerTurn()時,接受鍵盤輸入,此時玩家可以按F1~F6開啟菜單功能,按Esc退出游戲,左右回車控制下子。其中使用repaint()函數來重繪界面,返回游戲。代碼:
???? 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();
???????? }?????????????????????????????????????????????
???? ??? }
? }
?
?? 判斷輸贏部分?
??? 判斷輸贏或者平局函數judge,使用多重循環對棋盤進行掃描,遍歷所有可能情況。
??? 代碼較長,取一小段示意:
?????? 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;
???????????????????? }
?????? ……
?????? ……
?????? ……
?
?? 游戲存檔讀取 排名部分
結構體Info用來存儲玩家名字,排名,局勢,棋盤, 玩家密碼。
讀寫過程中需要用戶名和密碼的輸入。
游戲存讀取通過鏈表組織,通過dos的文件讀寫完成存檔的讀寫。讀檔流程圖:
?
存檔流程圖:
?
排名是在文件讀取出鏈表,然后根據鏈表內容進行冒泡排序,輸出實現的。關鍵代碼:
??? _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是本課設的難點,也是最后花費最多時間完成的內容。思路如下
?
????????
測試
用戶手冊
應用程序功能的詳細說明
?
本游戲為北京工業大學09級計算機學院劉某編寫的高級語言程序課程設計作業。
??? ??? 游戲簡單易上手,游戲過程中,按F6可以查詢幫助。
規則:連子游戲,游戲兩方交替部子,棋盤垂直,棋子受重力吸引。任意一方棋子橫縱斜連成4子即可獲勝。
操作:按左右方向鍵移動光標,按回車(Enter)進行部子。F1重新開始;F2讀取上局游戲;F3 存取當前游戲; F4查看排行榜; F5設置等級; F6查詢幫助; 游戲途中按Esc鍵隨時退出!(退出前會詢問是否確認退出以防誤操作。是Enter 取消Esc。)
?
應用程序運行環境要求
?????????????????? 本游戲代碼在TurboC 3.0 于 Dos 6.22 編譯成功運行。
???????? 所得二進制程序需在Dos或兼容Dos的操作系統內運行,如WinXP及以下。建議以全屏模式運行。
?
輸入數據類型、格式和內容限制
?????????????????? 要求輸入玩家名時,需輸入ASCII碼的可顯示字符。限八字符內。
?
總結提高
? 課程設計總結
這次課設作業,是我所做過的印象最深,收獲最大的作業之一!二到十二周學習的理論知識,一直停留在僅僅能完成簡單的課后作業的水平而已。自從學習高級語言程序設計以來,就特別有獨立完成一個使用程序的強烈愿望。而這次課程設計作業給了我一個很好實踐理論知識的機會。也許是應為需要完成一個富有趣味性但是開發起來有一定難度游戲的緣故吧,在開發過程中,體會到了很多課上學不到的東西。
首先,面對一個待構建的龐大程序,應該先組織好一個清晰的思路,用科學統籌的方法去做好規劃。而我在開始的時候沒有這樣的覺悟,毫無進展地度過了一個星期。
其次,我學會了用以致學。計算機科學是一門博大精深的學科,里面有相當多的知識,工具。但是,為了更好的提高我們的工作效率,我們不可能全面了解某些領域的知識。這個時候,就需要我們選擇性的學習,以提高我們的效率。
第三,學會利用圖書館。沒進入大學之前,我的知識主要從互聯網得來。以前認為,凡事都可以在互聯網上搜索得到,現在已經不需要圖書館了。然而,開始做課設時發現,應為TurboC是很久以前的東西,互聯網上關于它的資料不多,于是我開始檢索圖書館。在圖書館里,我發現了許多有用的書籍,比如當我對指針模棱兩可的時候,閱讀幾本書,我就基本明白了指針的原理和應用。當我對鏈表不太熟悉的時候,幾本書的實例又讓我長了知識。我恍然大悟,圖書館真的是一個知識的寶庫,我們應該好好利用它!
第四,讓我學會直面困難。初學者設計程序,錯誤,Bug在所難免。在一次次讓人沮喪的Compiling Error里頭,我不斷地鼓勵自己,不斷探索,思考,分析,修改,調試,直至一個個模塊被我順利調試通過,那難以言表的喜悅之情,重新點燃了我的斗志!
第五,我學會了如何更加有效率地Debug。無數次報錯,讓我我學會了巧妙設置斷點,巧妙地注釋掉代碼來排除Bug,提高了我的開發效率。
最后,也是最重要的一點,在完成課設的過程中,培養了我專注,堅持不懈的品質。
好幾個周末,我都沉醉的藍色的海洋,從早上直到深夜,幾乎一直專注地在思索,考慮,調試……
?? 開發中遇到的問題和解決方法
???????? ???????? 開發中遇到的問題可以說是無以計數的吧,下面就印象最深的談談。
??????????????????
在開始的一個星期里頭,只最沒有頭緒的時候。首先要解決的,也是最能看出問題的就是繪制圖形界面。其中設計是比較耗費時間的,為了日后調用方便,我定義了許多繪圖函數,同時做了大量的計算工作。
?
完成繪畫函數后,接下來就在調用鍵盤疑惑了,后來查看了兩三個課設的源代碼,知道了可以用一個無條件循環來運行程序, 在按下按鍵后跳出,長了見識。
?
在后期的開發中,輸贏的判斷和電腦下棋是一大難點。因為這兩種錯誤都不是顯而易見的。為了應對這種情況,我在Debug的過程中學會了合理地設置斷點,逐步或跳步運行,監視變量等技巧,大大的提高了開發效率。
?
最后,存讀檔是另一重點。課設讓我了解到先前我對鏈表,文件的應用掌握的不是很充分。于是,我查找了許多書籍,閱讀了許多案例,最終把鏈表,文件讀取的應用模塊編出來了。因為鏈表部分容易出隱藏錯誤,所以在老師的提示下,我將存讀取檔單獨編譯通過之后,在加入主函數。
隨著Debug次數的增加,我也認識了解了各種各樣的錯誤和處理方法。而遇到
然的問題但是總是報錯的時候,我也可以漸漸冷靜下來,仔細思考,逐步分析,排除錯誤。
??????????????????
????? 對自己完成課設完成情況的評價
在剛看到課設題目的時候,完全沒有頭緒。當時對C開發程序還是,一竅不通,更不會將諸多函數靈活貫通了。但是隨著自己編寫的函數一點一點的積累,逐漸的學會利用幫助文檔學習利用新的函數,學會利用參考書籍加深對不太明白的知識點的理解。眼看期末考檢查臨近,開發接近尾聲,突然想到對游戲功能再加強鞏固,可是時間不允許了。設計的程序離自己理想的目標還差很遠,我也會利用寒假時間進一步完善和改進。
至此,我對電腦的運算速度再次驚嘆,了解了編程的精萃是算法。如何利用更加高效的算法運行在速度有限的設備上,為人類解決更加多的問題,是程序員們永無止境的課題。
同時,我對人工智能算法產生了濃厚的興趣。同時也查閱了許多資料,涉及到剪枝,Alpha-beta算法等尚未涉獵的知識。相對于象棋算法和五子棋算法,連子游戲可能情況還是較少的,這進一步激發了我對更大規模,更高效的棋牌算法的興趣。
?
對于《高級語言程序設計Ⅲ》與《高級語言程序設計課程設計》兩門課程的意見與建議
在這兩門課程即將結束之際,我受益匪淺。我深深得知在計算機領域,學習的道路是永無止境的。計算機科學是一門注重實用的學科,掌握的所有理論,是為實際應用。應此,學習本門課程需要進行大量實踐。而高級語言程序設計和課程設計,為學生提供了一個絕佳的小試牛刀的機會。在開發的過程中,為了達成目標,而更加牢固地掌握所需知識,達到了很好的促進學習作用。同時巧妙的課設題目安排,也大大激發了學生的興趣,而老師的辛勤工作,上機輔導對我們的啟發良多!
?
我覺得使用TurboC進行圖形界面開發有些困難,而我曾見一些同學把主要精力放在了開發華麗界面上,而不是程序算法的優化完善,實在有些本末倒置了。而且TurboC圖形模式的運行環境要求比較苛刻,不是在所有的Windows虛擬Dos環境下都可以完美兼容,為此我曾用了許多時間調試DOS的模擬環境。因此我建議,課設作業不妨布置一些用TurboC在文本模式下進行一些注重算法的程序開發,而類Windows UI程序的開發,不妨準許同學們使用圖形界面下的IDE,這樣也許會事半功倍!
?
最后,在這兩門課程的學習過程中,培養了對計算機科學與技術這門專業強烈的興趣,在氣氛融洽的學習環境中,受益匪淺,感謝老師在這一學期里的耐心指導!
?
?
?
?
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!"); }?
總結
以上是生活随笔為你收集整理的北京工业大学 大一C语言课程设计--四子棋(Bingo)(连子游戏)in TurboC 3.0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基本统计分析
- 下一篇: Navicat出现1054错误