Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void generateScore(Gameboard g, Player p) {
- String temp [][] = g.getGameArea();
- int scoreCounter = 0;
- int highestScore = 0;
- // horisontal score check
- for(int i = 0; i < g.getSize(); i++) {
- scoreCounter = 0;
- for (int j = 0; j < g.getSize(); j++) {
- if (temp[i][j].equals(p.getIcon())) {
- scoreCounter++;
- } else {
- scoreCounter = 0;
- }
- if (scoreCounter > highestScore) {
- highestScore = scoreCounter;
- }
- }
- }
- // vertical score check
- for(int i = 0; i < g.getSize(); i++) {
- scoreCounter = 0;
- for (int j = 0; j < g.getSize(); j++) {
- if (temp[j][i].equals(p.getIcon())) {
- scoreCounter++;
- } else {
- scoreCounter = 0;
- }
- if (scoreCounter > highestScore) {
- highestScore = scoreCounter;
- }
- }
- }
- int indexI, indexJ;
- // diagonal score from upperrow to downright
- for (int j = 0; j < g.getSize(); j++) {
- indexI = 0;
- indexJ = j;
- scoreCounter = 0;
- for (int k = j; k < g.getSize(); k++ ) {
- if (temp[indexI][indexJ].equals(p.getIcon())) {
- scoreCounter++;
- } else {
- scoreCounter = 0;
- }
- if (scoreCounter > highestScore) {
- highestScore = scoreCounter;
- }
- indexI++;
- indexJ++;
- }
- }
- // diagonal score from leftrow to downright
- for (int i = 0; i < g.getSize(); i++) {
- indexI = i;
- indexJ = 0;
- scoreCounter = 0;
- for (int k = i; k < g.getSize(); k++ ) {
- if (temp[indexI][indexJ].equals(p.getIcon())) {
- scoreCounter++;
- } else {
- scoreCounter = 0;
- }
- if (scoreCounter > highestScore) {
- highestScore = scoreCounter;
- }
- indexI++;
- indexJ++;
- }
- }
- // diagonal score from downrow to upright
- for (int j = 0; j < g.getSize(); j++) {
- indexI = g.getSize()-1;
- indexJ = j;
- scoreCounter = 0;
- for (int k = j; k < g.getSize(); k++ ) {
- if (temp[indexI][indexJ].equals(p.getIcon())) {
- scoreCounter++;
- } else {
- scoreCounter = 0;
- }
- if (scoreCounter > highestScore) {
- highestScore = scoreCounter;
- }
- indexI--;
- indexJ++;
- }
- }
- // diagonal score from leftrow to upright
- for (int i = g.getSize()-1; i >= 0; i--) {
- indexI = i;
- indexJ = 0;;
- scoreCounter = 0;
- for (int k = i; k >= 0; k-- ) {
- if (temp[indexI][indexJ].equals(p.getIcon())) {
- scoreCounter++;
- } else {
- scoreCounter = 0;
- }
- if (scoreCounter > highestScore) {
- highestScore = scoreCounter;
- }
- indexI--;
- indexJ++;
- }
- }
- // highest found score will be set to player score
- p.setScore(highestScore);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement