Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. void execute_turn(int field[SIZE_Y][SIZE_X], const int player, const int pos_x, const int pos_y)
  2. {
  3.     // Process all possible directions
  4.     int opponent = 3 - player; // the same as: if player = 1 -> opponent = 2 else
  5.                                // if player = 2 -> opponent = 1
  6.  
  7.     if (field[pos_y][pos_x] != 0) //check if field is currently empty
  8.     {
  9.         return;
  10.     }
  11.  
  12.  
  13.     for (int i = -1; i <= 1; i++)
  14.     {
  15.         for (int j = -1; j <= 1; j++)
  16.         {
  17.  
  18.             //Skip current loop if neighboring field isn't opponent
  19.             if(field[pos_y + j][pos_x+i] == opponent) {
  20.  
  21.             int p = 2; //Used to find maximum path
  22.  
  23.             while((pos_x + p*i) < SIZE_X && (pos_y + p*j) < SIZE_Y && (pos_y + p*j) >= 0 && (pos_x + p*i) >= 0) {
  24.                 if(field[pos_y + p*j][pos_x + p*i] == player) {
  25.                     while(field[pos_y + p*j][pos_x + p*i]) {
  26.                         for(int k = p-1; k > 0; k--) {
  27.                             field[pos_y + k *j][pos_x + k*i] = player;
  28.                         }
  29.                     } break;
  30.                 } else if (field[pos_y + j][pos_x + i] == 0) break;
  31.                     break;
  32.                 p++;
  33.                 }
  34.             }
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement