C語言編寫中國象棋_第1頁
C語言編寫中國象棋_第2頁
C語言編寫中國象棋_第3頁
C語言編寫中國象棋_第4頁
C語言編寫中國象棋_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、/ main.c/ 象棋/ 車馬相仕帥仕相馬車/ 十十十十十十十十十/ 十炮十十十十十炮十/ 兵十兵十兵十兵十兵/ 十十十十十十十十十/ 楚河漢界/ 十十十十十十十十十/ 卒十卒十卒十卒十卒/ 十炮十十十十十炮十/ 十十十十十十十十十/ 車馬象士將士象馬車/ Created by tarena121 on 15/8/12./ Copyright (c) 2015年 Tarena. All rights reserved./#include <stdio.h>#include <stdbool.h>#include <math.h>#include <s

2、tdlib.h>#define R(piece) "03331m"#piece"0330m"/紅色棋子#define B(piece) "03330m"#piece"0330m"/黑色棋子#define CROSS "03333m十0330m"/定義外部變量,棋盤坐標char* array119;int xi,yi;/要移動的棋子int xj,yj;/移動的目標位置bool isStandard = 1;/是否符合規(guī)則,初始值1,符合bool gameOverSign = 1;/游戲是否結

3、束,0結束bool restart = 0;/生成棋盤void chessboardBuilding();/打印棋盤void printChessboard();/判斷是紅棋還是黑棋,紅旗返回1,黑棋返回-1,否則返回0int redOrBlack(int x,int y);/紅棋移動void redMove();/黑棋移動void blackMove();/每種棋子的規(guī)則void rulesOfAllKindsOfChessPieces();/判斷游戲結束void isGameOver();/*主函數(shù)*int main() /生成棋盤 chessboardBuilding(); /打印棋盤

4、printChessboard(); /開始下棋 int turn = -1; while (gameOverSign) isStandard = 1; turn *= (-1);/雙方交替下棋 switch (turn) case 1: redMove(); turn = (restart) ? (turn*-1) : turn; break; case -1: blackMove(); turn = (restart) ? (turn*-1) : turn; break; isGameOver(); printf("游戲結束!n"); /主函數(shù)結束/*自定義函數(shù)*/生成

5、棋盤void chessboardBuilding() for (int i = 0; i < 11; i +) for (int j = 0; j < 9 ; j +) arrayij = CROSS; printf("n"); array50 = array51 = array54 = array57 = array58 = "" array52 = B(楚); array53 = B(河); array55 = B(漢); array56 = B(界); /布置紅棋 array00 = array08 = R(車); array01 =

6、 array07 = R(馬); array02 = array06 = R(相); array03 = array05 = R(仕); array04 = R(帥); array21 = array27 = R(炮); array30 = array32 = array34 = array36 = array38 = R(兵); /布置黑棋 array100 = array108 = B(車); array101 = array107 = B(馬); array102 = array106 = B(相); array103 = array105 = B(仕); array104 = B(將)

7、; array81 = array87 = B(炮); array70 = array72 = array74 = array76 = array78 = B(卒);/打印棋盤void printChessboard() /顯示 printf(" 03343;30m中國象棋歡迎您0330mnn"); for (int i = 0; i < 11; i +) for (int j = 0; j < 9; j +) printf("%s",arrayij); printf("n"); /判斷是紅棋還是黑棋,紅旗返回1,黑棋返回

