/** * Write a description of class Grid here. * * @author * @version (a version number or a date) */ public class Grid { // instance variables - replace the example below with your own private int[][] grid; /** * Constructor for objects of class Grid */ public Grid() { grid = new int[5][5]; } public Grid(int[][] arr){ grid = arr; } public boolean set(int number, int row, int col){ if (grid[row-1][col-1] > 0) return false; grid[row-1][col-1] = number; return true; } public int score(){ int total = 0; for (int[] row: grid){ int b = 0; int e; do { e = b+1; int run = 1; int value = row[b]; while ( e < 5 && row[b]==row[e]){ run++; e++; } if (run >1) total += run*row[b]; b =e ; }while (e < 5); } //columns System.out.println(" row total:"+total); for(int col =0; col < 5 ; col++){ int b = 0; int e; do { e = b+ 1; int run =1; int value = grid[b][col]; while ( e < 5 && grid[b][col] == grid[e][col]){ run++; e++; } if (run >1 ) total += run*grid[b][col]; b = e; }while (e<5); } return total; } public String toString() { String result="|"; for (int[] row: grid){ for(int cell: row) { if(cell<10) result += " "; result += + cell +" |"; } result += "\n|"; } int last = result.length()-2; return result.substring(0, last)+ "Score: "+score(); } }