Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. public void generateScore(Gameboard g, Player p) {
  2.         String temp [][] = g.getGameArea();
  3.         int scoreCounter = 0;
  4.         int highestScore = 0;
  5.  
  6.    // horisontal score check
  7.         for(int i = 0; i < g.getSize(); i++) {
  8.             scoreCounter = 0;
  9.             for (int j = 0; j < g.getSize(); j++) {
  10.                 if (temp[i][j].equals(p.getIcon())) {
  11.                     scoreCounter++;
  12.                 } else {
  13.                     scoreCounter = 0;
  14.                 }
  15.                 if (scoreCounter > highestScore) {
  16.                     highestScore = scoreCounter;
  17.                 }
  18.             }
  19.         }
  20.  
  21.         // vertical score check
  22.         for(int i = 0; i < g.getSize(); i++) {
  23.             scoreCounter = 0;
  24.             for (int j = 0; j < g.getSize(); j++) {
  25.                 if (temp[j][i].equals(p.getIcon())) {
  26.                     scoreCounter++;
  27.                 } else {
  28.                     scoreCounter = 0;
  29.                 }
  30.                 if (scoreCounter > highestScore) {
  31.                     highestScore = scoreCounter;
  32.                 }
  33.             }
  34.         }
  35.  
  36.         int indexI, indexJ;
  37.         // diagonal score from upperrow to downright
  38.         for (int j = 0; j < g.getSize(); j++) {
  39.             indexI = 0;
  40.             indexJ = j;
  41.             scoreCounter = 0;
  42.             for (int k = j; k < g.getSize(); k++ ) {
  43.                 if (temp[indexI][indexJ].equals(p.getIcon())) {
  44.                     scoreCounter++;
  45.                 } else {
  46.                     scoreCounter = 0;
  47.                 }
  48.                 if (scoreCounter > highestScore) {
  49.                     highestScore = scoreCounter;
  50.                 }
  51.                 indexI++;
  52.                 indexJ++;
  53.             }  
  54.         }
  55.  
  56.         // diagonal score from leftrow to downright
  57.         for (int i = 0; i < g.getSize(); i++) {
  58.             indexI = i;
  59.             indexJ = 0;
  60.             scoreCounter = 0;
  61.             for (int k = i; k < g.getSize(); k++ ) {
  62.                 if (temp[indexI][indexJ].equals(p.getIcon())) {
  63.                     scoreCounter++;
  64.                 } else {
  65.                     scoreCounter = 0;
  66.                 }
  67.                 if (scoreCounter > highestScore) {
  68.                     highestScore = scoreCounter;
  69.                 }
  70.                 indexI++;
  71.                 indexJ++;
  72.             }  
  73.         }
  74.  
  75.         // diagonal score from downrow to upright
  76.         for (int j = 0; j < g.getSize(); j++) {
  77.             indexI = g.getSize()-1;
  78.             indexJ = j;
  79.             scoreCounter = 0;
  80.             for (int k = j; k < g.getSize(); k++ ) {
  81.                 if (temp[indexI][indexJ].equals(p.getIcon())) {
  82.                     scoreCounter++;
  83.                 } else {
  84.                     scoreCounter = 0;
  85.                 }
  86.                 if (scoreCounter > highestScore) {
  87.                     highestScore = scoreCounter;
  88.                 }
  89.                 indexI--;
  90.                 indexJ++;
  91.             }  
  92.         }
  93.  
  94.        // diagonal score from leftrow to upright
  95.         for (int i = g.getSize()-1; i >= 0; i--) {
  96.             indexI = i;
  97.             indexJ = 0;;
  98.             scoreCounter = 0;
  99.         for (int k = i; k >= 0; k-- ) {
  100.             if (temp[indexI][indexJ].equals(p.getIcon())) {
  101.                 scoreCounter++;
  102.             } else {
  103.                 scoreCounter = 0;
  104.             }
  105.             if (scoreCounter > highestScore) {
  106.                 highestScore = scoreCounter;
  107.             }
  108.             indexI--;
  109.             indexJ++;
  110.         }  
  111.     }
  112.     // highest found score will be set to player score
  113.     p.setScore(highestScore);
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement