Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercises;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.List;
- import java.util.Random;
- /**
- * For this program, you must give a list of 4 items, the 3 objects to me moved across the River,
- * and "You". Object 1 cannot be together with object 2, and object 2 cannot be together
- * with object 3.
- * @author theif519
- */
- public class RiverCrossing {
- private class River
- {
- Collection<String> order = new ArrayList<String>();
- List<String> listOfCharacters = null;
- Collection<String> otherSide = null;
- public River(List<String> listOfCharacters)
- {
- this.listOfCharacters = listOfCharacters;
- this.otherSide = listOfCharacters;
- }
- public boolean attemptTransport(String object, Option option)
- {
- if(option.equals(option.ADD))
- {
- return addAttempt(object);
- }
- else if(option.equals(option.REMOVE))
- {
- return removeAttempt(object);
- }
- else
- {
- return false;
- }
- }
- public boolean removeAttempt(String object)
- {
- otherSide.add(object);
- otherSide.add("You");
- order.remove(object);
- order.remove("You");
- if(otherSide.containsAll(Arrays.asList(listOfCharacters.get(0),listOfCharacters.get(1))))
- {
- otherSide.remove(object);
- otherSide.remove("You");
- order.add(object);
- order.add("You");
- return false;
- }
- else if(otherSide.containsAll(Arrays.asList(listOfCharacters.get(1),listOfCharacters.get(2))))
- {
- otherSide.remove(object);
- otherSide.remove("You");
- order.add(object);
- order.add("You");
- return false;
- }
- else
- {
- return true;
- }
- }
- public boolean addAttempt(String object)
- {
- otherSide.remove(object);
- otherSide.remove("You");
- order.add(object);
- order.add("You");
- if(otherSide.containsAll(Arrays.asList(listOfCharacters.get(0),listOfCharacters.get(1))) && !otherSide.contains("You"))
- {
- otherSide.add(object);
- otherSide.add("You");
- order.remove(object);
- order.remove("You");
- return false;
- }
- else if(otherSide.containsAll(Arrays.asList(listOfCharacters.get(1),listOfCharacters.get(2))) && !otherSide.contains("You"))
- {
- otherSide.add(object);
- otherSide.add("You");
- order.remove(object);
- order.remove("You");
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- public enum Option
- {
- ADD, REMOVE;
- }
- public static void main(String[] args)
- {
- RiverCrossing test = new RiverCrossing();
- test.computeSolution();
- }
- public void computeSolution()
- {
- List<String> list = Arrays.asList("Wolf", "Goat", "Salad", "You");
- Random random = new Random();
- River river = new River(list);
- while(!river.order.containsAll(list))
- {
- if(river.addAttempt(list.get(random.nextInt(2))) == false)
- {
- continue;
- }
- else
- {
- System.out.println(river.otherSide + "\n" + river.order);
- }
- if(river.removeAttempt(list.get(random.nextInt(2))) == false)
- {
- while(true)
- {
- if(river.removeAttempt(list.get(random.nextInt(2))) == true)
- {
- break;
- }
- }
- }
- else
- {
- System.out.println(river.otherSide + "\n" + river.order);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement