Commit 31cebd2c authored by mariarahime's avatar mariarahime
Browse files

1

parent 3b9c7f85
import java.util.*;
/**
* The class {@code Bettelmann} simulated the card game 'Bettelmann'. You can construct objects
* either by providing the piles of cards of the two players, or by requesting a shuffled
* distribution of cards.
*/
public class Bettelmann {
private Deque<Card> closedPile1;
private Deque<Card> closedPile2;
private int winner = -1;
/**
* Constructor which initializes both players with empty piles.
*/
public Bettelmann() {
closedPile1 = new LinkedList<>();
closedPile2 = new LinkedList<>();
}
/**
* Constructor which initializes both players with the provided piles of cards.
*
* @param pile1 pile of cards of player 1.
* @param pile2 pile of cards of player 2.
*/
public Bettelmann(Deque<Card> pile1, Deque<Card> pile2) {
closedPile1 = pile1;
closedPile2 = pile2;
}
/**
* Returns the closed pile of player 1 (required for the tests).
*
* @return The closed pile of player 1.
*/
public Deque<Card> getClosedPile1() {
return closedPile1;
}
/**
* Returns the closed pile of player 2 (required for the tests).
*
* @return The closed pile of player 2.
*/
public Deque<Card> getClosedPile2() {
return closedPile2;
}
/**
* Play one round of the game. This includes drawing more cards, when both players
* have drawn cards of the same rank. At the end of the round, the player with the
* higher ranked card wins the trick, so all drawn cards from that round are added
* to the bottom of her/his closed pile of cards.
*/
public void playRound() {
// TODO implement this method
}
/**
* Returns the winner of the game after the end, or -1 during the game.
*
* @return the winner of game (1 or 2), or -1 while the game is ongoing.
*/
public int getWinner() {
return winner;
}
/**
* Deal the given deck of cards alternately to the two players.
* Side effect: The deck is empty after calling this method.
*
* @param deck The deck of cards that is distributed to the players.
*/
public void distributeCards(Stack<Card> deck) {
closedPile1.clear();
closedPile2.clear();
// use addFirst() because the last distributed card should be drawn first
while (!deck.isEmpty()) {
Card card = deck.pop();
closedPile1.addFirst(card);
if (!deck.isEmpty()) {
card = deck.pop();
closedPile2.addFirst(card);
}
}
}
/**
* Shuffle a deck of cards and distribute it evenly to the two players.
*/
public void distributeCards() {
Stack<Card> deck = new Stack<>();
for (int i = 0; i < Card.nCards; i++){
deck.add(new Card(i));
}
Collections.shuffle(deck);
distributeCards(deck);
}
/**
* Returns a String representation of closed piles of cards of the two players.
*
* @return String representation of the state of the game.
*/
@Override
public String toString() {
return "Player 1: " + closedPile1 + "\nPlayer 2: " + closedPile2;
}
public static void main(String[] args) {
/*
// Game with a complete, shuffled deck
Bettelmann game = new Bettelmann();
game.distributeCards();
*/
// For testing, you may also use specific distribtions and a small number of cards like this:
int[] deckArray = {28, 30, 6, 23, 17, 14};
Stack<Card> deck = new Stack<>();
for (int id : deckArray) {
deck.push(new Card(id));
}
Bettelmann game = new Bettelmann();
game.distributeCards(deck);
// This part is the same for both of the above variants
System.out.println("Initial situation (top card first):\n" + game);
int round = 0;
while (round < 1000000 && game.getWinner()<0) {
round++;
game.playRound();
System.out.println("State after round " + round + ":\n" + game);
}
}
}
import java.util.Objects;
/**
* The Class {@code Card} represents playing cards of a 32-cards deck. It implements the interface
* Comparable, in which the comparison is based on the rank of the cards, while suits are ignored.
* This class is used in the class {@link Bettelmann}, which simulates the card game 'Bettelmann'.
*/
public class Card implements Comparable<Card> {
private int id;
public static int nCards = 32;
/**
* Constructor of {@code Card} objects
*
* @param id The id of the card from 0 (7 diamond) to 31 (ace of clubs)
*/
public Card(int id) {
this.id = id;
}
/**
* Returns the suit of this card as symbol String.
*
* @return unicode symbol of the suit of this card
*/
public String getSuit() {
//String[] suitList = {"Karo", "Herz", "Pik", "Kreuz"};
String[] suitList = {"\u2666", "\u2665", "\u2660", "\u2663"};
return suitList[id%4];
}
/**
* Returns the value of the card (7 -> 0, 8 -> 1, ... King -> 6, Ace -> 7)
*
* @return value of this card, i.e., the rank minus 7
*/
public int getValue() {
return id / 4;
}
/**
* Returns the value of this card as a short String.
*
* @return Value of the card as short String
*/
public String getValueAsString() {
// String[] valueList = {"Sieben", "Acht", "Neun", "Zehn", "Bube", "Dame", "König", "As"};
String[] valueList = {"7", "8", "9", "10", "B", "D", "K", "A"};
return valueList[getValue()];
}
/**
* Compares Card objects based on their value, regardless of their suit.
*
* @param that the other Card, to which this card is compared
* @return results of the comparison (<0 if this card has a lower rank than that card, =0 if equivalent, >0 else)
*/
@Override
public int compareTo(Card that) {
return Integer.compare(this.getValue(), that.getValue());
}
/**
* Returns a short String representation of this card (suit then value)
*
* @return String representation of the {@code Card} object
*/
@Override
public String toString() {
return getSuit() + getValueAsString();
}
/**
* Return whether this card coincides (in rank and suit) with a given object.
*
* @param o Object (typically a {@link Card}) with which this card is compared
* @return whether this card is the same as the given object
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card that = (Card) o;
return id == that.id;
}
public static void main(String[] args) {
for (int id = 0; id < nCards; id++) {
Card card = new Card(id);
System.out.println("ID = " + id + " -> " + card);
}
}
}
import java.util.Stack;
/** * A class for constructing a Decimal-to-Binary Number- Converter; * contains a main method for demonstration. */
public class Dec2Bin {
public Stack<Integer> binStack; // We make it public to modify it in our tests.
private int N;
/**
* Constructor of an empty object. Use method {@code convert()} to convert a number.
*/
public Dec2Bin() {
binStack = new Stack<>();
}
/**
* Returns the number that is converted as {@code int}.
*
* @return the converted number
*/
public int getN() {
return N;
}
/**
* Converts the given number into binary format, with each digit being represented in a
* stack of {@code int}.
*
* @param N the number that is to be converted.
*/
public void convert(int N) {
// TODO implement this method
this.N = N;
Stack <Integer> binStack = new Stack<>();
while(N>0) {
binStack.push(N % 2);
N = N / 2;
}
}
/**
* Returns the digits that are stored in {@code binStack} as a string. To is the binary format of the
* converted number.
* For testing purpose, we require that the function works also, if the variable {@code binStack} is
* modified externally.
*
* @return a string representation of the number in binary format.
*/
@Override
public String toString() {
// Caution: Stack.toString() does NOT respect stack order. Do not use it.
// TODO implement this method
String[] binStack = new String[];
return toString() ;
}
public static void main(String[] args) {
Dec2Bin dec2bin = new Dec2Bin();
dec2bin.convert(50);
System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
// Do it another time to demonstrate that toString does not erase the binStack.
System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment