Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 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.     if (field[pos_y][pos_x] != 0) return;//check if field is currently empty
  7.  
  8.     field[pos_y][pos_x] = player;
  9.  
  10.     for (int i = -1; i <= 1; i++)
  11.     {
  12.         for (int j = -1; j <= 1; j++)
  13.         {
  14.             //Skip current loop if neighboring field isn't opponent
  15.             if(field[pos_y + j][pos_x+i] != opponent) continue;
  16.  
  17.             int p = 2; //Used to find maximum path
  18.  
  19.             while((pos_x + p*i) < SIZE_X && (pos_y + p*j) < SIZE_Y && (pos_y + p*j) >= 0 && (pos_x + p*i) >= 0) {
  20.                 if(field[pos_y + p*j][pos_x + p*i] == player) {
  21.                     for(int k = p-1; k > 0; k--) {
  22.                         field[pos_y + k *j][pos_x + k*i] = player;
  23.                     } break;
  24.                 } else if (field[pos_y + p*j][pos_x + p*i] == 0) break;
  25.                 p++;
  26.             }
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement