0

だから私はトランプのデッキを並べ替えたいと思います。コンストラクターのDeckOfCardsは、カードのデッキをセットアップしてシャッフルします。ただし、カードは、カードタイプがカードクラスに属するタイプリストのリストに格納されます。DeckOfCardsを呼び出した後、カードのデッキを並べ替えるのにどのような種類の並べ替えアルゴリズムを使用するかをユーザーに尋ねる必要があります。たとえば、最後に挿入ソートを含めました。ただし、挿入ソート関数はdouble配列を取ります。したがって、リストリストをdouble配列に変換する必要があります。どうすればいいですか?

コード:

import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

// class to represent a Card in a deck of cards
class Card 
{    
public static enum Face { Ace, Deuce, Three, Four, Five, Six,
  Seven, Eight, Nine, Ten, Jack, Queen, King  };
public static enum Suit { Clubs, Diamonds, Hearts, Spades };

private final Face face; // face of card
private final Suit suit; // suit of card

// two-argument constructor
public Card( Face cardFace, Suit cardSuit ) 
{   
   face = cardFace; // initialize face of card
   suit = cardSuit; // initialize suit of card
} // end two-argument Card constructor

// return face of the card
public Face getFace() 
{ 
  return face; 
} // end method getFace

// return suit of Card
public Suit getSuit() 
{ 
  return suit; 
} // end method getSuit

// return String representation of Card
public String toString()
{
   return String.format( "%s of %s", face, suit );
} // end method toString
} // end class Card

// class DeckOfCards declaration
public class DeckOfCards 
{
private List<Card> list; // declare List that will store Cards

// set up deck of Cards and shuffle
public DeckOfCards()
{
  Card[] deck = new Card[ 52 ];
  int count = 0; // number of cards

  // populate deck with Card objects
  for ( Card.Suit suit : Card.Suit.values() )  
  {
     for ( Card.Face face : Card.Face.values() )   
     {
        deck[ count ] = new Card( face, suit );
        count++;
     } // end for
  } // end for

  list = Arrays.asList( deck ); // get List
  Collections.shuffle( list );  // shuffle deck
} // end DeckOfCards constructor

// output deck
public void printCards()
{
  // display 52 cards in two columns
  for ( int i = 0; i < list.size(); i++ )
     System.out.printf( "%-20s%s", list.get( i ),
        ( ( i + 1 ) % 2 == 0 ) ? "\n" : "" );
} // end method printCards

public static void main( String args[] )
{
  DeckOfCards cards = new DeckOfCards();
  cards.printCards();
  //add code here to take input from user and sort
  int a;
  System.out.println("\nSort the deck of cards");
  System.out.println("\nEnter your choice: \n1.Selection Sort \n2.Insertion Sort 
\n3.Merge       Sort\n");
  //take input
  Scanner reader = new Scanner(System.in);
  a=reader.nextInt();
  switch(a)
  {
    case 1:
        //call Selection sort
        break;
    case 2:
        //call Insertion sort
        break;
    case 3:
        //call Merge sort
        break;
  }
 } // end main  

//Insertion sort

} // end class DeckOfCards
public class InsertionSort {
/** The method for sorting the numbers */
public static void insertionSort(double[] list) {
for (int i = 1; i < list.length; i++) {
  /** insert list[i] into a sorted sublist list[0..i-1] so that
       list[0..i] is sorted. */
  double currentElement = list[i];
  int k;
  for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
    list[k + 1] = list[k];
  }

  // Insert the current element into list[k+1]
  list[k + 1] = currentElement;
}
}
}
4

2 に答える 2

0

Cardオブジェクトのリストを変換したい場合、つまり、それ自体が提供するメソッドを使用して、オブジェクトを配列(つまり)List<Card> listに簡単に変更できます。CardCard[]ListtoArray

しかし、あなたはあなたが求めているものにいくらかの混乱があります。突然魔法のように配列を期待するメソッドがdouble、型のオブジェクトを処理する方法を知っていることを期待していますCard。どうしてCard突然倍精度数のように振る舞うことができますか?

最初にメソッドを変更してinsertionSort()(おそらく、その動作を理解しようとせずにどこかからコピーした)、最初に配列を取得してから、プリミティブ ではなくオブジェクトCard[]のプロパティで比較が行われるように、内部で何を実行するかを変更する必要があります。Carddouble

一方、JavaがすでにArraysクラスを介してソート関数を提供していることを知りたいと思うかもしれません。関数を見ることができますArrays.sort()。ただし、Javaが配列の並べ替え方法を認識できるようにインターフェイスを実装する必要がありComparableますが、オブジェクトの並べ替え方法はわかりませんCard(顔、スーツ、順序、Javaはカードの再生方法を認識していません)。したがって、Cardオブジェクトを実装Comparableし、 Javaがどのインスタンスがどのインスタンスの前に来るcompareTo()かを決定するのに役立つメソッドを用意する必要があります。Card自分で並べ替えを実装し、メソッドinsertionSort()を変更してアイテムの配列を取得し、その中でどのアイテムが何の前に来るかを知るComparableために呼び出す場合は、同じアプローチを使用できます。compareTo()

于 2013-02-11T00:01:54.337 に答える
0

私が正しく理解していれば、あなたはあなたのカードを異なるアルゴリズムでソートしたいと思うでしょう。また、アルゴリズムの実装はdouble配列のみを受け入れます。

あなたが考える必要がある2つの質問:

  • カード比較のルール、例えばハートエース>ダイアモンドエース?

  • すべてのソートが比較ソートアルゴリズムである場合はどうなりますか?

2番目の質問の答えが次の場合: はい

カード配列型を二重配列に変換することを考えるのをやめることをお勧めします。代わりに、すべてのアルゴリズムの実装をより一般的にします。たとえば、Comparable[] arr配列を受け入れるか、さらにはCard[] arr。次に、CardクラスにComparableインターフェイスを実装させます。メソッドにカード比較ルールを実装しますcompareTo(Card c)

于 2013-02-11T00:05:26.277 に答える