8、-1,否則返回0int redOrBlack(int x,int y) if (arrayxy = R(車) | arrayxy = R(馬) | arrayxy = R(相) | arrayxy = R(仕) | arrayxy = R(帥) | arrayxy = R(炮) | arrayxy = R(兵) return 1; else if (arrayxy = B(車) | arrayxy = B(馬) | arrayxy = B(象) | arrayxy = B(仕) | arrayxy = B(將) | arrayxy = B(炮) | arrayxy = B(卒) return

9、-1; else return 0;/紅棋移動void redMove() if (restart) printf("違反游戲規(guī)則,請重新輸入n"); restart = 0; printf("紅棋請輸入你要移動的棋子:n"); scanf("%d %d",&xi,&yi); printf("紅棋請輸入你要放置的位置:n"); scanf("%d %d",&xj,&yj); rulesOfAllKindsOfChessPieces(); printChessboa

10、rd();/黑棋移動void blackMove() if (restart) printf("違反游戲規(guī)則,請重新輸入n"); restart = 0; printf("黑棋請輸入你要移動的棋子:n"); scanf("%d %d",&xi,&yi); printf("黑棋請輸入你要放置的位置:n"); scanf("%d %d",&xj,&yj); rulesOfAllKindsOfChessPieces(); printChessboard();/判斷游戲結

11、束void isGameOver() bool sign_r = 0; bool sign_b = 0; for (int i = 0; i < 11; i +) for (int j = 0; j < 9; j +) if (arrayij = R(帥) sign_r = 1; else if (arrayij = B(將) sign_b = 1; if (sign_r = 0)|(sign_b = 0) gameOverSign = 0; /每種棋子的規(guī)則void rulesOfAllKindsOfChessPieces()/R(車)- if (arrayxiyi = R(車)

12、 if (yi = yj)/列坐標不變,同列移動 for (int i = xi+1; i < xj; i +) if (i = 5) continue;/如果行等于5,跳過 if (arrayiyi != CROSS) isStandard = 0;/如果初始位置和目標位置之間有棋子,則不符合規(guī)則 for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳過 if (arrayxiyi != CROSS) isStandard = 0; else if (xi = xj)/行坐標不變,同行移動 for (int i

13、= yi+1; i < yj; i +) if (arrayxii != CROSS) isStandard = 0; for (int i = yi-1; i > yj; i -) if (arrayxii != CROSS) isStandard = 0; if (xi = xj | yi = yj)&& isStandard && (redOrBlack(xj, yj) != 1)/如果棋子直行、沒有犯規(guī)且落點不是紅棋,可以移動 arrayxiyi = CROSS; arrayxjyj = R(車); else restart = 1; /B(

14、車)- else if (arrayxiyi = B(車) if (yi = yj)/列坐標不變,同列移動 for (int i = xi+1; i < xj; i +) if (i = 5) continue;/如果行等于5,跳過 if (arrayiyi != CROSS) isStandard = 0;/如果初始位置和目標位置之間有棋子,則不符合規(guī)則 for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳過 if (arrayiyi != CROSS) isStandard = 0; else if (xi

15、= xj)/行坐標不變,同行移動 for (int i = yi+1; i < yj; i +) if (arrayxii != CROSS) isStandard = 0; for (int i = yi-1; i > yj; i -) if (arrayxii != CROSS) isStandard = 0; if (xi = xj | yi = yj)&& isStandard && redOrBlack(xj, yj) != -1)/如果棋子直行、沒有犯規(guī)且落點不是紅棋,可以移動 arrayxiyi = CROSS; arrayxjyj =

16、 B(車); else restart = 1; /R(馬)- else if (arrayxiyi = R(馬) if (redOrBlack(xj, yj) != 1) && (xj = xi-2 && yj = yi-1 &&redOrBlack(xi-1, yi) = 0) | (xj = xi-2 && yj = yi+1 &&redOrBlack(xi-1, yi) = 0) | (xj = xi-1 && yj = yi-2 &&redOrBlack(xi, yi-1)

17、 = 0) | (xj = xi-1 && yj = yi+2 &&redOrBlack(xi, yi+1) = 0) | (xj = xi+1 && yj = yi-2 &&redOrBlack(xi, yi-1) = 0) | (xj = xi+1 && yj = yi+2 &&redOrBlack(xi, yi+1) = 0) | (xj = xi+2 && yj = yi-1 &&redOrBlack(xi+1, yi) = 0) | (xj = xi+2 &

18、amp;& yj = yi+1 &&redOrBlack(xi+1, yi) = 0) arrayxiyi = CROSS; arrayxjyj = R(馬); else restart = 1; /B(馬)- else if (arrayxiyi = B(馬) if (redOrBlack(xj, yj) != -1) && (xj = xi-2 && yj = yi-1 &&redOrBlack(xi-1, yi) = 0) | (xj = xi-2 && yj = yi+1 &&red

19、OrBlack(xi-1, yi) = 0) | (xj = xi-1 && yj = yi-2 &&redOrBlack(xi, yi-1) = 0) | (xj = xi-1 && yj = yi+2 &&redOrBlack(xi, yi+1) = 0) | (xj = xi+1 && yj = yi-2 &&redOrBlack(xi, yi-1) = 0) | (xj = xi+1 && yj = yi+2 &&redOrBlack(xi, yi+1) =

20、0) | (xj = xi+2 && yj = yi-1 &&redOrBlack(xi+1, yi) = 0) | (xj = xi+2 && yj = yi+1 &&redOrBlack(xi+1, yi) = 0) arrayxiyi = CROSS; arrayxjyj = B(馬); else restart = 1; /R(炮)- else if (arrayxiyi = R(炮) int count = 0;/起始位置間棋子的個數(shù) if (yi = yj)/列坐標不變,同列移動 for (int i = xi+1;

21、i < xj; i +) if (i = 5) continue;/如果行等于5,跳過 if (redOrBlack(i, yi) != 0) count+; for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳過 if (redOrBlack(i, yi) != 0) count+; else if (xi = xj)/行坐標不變,同行移動 for (int i = yi+1; i < yj; i +) if (redOrBlack(xi, i) != 0) count+; for (int i = yi

22、-1; i > yj; i -) if (redOrBlack(xi, i) != 0) count+; if (xi = xj | yi = yj)&& (count <= 1) && redOrBlack(xj, yj) != 1)/如果棋子直行、沒有犯規(guī)且落點不是紅棋,可以移動 arrayxiyi = CROSS; arrayxjyj = R(炮); else restart = 1; /B(炮)- else if (arrayxiyi = B(炮) int count = 0;/起始位置間棋子的個數(shù) if (yi = yj)/列坐標不變,同列

23、移動 for (int i = xi+1; i < xj; i +) if (i = 5) continue;/如果行等于5,跳過 if (redOrBlack(i, yi) != 0) count+; for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳過 if (redOrBlack(i, yi) != 0) count+; else if (xi = xj)/行坐標不變,同行移動 for (int i = yi+1; i < yj; i +) if (redOrBlack(xi, i) != 0) c

24、ount+; for (int i = yi-1; i > yj; i -) if (redOrBlack(xi, i) != 0) count+; if (xi = xj | yi = yj)&& (count <= 1) && redOrBlack(xj, yj) != -1)/如果棋子直行、沒有犯規(guī)且落點不是紅棋,可以移動 arrayxiyi = CROSS; arrayxjyj = B(炮); else restart = 1; /R(兵)- else if (arrayxiyi = R(兵) if (xi > xj) isStanda

25、rd = 0;/如果倒退,則不符合規(guī)范 if (xi = 3) if (xj != xi+1) | (yi != yj) isStandard = 0;/第3行時只能前進一步 if (xi = 4) if (xj != xi+2) | (yi != yj) isStandard = 0;/第4行時只能前進兩步 if (xi > 4) if (xj = xi+1 && yi =yj)| (xj = xi && yi =yj+1)|(xj = xi && yi =yj-1) else isStandard = 0; if (xi = xj |

26、yi = yj)&& isStandard && redOrBlack(xj, yj) != 1)/ arrayxiyi = CROSS; arrayxjyj = R (兵); else restart = 1; /B(卒)- else if (arrayxiyi = B(卒) if (xi < xj) isStandard = 0;/如果倒退,則不符合規(guī)范 if (xi = 7) if (xj != xi-1) | (yi != yj) isStandard = 0;/第3行時只能前進一步 if (xi = 6) if (xj != xi-2) | (y

27、i != yj) isStandard = 0;/第4行時只能前進兩步 if (xi < 4) if (xj = xi-1 && yi =yj)| (xj = xi && yi =yj+1)|(xj = xi && yi =yj-1) else isStandard = 0; if (isStandard && redOrBlack(xj, yj) != -1)/ arrayxiyi = CROSS; arrayxjyj = R (卒); else restart = 1; /R(相)- else if (arrayxiyi

28、 = R(相) if (xj <= 4)&&(redOrBlack(xj, yj) != 1) && (xj = xi-2 && yj = yi-2 &&redOrBlack(xi-1, yi-1) = 0) | (xj = xi-2 && yj = yi+2 &&redOrBlack(xi-1, yi+1) = 0) | (xj = xi+2 && yj = yi-2 &&redOrBlack(xi+1, yi-1) = 0) | (xj = xi+2 &am

29、p;& yj = yi+2 &&redOrBlack(xi+1, yi+1) = 0) arrayxiyi = CROSS; arrayxjyj = R(相); else restart = 1; /B(象)- else if (arrayxiyi = B(象) if (xj >= 6)&&(redOrBlack(xj, yj) != -1) && (xj = xi-2 && yj = yi-2 &&redOrBlack(xi-1, yi-1) = 0) | (xj = xi-2 &&

30、 yj = yi+2 &&redOrBlack(xi-1, yi+1) = 0) | (xj = xi+2 && yj = yi-2 &&redOrBlack(xi+1, yi-1) = 0) | (xj = xi+2 && yj = yi+2 &&redOrBlack(xi+1, yi+1) = 0) arrayxiyi = CROSS; arrayxjyj = B(象); else restart = 1; /R(仕)- else if (arrayxiyi = R(仕) if (xj <= 2)&&(redOrBlack(xj, yj) != 1) && (xj = xi-1 && yj = yi-1 ) | (xj = xi-1 && yj = yi+1 )

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論