Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /**
- * Solitaire
- * Defines and creates objects of the solitaire class using array lists.
- * @Cody Young
- * @October 27, 2016
- */
- public class Solitaire
- {
- //Instance variable for piles array list
- private ArrayList<Integer> piles = new ArrayList<>();
- /**
- * Constructor method for Solitaire class objects. Initializes card piles to random sizes.
- */
- public Solitaire()
- {
- int piles_total = 0; //Variable that tracks running total number of cards across piles
- while (piles_total < 45)
- {
- //Generate random number, subtract max
- int rand_number = (int) (Math.random () * (45 - piles_total)) + 1;
- piles.add(rand_number);
- piles_total += rand_number; //Add # of cards in each pile to total - must equal 45
- }
- }
- /**
- * Method returning true if solitaire game is over, false by default.
- */
- public Boolean over()
- {
- for(int i = 1; i < 10; i++)
- {
- if (piles.indexOf(i) == -1) //Checks if value at index is 1-9; if not, continues
- {
- return false;
- }
- }
- return true; //If no values return -1, game is over
- }
- /**
- * Method that draws one card from each pile and adds to a new pile.
- * Each iteration represents one round.
- */
- public void round()
- {
- int newpile_total = 0; //Running total of cards taken from piles
- for (int i = 0; i < piles.size(); i++)
- {
- int pile_sub = piles.get(i) - 1; //Variable for pile subtraction
- piles.set(i, pile_sub); //Gets pile value, subtracts one and places back into pile
- if (piles.get(i) == 0)
- {
- piles.remove(i); //If value at index i = 0, remove pile
- i--; //Decrement i after removing piles
- }
- newpile_total += 1; //Tracks how many cards are subtracted, adds to total
- }
- piles.add(newpile_total); //Adds total cards pulled from piles, appends list
- }
- /**
- * Returns a string representation of a Solitaire object.
- */
- public String toString()
- {
- return piles.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement