Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static Deck deck = new Deck();
- public static boolean peace = true;
- public static Player player1 = new Player(); //player1's hand
- public static Player player2 = new Player(); //player2's hand
- public static Board board1 = new Board(); //player1's board
- public static Board board2 = new Board(); //player2's board
- public static Player[] players = {player1, player2};
- public static Board[] boards = {board1, board2};
- private static void deal(){
- for(int i = 1; i <= 26; i++){
- Card c1 = deck.getCard();
- player1.put(c1);
- Card c2 = deck.getCard();
- player2.put(c2);
- }
- }
- private static void playRound(){
- Card play1 = player1.get();
- Card play2 = player2.get();
- if (play1.compareTo(play2) > 0 //player1 gets the card
- player1.put(play1);
- );
- if (play1.compareTo(play2) < 0 //player2 gets the card
- );
- if (play1.compareTo(play2) == 0 //war
- );
- }
- private static void war() {
- }
- public static void main(String[] args) {
- deal();
- System.out.println(player1);
- }
- }
- // Card.java
- public class Card implements Comparable<Card> {
- private String mySymbol;
- private int myRank;
- private char mySuit;
- public Card (char rank, char suit) {
- // Note how switch works. It's really useful.
- // Note also the character arithmetic. Remember, a char has a Unicode
- // value and that needs to be converted.
- switch (rank) {
- case 'A': myRank = 14; break;
- case 'K': myRank = 13; break;
- case 'Q': myRank = 12; break;
- case 'J': myRank = 11; break;
- case 'T': myRank = 10; break;
- default: myRank = rank - '0';
- }
- mySuit = suit;
- mySymbol = "" + rank + suit;
- }
- public int getRank() { return myRank; }
- public char getSuit() { return mySuit; }
- public String getSymbol() { return mySymbol; }
- public int compareTo(Card card) {
- {
- if (getRank() < card.getRank())
- return -1;
- if (getRank() > card.getRank())
- return 1;
- else return 0;
- }
- }
- public boolean equals(Card card) {
- if (getRank() == card.getRank())
- return true;
- else return false;
- }
- public String toString() {
- return mySymbol;
- }
- }
- public class Player extends Queue<Card>{
- public Player(){
- super();
- }
- }
- // Deck.java
- import java.util.*;
- public class Deck { // This is slow--can you think of a better way? (There are many...)
- HashSet myCards = new HashSet();
- char[] suits = { 'S', 'H', 'C', 'D' };
- char[] ranks = { 'A', 'K', 'Q', 'J', 'T',
- '9', '8', '7', '6', '5',
- '4', '3', '2' };
- Iterator cardGetter; // This is used in a really ugly way
- Random random = new Random();
- public Deck() {
- init();
- }
- private void init() {
- for (int i = 0; i < suits.length; i++)
- for (int j = 0; j < ranks.length; j++)
- myCards.add(new Card(ranks[j], suits[i]));
- cardGetter = myCards.iterator();
- }
- // Note why we need to adhere to abstractions. The shuffle method
- // doesn't really shuffle at all upon inspection. However, in conjunction
- // with getCard(), the behavior of shuffling and getting cards from the
- // deck is perfectly reasonable.
- //
- // While I would not recommend writing shuffle() this way in general,
- // the point is that the Deck abstraction should be treated like a black
- // box, not an open book.
- public void shuffle() {
- myCards.clear();
- init();
- }
- public Card getCard() {
- Card c;
- cardGetter = myCards.iterator(); // Start from beginning each time
- int next = random.nextInt(size()) + 1;
- c = (Card) cardGetter.next();
- for (int i = 1; i < next; i++) {
- c = (Card) cardGetter.next();
- }
- cardGetter.remove(); // Note that if a card is dealt,
- return c; // it is taken out of the deck
- }
- public boolean isEmpty() {
- return myCards.isEmpty();
- }
- public int size() {
- return myCards.size();
- }
- }
- import java.util.LinkedList;
- public class Queue<E> extends LinkedList<E> {
- public Queue() {}
- public void put(E o) {
- addLast(o);
- }
- public E get() {
- if (!this.isEmpty()) {
- return removeFirst();
- } else {
- System.err.println("You can\'t do that!");
- return null;
- }
- }
- public E peek() {
- return getFirst();
- }
- }
- import java.util.LinkedList;
- public class Stack<E> extends LinkedList<E> {
- public Stack() {}
- public void put(E o) {
- addFirst(o);
- }
- public E get() {
- if (!this.isEmpty()) {
- return removeFirst();
- } else {
- System.err.println("You can\'t do that!");
- return null;
- }
- }
- public E peek() {
- return getFirst();
- }
- }
- public class Board extends Stack {
- public Board(){
- super();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement