Presentation is loading. Please wait.

Presentation is loading. Please wait.

TicTacToe 過三關 使用者可按 XO X char gb[3][3]; gb[0][1] gb[0][0] gb[0][2]

Similar presentations


Presentation on theme: "TicTacToe 過三關 使用者可按 XO X char gb[3][3]; gb[0][1] gb[0][0] gb[0][2]"— Presentation transcript:

1 TicTacToe 過三關 使用者可按 XO X char gb[3][3]; gb[0][1] gb[0][0] gb[0][2]
2019年2月23日 TicTacToe

2 void draw_game_board(){ int i,j; for(i=0;i<3;i++){
void init(){ int i,j; clrscr(); draw_game_board(); gotoxy(X,Y); } char gb[3][3]; for(i=0;i<3;i++) for(j=0;j<3;j++) gb[i][j]=' '; void draw_game_board(){ int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ } printf("%c", gb[i][j]); if(j<2) printf("|"); if(i<2) printf("\n-+-+-"); 2019年2月23日 TicTacToe

3 X main(){ int ch, finish=0; char gb[3][3]; init(); while (finish==0){
ch=getch(); if(ch==ESC) break; if(ch==DIR_KEY){ moves(ch); }else } char gb[3][3]; X gb[1][1]= 'X'; if(gb[Y/2][X/2]==' '){ if(ch=='O' || ch=='X'){ gb[Y/2][X/2]=ch; printf("%c\a", ch); finish=endgame(); } 2019年2月23日 TicTacToe

4 if(Y>=2) Y-=2; break; if(Y<=2) Y+=2; break;
void moves(int ch){ switch(ch){ case UP: case DOWN: case LEFT: case RIGHT: } gotoxy(X,Y); if(Y>=2) Y-=2; break; if(Y<=2) Y+=2; break; if(X>=2) X-=2; break; if(X<=2) X+=2; break; 2019年2月23日 TicTacToe

5 if(gb[i][j]==' ') count++;
打和 O勝 X勝 未完 -1 2 1 int endgame(){ int i,j,ch, count=0; for(i=0;i<3;i++) for(j=0;j<3;j++) if(gb[i][j]==' ') count++; if(count==0) return(-1); for(i=0;i<3;i++){ // check rows } if(gb[i][0]==gb[i][1] && gb[i][1]==gb[i][2]) if(gb[i][0]=='O') return 2; else return 1; 2019年2月23日 TicTacToe

6 for(j=0;j<3;j++){ // check columns
} // check diagonals if(gb[0][0]==gb[1][1]… if( return 0; // 未完 not yet finished } 2019年2月23日 TicTacToe


Download ppt "TicTacToe 過三關 使用者可按 XO X char gb[3][3]; gb[0][1] gb[0][0] gb[0][2]"

Similar presentations


Ads by Google