Hello, if possible I would appreciate some help in maknig sure I have commented
ID: 3814290 • Letter: H
Question
Hello, if possible I would appreciate some help in maknig sure I have commented evverything according to my professor's expectations. I have finished the code, but I am not sure if I have commented it correctly. I have included my code as well in case there are any minor issues, but I am mainly concerned with the comments
------------------assignment instructions--------------
Java BlueJ Programming
Lab 3 Solitaire
Purpose
The purpose of this lab is for you to work with array lists.
Problem specification
You will model a particular type of card game. The game starts with 15 cards. (They need not be playing cards. Unmarked index cards work just as well.) Randomly divide them into some number of piles of random size. For example, you might start with piles of size 2, 10, 1 and 2. In each round, you take one card from each pile, forming a new pile with these cards. For example, the sample starting configuration would be transformed into piles of size 1, 9, 1 and 4. The game is over when the piles have size 1, 2, 3, 4, and 5, in any order. (It can be shown that you always end up with such a configuration.)
You will write a Solitaire class to implement this behavior.
The SolitaireTester class
You are given a SolitaireTester class that uses Solitaire.
Source code is shown below, and can be downloaded from Blackboard, Course Documents, ‘Arrays and array lists’ folder, Example programs. SolitaireTester code must not be changed in any way.
public class SolitaireTester
{
public static void main(String[] args)
{
Solitaire s = new Solitaire();
System.out.println("Start: " + s.toString());
int rounds = 0;
while (!s.over()) {
s.round();
++rounds;
System.out.println(rounds + ": " + s.toString());
}
System.out.println("Rounds: " + rounds);
}
}
SolitaireTester produces a random starting configuration and prints it. Then keeps applying the solitaire step and prints the result. Stops when the solitaire final configuration is reached and prints the number of rounds.
A run of the program will look something like:
Start: [2, 10, 1, 2]
1: [1, 9, 1, 4]
2: [8, 3, 4]
3: [7, 2, 3, 3]
4: [6, 1, 2, 2, 4]
5: [5, 1, 1, 3, 5]
6: [4, 2, 4, 5]
7: [3, 1, 3, 4, 4]
8: [2, 2, 3, 3, 5]
9: [1, 1, 2, 2, 4, 5]
10: [1, 1, 3, 4, 6]
11: [2, 3, 5, 5]
12: [1, 2, 4, 4, 4]
13: [1, 3, 3, 3, 5]
14: [2, 2, 2, 4, 5]
15: [1, 1, 1, 3, 4, 5]
16: [2, 3, 4, 6]
17: [1, 2, 3, 5, 4]
Rounds: 17
The Solitaire class
Here are an outline and some hints for Solitaire:
Constants
As required, to avoid hard-coded magic numbers.
Hint: good constants mean the program can easily be changed if the solitaire is redefined
to be, say, 10 piles containing 1..10, which gives a total of 55 cards.
Exactly and only the following instance variable is required:
private ArrayList piles;
constructor()
Initialize piles to a random number of piles of random size, but exactly 15 cards total.
Use Math.random() as you do this. random() returns a double value greater than or
equal to 0.0 and less than 1.0. To set num to a random integer value between 1 and max
inclusive is, for example:
int num, max; //max is the upper limit
...
max = 7;
. . .
// this example sets num to a random int between 1…7 inclusive
num = (int) (Math.random() * max) + 1;
toString()
Return a string representation of a Solitaire object. To do this, simply:
return piles.toString();
over()
Return true if the solitaire is over, false otherwise.
The solitaire is over when the piles have size 1, 2, 3, 4, and 5, in any order. (It can be
shown that you always end up with such a configuration.)
Hint: use indexOf().
round()
In each round, you take one card from each pile, forming a new pile with these cards.
Be very careful here when you remove a pile that reaches a size of 0 cards. Realize that
when you remove() an element from an ArrayList elements move down, changing their
indexes.
When you have run and tested your program and are certain it is correct, save the output
as an output.txt file. Obviously your start state will be random.
Required
Your program must be written for 15 cards. Any other number of cards will be severely
penalized.
The piles in each round must not be sorted. This can be seen in the example output
provided above. Sorting the piles will be severely penalized.
ArrayList Java library methods can make this lab too easy. So with piles you may use
only these library methods: constructor; add(); size(); set(); get(); remove();
indexOf(); toString().
Finally, your program must look like the work of a professional computer programmer.
You will lose points if you do not meet ALL of the following requirements:
• clear, consistent layout and indentation
• you must use Javadoc documentation comments as you comment every class, every
method and anything that is not simple and clear. You will lose points otherwise.
(See ‘Debugging; Javadoc; number systems; arrays in Java’ lecture for Javadoc
comment examples)
• clear, meaningful variable and method names
• design simple, clear algorithms
• a method must do only a single task, and must be simple and short
• use constants to avoid hard-coded magic numbers
--------------------------------My Code----------------------------
/**
* Creates a game of Solitaire.
*
*/
import java.util.ArrayList;
import java.util.Collections;
// Initializing variables
public class Solitaire {
int maxSize = 15;
int newPile;
private ArrayList < Integer > piles;
// Defining public class Soliataire
public Solitaire() {
int numberOfStartingPiles = (int)(Math.random() * 10) + 1;
boolean continueLoop = true;
int total = 0;
int size = 15;
while (continueLoop) {
piles = new ArrayList < Integer > ();
for (int i = 0; i < numberOfStartingPiles; i++) {
int temp = getRandomPile(size - numberOfStartingPiles + i);
if (i == numberOfStartingPiles - 1) {
piles.add(size);
} else {
piles.add(temp);
size = size - temp;
}
}
for (int i = 0; i < piles.size(); i++) {
total += piles.get(i);
}
if (total == maxSize) {
continueLoop = false;
}
}
}
// Method for creating random piles
public int getRandomPile(int size) {
int numberOfStartingPiles = (int)(Math.random() * size) + 1;
return numberOfStartingPiles;
}
public String toString() {
return piles.toString();
}
// Defining the pile sizes
public boolean over() {
int[] a = {
1,
2,
3,
4,
5
};
boolean flag = true;
for (int i = 0; i < piles.size(); i++) {
if (a[i] != piles.get(i)) {
flag = false;
break;
}
}
return flag;
}
// Methid for creating a round
public void round() {
newPile = piles.size();
for (int i = 0; i < piles.size(); i++) {
piles.set(i, (piles.get(i)) - 1);
if (piles.get(i) == 0) {
piles.remove(i);
i--;
}
}
piles.add(newPile);
Collections.sort(piles);
}
}
Explanation / Answer
Here is your code fixed at multiple places so that you don't loose point (for example for sorting) and added apprpriate comment and javadoc.
/**
* Creates a game of Solitaire.
*
*/
import java.util.ArrayList;
import java.util.Collections;
/**
* Defining public class Solitaire
*/
public class Solitaire {
// constant for number of final piles.
int FINAL_NUMBER_OF_PILES = 5;
// field containing current piles
private ArrayList<Integer> piles;
public Solitaire()
{
// Randomly generating initial number of piles
int numberOfStartingPiles = (int) (Math.random() * 10) + 1;
// constant for number of cards
// Number of cards will be depending on number of final piles required.
// for example here 1,2,3,4,5 sum to 15 which n(n+1)/2 where n is final number of piles
int maxSize = FINAL_NUMBER_OF_PILES*(FINAL_NUMBER_OF_PILES+1)/2;
int size = maxSize;
boolean continueLoop = true;
int total = 0;
// Fill starting piles with random number of cards
while (continueLoop) {
piles = new ArrayList<Integer>();
for (int i = 0; i < numberOfStartingPiles; i++) {
int temp = getRandomPile(size - numberOfStartingPiles + i);
if (i == numberOfStartingPiles - 1) {
piles.add(size);
} else {
piles.add(temp);
size = size - temp;
}
}
for (int i = 0; i < piles.size(); i++) {
total += piles.get(i);
}
if (total == maxSize) {
continueLoop = false;
}
}
}
/**
* Method for creating random piles
* @param size size of the pile
* @return numbers in pile
*/
public int getRandomPile(int size) {
int numberOfStartingPiles = (int) (Math.random() * size) + 1;
return numberOfStartingPiles;
}
/**
* Returns a string representation of piles
*/
public String toString() {
return piles.toString();
}
/**
* Check for piles are same as final piles
* @return True if pile match required pile
*/
public boolean over() {
// create final piles configuration
int[] finalPile = new int[FINAL_NUMBER_OF_PILES];
for(int i = 0; i < FINAL_NUMBER_OF_PILES; i++)
finalPile[i] = i+1;
// check if current pile is same as final required piles(in any order)
boolean flag = true;
for (int i = 0; i < piles.size(); i++) {
if (! piles.contains(finalPile[i])) {
flag = false;
break;
}
}
return flag;
}
/**
* Method for creating a round
*/
public void round() {
int newPile = piles.size();
for (int i = 0; i < piles.size(); i++) {
piles.set(i, (piles.get(i)) - 1);
if (piles.get(i) == 0) {
piles.remove(i);
i--;
}
}
piles.add(newPile);
//Collections.sort(piles);
}
}
Please rate positively if this solved your query.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.