Advertisement
TheRightGuy

Right Diagonal Problem

Feb 4th, 2022
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. private boolean hasWonDiagonally(Player userCharacter) {
  2. /*
  3.         0 1 2
  4.         3 4 5
  5.         6 7 8  0 4 8 from left to right 2 4 6 from right to left
  6.          */
  7.         // if both increase at the same time we can check the LTR properly
  8.         int leftToRightDiagonalCount = 0;
  9.         int rightToLeftDiagonalCount = 0;
  10.         for (int i = 0; i < COLS; i++) {
  11.             int tileIndex = translateIndex(i, i);
  12.             if (grid[tileIndex] == userCharacter) {
  13.                 leftToRightDiagonalCount++;
  14.                 if (leftToRightDiagonalCount == 3 ){
  15.                     return true;
  16.                 }
  17.             }
  18.         }
  19.         // row * 3 + col 0 * 3 + 2, 0 * 3 + 1
  20.         for (int j = 0; j < ROWS; j++) { // from the max lowering to the minimal
  21.             for (int i = COLS - 1; i >= 0; i--) {
  22.                 int tileIndex = translateIndex(j, i);
  23.                 if (grid[tileIndex] == userCharacter) {
  24.                     rightToLeftDiagonalCount++;
  25.                     if (rightToLeftDiagonalCount == 3 ){
  26.                         return true;
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.         return false;
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement