Created
April 17, 2018 00:24
-
-
Save jamesamrundle/96e74b72c944677fdb7e521cfee9eac3 to your computer and use it in GitHub Desktop.
Assign5_JR.java is main file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.lang.Math; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.*; | |
public class Assign5_JR{ | |
public static void main(String[] args)throws InterruptedException{ | |
String s = " ";//six spaces | |
int playerScore = 0; | |
int dealerScore = 0; | |
Deck mainDeck = new Deck(); | |
mainDeck.shuffle(); | |
boolean playerTurn = true; | |
boolean gameOn = true; | |
Card[] dealerHand = mainDeck.deal(2); | |
Card[] playerHand = mainDeck.deal(2); | |
Card[] daFuck = null; | |
while(gameOn){ | |
while (gameOn){ | |
int playerTurnVal = playerPlay(dealerHand,playerHand,mainDeck, daFuck); | |
for(int i = 0;i<daFuck.length;i++) | |
{System.out.println("card "+i+makeCard(playerHand[i])); | |
} | |
if(playerTurnVal == 21){ | |
System.out.println("You win!\n P + 1 point."); | |
playerScore +=1; | |
break; | |
} | |
else if (playerTurnVal > 21){ | |
System.out.println("BUST!"); | |
System.out.println("Dealer Wins. BOOOO!\n D + 1 point."); | |
dealerScore += 1; | |
break; | |
} | |
int dealerTurnVal = dealerPlay(dealerHand,playerHand,mainDeck); | |
if(playerTurnVal == dealerTurnVal) | |
System.out.println("ITS A DRAW!\n...\nno points awarded" ); | |
else if (playerTurnVal>dealerTurnVal && playerTurnVal < 1000){ | |
System.out.println("You win!\n P + 1 point."); | |
playerScore +=1; | |
} | |
else{ | |
System.out.println("Dealer Wins. BOOOO!\n D + 1 point."); | |
dealerScore += 1; | |
} | |
int cardsLeft = mainDeck.cardsLeft(); | |
if (cardsLeft <4 ) | |
gameOn = false; | |
}//will break this layer and continue looping | |
dealerHand = mainDeck.deal(2); | |
playerHand = mainDeck.deal(2); | |
System.out.println("D: "+ dealerScore+"P: "+ playerScore); | |
} | |
System.out.println("**Final Scores**"); | |
System.out.println("Dealer : " + dealerScore); | |
System.out.println("You :"+ playerScore); | |
} | |
public static Card[] updatedHand(Card[] oldHand, Card nextCard){ | |
int oldLen = oldHand.length; | |
Card[] newHand = new Card[oldLen +1 ]; | |
for(int i=0; i <= oldLen; i++){ | |
if(i == oldLen) | |
newHand[i] = nextCard; | |
else | |
newHand[i] = oldHand[i]; | |
} | |
oldHand = newHand; | |
// for(int i = 0; i<newHand.length;i++){ PRINTS NEW HAND BEFORE RETURNING | |
// //System.out.println("new hand IN METHOD:"+newHand.length); | |
// System.out.println(makeCard(newHand[i]));} | |
return oldHand; | |
} | |
public static int cardVal(Card card){ | |
int cardVal=0; | |
String[] Rank = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; | |
String cardRank = card.getRank(); | |
int cardInd = java.util.Arrays.asList(Rank).indexOf(cardRank); | |
if (cardInd == ((Rank.length)-1)){ ///Ace is not of correct value! | |
cardVal = 11; | |
System.out.println("adding 11 for ace"); | |
} | |
else if (cardInd < java.util.Arrays.asList(Rank).indexOf("J")) | |
cardVal = Integer.parseInt(Rank[cardInd]); | |
else if (cardInd >= java.util.Arrays.asList(Rank).indexOf("J")) | |
cardVal = 10; | |
System.out.println("Cardval:"+cardVal); | |
return cardVal; | |
} | |
public static void showHand(Card[] dealerHand, Card[] playerHand , boolean showAll){ | |
String s = " ";//six spaces | |
int iters; | |
String dealerCard= ""; | |
String playerCard= ""; | |
if(dealerHand.length> playerHand.length) | |
iters = dealerHand.length; | |
else | |
iters = playerHand.length; | |
System.out.println(s+"Dealer"+s+s+"Player"+s+iters); | |
for(int i = 0;i<iters;i++){ | |
try { | |
if (showAll == false){ | |
if( i == 0) | |
dealerCard = makeCard(dealerHand[i]); | |
else | |
dealerCard = "XX"; | |
} | |
else if(showAll) | |
dealerCard =makeCard(dealerHand[i]); | |
// System.out.println(dealerCard); | |
} | |
catch (Exception exception1) { | |
//System.out.println("FUCK"); | |
dealerCard = " "; | |
// System.out.println(exception1); | |
} | |
try { | |
playerCard = makeCard(playerHand[i]); | |
// System.out.println(playerCard); | |
} | |
catch (Exception exception2) { | |
playerCard = " "; | |
// System.out.println(exception2); | |
} | |
System.out.printf(s+"%s"+s+s+s+"%s"+s+"\n",dealerCard,playerCard); | |
} | |
pause(); | |
} | |
// String dCard1 ,dCard2, pCard1,pCard2; | |
// dCard1 = makeCard(dealerHand[0]); | |
// dCard2 = makeCard(dealerHand[1]); | |
// pCard1 = makeCard(playerHand[0]); | |
// pCard2 = makeCard(playerHand[1]); | |
// System.out.printf(s+"%s"+s+s+s+"%s"+s,dCard1,pCard1); | |
// System.out.println(); | |
// System.out.printf(s+"%s"+s+s+s+"%s"+s,dCard2,pCard2); | |
// System.out.println(); | |
public static String makeCard(Card leCard){ | |
String cardString = leCard.getRank()+ leCard.getSuit(); | |
return cardString; | |
} | |
public static int dealerPlay(Card[] dealerHand, Card[] playerHand, Deck mainDeck) { | |
String s = " ";//six spaces | |
Card hitCard; | |
String hitCardString; | |
int dealerHandValue = cardVal(dealerHand[0])+cardVal(dealerHand[1]); | |
System.out.println("**Dealers turn!**"); | |
showHand(dealerHand,playerHand,true); | |
while (dealerHandValue < 17){ | |
hitCard = mainDeck.topCard(); | |
hitCardString = makeCard(hitCard); | |
dealerHand = updatedHand(dealerHand,hitCard); | |
showHand(dealerHand,playerHand,true); | |
//System.out.println(s+hitCardString); | |
int topCardVal = cardVal(hitCard); | |
if (topCardVal == 11){ | |
if(dealerHandValue + topCardVal > 21){ //compensates for Ace value of 11 | |
dealerHandValue += 1; | |
} | |
else | |
dealerHandValue += topCardVal; | |
} | |
else | |
dealerHandValue += topCardVal; // | |
System.out.println("Dealer hand: " +dealerHandValue); | |
} | |
return dealerHandValue; | |
} | |
public static int playerPlay(Card[] dealerHand, Card[] playerHand, Deck mainDeck,Card[] Fuck){ | |
boolean playerTurn = true; | |
String s = " ";//six spaces | |
int aVal; | |
Scanner scnr = new Scanner(System.in); | |
Card hitCard; | |
String hitCardString; | |
int handValue = cardVal(playerHand[0])+cardVal(playerHand[1]); | |
showHand(dealerHand,playerHand,false); | |
System.out.println("Current hand value :" + handValue); | |
while (playerTurn == true){ | |
System.out.println("Would you like another card?\n Yes or No?"); | |
String hitResponse = scnr.next(); | |
if (hitResponse.charAt(0) == ('N')) | |
playerTurn = false; | |
else if (hitResponse.charAt(0) == ('Y')) | |
{ | |
hitCard = mainDeck.topCard(); | |
playerHand = updatedHand(playerHand,hitCard); | |
//playerHand = transferHand; | |
// System.out.println("PLAYERhand length:"+playerHand.length); | |
// for(int i = 0; i<playerHand.length;i++){ | |
// System.out.println(makeCard(playerHand[i]));} | |
Fuck = playerHand; | |
hitCardString = makeCard(hitCard); | |
//showHand(dealerHand,playerHand); | |
System.out.println(s+s+s+s+" "+hitCardString); | |
if (hitCard.getRank() == "A"){ | |
System.out.println("You have an ace, would you want that valued at '1' or '11'?\n Please enter 1 or 11."); | |
aVal = scnr.nextInt(); | |
if (aVal == 1) | |
handValue += 1; | |
else | |
handValue += cardVal(hitCard); | |
} | |
else | |
handValue += cardVal(hitCard); | |
System.out.print("########\n"); | |
showHand(dealerHand,playerHand,false); | |
pause(); | |
} | |
handValue = scoreCheck(handValue); | |
if (handValue < 21){ | |
System.out.println("less tan 21"); | |
continue; | |
} | |
else if (handValue>= 21){ | |
System.out.println("more tan 21"); | |
return handValue; | |
} | |
// for(int i = 0; i < 2;++ i ){ | |
// playerHand[i].print(); | |
// if (i<1) | |
// System.out.print(" and "); | |
// } | |
// System.out.println("\n handvalue:"+handValue); | |
// System.out.println(mainDeck.cardsLeft()); | |
} | |
System.out.println("IDK WHY IM HERE"); | |
return handValue; | |
} | |
public static void pause(){ | |
try { | |
Thread.sleep(700); | |
} | |
catch (Exception e) {} | |
} | |
public static int scoreCheck(int handValue){ | |
if ( handValue ==21 ){ | |
System.out.println("Current hand value :" + handValue); | |
System.out.println("21!"); | |
pause(); | |
System.out.println("Blackjack Baby!"); | |
pause(); | |
return handValue; | |
} | |
if ( handValue > 21 ){ | |
System.out.println("Current hand value :" + handValue); | |
System.out.println("Over 21! Sorry! "); | |
pause(); | |
return 100000; | |
} | |
else{ | |
System.out.println("Current hand value :" + handValue); | |
pause(); | |
return handValue; | |
} | |
} | |
// public static Card[] dealHand(Deck mainDeck){ | |
// Card[] hand = mainDeck.deal(2); | |
// int handSize = hand.length; | |
// for(int i = 0; i < 2;++ i ){ | |
// hand[i].print(); | |
// if (i<(handSize-1)) | |
// System.out.print(" and "); | |
// } | |
// System.out.println(); | |
// System.out.println(mainDeck.cardsLeft()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Card{ | |
private String rank; // "2","3",..."9","10","J","Q","K","A" | |
private char suit; // spades \u2660, clubs \u2663 , hearts \u2665, diamonds \u2666, | |
public Card(String rankVal, char suitVal) { | |
rank = rankVal; | |
setSuit(suitVal); | |
} | |
public Card() { | |
rank = "2"; | |
setSuit('C'); | |
} | |
public String getRank() { | |
return (rank); | |
} //end getRank | |
public char getSuit() { | |
return (suit); | |
} //end getSuit | |
public void setRank(String rankVal) { | |
rank = rankVal; | |
} //end setRank | |
public void setSuit(char suitVal) { | |
String suits = "SCHD"; | |
String icons="\u2660\u2663\u2665\u2666"; | |
int suitIndex = suits.indexOf(suitVal); | |
suit = icons.charAt(suitIndex); | |
// suit = suitVal; | |
} //end setSuit | |
public void print() { | |
String cardFace = rank + " of " + suit; | |
System.out.print(cardFace); | |
} | |
} // Card |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class Deck { | |
final int numCards = 52; | |
final int suitSize = 13; | |
private Card[] cards; | |
private int top; | |
private int[] cardOrder; | |
public Deck() | |
{ | |
cards = new Card[numCards]; | |
cardOrder = new int[numCards]; | |
char[] Suit = {'S','C','H','D'}; | |
String[] Rank = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; | |
for(int n = 0; n < 52; n++) | |
{ | |
Card card = new Card(Rank[n % suitSize], Suit[n / suitSize]); | |
cards[n] = card; | |
cardOrder[n] = n; | |
} //for | |
top = 0; | |
} | |
public Card topCard() | |
{ | |
if (top == numCards) | |
{ | |
return null; | |
} | |
else | |
{ | |
return cards[cardOrder[top++]]; // note, top gets incremented | |
} | |
} | |
// helper method for determining if a prospective index for a card has already been generated | |
private boolean valueIn(int value, int[] intArray, int numElements) | |
{ | |
int index = 0; | |
while (index < numElements) { | |
if (value == intArray[index]) | |
return true; | |
else | |
index++; | |
} | |
return false; | |
} | |
public void shuffle() { | |
Random rand = new Random(); | |
cardOrder = new int[numCards]; | |
int cardsIn = 0; | |
int index; | |
while (cardsIn < numCards) { | |
index = rand.nextInt(numCards); | |
if (! valueIn(index, cardOrder,cardsIn)){ | |
cardOrder[cardsIn] = index; | |
cardsIn++; | |
} | |
} // while | |
} //end shuffle | |
public int cardsLeft() { | |
return numCards - top; | |
} // cardsLeft | |
public Card[] deal(int cardsToDeal) { | |
if ((numCards-top) < cardsToDeal) { | |
throw new RuntimeException("Not enough cards in deck for requested deal"); | |
} | |
else { | |
Card [] cardsDealt = new Card[cardsToDeal]; | |
for (int i = 0; i < cardsToDeal; i++) { | |
cardsDealt[i] = topCard(); | |
} | |
return cardsDealt; | |
} | |
} //end deal | |
public void print() | |
{ | |
int index; | |
for(int i = 0; i < numCards; i++) { | |
index = cardOrder[i]; | |
cards[index].print(); | |
System.out.println(); | |
} | |
} // Deck |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment