To Project Proj6
Source Code for Project Proj6
// Source code for the Card class.
public class Card
{
private int rank;
private int suit;
public Card(int theRank, int theSuit)
{
rank = theRank;
suit = theSuit;
}
public int getRank( )
{
return rank;
}
public int getSuit( )
{
return suit;
}
public int compareTo(Object other)
{
int otherRank = ((Card) other).rank;
return rank - otherRank;
}
public boolean equals(Object other)
{
int otherRank = ((Card) other).rank;
return rank == otherRank;
}
public String toString( )
{
String rankSymbol, suitSymbol;
switch(rank)
{
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
rankSymbol = String.valueOf(rank);
break;
case 11:
rankSymbol = "J";
break;
case 12:
rankSymbol = "Q";
break;
case 13:
rankSymbol = "K";
break;
case 14:
rankSymbol = "A";
break;
default:
rankSymbol = "Illegal Rank";
}
switch(suit)
{
case 1:
suitSymbol = "C";
break;
case 2:
suitSymbol = "D";
break;
case 3:
suitSymbol = "H";
break;
case 4:
suitSymbol = "S";
break;
default:
suitSymbol = "Illegal Suit";
break;
}
return rankSymbol + suitSymbol;
}
public static void main(String[ ] args)
{
Card c = new Card(6, 3);
System.out.println(c);
System.out.println(c.getRank( ) + " " + c.getSuit( ));
System.out.println( );
Card d = new Card(11, 2);
System.out.println(d);
System.out.println(d.getRank( ) + " " + d.getSuit( ));
System.out.println( );
Card e = new Card(13, 4);
System.out.println(e);
System.out.println(e.getRank( ) + " " + e.getSuit( ));
System.out.println( );
Card f = new Card(13, 1);
System.out.println(f);
System.out.println(f.getRank( ) + " " + f.getSuit( ));
System.out.println( );
System.out.println(f.equals(e) + " " + f.equals(d));
System.out.println(d.compareTo(e) + " " +
d.compareTo(c) + " " + d.compareTo(d));
}
}
// Source code of the toString method
// of the Deck class.
public String toString( )
{
String temp = "";
for(int i = 0; i <= count - 1 && array[i] != null; i++)
temp += array[i].toString( ) + " ";
return temp;
}
// Source code of the main method
// of the Deck class.
public static void main(String[ ] args)
{
// Create and test Deck d.
Deck d = new Deck( );
d.add(new Card(3, 2));
d.add(new Card(10, 3));
d.add(new Card(12, 4));
d.add(new Card(5, 1));
System.out.println(d);
System.out.println(d.draw( ));
System.out.println(d);
System.out.println("Count of d is " + d.getCount( ));
System.out.println( );
// Create and test Deck e.
Deck e = new Deck( );
e.add(new Card(9, 1));
e.add(new Card(14, 3));
e.add(new Card(6, 2));
System.out.println("Count of e is " + e.getCount( ));
System.out.println(e);
System.out.println(e.draw( ));
System.out.println("Count of e is " + e.getCount( ));
System.out.println(e);
System.out.println( );
// Test deal method.
d.deal(e);
System.out.println("Count of d is " + d.getCount( ));
System.out.println(d);
System.out.println("Count of e is " + e.getCount( ));
System.out.println(e);
// Create a new 52 card deck.
Deck f = new Deck( );
f.createNew( );
System.out.println("Count of f is " + f.getCount( ));
System.out.println(f);
}
// Source code for the toString method
// of the ShuffleableDeck class.
public String toString( )
{
return "array: " + super.toString( ) + "\r\n" +
"aux1: " + aux1.toString( ) + "\r\n" +
"aux2: " + aux2.toString( );
}
// Source code for the main method
// of the ShuffleableDeck class.
public static void main(String[ ] args)
{
ShuffleableDeck d = new ShuffleableDeck( );
d.createNew( );
System.out.println(d);
d.cut( );
d.shuffle( );
System.out.println(d);
System.out.println("Count of d is " + d.getCount( ));
ShuffleableDeck e = new ShuffleableDeck( );
e.add(new Card(3, 2));
e.add(new Card(10, 3));
e.add(new Card(12, 4));
e.add(new Card(5, 1));
e.add(new Card(14, 3));
System.out.println(e);
e.cut( );
e.shuffle( );
System.out.println(e);
System.out.println("Count of e is " + e.getCount( ));
}
// Source code for the toString method of the War class.
public String toString( )
{
return "A count: " + playerA.getCount( ) +
" B count: " + playerB.getCount( ) +
" Game over: " + gameOver( );
}
// Source code for the main method of the War class.
public static void main(String[ ] args)
{
War w = new War( );
System.out.println(w);
System.out.println(w.gameOver( ));
w.play( );
System.out.println(w.gameOver( ));
for(int i = 1; i <= 5; i++)
{
System.out.println(w);
w.play( );
}
System.out.println(w);
System.out.println(w.gameOver( ));
}
// Source code of the main method of the RunWar class.
public static void main(String[ ] args)
{
boolean debug = false;
War warGame = new War( );
int nMoves = 0;
for(nMoves = 0; !warGame.gameOver( ); nMoves++)
{
if (debug)
System.out.println(
nMoves + " " + warGame.toString( ));
warGame.play( );
}
System.out.println("Game over.");
System.out.println(nMoves + " " + warGame.toString( ));
}