import java.util.Scanner; /** * Write a description of class Neighbors here. * * @author (your name) * @version (a version number or a date) */ public class Neighbors { private Grid[] boards; private int n; public Neighbors(int n){ this.n = n; boards = new Grid[n]; for (int i = 0; i < n; i++){ boards[i] = new Grid(); } } public Grid get(int i){ return boards[i]; } public String toString(){ String result = ""; for (int i = 0; i< n; i++){ result += "\nPlayer "+(1+i)+":\n"; result += boards[i].toString()+"\n"; } return result; } public static int getInt(Scanner kb, String prompt){ System.out.print(prompt); String num = kb.nextLine(); int result = "012345".indexOf(num); if (result <0) { System.out.println("Type a number from 1 to 5"); return getInt(kb, prompt); } return result; } public static void testScoring(){ int[][] arr1 = {{8, 8, 4, 4, 3}, {8, 8, 1, 4, 3}, {6, 7,10, 7, 3}, {6, 6, 5, 7, 7}, {2, 2, 5, 9, 9}};//85 Grid g1 = new Grid(arr1); int[][] arr2 ={ {2, 2, 3, 3, 7}, {8, 8, 3, 7, 7}, {8, 8,10, 7, 9}, {4, 5, 6, 6, 5}, {4, 4, 6, 1, 5}};//100 Grid g2 = new Grid(arr2); int[][] arr3 ={ {6, 8, 8, 1, 7}, {6, 6, 8, 8, 7}, {5,10, 3, 9, 7}, {5, 4, 4, 9, 7}, {2, 2, 4, 3, 3}};//92 Grid g3 = new Grid(arr3); System.out.println( g1 +"\nscore:" + g1.score() + "(Should be 173)"); System.out.println( g2 +"\nscore:" + g2.score() + "(Should be 172)"); System.out.println( g3 +"\nscore:" + g3.score() + "(Should be 154)"); } public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("How many players? "); String line = kb.nextLine(); int n = Integer.parseInt(line); Neighbors game = new Neighbors(n); //System.out.println(game); for (int i = 1; i <= 25; i++){ int roll = 1 + (int)(10*Math.random()); System.out.println("Random Number "+i+" is "+roll); for (int player = 0; player < n; player++){ System.out.println(game); boolean okay; do { String prompt = "Player "+(player+1)+", choose a row (1 to 5) to place "+roll+":"; int row = getInt(kb, prompt); prompt = "Player "+(player+1)+", choose a column (1 to 5):"; int col = getInt(kb, prompt); okay = game.get(player).set(roll, row, col); } while (!okay); } } System.out.println(game); } }