我没有翻译,而是在代码中添加了评论。但它的主要字符串是瑞典语。
我无法添加一个功能,游戏会询问用户甲板的数量(每个牌组包含标准的52张牌。)
就像游戏如何询问牌组是否应该洗牌一样,用户应该被问到游戏应该包含多少套牌,剩下的就像现在一样。
我已经尝试过(但是它没有用):
int n = //amount of decks
int[] deck = new int[52*n];
以下是该计划:
import java.util.Scanner;
public class KortSpel {
public static void main(String[] args) {
Boolean fortsatt = true;
while(fortsatt){
Scanner scan = new Scanner(System.in);
int[] deck = new int[52];
String[] suits = {"Hjärter", "Spader", "Ruter", "Klöver"}; //the suits
String[] ranks = {"Ess", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Knäckt", "Drottning", "Kung"}; //the rank of the cards
for( int i = 0; i < deck.length; i++) deck[i] = i;
System.out.print("Skriv dra för att dra korten annars avsluta."); //ask the user if he want to keep playing or not
String svar2 = scan.nextLine();
if (svar2.equalsIgnoreCase("Avsluta")){
fortsatt = false;
System.out.println("Du har nu avslutat."); //tells the user he has exit
}
else {
System.out.print("Vill du bland korten? (ja/nej) "); //ask the user if he want to shuffle the cards Y/N
String svar = scan.nextLine();
if (svar.equalsIgnoreCase("ja")) { // if shuffled
for( int i = 0; i < deck.length; i++) {
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for( int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Kort nummer " + deck[i] + ": " + suit + " " + rank);
}
}
else { //if not suffled
for( int i = 0; i < deck.length; i++) deck[i] = i;
for( int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Kort nummer " + deck[i] + ": " + suit + " " + rank);
}
}
}
}
}
}
所有的帮助都很受欢迎,因为我对java不是很好,所以简单是首选。
提前致谢!
答案 0 :(得分:1)
如果你采用正式的面向对象模型而不是非正式地定义卡片,这一切都会变得容易得多。
什么是卡?在这种情况下,这很简单;它是一对价值观,其中一个是西装,另一个是排名。只要套装和等级都有效,两者都是牌。这给了我们以下课程:
public class Card{
public final String suit;
public final String rank;
public Card(String s, String r){
suit = s;
rank = r;
}
}
在我们进一步讨论之前,我想在此注意,对String
和suit
使用rank
类型并不是一个好主意;两者都有一组有限的潜在值,因此可以更好地表示为枚举类型(或Enum
)。我们可以像这样定义枚举(虽然显然你可以将这些词改为瑞典语):
public enum Suit{
HEARTS,
SPADES,
CLUBS,
DIAMONDS
}
public enum Rank{
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
}
我们的Card类改变如下:
public class Card{
public final Suit suit;
public final Rank rank;
public Card(Suit s, Rank r){
suit = s;
rank = r;
}
}
从这里开始,创建一个甲板课很容易:
public class Deck{
private ArrayList<Card> deck; //An array of cards that represents this deck.
public Deck(){
deck = new ArrayList<Card>();
//For every combination of suit and rank, create and add a card to this deck
for(Suit s : Suit.values()){
for(Rank r : Rank.values()){
deck.add(new Card(s, r));
}
}
}
}
从这里你可以为甲板类添加功能,用于改组,绘图卡等。 对于您的原始问题,创建多个套牌只是创建许多单个套牌并将所有内容添加到一起的问题。我们可以创建一个构造函数,并将其添加到我们的deck类:
public class Deck{
private ArrayList<Card> deck; //An array of cards that represents this deck.
public Deck(){
deck = new ArrayList<Card>();
//For every combination of suit and rank, create and add a card to this deck
for(Suit s : Suit.values()){
for(Rank r : Rank.values()){
deck.add(new Card(s, r));
}
}
}
/** Creates a deck that is the sum of all the cards in the input deck(s) */
public Deck(Deck... decks){
deck = new ArrayList<Card>();
for(Deck d : decks){
deck.addAll(d.deck);
}
}
}