// Juliana Wu
// Holmdel High
School
// Contest 3
2010-2011
// Intermediate 3
Division
import
java.util.*;
class Mancala
{
private static int[] bowls =
{0,4,4,4,4,4,4,4,4,4,4,4,4,0};
//[0] represents B's mancala, [13]
represents A's mancala
public static void main (String[] args)
{
String A = "A";
String B = "B";
System.out.println("There will
be five lines of input. In each line, enter a one-character string (L or R), a
positive integer (1-12), and a second one-character string (1-12 or A or
B).");
Scanner kb = new
Scanner(System.in).useDelimiter("\\s*[,\\n]\\s*");
String AorB;
int[] result = {0,0,0,0,0};
for(int state = 0; state < 5;
state++)
{
String n = kb.next();
if (state % 2 == 0)
{
AorB = A;
}
else
{
AorB = B;
}
int[] array = new int[2];
for (int i = 0; i<2; i++)
{
String temp = kb.next();
if (temp.equals(A))
{
array[i] = 13;
}
else if (temp.equals(B))
{
array[i] = 0;
}
else
{
array[i] =
Integer.valueOf(temp);
}
}
result[state] = playGame (AorB, n,
array[0], array[1]);
}
for (int i = 0; i <
result.length; i++)
{
System.out.println(i+1 +
": " + result[i]);
}
}
public static int playGame (String AorB,
String RorL, int input, int printTarget)
{
int StoneStart = bowls[input];
int StoneLeft = StoneStart;
boolean isA = true;
//if moving right, the direction is
1, otherwise -1
int direction = 1;
if
(RorL.equalsIgnoreCase("L"))
{
direction = -1;
}
if
(AorB.equalsIgnoreCase("A"))
isA = true;
else
isA = false;
bowls[input] = 0;
{
int newinput = StoneStart;
int position = input;
for (int i = 1; i <=
newinput; i++)
{
if (
(isA
&& StoneLeft >= 1 && (direction > 0) && (position
== 6)) ||
(isA
&& StoneLeft >=1 && (direction < 0) && (position
== 7)) ||
((!isA)
&& StoneLeft >= 1 && (direction > 0) && (position
== 12))||
((!isA)
&& StoneLeft >= 1 && (direction < 0) && (position
== 1))
)
{
// handle the
position in the range 1-->12, cannot out of the bounds
position =
input+direction*i;
if (position > 12 )
position %=
12;
else if (position
< 1)
position +=
12;
if (StoneLeft >
1) {
bowls[position]++;
StoneLeft--;
}
if ( isA)
bowls[13]++;
else
bowls[0]++;
StoneLeft--;
newinput--;
}
else
{
// handle the
position in the range 1-->12, cannot out of the bounds
position =
input+direction*i;
if (position > 12 )
position %=
12;
else if (position
< 1)
position +=
12;
bowls[position]++;
StoneLeft--;
}
}
}
return bowls[printTarget];
}
}