因此,我们的任务是:
您将为特定类型的纸牌建模。游戏以45张牌开始。 (他们需要 不是打牌。未标记的索引卡也可以正常工作。)随机分割它们 进入一些随机大小的堆。例如,你可以从成堆开始 尺寸分别为20,5,1,9和10.在每一轮中,你从每一堆中取出一张牌,形成一个新牌 堆这些卡。例如,样本启动配置将是 变成了大小为19,4,8,9和5的成堆。当成堆时,纸牌已经结束 大小为1,2,3,4,5,6,7,8和9,按某种顺序排列。 (可以证明你总是最终结束 有这样的配置。)
我们获得了SolitaireTester课程,我将在下面提供:
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);
}
}
我们必须写Solitaire课程。以下是我到目前为止的情况:
import java.util.*;
import java.math.*;
public class Solitaire
{
int size = 45;
private ArrayList piles;
//Constructor for the initialization of random values
public Solitaire()
{
piles = new ArrayList<Integer>();
for(int i = 0; i < size; i++)
{
int temp = (int)(Math.random()*10)+1;
piles.add(temp);
}
}
//toString() will return a string representation of a Solitaire object
public String toString()
{
return piles.toString();
}
/*
* over() will return true if the solitaire is over, false otherwise. The solitaire is over when
* the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and 9, in some order
*/
public boolean over()
{
int sum = 0;
int a[] = {1,2,3,4,5,6,7,8,9};
Iterator itr = piles.iterator();
while(itr.hasNext())
{
int check = (int) itr.next();
for(int i = 0; i < 9; i++)
{
if(a[i] == check)
{
a[i] = 0;
}
}
}
if(sum == 0)
{
return true;
}
else
{
return false;
}
}
//round() takes one card from each pile, forming a new pile with these cards
public boolean round()
{
Iterator itr = piles.iterator();
int count = 0;
while(itr.hasNext())
{
int check = (int) itr.next();
count += 1;
}
if(count == 9)
{
return true;
}
else
{
return false;
}
}
}
我显然做错了什么。我得到的输出是:
Start: [5, 1, 2, 1, 7, 10, 5, 8, 6, 3, 8, 6, 6, 6, 10, 4, 4, 9, 7, 4, 10, 2, 8, 4, 8, 9, 10, 3, 3, 5, 9, 2, 5, 5, 3, 6, 6, 5, 3, 5, 4, 3, 1, 9, 6]
Rounds: 0
实际输出应该有更多轮次并实际给出正确的输出。
如果有人可以指出我正确的方向,我在犯错误的地方。此外,如果有任何无关的内容,我包括但不应该,那么这也会有所帮助。拜托,我不是在寻找答案,只是朝着正确的方向努力。谢谢!
答案 0 :(得分:0)
import java.util.ArrayList;
import java.util.Collections;
public class Solitaire
{
int maxSize = 45;
int newPile;
private ArrayList<Integer> piles;
/*
* Constructor for the initialization of random values.
* Initialize piles to a random number of piles of random size, but exactly 45 cards total.
*/
public Solitaire()
{
int totalNumberOfStartingPiles = (int) (Math.random() * 10) + 1;
boolean continueLoop = true;
int total = 0;
int size = 45;
while (continueLoop)
{
piles = new ArrayList<Integer>();
for (int i = 0; i < totalNumberOfStartingPiles; i++)
{
int temp = getRandomPile(size - totalNumberOfStartingPiles + i);
if (i == totalNumberOfStartingPiles - 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;
}
}
}
/*
* Randomizes and returns the total number of starting piles.
*
* @return Integer that is the total number of starting piles.
*/
public int getRandomPile(int size)
{
int totalNumberOfStartingPiles = (int) (Math.random() * size) + 1;
return totalNumberOfStartingPiles;
}
/*
* toString() will return a string representation of a Solitaire object.
*
* @return String representation of a Solitaire object.
*/
public String toString()
{
return piles.toString();
}
/*
* over() will return true if the solitaire is over, false otherwise. The
* solitaire is over when the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and
* 9, in some order.
*
* @return true if the solitaire is over, false otherwise.
*/
public boolean over()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
boolean flag = true;
for (int i = 0; i < piles.size(); i++)
{
if (a[i] != piles.get(i))
{
flag = false;
break;
}
}
return flag;
}
/*
* In each round, you take one card from each pile, forming a new pile with these cards.
* Removes a pile that reaches a size of 0 cards.
*/
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); //sorts the pile into ascending numerical order
}
}