Advertisement
framp

Othello Challenge

Apr 18th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. const int a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,black=1,white=2,suggestion=10;
  5. const string space = "\t  ";
  6. //Othello challenge
  7. //Copyright: Federico Rampazzo, 2011
  8.  
  9. int followLine(int grid[9][9], int i, int j, int vi, int vj,
  10.                int color, int replace, bool target=true,
  11.                int limit=0, int captured=0){
  12.  
  13.   //follow a line of color pieces using the vectors vi e vj starting from i,j
  14.   //target==true: place a replace piece as soon as it founds a limit piece
  15.   //target==false: place replace pieces along the line until it founds a limit piece
  16.   //captured holds the number of captured pieces
  17.  
  18.   if (i>=1 && i<=8 && j>=1 && j<=8){
  19.     if (grid[i][j]==color){
  20.       int result = followLine(grid, i+vi, j+vj, vi, vj, color, replace, target, limit, captured+1);
  21.       if (!target && result) grid[i][j] = replace;
  22.       return result;
  23.     }else if(grid[i][j]==limit && captured>0){
  24.       if (target) grid[i][j] = replace;
  25.       return 1;
  26.     }else{
  27.       return 0;
  28.     }
  29.   }
  30.   return 0;
  31. }
  32.  
  33. int showValidMoves(int grid[9][9], int color){ //display suggestions
  34.   int opposite = ((color==black) ? white : black);
  35.   int moves = 0;
  36.   for (int i=1; i<=8; i++)
  37.     for (int j=1; j<=8; j++)
  38.       if (grid[i][j]==color)
  39.         for (int k=-1; k<=1; k++)
  40.           for (int h=-1; h<=1; h++)
  41.             moves += followLine(grid, i+k, j+h, k, h, opposite, suggestion);
  42.   return moves;
  43. }
  44.  
  45. void cleanSuggestions(int grid[9][9]){ //clean the grid
  46.   for (int i=1; i<=8; i++){
  47.     for (int j=1; j<=8; j++){
  48.       if (grid[i][j]==suggestion)
  49.         grid[i][j] = 0;
  50.     }
  51.   }
  52. }
  53.  
  54. int getWinner(int grid[9][9], int &blacks, int &whites){ //calculate the winner
  55.   for (int i=1; i<=8; i++){
  56.     for (int j=1; j<=8; j++){
  57.       if (grid[i][j]==black)
  58.         blacks++;
  59.       else if (grid[i][j]==white)
  60.         whites++;
  61.     }
  62.   }
  63.   if (blacks>whites)
  64.     return black;
  65.   else if (blacks<whites)
  66.     return white;
  67.   else
  68.     return 0;
  69. }
  70.  
  71. void printGrid(int grid[9][9]){ //print the grid
  72.   cout << space;
  73.   cout << "  a b c d e f g h\n";
  74.   for (int i=1; i<=8; i++){
  75.     cout << space;
  76.     cout << i << " ";
  77.     for (int j=1; j<=8; j++){
  78.       cout << ((grid[i][j]==white) ? "\u25CF" :
  79.                 ((grid[i][j]==black) ? "\u25CB" :
  80.                   ((grid[i][j]==suggestion) ? "\u00B7" :
  81.                   " "))) << " ";
  82.     }
  83.     cout << i << "\n";
  84.   }
  85.   cout << space;
  86.   cout << "  a b c d e f g h\n";
  87. }
  88.  
  89. int main(){
  90.   int grid[9][9]={};
  91.  
  92.   grid[4][d] = grid[5][e] = white;
  93.   grid[5][d] = grid[4][e] = black;
  94.   int color = black; //black is first
  95.  
  96.   int consecutive = 0;
  97.   bool legal = showValidMoves(grid, color)>0;
  98.  
  99.   while (legal || consecutive<2){ //2 stalls make the game end
  100.     string player = ((color==black) ? "BLACK" : "WHITE");
  101.     int opposite = ((color==black) ? white : black);
  102.    
  103.     printGrid(grid);
  104.    
  105.     if (!legal){
  106.       consecutive++;
  107.       cout << space << "No valid moves for " << player << "\n";
  108.     }else{ //user input
  109.       consecutive = 0;
  110.       bool enteredMove = false;
  111.       while (!enteredMove){
  112.         cout << space << "It's " << player << "'s turn: ";
  113.         string move; getline(cin, move);
  114.         int i = (int) move[1]-48, j = (int) tolower(move[0])-96;
  115.         if (grid[i][j]==suggestion){
  116.           enteredMove = true;
  117.           grid[i][j] = color;
  118.           for (int k=-1; k<=1; k++)
  119.             for (int h=-1; h<=1; h++)
  120.               followLine(grid, i+k, j+h, k, h, opposite, color, false, color);
  121.         }else{
  122.           cout << space << "Unvalid move\n";
  123.         }
  124.       }
  125.       cleanSuggestions(grid);
  126.     }
  127.    
  128.     color = opposite;
  129.     legal = showValidMoves(grid, color)>0;
  130.   }
  131.   //game over
  132.   int blacks = 0, whites = 0;
  133.   int winner = getWinner(grid, blacks, whites);
  134.   cout << space;
  135.   cout << ((winner==black) ? "BLACK" :
  136.           ((winner==white) ? "WHITE" :
  137.             "NOBODY")) << " wins! " <<
  138.             blacks << "-" << whites << "\n";
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement