// Arup Guha
// 4/15/2019
// Solution to 2013 AP Comp Sci A FR Question 2: Token Pass

import java.util.*;

public class TokenPass {
	
	final public static int MAX = 10;
	public static Random r = new Random();
	
	private int[] board;
	private int currentPlayer;
	
	/*** Solution to Part A ***/
	public TokenPass(int playerCount) {
		board = new int[playerCount];
		
		// The AP People would probably do board[i] = (int)(1 + Math.random()*10);
		for (int i=0; i<playerCount; i++)
			board[i] = r.nextInt(MAX) + 1;
		
		// Same idea here...
		currentPlayer = r.nextInt(playerCount);
	}
	
	/*** Solution to Part B ***/
	public void distributePlayerTokens() {
		
		// Pick up tokens.
		int numTokens = board[currentPlayer];
		board[currentPlayer] = 0;
		
		// Set idx to the first player who will receive a token.
		int idx = (currentPlayer+1)%board.length;
		
		// Distribute the tokens, advancing the next player appropriately.
		for (int i=0; i<numTokens; i++) {
			board[idx]++;
			idx = (idx+1)%board.length;
		}
	}
	
	public void advanceTurn() {
		currentPlayer = (currentPlayer+1)%board.length;
	}
	
	public String toString() {
		String res = "[";
		for (int i=0; i<board.length; i++) {
			res = res + board[i];
			if (i<board.length-1) res = res + ", ";
		}
		return res +"]"+" current turn is "+currentPlayer;
	}
	
	public static void main(String[] args) {
		
		// Just play four turns...
		TokenPass tp = new TokenPass(4);
		System.out.println(tp);
		for (int i=0; i<4; i++) {
			tp.distributePlayerTokens();
			tp.advanceTurn();
			System.out.println(tp);
		}
	}

}