Advertisement
mikhail_dvorkin

NimModel

Feb 27th, 2017
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. package me.dvorkin;
  2.  
  3. import java.util.Random;
  4.  
  5. public class NimModel {
  6.     private int[] piles;
  7.     private boolean move;
  8.    
  9.     public NimModel() {
  10.         move = true;
  11.         Random random = new Random();
  12.         piles = new int[5];
  13.         for (int i = 0; i < piles.length; i++) {
  14.             piles[i] = random.nextInt(10) + 1;
  15.         }
  16.     }
  17.    
  18.     public int[] getField() {
  19.         return piles.clone();
  20.     }
  21.    
  22.     /**
  23.      * @return true if first player is to move
  24.      */
  25.     public boolean whoMoves() {
  26.         return move;
  27.     }
  28.    
  29.     public boolean isOver() {
  30.         for (int i : piles) {
  31.             if (i != 0) {
  32.                 return false;
  33.             }
  34.         }
  35.         return true;
  36.     }
  37.    
  38.     public boolean update(int index, int amount) {
  39.         if (index < 0 || index >= piles.length) {
  40.             return false;
  41.         }
  42.         if (amount <= 0 || amount > piles[index]) {
  43.             return false;
  44.         }
  45.         piles[index] -= amount;
  46.         move ^= true;
  47.         return true;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement