| dorainm's profiledorainm's spaceBlogLists | Help |
|
April 25 a game#include "stdio.h" #include "malloc.h" #define SIZE 3 #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endif #define NONE 0 #define PLAYER_A 1 #define PLAYER_B 2 #define WARNNING 255 #define COMPETITOR 200 #define WINNER -1 char chessboard[SIZE][SIZE]; struct CHESS_MAN { int row; int col; }; /*get the value of current chess board: count and retrun how many ways the player can win the game*/ int get_value(int player) { int i,j,ret=0; int row,col,inc; int bNONE=FALSE; /*check the row*/ for(i=0;i<SIZE;i++) { row=SIZE; bNONE=FALSE; for(j=0;j<SIZE;j++) { /*if there is a competitor's chess man at the location sub row*/ if(chessboard[i][j]==player) row--; /*if there is any empty location in the row, set bNONE as TRUE*/ if(chessboard[i][j]==NONE) bNONE=TRUE; } /*computer : one empty and others are competitor's chess man, oh my god, danger, you may lose the game*/ if(row==1&&bNONE==TRUE) return WARNNING; /*computer : no competitor's chess man in the row, there is one way to make me win the game*/ else if(row==SIZE) ret++; } /*check the col*/ for(i=0;i<SIZE;i++) { col=SIZE; bNONE=FALSE; for(j=0;j<SIZE;j++) { if(chessboard[j][i]==player) col--; if(chessboard[j][i]==NONE) bNONE=TRUE; } /*computer : warnning : the competitor may be win the game*/ if(col==1&&bNONE==TRUE) return WARNNING; /*computer : this is my chance.*/ else if(col==SIZE) ret++; } /*check inc*/ inc=SIZE; bNONE=FALSE; for(i=0,j=0;i<SIZE;i++,j++) { if(chessboard[i][j]==player) inc--; if(chessboard[i][j]==NONE) bNONE=TRUE; } /*computer : i won't lose the game*/ if(inc==1&&bNONE==TRUE) return WARNNING; /*my chance?*/ else if(inc==SIZE) ret++; /*check inc*/ inc=SIZE; bNONE=FALSE; for(i=0,j=SIZE-1;i<SIZE;i++,j--) { if(chessboard[i][j]==player) inc--; if(chessboard[i][j]==NONE) bNONE=TRUE; } /*be careful*/ if(inc==1&&bNONE==TRUE) return WARNNING; /*another chance*/ else if(inc==SIZE) ret++; return ret; }; /*display the chess board*/ void disp_chess_board(void) { int i,j; /*print the head*/ for(i=0;i<SIZE*4+1;i++) printf("-"); printf("\n"); /*print the contect*/ for(i=0;i<SIZE;i++) { printf("|"); for(j=0;j<SIZE;j++) { if(chessboard[i][j]==PLAYER_A) printf(" o |"); else if(chessboard[i][j]==PLAYER_B) printf(" x |"); else printf(" |"); } printf("\n"); /*print the floor*/ for(j=0;j<SIZE*4+1;j++) printf("-"); printf("\n"); } return; }; /*init the chess board*/ void init_chess_board(void) { int i,j; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) chessboard[i][j]=NONE; return; }; int enter_chess_man(int row, int col, int player) { /*out of size*/ if(row>=SIZE||col>=SIZE) return FALSE; /*the pionted location is not empty*/ if(chessboard[row][col]!=NONE) return FALSE; /*okay, put down the chess man*/ chessboard[row][col]=player; return TRUE; }; /*check whetch the player win the game*/ int chk_winner(int player) { int i,j; int col,row,inc; /*are there all the player's chess men in the same row*/ for(i=0;i<SIZE;i++) { row=TRUE; for(j=0;j<SIZE;j++) { if(chessboard[i][j]!=player) row=FALSE; } if(row==TRUE) return TRUE; } /*are there all the player's chess men in the same col*/ for(i=0;i<SIZE;i++) { col=FALSE; for(j=0;j<SIZE;j++) { if(chessboard[j][i]!=player) col=FALSE; } if(col==TRUE) return TRUE; } /*what about the inc*/ inc=TRUE; j=0; for(i=0;i<SIZE;i++) if(chessboard[i][i+j]!=player) inc=FALSE; if(inc==TRUE) return TRUE; /*and this?*/ inc=TRUE; j=SIZE-1; for(i=0;i<SIZE;i++) if(chessboard[i][j-i]!=player) inc=FALSE; if(inc==TRUE) return TRUE; /*sorry, the player has not won yet.*/ return FALSE; }; /*get the best chess man for player*/ int get_best_chess(struct CHESS_MAN *best_chess, int player, int other) { int tat_num=SIZE*SIZE; int chess_value[tat_num]; struct CHESS_MAN chess[tat_num]; int i,j,cur=0; /*init chess[]*/ for(i=0;i<SIZE;i++) { for(j=0;j<SIZE;j++) { chess[cur].row=i; chess[cur++].col=j; } } /*when i take one of the chess man, what's the chess_value of my competitor i will choose the min value, because it means that's the worst case for him*/ for(i=0;i<tat_num;i++) { /*i try to take this chess_man*/ if(enter_chess_man(chess[i].row,chess[i].col,player)==TRUE) { chess_value[i]=get_value(other); /**/ if(chk_winner(player)==TRUE) chess_value[i]=WINNER; chessboard[chess[i].row][chess[i].col]=NONE; } else /*can not take, means that chess_board has layed my cpmpetitor's chess_man*/ chess_value[i]=COMPETITOR; } /*choose the lowest chess_value*/ cur=0; for(i=0;i<tat_num;i++) { if(chess_value[cur]>chess_value[i]) cur=i; } /*my best is my competitor's worst*/ best_chess->row=chess[cur].row; best_chess->col=chess[cur].col; return chess_value[cur]; }; int chk_full(void) { int i,j; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) if(chessboard[i][j]==NONE) return FALSE; return TRUE; }; int main(void) { int i; struct CHESS_MAN best_chess; int player=PLAYER_A; int competitor=PLAYER_B; int bEND=FALSE; /*whetch need end of the program*/ int row,col; /*user's input location*/ //best_chess=(struct CHESS_MAN*)malloc(sizeof(struct CHESS_MAN)); init_chess_board(); disp_chess_board(); while(bEND==FALSE) { if(player==PLAYER_A) { /*user's turn*/ do { printf("] Input your chess location : \n"); printf("] location > row : "); scanf("%d",&row); printf("] location > col : "); scanf("%d",&col); if(enter_chess_man(row-1,col-1,player)==TRUE) { printf("] You have take chess man at [%d][%d]\n",row,col); break; } else printf("] Error : You put the chess to a wrong location\n"); }while(TRUE); } else { /*computer says : it is my turn.*/ get_best_chess(&best_chess,player,competitor); enter_chess_man(best_chess.row,best_chess.col,player); printf("] Player %d put chess at [%d][%d]\n",player,best_chess.row+1,best_chess.col+1); } /*display the current chess board*/ disp_chess_board(); /*anybody win?!*/ bEND=TRUE; if(chk_winner(player)) printf("] Player %d Win the Game.\n",player); else if(chk_winner(competitor)) printf("] Player %d Win the Game.\n",competitor); else if(chk_full()) printf("] No One Win the Game.\n"); else bEND=FALSE; /*change the turn of the players*/ competitor=player; if(player==PLAYER_A) player=PLAYER_B; else player=PLAYER_A; }; printf("\n\nthe command completed successfully.\n\n"); return 0; }; TrackbacksWeblogs that reference this entry
|
|
|