====== Neighbors ======
This game from Math Games with Bad Drawings can be played with 1 to 30 people. Everyone has an empty 5 by 5 grid. A random number from 1 to 10 is chosen, and everyone must place it into their grid. After the grid is filled a score is computed. On each row, circle any groups with 2 or more of the same number, and add these circled numbers. Do the same for each column. The score is the sum of these numbers.
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);
}
}
/**
* 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();
}
}
Back to [[New Labs]]