使用选择排序 - Java卡片组

时间:2017-03-27 18:19:36

标签: java arrays sorting

我将从我拥有的代码开始。

public class DeckOfCards2 {
  public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    // Initialize cards
    for (int i = 0; i < deck.length; i++) {
      deck[i] = i;
    }

    // Shuffle the cards
    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;
    }

    // Display the all the cards
    for (int i = 0; i < 52; i++) {
      String suit = suits[deck[i] / 13];
      String rank = ranks[deck[i] % 13];
      System.out.println( rank + " of " + suit);
    }
  }
}

以下是我尝试做的事情,并且不知道该怎么做。我是初学者。我需要帮助:

  • 创建一个随机卡数组。 (已经完成)
  • 使用toString在格式化的print语句中输出此数组 方法(未分类)。(尚未完成,但我知道如何获得这部分。)
  • 使用选择排序对数组进行排序。(不知道如何。)
  • 使用toString方法输出已排序的数组。上升 字母顺序:俱乐部(最低),其次是钻石,心脏和 黑桃(最高)。 (不知道如何打印此订单)此排名用于桥牌游戏。
  • 同时打印出数字迭代次数。 (我很容易放入)

2 个答案:

答案 0 :(得分:0)

对于选择排序算法,请搜索包含大量文章和示例的Google

从简单的示例开始,了解算法。然后用它来完成你的任务。
这些示例使用数字并与<>运算符进行比较。但对于卡片,你可以创建另一种方法,用它们的名字比较两张牌:

Ace of Clubs < 2 of Clubs
6 of Hearts < 3 of Spades

因此,在同一套装中只需比较ranks数组的索引。对于不同的套装,比较suits数组的索引。数组必须在它之前预先排序。所以suits数组应该是:

String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};

所以上一个例子的比较是:

Ace of Clubs < 2 of Clubs  =>  // because ranks[0] < ranks[1] and 0 < 1, the suit doesn't matter
6 of Hearts < 3 of Spades  =>  // because suits[2] < suits[3] and 2 < 3, the rank doesn't matter

希望这对你的研究有所帮助。

答案 1 :(得分:0)

import java.util.*;

public class Dealer {

public static void main(String[] args){
    ArrayList<Card> cards = new ArrayList<Card>();
        for (Suits s : Suits.values()) {
        for (Cards r : Cards.values()) {
             Card value = new Card(s,r);
             cards.add(value);
        }  
    }
    shuffle(cards);
    present(cards);
    System.out.println();
    cards = selectionSort(cards);
    present(cards);
}
private static void shuffle(ArrayList<Card> list){
     Collections.shuffle(list);
}
private static void present(ArrayList<Card> list){
    int count = 0;
    for (Card c : list){
        count++;
        System.out.println(c.toString());
    }
}
private static ArrayList<Card> selectionSort(ArrayList<Card> list){
    int min;
    int count = 0;
    for (int i = 0; i < list.size(); i++) {
        count++;
        // Assume first element is min
        min = i;
        for (int j = i + 1; j < list.size(); j++) {
            if (list.get(j).Get() < list.get(min).Get()) {
                min = j;



}
    }

if(min != i){
    final Card temp = list.get(i);
    list.set(i,list.get(min));
    list.set(min,temp);
}

  //  System.out.println(list.get(i));// I print the in ascending order
}
System.out.println("================================== Sorted Deck Below =========================================================");
System.out.println("It took "+count+" iterations to sort this deck using selection sort.");
System.out.println();
return list;

}

private static ArrayList<Card> InsertionSort(ArrayList<Card> list){
    int count = 0;
    for (int i = 1; i < list.size(); i++) {
        Card temp = list.get(i);
        int j;
        for(j = i-1;j>=0&&temp.Get()<list.get(j).Get(); j--){
            list.set(j+1,list.get(j));
        }
       list.set(j+1,temp); 
       count ++;
    }
    System.out.println("================================== Sorted Deck Below =========================================================");
    System.out.println("It took "+count+" iterations to sort this deck using insertion sort.");
    System.out.println();
    return list;
}

public static ArrayList<Card> mergeSort(ArrayList<Card> list){
        if (list.size() ==1){
            return list;
        }
        else {
           int mid= list.size()/2;
           ArrayList<Card> left= new ArrayList<Card>(mid);
           ArrayList<Card> right=new ArrayList<Card>(list.size()-mid);



for (int i = 0; i < mid; i++) {
           left.add(list.get(i));
       }







for (int i = 0; i < list.size()-mid; i++) {
           right.add(list.get(i));
       } 



left=mergeSort(left); 
   right=mergeSort(right);
   merge(left,right,list);
}
return list;

}


public static void merge (ArrayList<Card>left,ArrayList<Card>right,ArrayList<Card>list){
        int i1=0;// left Index
        int i2=0;// right Index
        int ListIndex=0;



for (int i = 0; i < list.size(); i++) {
        if (i2>=right.size() || (i1<left.size() && (right.get(i)>=left.get(i)))) 
        {
            list.set(ListIndex,left.get(i1));
            ListIndex++;
        }
        else {
            list.set(ListIndex, right.get(i2));
            ListIndex++;
        }
    }

}

}

=============================================== ============================

// Created enums
enum Cards{
   Ace,King,Queen,Jack,Ten,Nine,Eight,Seven,Six,Five,Four,Three,Two
}

enum Suits{
    Spades,
    Hearts,
    Diamonds,
    Clubs
}

public class Card{

 private Suits suit;
 private Cards card;

public Card(Suits suit, Cards card){


this.suit = suit;
    this.card = card;
}

public int Get(){
     return (suit.ordinal())+card.ordinal()*4;
}

public String toString(){


  return card.toString()+" of " +suit.toString();
    }
}

这是我在问我的一些朋友之后我所知道的,他们说尝试对象卡的ArrayList,我认为现在个人更难,因为合并排序,我知道不起作用,但我似乎无法找出如何使用ArrayList实现它。