《2022年c语言编译的简单黑白棋源代码 .pdf》由会员分享,可在线阅读,更多相关《2022年c语言编译的简单黑白棋源代码 .pdf(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include #include #define SIZE 8 void display(char boardSIZE); int valid_moves(char boardSIZE,int movesSIZE,char player); void make_move(char boardSIZE,int row,int col,char player); void computer_move(char boardSIZE,int movesSIZE,char player); int get_score(char boardSIZE,char player); int best_move
2、(char boardSIZE,int movesSIZE,char player); int main() char boardSIZESIZE=0; int movesSIZESIZE=0; int row=0; int col=0; int no_of_games=0; int no_of_moves=0; int invalid_moves=0; int comp_score=0; int user_score=0; char y=0; char x=0; char again=0; int player=0; printf(nREVERSInn); printf(You can go
3、 first on the first game,then we will take truns.n); printf( You will be white - (0)n I will be black - ().n); printf(Select a square for your move by typing a digit for the rown and a letter for the column with no spaces between.n); printf(nGood luck! press Enter to start.n); scanf(%c,&again); do p
4、layer=+no_of_games%2; no_of_moves=4; for(row=0;rowSIZE;row+) for(col=0;col=0&y=0&xSIZE&ySIZE&movesxy) make_move(board,x,y,0); no_of_moves+; break; else printf(Not a valid move,try again.n); else if(+invalid_moves2) fflush(stdin); printf(nYou have to pass,press return); scanf(%c,&again); else printf(
5、nNeither of us can go, so the game is over.n); else if(valid_moves(board,moves,) invalid_moves=0; computer_move(board,moves,); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 8 页 - - - - - - - - - no_of_moves+; else if(+invalid_moves2) printf(nI have to pass, yo
6、ur gon); else printf(nNeither of us can go, so the game is over.n); while(no_of_movesSIZE*SIZE&invalid_moves2); display(board); comp_score=user_score=0; for(row=0;rowSIZE;row+) for(col=0;colSIZE;col+) comp_score+=boardrowcol=; user_score+=boardrowcol=0; printf(The final score is:n); printf(Computer
7、%dn User %dnn,comp_score,user_score); fflush(stdin); printf(Do you want to play again (y/n): ); scanf(%c,&again); while(tolower(again)=y); printf(nGoodbyen); void display(char boardSIZE) int row=0; int col=0; char col_label=a; printf(n ); for(col=0;colSIZE;col+) printf( %c,col_label+col); printf(n);
8、 for(row=0;rowSIZE;row+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 8 页 - - - - - - - - - printf( +); for(col=0;colSIZE;col+) printf(-+); printf(n%2d|,row+1); for(col=0;colSIZE;col+) printf( %c |,boardrowcol); printf(n); printf( +); for(col=0;colSIZE;col+)
9、printf(-+); printf(n); int valid_moves(char boardSIZE,int movesSIZE,char player) int rowdelta=0; int coldelta=0; int row=0; int col=0; int x=0; int y=0; int no_of_moves=0; char opponent=(player=0)?:0; for(row=0;rowSIZE;row+) for(col=0;colSIZE;col+) movesrowcol=0; for(row=0;rowSIZE;row+) for(col=0;co
10、lSIZE;col+) if(boardrowcol!= ) continue; for(rowdelta=-1;rowdelta=1;rowdelta+) for(coldelta=-1;coldelta=1;coldelta+) if(row+rowdelta=SIZE| col+coldelta=SIZE| (rowdelta=0&coldelta=0) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 8 页 - - - - - - - - - continue;
11、if(boardrow+rowdeltacol+coldelta=opponent) x=row+rowdelta; y=col+coldelta; for(;) x+=rowdelta; y+=coldelta; if(x=SIZE|y=SIZE) break; if(boardxy= ) break; if(boardxy=player) movesrowcol=1; no_of_moves+; break; return no_of_moves; void make_move(char boardSIZE,int row,int col,char player) int rowdelta
12、=0; int coldelta=0; int x=0; int y=0; char opponent=(player=0)?:0; boardrowcol=player; for(rowdelta=-1;rowdelta=1;rowdelta+) for(coldelta=-1;coldelta=1;coldelta+) if(row+rowdelta=SIZE| col+coldelta=SIZE| 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 8 页 - - -
13、- - - - - - (rowdelta=0&coldelta=0) continue; if(boardrow+rowdeltacol+coldelta=opponent) x=row+rowdelta; y=col+coldelta; for(;) x+=rowdelta; y+=coldelta; if(x=SIZE|y=SIZE) break; if(boardxy= ) break; if(boardxy=player) while(boardx-=rowdeltay-=coldelta=opponent) boardxy=player; break; int get_score(
14、char boardSIZE,char player) int score=0; int row=0; int col=0; char opponent=player=0?:0; for(row=0;rowSIZE;row+) for(col=0;colSIZE;col+) score-=boardrowcol=opponent; score+=boardrowcol=player; return score; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 8 页 -
15、- - - - - - - - int best_move(char boardSIZE,int movesSIZE,char player) int row=0; int col=0; int i=0; int j=0; char opponent=player=0?:0; char new_boardSIZESIZE=0; int score=0; int new_score=0; for(row=0;rowSIZE;row+) for(col=0;colSIZE;col+) if(!movesrowcol) continue; for(i=0;iSIZE;i+) for(j=0;jSIZ
16、E;j+) new_boardij=boardij; make_move(new_board,row,col,player); new_score=get_score(new_board,player); if(scorenew_score) score=new_score; return score; void computer_move(char boardSIZE,int movesSIZE,char player) int row=0; int col=0; int best_row=0; int best_col=0; int i=0; int j=0; int new_score=
17、0; int score=100; char temp_boardSIZESIZE; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 8 页 - - - - - - - - - int temp_movesSIZESIZE; char opponent=player=0?:0; for(row=0;rowSIZE;row+) for(col=0;colSIZE;col+) if(movesrowcol=0) continue; for(i=0;iSIZE;i+) for(
18、j=0;jSIZE;j+) temp_boardij=boardij; make_move(temp_board,row,col,player); valid_moves(temp_board,temp_moves,opponent); new_score=best_move(temp_board,temp_moves,opponent); if(new_scorescore) score=new_score; best_row=row; best_col=col; make_move(board,best_row,best_col,player); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 8 页 - - - - - - - - -