java - Blackjack Game, styling help needed -
introduction
hey guys! i'm old programmer hasn't programmed 3 years, , starting java @ university (class has been going 2 weeks now.)
i made blackjack program , wondering if there ways cut down code in blackjack class, not sure if it's me seems long , messy. program pretty sloppy, putting in values when needed , hack away @ it. hoping suggestions , changes use them in program , future study in school :)
note: hard hit, not soft, less chance player.
thank you!
run class
import java.util.scanner; public class run { // instance variables - replace example below own public static void main (string[] args) { try { blackjack.blackjack(); } catch(exception e) {} } }
blackjack class
import java.util.scanner; import java.util.objects; public class blackjack { public static void blackjack () throws interruptedexception { // sleep // thread.sleep(x); // total seconds sleep = x/1000 /////////////////////////////// // variables string[] deck = deck.generatedeck(); string[] player = new string[6]; string[] dealer = new string[6]; int money = 0, bet = 0, decknumber = 0, handnumber = 0, stay = 0, cardtotal = 0, = 0, totalvalue = 0, totalvalueace = 0; boolean win = false, lose = false, quit = false, play = false, bettrue = false, blackjack = false; string enter = ""; // scanner scanner input = new scanner(system.in); system.out.println("///// blackjack //////"); system.out.print("enter starting money: $"); money = input.nextint(); system.out.println("//////////////////////\n"); system.out.println("current cash amount: $" + money); while (quit == false) { while (quit == false && play == false) { system.out.print("would play? (yes/no): "); player[0] = input.next(); if (objects.equals(player[0], "yes")) { play = true; system.out.print("\n"); } else if (objects.equals(player[0], "no")) { quit = true; } else { system.out.println("error enter either yes/no!"); } } while (bettrue == false && quit == false) { system.out.print("enter bet amount: $"); bet = input.nextint(); if (bet > money) { system.out.println("error, entered more money had!\n"); } else if (bet < 0) { system.out.println("you can't bet negative dollars silly!\n"); } else { bettrue = true; } system.out.println("good luck!\n"); } bettrue = false; // deal format: // deck.deal(cardplace deal, player cardhand#, deck, player name); // // randomize cards in deck deck.totalrandom(deck,1000); deck.deal(0,0,deck,player); deck.deal(1,1,deck,player); deck.deal(2,0,deck,dealer); deck.deal(3,1,deck,dealer); // total cards in player hand, used in loop cardtotal = 2; // next hand number, used in equation handnumber = 2; // tells deal fourth card of deck next decknumber = 4; // player turn loop while (stay == 0 && lose == false && win == false && quit == false) { system.out.print("you've gotten cards: "); = (cardtotal - 1); while (i > -1) { system.out.print(player[i]); if (i > 1) { system.out.print(", "); } else if (i > 0) { if (handnumber > 3) { system.out.println(","); } system.out.print(" , "); } else { system.out.println("."); } --i; } if (((deck.checkvalue(player[0]) == -1)) && ((deck.checkvalue(player[1])) == 10) || ((deck.checkvalue(player[1]) == -1)) && ((deck.checkvalue(player[0])) == 10)) { win = true; blackjack = true; } else { = cardtotal; totalvalue = 0; totalvalueace = 0; while (i>0) { --i; if (deck.checkvalue(player[i]) == -1) { totalvalueace = totalvalue + 1; totalvalue += 11; } else { totalvalue+=deck.checkvalue(player[i]); if (totalvalueace != 0) { totalvalueace+=deck.checkvalue(player[i]); } } } if (totalvalue == 21 || totalvalueace == 21) { } else if (totalvalueace == 0 && totalvalue > 21) { lose = true; } else if (totalvalueace > 21) { lose = true; } else if (handnumber == 5) { lose = true; } } if (totalvalue > 21 && totalvalueace != 0) { totalvalue = totalvalueace; totalvalueace = 0; } if (totalvalueace == 0) { system.out.println("you have total value: " + totalvalue); } else { system.out.println("you have total value of either " + totalvalue + " or " + totalvalueace); } if (lose == false && win == false) { system.out.println("\ndealer has face card: " + dealer[0]); system.out.println("would hit or stay?"); enter = input.next(); system.out.println(""); if (objects.equals(enter, "hit")) { deck.deal(decknumber,handnumber,deck,player); ++decknumber; ++handnumber; ++cardtotal; } else if (objects.equals(enter, "stay")) { stay = 1; } else { system.out.println("invalid value!\n"); } } } stay = 0; cardtotal = 2; handnumber = 2; int totalvaluedealer = 0; int totalvaluedealerace = 0; // dealer turn loop while (stay == 0 && lose == false && win == false && quit == false && (totalvaluedealer < totalvalue || totalvaluedealerace < totalvalueace || totalvaluedealer < totalvalueace || totalvaluedealerace < totalvalue)) { system.out.print("dealer has cards: "); = (cardtotal - 1); while (i > -1) { system.out.print(dealer[i]); if (i > 1) { system.out.print(", "); } else if (i > 0) { if (handnumber == 3) { system.out.print(","); } else if (handnumber > 3) { system.out.println(","); } system.out.print(" , "); } else { system.out.println("."); } --i; } if (((deck.checkvalue(dealer[0]) == -1)) && ((deck.checkvalue(dealer[1])) == 10)) { system.out.println("you lose"); } else { totalvaluedealer = 0; totalvaluedealerace = 0; = cardtotal; while (i>0) { --i; if (deck.checkvalue(dealer[i]) == -1) { totalvaluedealerace = totalvaluedealer + 1; totalvaluedealer += 11; } else { totalvaluedealer+=deck.checkvalue(dealer[i]); if (totalvaluedealerace != 0) { totalvaluedealerace+=deck.checkvalue(dealer[i]); } } } if (totalvaluedealer > 21 && totalvalueace != 0) { totalvaluedealer = totalvaluedealerace; totalvaluedealerace = 0; } if (totalvaluedealer == 21 || totalvaluedealerace == 21) { } else if (totalvaluedealerace == 0 && totalvaluedealer > 21) { win = true; } else if (totalvaluedealerace > 21) { win = true; } else if (handnumber == 5) { lose = true; } } if (totalvaluedealerace == 0) { system.out.println("dealer has total value: " + totalvaluedealer); } else { system.out.println("dealer has total value of either " + totalvaluedealer + " or " + totalvaluedealerace); } system.out.println(""); thread.sleep(3000); if (totalvaluedealer < 17 || (totalvaluedealerace != 0 && totalvaluedealerace < 17)) { deck.deal(decknumber,handnumber,deck,dealer); ++decknumber; ++handnumber; ++cardtotal; } else { stay = 1; } } if (play == true) { if (win == true && lose == false) { system.out.println("you win!\n"); if (blackjack == true) { system.out.println("black jack!!! amount won: " + (bet * 1.5)); money += (bet * 1.5); } else { money += bet; } } else if ((totalvaluedealer == totalvalue && totalvalue > totalvaluedealerace || totalvaluedealer == totalvalueace && totalvalueace != 0)) { system.out.println("it's tie!\n"); } else if ((totalvaluedealer > totalvalue && totalvaluedealer > totalvalueace && totalvaluedealer < 22) || (totalvaluedealerace > totalvalue && totalvaluedealerace > totalvalueace) || lose == true) { system.out.println("you lose!\n"); money -= bet; } else { system.out.println("you win!\n"); if (blackjack == true) { system.out.println("black jack!!! amount won: " + (bet * 1.5)); money += (bet * 1.5); } else { money += bet; } } blackjack = false; play = false; lose = false; win = false; stay = 0; if (money < 1) { quit = true; system.out.println("you've lost money!"); } else { system.out.println("current cash amount: $" + money); } } /* print deck (int i=0;i<52;i++) { system.out.println(deck[i]); } */ } } }
deck class
(not changing, if need functionality check here)
import java.util.random; import java.util.objects; public class deck { // generate deck // description: used generate array length of 52 holds each card of deck of cards. // tip: use in program, set string array equal method. public static string[] generatedeck() { // variables int = 0; // initialize deck string[] deck = new string[52]; deck[0] = "ace of spades"; (i=1; i<12; i++) { deck[i] = integer.tostring(i + 1) + " of spades"; } deck[10] = "jack of spades"; deck[11] = "queen of spades"; deck[12] = "king of spades"; deck[13] = "ace of hearts"; (i=1; i<12; i++) { deck[(i + 13)] = integer.tostring(i + 1) + " of hearts"; } deck [23] = "jack of hearts"; deck [24] = "queen of hearts"; deck [25] = "king of hearts"; deck [26] = "ace of clubs"; (i=1; i<12; i++) { deck[(i + 26)] = integer.tostring(i + 1) + " of clubs"; } deck [36] = "jack of clubs"; deck [37] = "queen of clubs"; deck [38] = "king of clubs"; deck [39] = "ace of diamonds"; (i=1; i<12; i++) { deck[(i + 39)] = integer.tostring(i + 1) + " of diamonds"; } deck [49] = "jack of diamonds"; deck [50] = "queen of diamonds"; deck [51] = "king of diamonds"; return deck; } // shuffle 1 card of deck // description: not use, helps see how 1 might swap 2 random cards each other. public static void onecardshuffle(string[] deck) { random randomgenerator = new random(); // generates number 0 51 int cardfirst = randomgenerator.nextint(52); int cardsecond = randomgenerator.nextint(52); // swap string card = deck[cardsecond]; deck[cardsecond] = deck[cardfirst]; deck[cardfirst] = card; } // standard riffle shuffle // description: cuts deck 2 pieces , lays on card on top of other. // tip: use in program, call method deck generated earlier. // warning: if riffleshuffle enough, go order started out in. fix, use "totalrandom" method. public static void riffleshuffle(string[] deck) { string[] cutone = new string[26]; string[] cuttwo = new string[26]; (int i=0;i<26;i++) { cutone[i] = deck[i]; } (int i=0;i<26;i++) { cuttwo[i] = deck[i+26]; } (int i=0;i<26;i++) { deck[2*i] = cutone[i]; deck[2*i + 1] = cuttwo[i]; } } // shuffle top bottom // description: takes top card of deck , bottom card , swaps them, moving down center of deck. public static void topbottomshuffle(string[] deck) { (int i=0;i<26;i+=1) { string card = deck[51 - i]; deck [51 - i] = deck[i]; deck [i] = card; } } // cut // description: cuts deck through middle // tip: use if there specific need cut deck, otherwise pointless. public static void cut(string[] deck) { string[] cut = new string[26]; (int i=0;i<26;i++) { cut[i] = deck[i]; } (int i=0;i<26;i++) { deck[i] = deck[i+26]; } (int i=1;i<27;i++) { deck[i+25] = cut[i - 1]; } } // random shuffle // description: onecardshuffle except repeated number of times // tip: use method shuffle, may not fastest (not noticeable however), makes deck totally randomized. public static void totalrandom(string[] deck, int times) { // random initialize random randomgenerator = new random(); // variables int cardfirst = 0; int cardsecond = 0; // swap (int i=0;i<times;i++) { // generates number 0 51 cardfirst = randomgenerator.nextint(52); cardsecond = randomgenerator.nextint(52); string card = deck[cardsecond]; deck[cardsecond] = deck[cardfirst]; deck[cardfirst] = card; } } // deal // description: function deals object holding cards card out of deck. public static void deal(int cardplace, int handnumber, string[] deck, string[] holder) { holder[handnumber] = deck[cardplace]; } // check value of card // description: checks value of card, , turns string integer. aces come -1, if need can read 1 or 11. // every other card either number or if face card, 10. // tip: use if need value cards , have face card or ace equal 1 || 10 || 11. public static int checkvalue(string card) { string[] name = card.split(" "); if (objects.equals(name[0], "jack") || (objects.equals(name[0], "queen")) || (objects.equals(name[0], "king"))) { return 10; } else if (objects.equals(name[0], "ace")) { return -1; } else { return integer.parseint(name[0]); } } // check value order // description: used check order in cards should lay if 1 after public static int checkvalueorder(string card) { string[] name = card.split(" "); switch(name[0]) { case "jack": return 11; case "queen": return 12; case "king": return 13; case "ace": return 1; default: return integer.parseint(name[0]); } } // check suit of card // description: checks suit of cartain card in deck, or in other words cuts off first part of card, , returns suit. // tip: use if need check card combinations require same suit. public static string checksuit(string card) { string[] name = card.split(" "); return name[2]; } // sorts cards smallest greatest // description: use if need know numbered order of cards. // tip: can used size string of cards. public static void sortcards (string[] card) { string sortingcard = ""; int sortingcount = 0; int cardstotal = card.length; { sortingcount = 0; (int = 0; i<cardstotal-1; i++) { if (checkvalueorder(card[i]) > checkvalueorder(card[i+1])) { sortingcard = card[i+1]; card[i+1] = card[i]; card[i] = sortingcard; } else { ++sortingcount; } } } while (sortingcount != cardstotal-1); } }
below few observations have made enhance use many inbuilt java api's
- many place have used string array instead have used collections api.
- there many if else conditions can replaced ternary operator.
- sorting can done using inbuilt functions arrays.sort() , collections.sort() etc.
- could have written more functions within class rather have single functions.
- avoid using string unless if necessary instead using stringbuilder or stringbuffer.
Comments
Post a Comment