Commit 60809864 authored by Siyu Zhang's avatar Siyu Zhang
Browse files

update all

parent 5fef02d0
// CS 0445 Spring 2018
// Some hints / help for Assignment 1.
// Note: Parts of this program (identified below) will not work until you
// have completed the RandIndexQueue<T> implementation. To see the rest of it execute
// simply comment out that section.
import java.util.*;
public class A1Help
{
public static void main(String [] args)
{
// Demo of Card class
Card c1 = new Card(Card.Suits.Diamonds, Card.Ranks.Seven);
Card c2 = new Card(Card.Suits.Hearts, Card.Ranks.Ace);
Card c3 = new Card(Card.Suits.Spades, Card.Ranks.Ace);
Card c4 = new Card(Card.Suits.Spades, Card.Ranks.Queen);
System.out.println("Card 1 is " + c1.toString());
System.out.println("Card 2 is " + c2.toString());
System.out.println("Card 3 is " + c3.toString());
System.out.println("Card 4 is " + c4.toString());
showValues(c1);
showValues(c2);
showValues(c3);
showValues(c4);
System.out.println("Here are all of the suits:");
for (Card.Suits s: Card.Suits.values())
System.out.print(s + " ");
System.out.println("\n");
System.out.println("Here are all of the ranks:");
for (Card.Ranks r: Card.Ranks.values())
System.out.print(r + " ");
System.out.println("\n");
// Below this line requires the RandIndexQueue<T> class. Comment this out if you
// have not yet finished your RandIndexQueue<T> class
// First let's make two RandIndexQueue objects of Cards
RandIndexQueue<Card> myCards = new RandIndexQueue<Card>(10);
RandIndexQueue<Card> otherCards = new RandIndexQueue<Card>(10);
// Now put some cards into one
myCards.offer(c1);
myCards.offer(c2);
myCards.offer(c3);
// Print it out
System.out.println("myCards " + myCards.toString());
// Move two into the other one
otherCards.offer(myCards.poll());
otherCards.offer(myCards.poll());
System.out.println("myCards " + myCards.toString());
System.out.println("otherCards " + otherCards.toString());
}
public static void showValues(Card c)
{
System.out.print(c.toString() + " has Value1: " + c.value());
System.out.println(" and Value2: " + c.value2());
}
}
0 added to Q
1 added to Q
2 added to Q
3 added to Q
4 added to Q
5 added to Q
6 added to Q
7 added to Q
8 added to Q
Total moves needed: 9
0 retrieved from Q
1 retrieved from Q
2 retrieved from Q
3 retrieved from Q
4 retrieved from Q
5 retrieved from Q
6 retrieved from Q
7 retrieved from Q
8 retrieved from Q
Total moves needed: 9
Nothing in the Q
Adding Item 1
Adding Item 2...and removing Item 1
Adding Item 3...and removing Item 2
Adding Item 4...and removing Item 3
Adding Item 5...and removing Item 4
Adding Item 6...and removing Item 5
Adding Item 7...and removing Item 6
Adding Item 8...and removing Item 7
Adding Item 9...and removing Item 8
There are 1 items in the buffer
Initializing a new RandIndexQueue...
Contents: 0 1 2 3 4 5 6 7
Removing 3 items then adding 1
Contents: 3 4 5 6 7 8
Testing Indexable...
Swapping 3 and 4
Swapping 3 and 5
Swapping 3 and 6
Swapping 3 and 7
Swapping 3 and 8
Testing Indexable...
Swapping 4 and 5
Swapping 4 and 6
Swapping 4 and 7
Swapping 4 and 8
Swapping 4 and 3
Contents: 5 6 7 8 3 4
About to test Shufflable...
Contents: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Shuffling...
Contents: 0 8 1 9 5 10 3 14 4 13 11 6 12 2 7
Removing 2 and adding 1
Contents: 1 9 5 10 3 14 4 13 11 6 12 2 7 22
Shuffling again...
Contents: 11 9 2 10 13 14 1 22 4 12 3 6 5 7
// CS 0445 Spring 2018
// Assig1A driver program. This program must work as is with your
// RandIndexQueue<T> class. Look carefully at all of the method calls so that
// you create your RandIndexQueue<T> methods correctly. For example, note the
// constructor calls and the toString() method call. The output should
// be identical to my sample output, with the exception of the result of
// the shuffle() method -- since this should be random yours should not
// match mine.
public class Assig1
{
public static void main(String [] args)
{
// Testing constructor
MyQ<Integer> theQ = new RandIndexQueue<Integer>(4);
// Testing offer
theQ.setMoves(0);
for (int i = 0; i < 9; i++)
{
Integer newItem = Integer.valueOf(i);
if (!(theQ.isFull()))
{
theQ.offer(newItem);
System.out.println(newItem + " added to Q");
}
else
{
System.out.println("No room for " + newItem);
}
}
// The moves should be tracked in the same way that you tracked them
// in your recitation exercise. Note, however, that in this implementation
// the number of moves needed for both offer() and poll() should only be
// 1 per operation. See more on this requirement in the MyQ.java comments
// and see the output in A1Out.txt
int mv = theQ.getMoves();
System.out.println("Total moves needed: " + mv);
System.out.println();
// Testing poll
theQ.setMoves(0);
while (!(theQ.isEmpty()))
{
Integer oldItem = theQ.poll();
System.out.println(oldItem + " retrieved from Q");
}
System.out.println("Total moves needed: " + mv);
System.out.println();
Integer noItem = theQ.poll();
if (noItem == null)
System.out.println("Nothing in the Q");
// Testing array management
int count = 1;
MyQ<String> theQ2 = new RandIndexQueue<String>(5);
String theItem = new String("Item " + count);
System.out.println("Adding " + theItem);
theQ2.offer(theItem);
for (int i = 0; i < 8; i++)
{
count++;
theItem = new String("Item " + count);
System.out.print("Adding " + theItem);
theQ2.offer(theItem);
theItem = theQ2.poll();
System.out.println("...and removing " + theItem);
}
int sz = theQ2.size();
System.out.println("There are " + sz + " items in the buffer\n");
// This code will test the toString() method, the Shufflable interface
// and the Indexable interface.
System.out.println("Initializing a new RandIndexQueue...");
RandIndexQueue<Integer> newData = new RandIndexQueue<Integer>(15);
for (int i = 0; i < 8; i++)
{
newData.offer(Integer.valueOf(i));
}
// The toString() method for your RandIndexQueue<T> should produce
// a String containing the contents of your collection from logical
// front (of the Queue) to the logical end (of the Queue). Note that
// this will not necessarily be the beginning to the end of the
// underlying array.
System.out.println(newData.toString());
System.out.println("Removing 3 items then adding 1");
Integer bogus = newData.poll();
bogus = newData.poll();
bogus = newData.poll();
newData.offer(Integer.valueOf(8));
System.out.println(newData.toString());
System.out.println();
// Testing Indexable<T> interface
iterate(newData);
iterate(newData);
System.out.println(newData.toString());
System.out.println("\nAbout to test Shufflable...");
newData.clear();
for (int i = 0; i < 15; i++)
{
newData.offer(Integer.valueOf(i));
}
System.out.println(newData.toString());
System.out.println("Shuffling...");
newData.shuffle();
System.out.println(newData.toString());
System.out.println("Removing 2 and adding 1");
bogus = newData.poll();
bogus = newData.poll();
newData.offer(Integer.valueOf(22));
System.out.println(newData.toString());
System.out.println("Shuffling again...");
newData.shuffle();
System.out.println(newData.toString());
}
// Generic method to test the Indexable interface
public static <T> void iterate(Indexable<T> R)
{
System.out.println("Testing Indexable...");
for (int i = 0; i < R.size()-1; i++)
{
T item1 = R.get(i);
int j = i + 1;
T item2 = R.get(j);
System.out.println("Swapping " + item1 + " and " + item2);
R.set(i, item2);
R.set(j, item1);
}
System.out.println();
}
}
import java.util.*;
public class Blackjack
{
static boolean checkRound = true; //用于debug的时候 判断round是否小于等于10,是就显示details
static int playerWins = 0;
static int dealerWins = 0;
static int push = 0;
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in); //接收输入的两个int一个是round一个是牌堆牌的副数
int desiredRounds = kbd.nextInt();
int desiredDecks = kbd.nextInt();
int deck = 0;
int round = 0;
final int NUMBER_OF_A_DECK = 52;
RandIndexQueue<Card> shoe = new RandIndexQueue<Card>(desiredDecks * NUMBER_OF_A_DECK); //将RandIndexQueue的大小设置为刚好
RandIndexQueue<Card> discard = new RandIndexQueue<Card>();
RandIndexQueue<Card> player = new RandIndexQueue<Card>();
RandIndexQueue<Card> dealer = new RandIndexQueue<Card>();
if (desiredRounds <= 10) //如果输入的个数小于等于10 就要将checkRound变为false,用于debug输出细节
checkRound = false;
while (deck < desiredDecks) //将shoe里面所有的Card初始化
{
for (Card.Suits s : Card.Suits.values())
{
for (Card.Ranks r : Card.Ranks.values())
shoe.offer(new Card(s, r));
}
deck++;
}
System.out.println("Starting Blackjack with "+ desiredRounds
+ " and " + desiredDecks + " decks in the shoe");
System.out.println();
while (round < desiredRounds)
{
shoe.shuffle();
debug("Round " + round + " beginning");
initialCards(player, dealer, shoe);
if (getValue(player) == 21 && getValue(dealer) == 21) //check blackjack
{
debug("Result: Push!");
push++;
}
else if (getValue(player) == 21)
{
debug("Result: Player Blackjack wins!");
playerWins++;
}
else if (getValue(dealer) == 21)
{
debug("Result: Dealer Blackjack wins!");
dealerWins++;
}//end blackjack
else if (getValue(player) >= 17 && getValue(dealer) >= 17) //begins comparison
{
getResult(player,dealer);
}//end comparison
else //begins hits
{
while (getValue(player) < 17) //如果初始两张手牌点数大小小于17,就要摸牌,知道大于等于17
{
debug("Player hits: " + shoe.peek());
player.offer(shoe.poll());
}
if (getValue(player) > 21) //一旦大于21就bust
{
debug("Player BUSTS: " + player.toString() + " : " + getValue(player));
debug("Result: Dealer wins!");
dealerWins++;
}
else //如果没有bust就stand,看dealer是否需要摸牌
{
debug("Player STANDS: " + player.toString() + " : " + getValue(player));
while (getValue(dealer) < 17)
{
debug("Dealer hits: " + shoe.peek());
dealer.offer(shoe.poll());
}
if (getValue(dealer) > 21)
{
debug("Dealer BUSTS: " + dealer.toString() + " : " + getValue(dealer));
debug("Result: Player wins!");
playerWins++;
}
else
{
debug("Dealer STANDS: " + dealer.toString() + " : " + getValue(dealer));
getResult(player, dealer); //两个人都stand 比较点数大小
}
}
} //end hits
while (!player.isEmpty()) //把手牌弃到弃牌堆里
discard.offer(player.poll());
while (!dealer.isEmpty())
discard.offer(dealer.poll());
player.clear(); //clear两个数组
dealer.clear();
if (shoe.size() <= ((desiredDecks * NUMBER_OF_A_DECK) / 4)) //当当前牌堆里牌的个数小于等于原个数的四分之一,就把弃牌堆里的card放到shoe里面
{
while (!discard.isEmpty())
shoe.offer(discard.poll());
System.out.println("Reshuffling the shoe in round " + round);
System.out.println();
shoe.shuffle(); //弃牌堆里面的card放完后洗牌
}
debug("");
round++;
}
System.out.println("After " + desiredRounds + " rounds, here are the results:");
System.out.println("\t \t Dealer Wins: " + dealerWins);
System.out.println("\t \t Player Wins: " + playerWins);
System.out.println("\t \t Pushes: " + push);
}
private static void debug(String text) //当round小于等于10的时候才print细节
{
if (!checkRound)
System.out.println(text);
}
private static void initialCards(RandIndexQueue<Card> player, RandIndexQueue<Card> dealer, RandIndexQueue<Card> shoe)
{
player.offer(shoe.poll()); //player的初始手牌
player.offer(shoe.poll());
debug("Player: " + player.toString() + " : " + getValue(player));
dealer.offer(shoe.poll()); //dealer的初始手牌
dealer.offer(shoe.poll());
debug("Dealer: " + dealer.toString() + " : " + getValue(dealer));
}
private static int getValue(RandIndexQueue<Card> player) //计算当前手牌的点数
{
int totalValue = 0;
int nowValue = 0;
int nowValue2 = 0;
for (int i = 0; i < player.size(); i++)
{
nowValue = player.get(i).value();
nowValue2 = player.get(i).value2();
if (nowValue == 11 )
{
if ((totalValue + nowValue) > 21 )
totalValue += nowValue2;
else
totalValue += nowValue;
}
else
totalValue += nowValue;
}
return totalValue;
}
private static void getResult(RandIndexQueue<Card> player, RandIndexQueue<Card> dealer) //比较点数出结果
{
if (getValue(player) > getValue(dealer))
{
debug("Result: Player wins!");
playerWins++;
}
else if (getValue(player) == getValue(dealer))
{
debug("Result: Push!");
push++;
}
else if (getValue(player) < getValue(dealer))
{
debug("Result: Dealer wins!");
dealerWins++;
}
}
}
A1Solution > java Blackjack 10 4
Starting Blackjack with 10 rounds and 4 decks in the shoe
Round 0 beginning
Player: Contents: Jack-of-Hearts Eight-of-Hearts : 18
Dealer: Contents: Jack-of-Hearts Jack-of-Diamonds : 20
Player STANDS: Contents: Jack-of-Hearts Eight-of-Hearts : 18
Dealer STANDS: Contents: Jack-of-Hearts Jack-of-Diamonds : 20
Result: Dealer Wins!
Round 1 beginning
Player: Contents: Eight-of-Clubs Ten-of-Diamonds : 18
Dealer: Contents: Three-of-Spades Ten-of-Hearts : 13
Player STANDS: Contents: Eight-of-Clubs Ten-of-Diamonds : 18
Dealer hits: Queen-of-Diamonds
Dealer BUSTS: Contents: Three-of-Spades Ten-of-Hearts Queen-of-Diamonds : 23
Result: Player wins!
Round 2 beginning
Player: Contents: Two-of-Clubs King-of-Clubs : 12
Dealer: Contents: Seven-of-Spades Six-of-Clubs : 13
Player hits: Ace-of-Spades
Player hits: Three-of-Clubs
Player hits: Two-of-Hearts
Player STANDS: Contents: Two-of-Clubs King-of-Clubs Ace-of-Spades Three-of-Clubs Two-of-Hearts : 18
Dealer hits: Ten-of-Diamonds
Dealer BUSTS: Contents: Seven-of-Spades Six-of-Clubs Ten-of-Diamonds : 23
Result: Player wins!
Round 3 beginning
Player: Contents: King-of-Hearts Six-of-Clubs : 16
Dealer: Contents: Ace-of-Clubs Five-of-Diamonds : 16
Player hits: Ace-of-Diamonds
Player STANDS: Contents: King-of-Hearts Six-of-Clubs Ace-of-Diamonds : 17
Dealer hits: Six-of-Hearts
Dealer hits: Nine-of-Spades
Dealer STANDS: Contents: Ace-of-Clubs Five-of-Diamonds Six-of-Hearts Nine-of-Spades : 21
Result: Dealer Wins!
Round 4 beginning
Player: Contents: Five-of-Clubs Four-of-Spades : 9
Dealer: Contents: Three-of-Hearts Jack-of-Clubs : 13
Player hits: Nine-of-Diamonds
Player STANDS: Contents: Five-of-Clubs Four-of-Spades Nine-of-Diamonds : 18
Dealer hits: Eight-of-Diamonds
Dealer STANDS: Contents: Three-of-Hearts Jack-of-Clubs Eight-of-Diamonds : 21
Result: Dealer Wins!
Round 5 beginning
Player: Contents: Queen-of-Hearts King-of-Clubs : 20
Dealer: Contents: Ten-of-Hearts Queen-of-Hearts : 20
Player STANDS: Contents: Queen-of-Hearts King-of-Clubs : 20
Dealer STANDS: Contents: Ten-of-Hearts Queen-of-Hearts : 20
Result: Push!
Round 6 beginning
Player: Contents: Seven-of-Clubs Queen-of-Spades : 17
Dealer: Contents: Seven-of-Spades Six-of-Hearts : 13
Player STANDS: Contents: Seven-of-Clubs Queen-of-Spades : 17
Dealer hits: Three-of-Spades
Dealer hits: King-of-Diamonds
Dealer BUSTS: Contents: Seven-of-Spades Six-of-Hearts Three-of-Spades King-of-Diamonds : 26
Result: Player wins!
Round 7 beginning
Player: Contents: Eight-of-Clubs Two-of-Diamonds : 10
Dealer: Contents: Three-of-Diamonds Ace-of-Hearts : 14
Player hits: Nine-of-Hearts
Player STANDS: Contents: Eight-of-Clubs Two-of-Diamonds Nine-of-Hearts : 19
Dealer hits: Five-of-Hearts
Dealer STANDS: Contents: Three-of-Diamonds Ace-of-Hearts Five-of-Hearts : 19
Result: Push!
Round 8 beginning
Player: Contents: King-of-Clubs Jack-of-Clubs : 20
Dealer: Contents: King-of-Diamonds King-of-Clubs : 20
Player STANDS: Contents: King-of-Clubs Jack-of-Clubs : 20
Dealer STANDS: Contents: King-of-Diamonds King-of-Clubs : 20
Result: Push!
Round 9 beginning
Player: Contents: Five-of-Spades Two-of-Spades : 7
Dealer: Contents: Ace-of-Spades Queen-of-Hearts : 21
Result: Dealer Blackjack wins!
After 10 rounds, here are the results:
Dealer Wins: 4
Player Wins: 3
Pushes: 3
------------------------
A1Solution > java Blackjack 10 6
Starting Blackjack with 10 rounds and 6 decks in the shoe
Round 0 beginning
Player: Contents: Two-of-Clubs Jack-of-Diamonds : 12
Dealer: Contents: Three-of-Clubs Eight-of-Clubs : 11
Player hits: Two-of-Diamonds
Player hits: Jack-of-Clubs
Player BUSTS: Contents: Two-of-Clubs Jack-of-Diamonds Two-of-Diamonds Jack-of-Clubs : 24
Result: Dealer wins!
Round 1 beginning
Player: Contents: Seven-of-Hearts Seven-of-Hearts : 14
Dealer: Contents: King-of-Spades Ten-of-Spades : 20
Player hits: Ace-of-Hearts
Player hits: Six-of-Clubs
Player STANDS: Contents: Seven-of-Hearts Seven-of-Hearts Ace-of-Hearts Six-of-Clubs : 21
Dealer STANDS: Contents: King-of-Spades Ten-of-Spades : 20
Result: Player Wins!
Round 2 beginning
Player: Contents: Six-of-Diamonds Five-of-Clubs : 11
Dealer: Contents: Jack-of-Spades Six-of-Diamonds : 16
Player hits: Queen-of-Diamonds
Player STANDS: Contents: Six-of-Diamonds Five-of-Clubs Queen-of-Diamonds : 21
Dealer hits: Eight-of-Diamonds
Dealer BUSTS: Contents: Jack-of-Spades Six-of-Diamonds Eight-of-Diamonds : 24
Result: Player wins!
Round 3 beginning
Player: Contents: Nine-of-Spades Jack-of-Diamonds : 19