奥赛罗游戏 #include <iostream> #include <ctype.h> #include <iomanip> #define SIZE 6 using namespace std; void display(char board[][SIZE]); int valid_moves(char board[][SIZE],int moves[][SIZE],char player); void make_move(char board[][SIZE],int row,int col,char player); void computer_move(char board[][SIZE],int moves[][SIZE],char player); int best_move(char board[][SIZE],int moves[][SIZE],char player); int get_score(char board[][SIZE],char player); int main() { char board[SIZE][SIZE] ={0}; int moves[SIZE][SIZE] ={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; int x=0; char again; int player = 0; cout<<endl; cout<<"REVERSI"<<endl; cout<<"You can go first on the first game,then we will take turns."<<endl; cout<<"You will be whihe-(O)"<<endl<<"I will be black -(@)."<<endl; cout<<"Select a square for your move by typing a digit for the row"/ <<endl<<"and a letter for the column with no spaces between."<<endl; cout<<endl<<"Good Luck! Please start."<<endl; do { player = ++no_of_games %2; no_of_moves = 4; for(row =0;row<SIZE;row++) for(col=0;col<SIZE;col++) board[row][col] =' '; board[SIZE/2 -1][SIZE/2 -1] = board[SIZE/2][SIZE/2]='O'; board[SIZE/2 -1][SIZE/2] = board[SIZE/2][SIZE/2 -1] ='@'; do { display(board); if(player++ %2) { if(valid_moves(board,moves,'O')) { for(;;) { fflush(stdin); cout<<"Please enter your move(row column):"; cin>>x>>y; y = tolower(y) - 'a'; x--; if(x>=0 && y>= 0 && x<SIZE && y<SIZE && moves[x][y]) { make_move(board,x,y,'O'); no_of_moves++; break; } else { cout<<"Not a valid move ,try again!"<<endl; } } } else if(++invalid_moves<2) { fflush(stdin); cout<<endl<<"You have to pass,press return"; cin>>again; } else cout<<endl<<"Neither of us can go,so the game is over."<<endl; } else { if(valid_moves(board,moves,'@')) { invalid_moves = 0; computer_move(board,moves,'@'); no_of_moves++; } else { if(++invalid_moves < 2) { cout<<endl<<"I have to pass,your go"<<endl; } else cout<<endl<<"Neither of us can go,so the game is over."<<endl; } } }while(no_of_moves < SIZE*SIZE && invalid_moves <2); display(board); comp_score = user_score = 0; for(row =0;row <SIZE;row++) for(col =0;col<SIZE;col++) { comp_score +=board[row][col] == '@'; user_score +=board[row][col] == 'O'; } cout<<"The final scor is :"<<endl; cout<<"Computer "<<comp_score<<endl<</ "User "<<user_score<<endl<<endl; fflush(stdin); cout<<"Do you want to play again(y/n):"; cin>>again; }while(tolower(again) =='y'); cout<<endl<<"Goodbye"<<endl; return 0; } void display(char board[][SIZE]) { int row =0; int col =0; char col_label ='a'; cout<<endl; for(col=0;col<SIZE;col++) cout<<" "<<(char)(col_label +col); cout<<endl; for(row =0;row<SIZE;row++) { cout<<" +"; for(col =0;col<SIZE;col++) cout<<"---+"; cout<<endl<<setw(2)<<row + 1<<"|"; for(col =0;col<SIZE;col++) cout<<" "<<board[row][col]<<" |"; cout<<endl; } cout<<"+"; for(col =0;col<SIZE;col++) cout<<"---+"; cout<<endl; } int valid_moves(char board[][SIZE],int moves[][SIZE],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 =='O')?'@':'O'; for(row =0;row<SIZE;row++) for(col =0;col <SIZE; col++) moves[row][col] =0; for(row =0;row<SIZE;row++) for(col=0;col<SIZE;col++) { if(board[row][col] !=' ') continue; for(rowdelta =-1;rowdelta <=1;rowdelta++) for(coldelta =-1;coldelta <=1;coldelta++) { if(row+rowdelta<0||row+rowdelta>=SIZE||col +coldelta<0||col+coldelta >=SIZE||/ (rowdelta==0 && coldelta ==0)) continue; if(board[row + rowdelta][col + coldelta]== opponent) { x=row + rowdelta; y=col + coldelta; for(;;) { x+=rowdelta; y+=coldelta; if(x<0 || x>=SIZE || y<0|| y>=SIZE) break; if(board[x][y]==' ') break; if(board[x][y]==player) { moves[row][col] =1; no_of_moves++; break; } } } } } return no_of_moves; } void computer_move(char board[][SIZE],int moves[][SIZE],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 =0; int score =100; char temp_board[SIZE][SIZE]; int temp_moves[SIZE][SIZE]; char opponent=(player =='O')?'@':'O'; for(row=0;row<SIZE;row++) for(col=0;col<SIZE;col++) { if(moves[row][col]==0) continue; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) temp_board[i][j]=board[i][j]; 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_score < score) { score= new_score; best_row =row; best_col =col; } } make_move(board,best_row,best_col,player); } int get_score(char board[][SIZE],char player) { int score =0; int row =0; int col =0; char opponent =player=='O'?'@':'O'; for(row=0;row<SIZE;row++) for(col=0;col<SIZE;col++) { score-=board[row][col]==opponent; score+=board[row][col]==player; } return score; } int best_move(char board[][SIZE],int moves[][SIZE],char player) { int row=0; int col =0; int i = 0; int j = 0; char opponent = player == 'O'?'@':'O'; char new_board[SIZE][SIZE] ={0}; int score = 0; int new_score = 0; for(row =0;row<SIZE;row++) for(col=0;col<SIZE;col++) { if(!moves[row][col]) continue; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) new_board[i][j]=board[i][j]; make_move(new_board,row,col,player); new_score = get_score(new_board,player); if(score < new_score) score = new_score; } return score; } void make_move(char board[][SIZE],int row,int col,char player) { int rowdelta =0; int coldelta = 0; int x=0; int y=0; char opponent = (player == 'O')?'@':'O'; board[row][col] = player; for(rowdelta = -1;rowdelta<=1;rowdelta++) for(coldelta =-1;coldelta<=1;coldelta++) { if(row + rowdelta<0 || row + rowdelta >=SIZE||/ col+coldelta<0 || col +coldelta >=SIZE||/ (rowdelta ==0 && coldelta ==0)) continue; if(board[row+rowdelta][col+coldelta] == opponent) { x = row + rowdelta; y = col + coldelta; for(;;) { x+=rowdelta; y+=coldelta; if(x<0||x>=SIZE ||y<0||y>=SIZE) break; if(board[x][y] ==' ') break; if(board[x][y]==player) { while(board[x-=rowdelta][y-=coldelta]==opponent) board[x][y]=player; break; } } } } }