/////////////////////////////////

// Written in ActionScript     //

// 2.0 by Michael Lin          //

// NOTES: input, output, and   //

// clearBtn are all dynamic    //

// components. Any properties, //

// methods, or events held     //

// within their class is built //

// into Adobe Flash CS4.       //

/////////////////////////////////

 

 

var holes:Array; // Creates an array for the holes

var mancalaA:Number = 0; // Holds the number of stones in player A's mancala

var mancalaB:Number = 0; // Holds the number of stones in player B's mancala

var turn:Number = 0; // Holds the number of turns so far in a game

var numGames:Number = 0; // Holds the number of games so far

function initGame():Void{

      holes = new Array(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4); // Resets holes

      mancalaA = 0; // Resets mancalaA

      mancalaB = 0; // Resets mancalaB

      turn = 0; // Resets turn

      numGames++; // Increments numGames by 1

      output.text += "GAME " + numGames + "\n"; // Prints the game number

}

function parse(input_txt:String):Array{

      var input1:String = input_txt.split(" ").join(""); // Gets rid of spaces

      var input2:Array = input1.split(","); // Splits the string into an array divided by the commas

      return input2;

}

function move(hole:Number, dir:String):Void{

      var sideA:Boolean = turn / 2 == Math.floor(turn / 2); // If the turn number is odd, sideA is true; if it's even, sideA is false

      var stonesLeft:Number = holes[hole]; // The number of stones remaining as we drop them

      var origStonesLeft:Number = stonesLeft; // The number of stones that were originally in the hole

      var lastHole:Number = hole; // The last hole that we have gone through

      holes[hole] = 0; // The number of stones in this hole is 0

      for (var i:Number = 0; i < origStonesLeft + 1; i++){ // While we still have enough stones to continue moving them

            if (stonesLeft > 0){ // Placement of a stone into a mancala results in an offset in stonesLeft

                  if (dir == "R"){ // If we are going to the right

                        var currentHole:Number = hole + i; // The current hole is the hole that we started with PLUS the number of holes we've gone through so far

                  } else { // If we are going to the left

                        var currentHole:Number = hole - i; // The current hole is the hole that we started with MINUS the number of holes we've gone through so far

                  }

                  // Keeps the current hole within our domain

                  if (currentHole > 11){

                        currentHole -= 12;

                  }

                  if (currentHole < 0){

                        currentHole += 12;

                  }

                  if (lastHole + currentHole == 11){ // If we passed a mancala

                        if ((lastHole == 5 || lastHole == 6) && sideA){ // If it is Player A's mancala and turn

                              mancalaA++; // Increase the number of stones in the mancala by 1

                              stonesLeft--; // Decrease the stones left by 1

                        }

                        if ((lastHole == 11 || lastHole == 0) && !sideA){ // If it is Player B's mancala and turn

                              mancalaB++; // Increase the number of stones in the mancala by 1

                              stonesLeft--; // Decrease the stones left by 1

                        }

                  }

                  if (i > 0 && stonesLeft > 0){ // If this is not the starting hole and we still have stones left

                        holes[currentHole]++; // Increase the number of stones in the current hole by 1

                        stonesLeft--; // Decrease the stones left by 1

                  }

                  lastHole = currentHole; // Update the last hole

            }

      }

      turn++; // Increase the number of turns by 1

}

function getOutput(hole:String){

      var returnVal:Number;

      if (hole == "A"){ // If the third part of the input is A

            returnVal = mancalaA; // We will return the number of stones in mancala A

      } else if (hole == "B"){ // If it is B

            returnVal = mancalaB; // We will return the number of stones in mancala B

      } else { // If it is an integer

            returnVal = holes[parseInt(hole) - 1]; // We will return the number of stones in that hole

      }

      return returnVal + "\n";

}

function onEnter():Void{

      if (input.text != ""){ // If the player typed in something

            output.text += "INPUT: " + input.text + "\n"; // Prints the user's input

            var input1:Array = parse(input.text); // Parses the input

            input.text = ""; // Resets the input text field

            if (turn > 5){ // There are 5 turns in a game

                  initGame(); // Start a new game

            }

            move(parseInt(input1[1]) - 1, input1[0]); // Make the move

            output.text += "OUTPUT: " + getOutput(input1[2]); // Prints the output

      }

}

function clearOutput():Void{

      output.text = ""; // Resets the output text field

}

initGame() // Starts the game

 

input.addEventListener("enter", onEnter); // When the user hits enter and the input text field has focus, call the function onEnter

clearBtn.onRelease = clearOutput; // When the user clicks the Clear button, call the function clearOutput