Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- import java.util.LinkedList;
- public class PokerV1 {
- static Scanner scanner = new Scanner(System.in);
- public static void main(String[] args){
- String cont = "y";
- int total = 100;
- while(cont.equals("y")){
- int[] cards = cards(5);
- int[] suits = suits(5);
- int[] compCards = cards(5);
- int[] compSuits = suits(5);
- int[] tempCards = new int[5];
- int[] tempCompCards = new int[5];
- for(int i = 0; i < cards.length; i++){
- tempCards[i] = cards[i];
- tempCompCards[i] = compCards[i];
- }
- int playerScore = score(tempCards, suits);
- int compScore = score(tempCompCards, compSuits);
- int bet = pollInteger("Please enter a bet, $25 Max");
- while(bet > 25 || bet < 1){
- bet = (bet > 25) ?
- pollInteger("You are over the max please bet a number below $25") :
- pollInteger("You are under the min please bet a number below $25");
- }
- System.out.println("Your hand:");
- printHand(cards, suits, playerScore);
- LinkedList<Integer> selection = new LinkedList<Integer>();
- System.out.println("Chose the number you wish to exchange(1-5) \nEnter <done> when finished");
- while(true){
- String add = scanner.nextLine().toLowerCase();
- if(add.equals("done"))
- break;
- int addition = 0;
- try{
- addition = Integer.parseInt(add);
- }catch(NumberFormatException e){
- System.out.println("Error, try again: ");
- continue;
- }
- if(selection.contains(addition)){
- selection.remove(selection.indexOf(addition));
- System.out.println(selection);
- continue;
- }
- while(addition > 5 || addition < 1){
- addition = pollInteger("You already chose that or chose a number greater then 5 or less then 1! \nPlease Select another number!");
- }
- selection.add(addition);
- System.out.println(selection);
- }
- System.out.println("You exchanged: " + selection);
- for(int choice : selection){
- cards[choice - 1] = (int)(Math.random() * 12) + 2;
- suits[choice - 1] = (int)(Math.random() * 4) + 1;
- }
- for(int i = 0; i < 5; i++){
- tempCards[i] = cards[i];
- }
- playerScore = score(tempCards, suits);
- compScore = score(tempCompCards, compSuits);
- System.out.println("\nYour new hand:");
- printHand(cards, suits, playerScore);
- System.out.println("\nComputer's hand:");
- printHand(compCards, compSuits, compScore);
- System.out.println();
- if(playerScore == compScore){
- Arrays.sort(cards);
- Arrays.sort(compCards);
- if(cards[4] > compCards[4]){
- System.out.println("Your highest number <" + cards[4] + "> \nis bigger then the computer! <" + compCards[4]+ "> You win!");
- total += bet;
- System.out.println("Your money: $" + total);
- }else{
- System.out.println("Your highest number <" + cards[4] + "> \nis smaller then the computer!<" + compCards[4]+ "> You lose!");
- total -= bet;
- System.out.println("Your money: $" + total);
- }
- }
- else if(playerScore > compScore){
- System.out.println("You win!");
- total += bet;
- System.out.println("Your money: $" + total);
- }else{
- System.out.println("You lose!");
- total -= bet;
- System.out.println("Your money: $" + total);
- }
- System.out.println("Would you like to continue? (Y/N)");
- cont = scanner.nextLine().toLowerCase();
- }
- System.out.println("You ran with: $" + total + "\nPlease play again!");
- }
- private static int score(int[] cards, int[] suits){
- int score = 0;
- int pairs = 0;
- Arrays.sort(cards);
- boolean straight = false;
- boolean flush = false;
- for(int i = 0; i < cards.length; i++){
- for(int j = 0; j < cards.length; j++){
- if(i == j)
- continue;
- if(cards[i] == cards[j])
- pairs++;
- }
- }
- for(int i = 0; i < 4; i++){
- if(cards[i + 1] == cards[i] + 1){
- score = 7;
- straight = true;
- }else{
- score = 0;
- straight = false;
- break;
- }
- }
- for(int i = 0; i < 4; i++){
- if(suits[i] == suits[i+1]){
- score = 8;
- flush = true;
- }else{
- score = 0;
- flush = false;
- break;
- }
- }
- if(straight && flush == false) score = 7;
- if(straight && flush)
- score = 14;
- for(int i = 0; i < 5; i++){
- if(straight && flush && cards[i] > 9)
- score = 16;
- else{
- break;
- }
- }
- score = (score > pairs) ? score : pairs;
- if(pairs == 8) score = 9;
- return score;
- }
- private static void printHand(int[] cards, int[] suits, int score){
- String[] suitValue = new String[5];
- String[] handValue = new String[5];
- for(int i = 0; i < handValue.length; i++){
- handValue[i] = "| " + cards[i] + "|";
- if(cards[i] == 10)
- handValue[i] = "|" + cards[i] + "|";
- if(cards[i] == 11)
- handValue[i] = "| J|";
- if(cards[i] == 12)
- handValue[i] = "| Q|";
- if(cards[i] == 13)
- handValue[i] = "| K|";
- if(cards[i] == 14)
- handValue[i] = "| A|";
- if(suits[i] == 1)
- suitValue[i] = "| S|";
- if(suits[i] == 2)
- suitValue[i] = "| C|";
- if(suits[i] == 3)
- suitValue[i] = "| D|";
- if(suits[i] == 4)
- suitValue[i] = "| H|";
- }
- for(int i = 0; i < 5; i++){System.out.print(handValue[i]);}
- System.out.println();
- for(int i = 0; i < 5; i++){System.out.print(suitValue[i]);}
- System.out.println();
- if(score == 2)
- System.out.println("You have a pair!");
- if(score == 4)
- System.out.println("You have Two Pairs!");
- if(score == 6)
- System.out.println("You have a 3 of a kind!");
- if(score == 7)
- System.out.println("You have a Straight!");
- if(score == 8)
- System.out.println("You have a Flush!");
- if(score == 9)
- System.out.println("You have a Full House!");
- if(score == 12)
- System.out.println("You have a 4 of a Kind!");
- if(score == 14)
- System.out.println("You have a Straight Flush!");
- if(score == 16)
- System.out.println("You have a Royal Straight Flush!");
- }
- private static int[] cards(int cardNumber){
- int[] cards = new int[cardNumber];
- for(int i = 0; i < cards.length; i++){
- cards[i] = (int)(Math.random() * 12) + 2;
- }
- return cards;
- }
- private static int[] suits(int cardNumber){
- int[] suit = new int[cardNumber];
- for(int i = 0; i < suit.length; i++){
- suit[i] = (int)(Math.random() * 4) + 1;
- }
- return suit;
- }
- private static int pollInteger(String message){
- System.out.println(message);
- int valReturn = 0;
- boolean Valid = true;
- while(Valid){
- try{
- valReturn = Integer.parseInt(scanner.nextLine());
- Valid = false;
- }catch(NumberFormatException e){
- System.out.println("Error: Please enter a number");
- Valid = true;
- }
- }
- return valReturn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement