Skip to content
Snippets Groups Projects
Commit 08e30060 authored by Andreas Omholt Olsen's avatar Andreas Omholt Olsen
Browse files

LF oving3

parent 0125b995
Branches
No related tags found
No related merge requests found
package oving3;
import java.util.ArrayList;
public class CardDeck {
// List to hold Card objects
private ArrayList<Card> cards;
public CardDeck(int suitSize) {
if (suitSize < 0 || suitSize > 13) {
throw new IllegalArgumentException("Illegal suit size " + suitSize);
}
this.cards = new ArrayList<Card>();
for (char suit : Card.SUITS.toCharArray()) {
for (int face = 1; face <= suitSize; face++) {
Card card = new Card(suit, face);
cards.add(card);
}
}
}
public int getCardCount() {
return this.cards.size();
}
public Card getCard(int i) {
if (i < 0 || i >= this.getCardCount()) {
throw new IllegalArgumentException(
String.format("%s is an illegal card index, when the size of the deck is %s", i, getCardCount()));
}
return cards.get(i);
}
public void shufflePerfectly() {
int halfSize = cards.size() / 2;
for (int i = 0; i < halfSize; i++) {
Card card = cards.remove(halfSize + i);
cards.add(i * 2 + 1, card);
}
}
@Override
public String toString() {
return "[Deck " + cards.toString().substring(1);
}
}
package oving3;
public class Nim {
private int[] piles;
public Nim() {
this(10);
}
public Nim(int pileSize) {
this.piles = new int[] { pileSize, pileSize, pileSize };
}
public void removePieces(int number, int targetPile) {
if (this.isGameOver()) {
throw new IllegalStateException("Cannot remove pieces when game is over");
}
if (!this.isValidMove(number, targetPile)) {
throw new IllegalArgumentException("Move is not valid");
}
piles[targetPile] -= number;
}
public boolean isValidMove(int number, int targetPile) {
return number > 0 && this.getPile(targetPile) >= number && !this.isGameOver();
}
public boolean isGameOver() {
return this.getPile(0) == 0 || this.getPile(1) == 0 || this.getPile(2) == 0;
}
public int getPile(int targetPile) {
return this.piles[targetPile];
}
@Override
public String toString() {
return String.format("Piles: %d, %d, %d", piles[0], piles[1], piles[2]);
}
}
package oving3;
import java.util.Stack;
public class RPNCalc {
private Stack<Double> operandStack;
public RPNCalc() {
operandStack = new Stack<Double>();
}
public void push(double value) {
operandStack.push(value);
}
public int getSize() {
return operandStack.size();
}
public double peek(int n) {
if (n < 0 || operandStack.size() <= n) {
return Double.NaN;
}
return operandStack.get(operandStack.size() - n - 1);
}
public double pop() {
return this.pop(Double.NaN);
}
public double pop(double defaultValue) {
if (operandStack.isEmpty()) {
return defaultValue;
}
return operandStack.pop();
}
// perform the operation denoted by op
// each operation pops and pushes values off and onto the operand stack,
public void performOperation(char op) {
double d1, d2;
switch (op) {
case '+':
// pop two operands and push the sum, missing values default to 0.0
d1 = pop(0.0);
d2 = pop(0.0);
push(d2 + d1);
break;
case '-':
// pop two operands and push the difference, missing values default to 0.0
d1 = pop(0.0);
d2 = pop(0.0);
push(d2 - d1);
break;
case '*':
// pop two operands and push the product, missing values default to 1.0
d1 = pop(1.0);
d2 = pop(1.0);
push(d2 * d1);
break;
case '/':
// pop two operands and push the quotient, missing values default to 1.0
d1 = pop(1.0);
d2 = pop(1.0);
push(d2 / d1);
break;
// dup
case ',':
d1 = pop(0.0);
// push back twice
push(d1);
push(d1);
break;
// pop
case '.':
// remove the topmost value
pop(0.0);
break;
// swap
case '~':
// swap the two topmost values, by popping them and pushing them in reverse
// order
d1 = pop(0.0);
d2 = pop(0.0);
push(d1);
push(d2);
break;
// extra operators
// remainder
case '%':
d1 = pop(1.0);
d2 = pop(1.0);
push(d2 % d1);
break;
// absolute
case '|':
push(Math.abs(pop(0.0)));
break;
// square root
case 'v':
push(Math.sqrt(pop(1.0)));
break;
// power of
case '^':
d1 = pop(1.0);
d2 = pop(1.0);
push(Math.pow(d2, d1));
break;
// floor
case '_':
double d = pop(0.0);
push(Math.floor(d));
break;
// compare
case '=':
d1 = pop(1.0);
d2 = pop(1.0);
push(Double.compare(d2, d1));
break;
// signum
case '?':
push(Math.signum(pop(0.0)));
break;
// pi
case 'p':
case 'π':
push(Math.PI);
break;
// e
case 'e':
push(Math.exp(1.0));
break;
}
}
@Override
public String toString() {
return operandStack.toString();
}
}
package oving3.debugging;
public class CoffeeCup {
private double capacity;
private double currentVolume;
public CoffeeCup() {
this.capacity = 0.0;
this.currentVolume = 0.0;
}
public CoffeeCup(double capacity, double currentVolume) {
if (isValidCapacity(capacity)) {
this.capacity = capacity;
} else {
throw new IllegalArgumentException("Illegal capacity given.");
}
if (isValidVolume(currentVolume)) {
this.currentVolume = currentVolume;
} else {
throw new IllegalArgumentException("Illegal volume given.");
}
}
private boolean isValidCapacity(double capacity) {
if (capacity >= 0.0) {
return true;
}
return false;
}
public void increaseCupSize(double biggerCapacity) {
if (isValidCapacity(biggerCapacity)) {
this.capacity += biggerCapacity;
}
}
private boolean isValidVolume(double volume) {
if (volume > this.capacity || volume < 0.0) {
return false;
}
return true;
}
private boolean canDrink(double volume) {
if (this.currentVolume >= volume) {
return true;
}
return false;
}
public void drinkCoffee(double volume) {
if (isValidVolume(volume) && canDrink(volume)) {
this.currentVolume -= volume;
} else {
throw new IllegalArgumentException("You can't drink that much coffee!");
}
}
public void fillCoffee(double volume) {
if (isValidVolume(this.currentVolume + volume)) {
this.currentVolume += volume;
} else {
throw new IllegalArgumentException("You just poured coffee all over the table. Good job.");
}
}
}
package oving3.debugging;
import java.util.Random;
public class CoffeeCupProgram {
private CoffeeCup cup;
private Random r;
public void init() {
cup = new CoffeeCup();
r = new Random(123456789L);
}
public void run() {
part1();
part2();
}
private void part1() {
cup.increaseCupSize(40.0);
cup.fillCoffee(20.5);
cup.drinkCoffee(Math.floor(r.nextDouble() * 20.5));
cup.fillCoffee(32.5);
cup.drinkCoffee(Math.ceil(r.nextDouble() * 38.9));
cup.drinkCoffee(Math.ceil(r.nextDouble() * 42));
cup.increaseCupSize(17);
cup.drinkCoffee(40);
cup.drinkCoffee(Math.ceil(r.nextDouble() * 42));
cup.drinkCoffee(Math.floor(r.nextDouble() * 20.5));
cup.fillCoffee(32.5);
cup.drinkCoffee(Math.ceil(r.nextDouble() * 38.9));
cup.drinkCoffee(Math.ceil(r.nextDouble() * 42));
cup.increaseCupSize(17);
}
private void part2() {
cup = new CoffeeCup(40.0, 20.5);
r = new Random(987654321L);
cup.drinkCoffee(Math.floor(r.nextDouble() * 20.5));
cup.fillCoffee(Math.floor(r.nextDouble() * 30));
cup.drinkCoffee(Math.ceil(r.nextDouble() * 38.9));
cup.drinkCoffee(Math.ceil(r.nextDouble() * 42));
cup.increaseCupSize(Math.floor(r.nextDouble() * 26));
cup.fillCoffee(Math.ceil(r.nextDouble() * 59));
cup.drinkCoffee(Math.ceil(r.nextDouble() * 42));
cup.increaseCupSize(Math.floor(r.nextDouble() * 35));
cup.fillCoffee(Math.floor(r.nextDouble() * 30));
cup.increaseCupSize(Math.floor(r.nextDouble() * 26));
}
public static void main(String[] args) {
CoffeeCupProgram program = new CoffeeCupProgram();
program.init();
program.run();
}
}
package oving4;
public class Card {
// the suit (farge), one of the values 'S' (spades), 'H' (hearts), 'D'
// (diamonds) and 'C' (clubs)
private char suit;
// the value, 1 for the ace, 2 - 10, 11 (knight), 12 (queen) and 13 (king). -1
// is invalid
private int face = -1;
// the set of suits in decreasing order
public final static String SUITS = "SHDC";
public Card(char suit, int face) {
if (SUITS.indexOf(suit) < 0) {
throw new IllegalArgumentException("Illegal suit: " + suit);
}
if (face < 1 || face > 13) {
throw new IllegalArgumentException("Illegal face: " + face);
}
this.suit = suit;
this.face = face;
}
/*
* Returns suit and face as a string
* E.g. Ace of spades is S1 and king of clubs is C13
*/
public String toString() {
return String.valueOf(suit) + face;
}
public char getSuit() {
return this.suit;
}
public int getFace() {
return this.face;
}
}
package oving4;
import java.util.ArrayList;
public class CardDeck {
// array to hold Card objects, filled in the constructor
private ArrayList<Card> cards;
public CardDeck(int suitSize) {
cards = new ArrayList<Card>();
for (int i = 0; i < Card.SUITS.length(); i++) {
for (int face = 1; face <= suitSize; face++) {
Card card = new Card(Card.SUITS.charAt(i), face);
cards.add(card);
}
}
}
@Override
public String toString() {
return "[Deck " + cards.toString().substring(1);
}
public void deal(CardHand hand, int handSize) {
for (int i = 0; i < handSize; i++) {
hand.addCard(cards.remove(cards.size() - 1));
}
}
public int getCardCount() {
return cards.size();
}
public Card getCard(int i) {
if (i < 0 || i >= getCardCount()) {
throw new IllegalArgumentException(
String.format("%s is an illegal card index, when the size of the deck is %s", i, getCardCount()));
}
return cards.get(i);
}
public void shufflePerfectly() {
int halfSize = cards.size() / 2;
for (int i = 0; i < halfSize; i++) {
Card card = cards.remove(halfSize + i);
cards.add(i * 2 + 1, card);
}
}
}
package oving4;
import java.util.ArrayList;
public class CardHand {
// array to hold Card objects, filled in the constructor
private ArrayList<Card> cards;
public CardHand() {
cards = new ArrayList<Card>();
}
@Override
public String toString() {
return "[Hand " + cards.toString().substring(1);
}
public void addCard(Card card) {
this.cards.add(card);
}
public int getCardCount() {
return cards.size();
}
public Card getCard(int i) {
if (i < 0 || i >= getCardCount()) {
throw new IllegalArgumentException(
String.format("%s is an illegal card index, when the size of the hand is %s", i, getCardCount()));
}
return cards.get(i);
}
public Card play(int i) {
return cards.remove(i);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment