0% found this document useful (0 votes)
14 views

Message

The document defines a Poker game application written in Java. It includes classes for a Deck of Cards, Poker Players with Hands of Cards, and a PokerApp class to run the game. The PokerApp class initializes a deck of cards, registers players, deals cards to each player's hand, and checks and compares the hands to determine a winner. Methods are defined to draw cards from the deck, initialize hands, get card information, and check for winning hand types like pairs, flushes, and straights.

Uploaded by

S. Balcrick
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Message

The document defines a Poker game application written in Java. It includes classes for a Deck of Cards, Poker Players with Hands of Cards, and a PokerApp class to run the game. The PokerApp class initializes a deck of cards, registers players, deals cards to each player's hand, and checks and compares the hands to determine a winner. Methods are defined to draw cards from the deck, initialize hands, get card information, and check for winning hand types like pairs, flushes, and straights.

Uploaded by

S. Balcrick
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

ArrayList;
import java.util.Random;

class Main{
public static void main(String[] args) {
Main main = new Main();
main.run();
}
private void run(){
PokerApp spil = new PokerApp();
spil.startGame();
}
}

class PokerApp {
Deck deck = new Deck();
ArrayList<PokerPlayer> pokerPlayers = new ArrayList<>();

PokerApp (){

public void startGame (){


registerPlayers("Günther","Olga");
welcomeToTheGame();
dealCards();
checkHands();
}

private void registerPlayers (String... players){


System.out.println("Registering players...");
System.out.println();
System.out.println("Players registered: "+players.length);
System.out.println();
for (String player:players) {
pokerPlayers.add(new PokerPlayer(player, deck));
}
}

private void welcomeToTheGame() {


System.out.println("Welcome to PokerApp!");
System.out.println();
System.out.println("Initiating game...");
System.out.println();
}

private void dealCards (){


System.out.println("Dealing cards...");
for (PokerPlayer player:pokerPlayers) {
System.out.print(player);
}
System.out.println("Deck cards remaining: "+deck.deck.size()+"\n");
}
// highestCard() – find and return the highest card of a players hand.
// int noOfSpades() – return the number of spades in a players hand.
// int noOfSuit(String suit) - return the number of cards of the same suit as
the parameter suit.
// boolean flush() – return true if all cards in a players hand are of the same
suit.
private void checkHands (){
System.out.println("Checking hands...");

for (PokerPlayer player:pokerPlayers) {


System.out.print(player);
checkHand(player.handOfPlayer.hand);
}
}

private void checkHand(ArrayList<Card> hand) {

// check for 1) royal flush, 2) straight flush, 3) 4 of a kind, 4) full


house, 5) flush, 6) straight
// check for 5) 4 of a kind, 6) full house, 7) 3 of a kind, 8) 2 pair, 9) 1
pair, 10) high card

private void compareHands (ArrayList<Card> hand) {


System.out.println("Comparing hands...");
System.out.println();
System.out.println("The winner is: " + hand);
}
}

class PokerPlayer {
String name;
Hand handOfPlayer;

PokerPlayer(String playerName, Deck currentDeck) {


this.handOfPlayer = new Hand(currentDeck);
this.name = playerName;
}

@Override
public String toString (){
ArrayList<String> handString = new ArrayList<>();
for (Card card : this.handOfPlayer.hand) {
handString.add(card.getCardInfo());
}
return "Player: "+this.name+"'s hand: "+handString.toString()+"\n";
}
}

class Hand {
private final static int handSize = 5;
ArrayList<Card> hand = new ArrayList<>(handSize);

Hand(Deck currentDeck) {
hand = initializeHand(currentDeck);
}

private ArrayList<Card> initializeHand (Deck currentDeck){


for (int i=0;i<handSize;i++) {
hand.add(currentDeck.drawCard());
}
return hand;
}
}

class Deck {
private static final int DECK_SIZE = 52; // constants: static final
private static final int SUIT_SIZE = 4;
private static final int RANK_SIZE = 13;

final ArrayList<Card> deck;

Deck() {
deck = new ArrayList<>(DECK_SIZE);
initializeDeck();
}

private void initializeDeck (){


for (int s=0;s<SUIT_SIZE;s++){
for (int r=0;r<RANK_SIZE;r++){
Card card = new Card();
card.setSuit(s);
card.setRank(r);
deck.add(card);
}
}
}

public Card drawCard (){


int nr = getRandomCardIndex();
return deck.remove(nr);
}
private int getRandomCardIndex (){
Random random = new Random();
return random.nextInt(deck.size());
}
}

class Card {
private String suit;
private int rank;
private final String[] suits= {"Hearts", "Diamonds", "Clubs", "Spades"};

Card (){
this.suit = getRandomSuit();
this.rank = getRandomRank();
}

// getCardInfo
String getCardInfo (){
String cardInfo;
return cardInfo = this.rank + " of " + this.suit;
}

// getRandom functions for ...


String getRandomSuit(){
return this.suits[(int)(Math.random()*4)];
}
int getRandomRank(){
return this.rank = (int)(Math.random()*13+1);
}

// set functions for Deck(), and maybe cheating later.


void setSuit(int s){
if (s<=4) {
this.suit = suits[s];
}
}
void setRank(int r){
if (r<=13) {
this.rank = r;
}
}

@Override
public String toString(){
return suit+" of "+rank;
}
}

You might also like