commit
092da7ba35
4 changed files with 231 additions and 0 deletions
-
39Card.java
-
102Deck.java
-
31SortingAlgs.java
-
59TestCardSorting.java
@ -0,0 +1,39 @@ |
|||||
|
// The Card class that represents cards |
||||
|
// Do not make any changes to this file! |
||||
|
// Xiwei Wang |
||||
|
|
||||
|
import java.io.*; |
||||
|
|
||||
|
public class Card implements Serializable |
||||
|
{ |
||||
|
// instance variables |
||||
|
private int m_suit; |
||||
|
private int m_rank; |
||||
|
|
||||
|
// constructor |
||||
|
public Card(int suit, int rank) |
||||
|
{ |
||||
|
m_suit = suit; |
||||
|
m_rank = rank; |
||||
|
} |
||||
|
|
||||
|
// getters |
||||
|
public int getSuit() |
||||
|
{ |
||||
|
return m_suit; |
||||
|
} |
||||
|
|
||||
|
public int getRank() |
||||
|
{ |
||||
|
return m_rank; |
||||
|
} |
||||
|
|
||||
|
// return a string representation of the card |
||||
|
public String toString() |
||||
|
{ |
||||
|
String[] suitChars = {"C", "D", "H", "S"}; |
||||
|
String[] rankChars = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; |
||||
|
|
||||
|
return suitChars[m_suit] + rankChars[m_rank]; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,102 @@ |
|||||
|
// The Deck class that represents decks |
||||
|
// Do not make any changes to this file! |
||||
|
// Xiwei Wang |
||||
|
|
||||
|
import java.io.*; |
||||
|
|
||||
|
public class Deck implements Serializable |
||||
|
{ |
||||
|
// instance variables |
||||
|
private Card[][] myCards; |
||||
|
private int m_numCards; // the number of card that are picked up to build the partial deck |
||||
|
private String m_sortedDeck; |
||||
|
|
||||
|
// constructor |
||||
|
public Deck() |
||||
|
{ |
||||
|
myCards = new Card[4][13]; |
||||
|
|
||||
|
// populate the card array |
||||
|
for (int i = 0; i < 4; i++) |
||||
|
for (int j = 0; j < 13; j++) |
||||
|
myCards[i][j] = new Card(i, j); |
||||
|
} |
||||
|
|
||||
|
// shuffle the deck |
||||
|
public void shuffle(int numShuffles) |
||||
|
{ |
||||
|
for (int i = 0; i < numShuffles; i++) |
||||
|
{ |
||||
|
// generate random positions |
||||
|
int suit1 = (int)(Math.random() * 4); |
||||
|
int suit2 = (int)(Math.random() * 4); |
||||
|
int rank1 = (int)(Math.random() * 13); |
||||
|
int rank2 = (int)(Math.random() * 13); |
||||
|
|
||||
|
// swap the cards |
||||
|
Card temp = myCards[suit1][rank1]; |
||||
|
myCards[suit1][rank1] = myCards[suit2][rank2]; |
||||
|
myCards[suit2][rank2] = temp; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// return a particular card in the deck |
||||
|
public Card getCard(int suit, int rank) |
||||
|
{ |
||||
|
return myCards[suit][rank]; |
||||
|
} |
||||
|
|
||||
|
// return a specific number of cards as a card array |
||||
|
public Card[] getPartialDeck(int numCards) |
||||
|
{ |
||||
|
Card[] partialDeck = new Card[numCards]; |
||||
|
int counter = 0; |
||||
|
|
||||
|
for (int i = 0; i < 4; i++) |
||||
|
for (int j = 0; j < 13; j++) |
||||
|
{ |
||||
|
partialDeck[counter] = myCards[i][j]; |
||||
|
counter++; |
||||
|
|
||||
|
if (counter == numCards) |
||||
|
return partialDeck; |
||||
|
} |
||||
|
|
||||
|
return partialDeck; |
||||
|
} |
||||
|
|
||||
|
// return a string reprentation of the deck |
||||
|
public String toString() |
||||
|
{ |
||||
|
String deckContent = ""; |
||||
|
|
||||
|
for (int i = 0; i < 4; i++) |
||||
|
{ |
||||
|
for (int j = 0; j < 13; j++) |
||||
|
deckContent += myCards[i][j].toString() + " "; |
||||
|
|
||||
|
deckContent += "\n"; |
||||
|
} |
||||
|
|
||||
|
return deckContent; |
||||
|
} |
||||
|
|
||||
|
// set the number of cards that would be picked up and the sorted deck string |
||||
|
public void setVars(int numCards, String sortedDeck) |
||||
|
{ |
||||
|
m_numCards = numCards; |
||||
|
m_sortedDeck = sortedDeck; |
||||
|
} |
||||
|
|
||||
|
// return the number of cards that would be picked up |
||||
|
public int getnumCards() |
||||
|
{ |
||||
|
return m_numCards; |
||||
|
} |
||||
|
|
||||
|
// return the sorted deck string |
||||
|
public String getSortedDeck() |
||||
|
{ |
||||
|
return m_sortedDeck; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
// The SortingAlgs class that implements insertion sort and iterative merge sort |
||||
|
// your name here |
||||
|
|
||||
|
public class SortingAlgs |
||||
|
{ |
||||
|
// card comparison |
||||
|
public int compares(Card c1, Card c2) |
||||
|
{ |
||||
|
// TODO: implement this method |
||||
|
|
||||
|
return -1; // replace this statement with your own return |
||||
|
} |
||||
|
|
||||
|
// insertion sort |
||||
|
public void insertionSort(Card[] cardArray) |
||||
|
{ |
||||
|
// TODO: implement this method} |
||||
|
} |
||||
|
|
||||
|
// merge sort |
||||
|
public void mergeSort(Card[] cardArray) |
||||
|
{ |
||||
|
// TODO: implement this method (in an iterative way) |
||||
|
} |
||||
|
|
||||
|
// merge two sorted arrays into one sorted array |
||||
|
public void merge(Card[] cardArray, int first, int mid, int last) |
||||
|
{ |
||||
|
// TODO: implement this method |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
// Test driver for the SortingAlgs class |
||||
|
// Do not make any changes to this file! |
||||
|
// Xiwei Wang |
||||
|
|
||||
|
import java.io.*; |
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public class TestCardSorting |
||||
|
{ |
||||
|
public static void main(String[] args) |
||||
|
{ |
||||
|
SortingAlgs mySort = new SortingAlgs(); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
// read the decks from file |
||||
|
ObjectInputStream in = new ObjectInputStream(new FileInputStream("Decks.dat")); |
||||
|
Deck[] myDeck = new Deck[5]; |
||||
|
myDeck = (Deck[])in.readObject(); |
||||
|
|
||||
|
for (int i = 0; i < 5; i++) |
||||
|
{ |
||||
|
Card[] myCards = myDeck[i].getPartialDeck(myDeck[i].getnumCards()); |
||||
|
String before = Arrays.toString(myCards); |
||||
|
|
||||
|
System.out.println("Test " + (i + 1) + ":"); |
||||
|
System.out.println("The cards before sorting are\n" + before); |
||||
|
System.out.println("------------------------------------"); |
||||
|
|
||||
|
Card[] testCards = myCards.clone(); |
||||
|
mySort.insertionSort(testCards); |
||||
|
String after = Arrays.toString(testCards); |
||||
|
|
||||
|
System.out.println("The cards after Insertion Sort are\n" + after); |
||||
|
if (after.equals(myDeck[i].getSortedDeck())) |
||||
|
System.out.println("Your Insertion Sort works correctly.\n"); |
||||
|
else |
||||
|
System.out.println("Something is wrong with your Insertion Sort.\n"); |
||||
|
|
||||
|
testCards = myCards.clone(); |
||||
|
mySort.mergeSort(testCards); |
||||
|
after = Arrays.toString(testCards); |
||||
|
|
||||
|
System.out.println("The cards after Merge Sort are\n" + after); |
||||
|
if (after.equals(myDeck[i].getSortedDeck())) |
||||
|
System.out.println("Your Merge Sort works correctly."); |
||||
|
else |
||||
|
System.out.println("Something is wrong with your Merge Sort."); |
||||
|
|
||||
|
System.out.println("====================================\n"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
System.out.println("Error occurred: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue