neighbors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
neighbors [2025/05/27 10:53] – frchris | neighbors [2025/05/27 10:59] (current) – frchris | ||
---|---|---|---|
Line 3: | Line 3: | ||
This game from Math Games with Bad Drawings can be played with 1 to 30 people. | This game from Math Games with Bad Drawings can be played with 1 to 30 people. | ||
+ | <code java Neighbors.java> | ||
+ | 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 += " | ||
+ | result += boards[i].toString()+" | ||
+ | |||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | public static int getInt(Scanner kb, String prompt){ | ||
+ | System.out.print(prompt); | ||
+ | String num = kb.nextLine(); | ||
+ | int result = " | ||
+ | if (result <0) { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | 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 +" | ||
+ | System.out.println( g2 +" | ||
+ | System.out.println( g3 +" | ||
+ | } | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | Scanner kb = new Scanner(System.in); | ||
+ | System.out.print(" | ||
+ | String line = kb.nextLine(); | ||
+ | int n = Integer.parseInt(line); | ||
+ | Neighbors game = new Neighbors(n); | ||
+ | // | ||
+ | for (int i = 1; i <= 25; i++){ | ||
+ | int roll = 1 + (int)(10*Math.random()); | ||
+ | System.out.println(" | ||
+ | for (int player = 0; player < n; player++){ | ||
+ | System.out.println(game); | ||
+ | boolean okay; | ||
+ | do { | ||
+ | String prompt = " | ||
+ | int row = getInt(kb, prompt); | ||
+ | prompt = " | ||
+ | int col = getInt(kb, prompt); | ||
+ | okay = game.get(player).set(roll, | ||
+ | } while (!okay); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | System.out.println(game); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java Grid.java> | ||
+ | |||
+ | /** | ||
+ | * 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(" | ||
+ | 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); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | public String toString() | ||
+ | { | ||
+ | String result=" | ||
+ | for (int[] row: grid){ | ||
+ | for(int cell: row) | ||
+ | { | ||
+ | if(cell< | ||
+ | | ||
+ | result += + cell +" |"; | ||
+ | } | ||
+ | result += " | ||
+ | } | ||
+ | int last = result.length()-2; | ||
+ | return result.substring(0, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
Back to [[New Labs]] | Back to [[New Labs]] |
neighbors.txt · Last modified: 2025/05/27 10:59 by frchris