Hey everybody, I am posting this because I am having trouble with the last few steps of my program. I am currently trying to write a loop that ends at 3 times (the user only has 3 times to enter and get the answer correct and if they don't it breaks loop). Also I am having trouble assigning the values once the user has moved one stack to another.
Here is my code:
import java.util.Scanner;
public class ToothpickPuzzle {
static int A,B,C; // did this so I can access ABC to all the functions.
//static means its allocated the entire time the program is run and can access the data
public static void main(
String[] args)
{
// create an instance of this class
ToothpickPuzzle theSampleInstance = new ToothpickPuzzle();
// call a non-static method to do everything else
theSampleInstance.mainLoop();
}//CLOSED
void mainLoop()
{//OPEN
Scanner keyboard = new Scanner(System.in); // creating and object of the class scanner, name=keyboard! good name.
//identifying information.
//Game instructions
System.out.println("Welcome to the Toothpick Puzzle.Think of having 3 piles of toothpicks in front of you, where there are 24 toothpicks total:"); // intro
System.out.println(" Stack: A B C");
System.out.println("Number of Toothpicks: 11 7 6 ");
System.out.println("The goal is to create 3 piles of 8 toothpicks in exactly 3 moves.");
System.out.println("A move consists of moving toothpicks from one stack to a second stack, where the number of toothpicks moved is exactly the number that is in the destination stack.");
System.out.println("In other words, to move from stack B (7 toothpicks) to stack C (6) as shown above, we would move 6 from B to C, leaving us with 1 in B and 12 in stack C.");
System.out.println("Here we go...");
System.out.println(" Stack: A B C");
System.out.println("Number of Toothpicks: 11 7 6 ");
String userInput = "";
String userInput2= "";
A = 11;
B = 7; // A+B+C=WIN! (casting abc to integers)
C = 6;
char stackFrom, stackTo; //string names which will be converted to char
int stackFrom_size, stackTo_size; // sizing them up
stackFrom_size = 0;
stackTo_size = 0;
stackFrom = ' '; //NULL CHARACTER
stackTo = ' ';
while(true) //truth loop
{
//prompts user for beginning stack
System.out.print("1. Enter stack from: ");
userInput = keyboard.nextLine(); // read user input n stuff
stackFrom = userInput.charAt(0); // converts string to char and grabs the first letter in the string. An array sort of.
stackFrom_size = GetNumberOfToothpicks(stackFrom);// calling function to take stack from
//prompt user to enter ending stack.
System.out.print("Enter stack to: ");
userInput = keyboard.nextLine(); //collecting information n such
stackTo = userInput.charAt( 0); //converts string to char and grabs the first letter in the string.
stackTo_size = GetNumberOfToothpicks(stackTo);//calling function to stack to
}
System.out.println(" Stack: A B C");
System.out.println("Number of Toothpicks: " );
//System.out.println("StackFrom: " + stackFrom_size + " + StackTo: " + stackTo_size);
if(stackFrom_size < stackTo_size)
{
System.out.println("Sorry not enough toothpicks in " + stackFrom + " try again.");
continue;
}//closing if statement
else //continues the move if valid
{
DoMove(stackFrom,stackTo); //calls method from DoMove
}// close else
}//while(true) loop closing
}//mainLoop Closing.
// +----+-----+RETURNS TOOTHPICKS IN A GIVEN FILE+----+-----+
public static int GetNumberOfToothpicks(char pile)
{
int numberThatIWant;
numberThatIWant = 0;
switch(pile)
{
case 'A':
numberThatIWant = A;
break;
case 'B':
numberThatIWant = B;
break;
case 'C':
numberThatIWant = C;
break;
default:
break;
}//closes switch
return numberThatIWant; // returns the number in the pile
}//closes function 1
public static void DoMove(char stackFrom,char stackTo)//move it
{
// get stack sizes
int stackFrom_size = GetNumberOfToothpicks(stackFrom);
int stackTo_size = GetNumberOfToothpicks(stackTo);
//calculate new stack sizes
stackFrom_size = stackFrom_size - stackTo_size; //subtract what was taken
stackTo_size = stackTo_size * 2; // the stack the toothpicks are put in, doubles.
SetNumberOfToothpicks(stackFrom, stackFrom_size);
SetNumberOfToothpicks(stackTo, stackTo_size);
}//closes function 2
public static void SetNumberOfToothpicks(char stack, int stack_size)
{
switch(stack)
{
case 'A':
A = stack_size;
break;
case 'B':
B = stack_size;
break;
case 'C':
C = stack_size;
break;
default:
break;
}
}
}//closes entire program
The program works, but as said before I am having trouble showing the NEW stacked values. So if I move B to C, it does the math, but I need the new println to show the new stack values, so it would be: A,B,C = 11 1 12. Can anyone help?