Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private boolean hasWonDiagonally(Player userCharacter) {
- /*
- 0 1 2
- 3 4 5
- 6 7 8 0 4 8 from left to right 2 4 6 from right to left
- */
- // if both increase at the same time we can check the LTR properly
- int leftToRightDiagonalCount = 0;
- int rightToLeftDiagonalCount = 0;
- for (int i = 0; i < COLS; i++) {
- int tileIndex = translateIndex(i, i);
- if (grid[tileIndex] == userCharacter) {
- leftToRightDiagonalCount++;
- if (leftToRightDiagonalCount == 3 ){
- return true;
- }
- }
- }
- // row * 3 + col 0 * 3 + 2, 0 * 3 + 1
- for (int j = 0; j < ROWS; j++) { // from the max lowering to the minimal
- for (int i = COLS - 1; i >= 0; i--) {
- int tileIndex = translateIndex(j, i);
- if (grid[tileIndex] == userCharacter) {
- rightToLeftDiagonalCount++;
- if (rightToLeftDiagonalCount == 3 ){
- return true;
- }
- }
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement