Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use maven; add short deck variant #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ build.xml
manifest.mf
sonar-project.properties
.sonar/*
.jacocoverage/*
.jacocoverage/*
target/*
18 changes: 18 additions & 0 deletions pokerenlighter-simulator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/use-example" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.javafling</groupId>
<artifactId>pokerenlighter</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.javafling.pokerenlighter.combination;

import java.util.ArrayList;
import java.util.List;

/**
* One of the most important classes in the program.
* It describes a (poker) card, by its 2 elements: rank and color.
*
*
* @author Radu Murzea
*/
public class Card
Expand All @@ -26,7 +29,7 @@ public Card(char x, char y)
{
this(Card.getRank(x), y);
}

/**
* Creates a card of the specified rank and color.
*
Expand All @@ -39,7 +42,7 @@ public Card(int x, char y)
{
// The rank must be a valid card rank (between 2 and 14 inclusively)
if (x < 2 || x > 14) {
throw new IllegalArgumentException("Invalid card rank");
throw new IllegalArgumentException("Invalid card rank " + x);
} else {
this.card = x;
}
Expand All @@ -52,6 +55,17 @@ public Card(int x, char y)
}
}

/**
* Creates a card of the specified rank and color.
*
* @param card the card As, Ah, Td, 6s...
*
* @throws IllegalArgumentException If x or y have unacceptable values.
*/
public Card(String card) {
this(Card.getRank(card.charAt(0)), card.charAt(1));
}

/**
* Checks whether 2 cards are suited (they have the same color).
*
Expand All @@ -73,7 +87,7 @@ public final int getRank()
{
return this.card;
}

/**
* Returns the card color (same one used to construct the card).
*
Expand All @@ -83,7 +97,7 @@ public final char getColor()
{
return this.color;
}

/**
* Returns the card rank as a character ('3', 7', 'A', 'K', etc.).
*
Expand Down Expand Up @@ -152,7 +166,7 @@ public static char getCharCard(int x)
* Compares 2 cards. Two cards are equal if they have the same rank and the same color.
*
* @param c The card to compare.
*
*
* @return true if this object equals c, false otherwise.
*/
@Override
Expand Down Expand Up @@ -189,4 +203,21 @@ public String toString()
{
return Character.toString(getCharCard()) + Character.toString(this.color);
}

/**
* Returns an array of cards based on the given string. The string should look like <b>TcJh</b> for hole cards or
* <b>AsKc6s</b> for a flop.
*
* @param cards the card string to parse
* @return array of cards
*/
public static Card[] of(String cards) {
List<Card> cardList = new ArrayList<>();
for (int i = 0; i < cards.length(); i = i + 2) {
cardList.add(new Card(cards.substring(i, i + 2)));
}
Card[] cardArray = new Card[cardList.size()];
return cardList.toArray(cardArray);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,54 @@
* Representation of a standard deck of 52 (French) cards. There are 13 cards of each color (4 colors).
* <br /><br />
* This class is not thread-safe.
*
*
* @author Radu Murzea
*/
public class Deck
{
//storage place for the cards
private Card[] cards;
protected Card[] cards;

//random number generator. Needed for the shuffle () method.
private HighQualityRandomGenerator rand;
protected HighQualityRandomGenerator rand;

//size of the deck (needed since adding and removal of cards from the deck is permitted)
private int size;

protected int size;

protected int fullDeckSize;

/**
* Constructs a deck of cards with the 52 unique cards.
* The initial order is: 2c, 3c, 4c, ... Kc, Ac, 2d, 3d, 4d, ... Kd, Ad, 2h, 3h, 4h, ... Kh, Ah,
* 2s, 3s, 4s, ... Ks, As.
* <br />
* By shuffling the deck, this arrangement is lost and cannot be restored.
*/
public Deck()
public Deck() {
this(52, 2, 13);
}

public Deck(int deckSize, int lowCard, int highCard)
{
size = 52;
size = deckSize;
fullDeckSize = deckSize;
cards = new Card[size];

for (int j = 2, i = 0; i < 13; ++j, ++i) {
cards[i + 0 * 13] = new Card(j, 'c');
cards[i + 1 * 13] = new Card(j, 'd');
cards[i + 2 * 13] = new Card(j, 'h');
cards[i + 3 * 13] = new Card(j, 's');
int offset = highCard - lowCard + 2;
for (int j = lowCard, i = 0; i < offset; ++j, ++i) {
cards[i + 0 * offset] = new Card(j, 'c');
cards[i + 1 * offset] = new Card(j, 'd');
cards[i + 2 * offset] = new Card(j, 'h');
cards[i + 3 * offset] = new Card(j, 's');
}

rand = new HighQualityRandomGenerator();
}

public int getFullDeckSize() {
return fullDeckSize;
}

/**
* Shuffles the <code>Deck</code>.
*
Expand All @@ -53,7 +65,7 @@ public final void shuffle(int intensity)
if (intensity < 5 || intensity > 30) {
throw new IllegalArgumentException("Invalid intensity range");
}

//repeat "intensity" times
for (int j = 0; j < intensity; ++j) {
//each card is swapped with another card (random)
Expand All @@ -65,19 +77,19 @@ public final void shuffle(int intensity)
}
}
}

/**
* Removes from the <code>Deck</code> the <code>Card</code> given by its parameter.
* If the <code>Card</code> is not in the <code>Deck</code> nothing happens.
*
* @param c The <code>Card</code> to be removed.
* @param c The <code>Card</code> to be removed.
*/
public final void removeCard(Card c)
{
if (c == null) {
throw new NullPointerException("Attempted to remove a NULL card from the deck");
}

//go through the whole deck
for (int i = 0; i < size; ++i) {
//find the card
Expand All @@ -97,26 +109,26 @@ public final void removeCard(Card c)
}
}
}

/**
* Adds a <code>Card</code> to the <code>Deck</code>. If the <code>Deck</code> is full or
* the <code>Card</code> already exists in the <code>Deck</code>, nothing happens.
*
* @param c The <code>Card</code> to be added.
*
* @param c The <code>Card</code> to be added.
*
* @throws NullPointerException if the parameter is NULL.
*/
public final void addCard(Card c)
{
if (c == null) {
throw new NullPointerException("Attempted to add a NULL card to the deck");
}

//if the deck is full... well, sorry.
if (size == 52) {
if (size == getFullDeckSize()) {
return;
}

//if the card is already in the deck, do nothing.
for (int i = 0; i < size; ++i) {
if (cards[i].equals(c)) {
Expand All @@ -135,7 +147,7 @@ public final void addCard(Card c)
*/
public final boolean isFull()
{
return (size == 52);
return (size == getFullDeckSize());
}

/**
Expand Down Expand Up @@ -176,7 +188,7 @@ public final int getCardIndex(Card c)
return i;
}
}

return -1;
}

Expand Down
Loading