Skip to main content
Code Review

Return to Question

deleted 2 characters in body
Source Link
Golden
  • 105
  • 6
import java.util.ArrayList;
//Player class
public class Player {
 private final String vem;
 private final ArrayList<Card> hand;
 public Player(String vem) {
 this.vem = vem;
 this.hand = new ArrayList<Card>();
 }
 public String getVem() {
 return vem;
 }
 public void addCard(Card card) {
 hand.add(card);
 }
 public int getHandSum() {
 int handSum = 0;
 for (Card card : hand) {
 handSum += card.getFace().getValue();
 }
 return handSum;
 }
 public String getHandAsString(boolean b) {
 StringBuilder sb = new StringBuilder();
 sb.append(vem); //'s'\
 sb.append('\n');
 for (int i = 0; i < hand.size(); i++) {
 if (i == 0 && b) {
 sb.append('\n');
 } else {
 sb.append(hand.get(i));
 sb.append('\n');
 }
 }
 return sb.toString();
 }
}
import java.util.ArrayList;
//Player class
public class Player {
 private final String vem;
 private final ArrayList<Card> hand;
 public Player(String vem) {
 this.vem = vem;
 this.hand = new ArrayList<Card>();
 }
 public String getVem() {
 return vem;
 }
 public void addCard(Card card) {
 hand.add(card);
 }
 public int getHandSum() {
 int handSum = 0;
 for (Card card : hand) {
 handSum += card.getFace().getValue();
 }
 return handSum;
 }
 public String getHandAsString(boolean b) {
 StringBuilder sb = new StringBuilder();
 sb.append(vem); //'s'\
 sb.append('\n');
 for (int i = 0; i < hand.size(); i++) {
 if (i == 0 && b) {
 sb.append('\n');
 } else {
 sb.append(hand.get(i));
 sb.append('\n');
 }
 }
 return sb.toString();
 }
}
import java.util.ArrayList;
//Player class
public class Player {
 private final String vem;
 private final ArrayList<Card> hand;
 public Player(String vem) {
 this.vem = vem;
 this.hand = new ArrayList<Card>();
 }
 public String getVem() {
 return vem;
 }
 public void addCard(Card card) {
 hand.add(card);
 }
 public int getHandSum() {
 int handSum = 0;
 for (Card card : hand) {
 handSum += card.getFace().getValue();
 }
 return handSum;
 }
 public String getHandAsString(boolean b) {
 StringBuilder sb = new StringBuilder();
 sb.append(vem); //'s'\
 sb.append('\n');
 for (int i = 0; i < hand.size(); i++) {
 if (i == 0 && b) {
 sb.append('\n');
 } else {
 sb.append(hand.get(i));
 sb.append('\n');
 }
 }
 return sb.toString();
 }
}
Rollback to Revision 2
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 113

Card Blackjack game in Java

So this is a cardblackjack game, any feedback or tips on what I should remove from the code that is not needed is very appreciated.

Note: some of the names of classes or variables are in Swedish but it should not interfere with understanding the code I think.

Blackjack.java

Apparently I wasn't allowed to post this here.
Please delete the post if possible.
Thx.
import java.util.Scanner;
class Card {
 private final Face face;
 private final Suit suit;
 public Card(Face face, Suit suit) {
 this.face = face;
 this.suit = suit;
 }
 public Face getFace() {
 return face;
 }
 public Suit getSuit() {
 return suit;
 }
 @Override
 public String toString() {
 return face + " of " + suit;
 }
}
enum Face {
 Ace(11), Deuce(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(10), Queen(10), King(10);
 private final int value;
 private Face(int value) {
 this.value = value;
 }
 
 public int getValue() {
 return value;
 }
}
enum Suit {
 hearts, spades, diamonds, clubs;
}
public class BlackJack {
 public static void main(String[] args) {
 int wins = 0;
 int losses = 0;
 Scanner scanner = new Scanner(System.in);
 String input;
 
 //Clear Terminal from file paths
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 
 //Start loop
 do {
 //Clear Terminal from last game
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 //Create players, shuffle deck and turn gameOver off
 Player player = new Player("You");
 Player dealer = new Player("Dealer");
 Deck deck = new Deck();
 deck.shuffle();
 boolean gameOver = false;
 //Get cards for player
 player.addCard(deck.draw());
 player.addCard(deck.draw());
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 //Get cards for dealer
 dealer.addCard(deck.draw());
 dealer.addCard(deck.draw());
 
 //Player turn
 do {
 if (player.getHandSum() == 21) {
 System.out.println("Super lucky Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 break;
 }
 if (player.getHandSum() > 21) {
 System.out.println("Super unlucky! You lost.");
 losses = losses + 1;
 gameOver = true;
 break;
 }
 System.out.println("");System.out.println("");
 System.out.println("Draw or stay?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Draw") && !input.equalsIgnoreCase("Stay"));
 //Draw
 if (input.equalsIgnoreCase("Draw")) {
 player.addCard(deck.draw());
 System.out.println("");
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 if (player.getHandSum() == 21) {
 System.out.println("Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 }
 if (player.getHandSum() > 21) {
 System.out.println("You busted with " + player.getHandSum() + " in your hand. Dealer wins!");
 losses = losses + 1;
 gameOver = true;
 }
 }
 //Stay
 if (input.equalsIgnoreCase("stay")) {
 System.out.println("You have chosen to stay. Your hand: " + player.getHandSum());
 }
 } while (input.equalsIgnoreCase("Draw") && !gameOver);
 //Dealer turn
 if (!gameOver) {
 System.out.println("");System.out.println("");System.out.println("");System.out.println("");System.out.println("");
 System.out.println("________________________________________________________________________");
 System.out.println("Dealers turn");
 System.out.println("________________________________________________________________________");
 
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 while (!gameOver) {
 if (dealer.getHandSum() <= 17) {
 //Draw card
 dealer.addCard(deck.draw());
 System.out.println(dealer.getVem() + " drew another card");
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 
 if (dealer.getHandSum() == 17) {
 if (player.getHandSum() == 17) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 18) {
 if (player.getHandSum() == 18) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 19) {
 if (player.getHandSum() == 19) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 
 if (dealer.getHandSum() == 20) {
 if (player.getHandSum() == 20) {
 System.out.println("It's a draw!");
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 if (dealer.getHandSum() > 21) {
 System.out.println("Dealer busted with " + dealer.getHandSum() + " in their hand. You win!");
 wins = wins + 1;
 gameOver = true;
 }
 } else {
 //Stay
 System.out.println("Dealer chose to stay!");
 System.out.println("");
 int totalDealerSum = dealer.getHandSum();
 int totalPlayerSum = player.getHandSum();
 if (totalDealerSum > totalPlayerSum) {
 System.out.println("Both players decided to stay. The Dealer won with a total of " + totalDealerSum + " in their hand.");
 losses = losses + 1;
 } else {
 System.out.println("Both players decided to stay. You win with a total of " + totalPlayerSum + " in your hand.");
 wins = wins + 1;
 }
 gameOver = true;
 }
 }
 //New game? And Score
 System.out.println("");System.out.println("");System.out.println("");
 
 if(wins==1 && losses==0){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 if(wins==0 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 
 if(wins>1 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins==0 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==0){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 if(wins==1 && losses==1){
 System.out.println("You have won " + wins + " time and lost " + losses + " time.");
 }
 if(wins==1 && losses>1){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 System.out.println("");
 System.out.println("Play again?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Yes") && !input.equalsIgnoreCase("No"));
 
 } while (input.equalsIgnoreCase("Yes"));
 
 scanner.close();
 }
}

Deck.java

import java.util.ArrayList;
import java.util.Collections;
public class Deck {
 private final ArrayList<Card> cards;
 public Deck() {
 cards = new ArrayList<Card>();
 // populate deck with cards
 for (Suit suit : Suit.values()) {
 for (Face face : Face.values()) {
 cards.add(new Card(face, suit));
 }
 }
 }
 public void shuffle() {
 Collections.shuffle(cards);
 }
 public Card draw() {
 return cards.remove(0);
 }
 @Override
 public String toString() {
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < cards.size(); i++) {
 sb.append(i + 1);
 sb.append('/');
 sb.append(cards.size());
 sb.append(' ');
 sb.append(cards.get(i));
 sb.append('\n');
 }
 return sb.toString();
 }
}

Player.java

import java.util.ArrayList;
//Player class
public class Player {
 private final String vem;
 private final ArrayList<Card> hand;
 public Player(String vem) {
 this.vem = vem;
 this.hand = new ArrayList<Card>();
 }
 public String getVem() {
 return vem;
 }
 public void addCard(Card card) {
 hand.add(card);
 }
 public int getHandSum() {
 int handSum = 0;
 for (Card card : hand) {
 handSum += card.getFace().getValue();
 }
 return handSum;
 }
 public String getHandAsString(boolean b) {
 StringBuilder sb = new StringBuilder();
 sb.append(vem); //'s'\
 sb.append('\n');
 for (int i = 0; i < hand.size(); i++) {
 if (i == 0 && b) {
 sb.append('\n');
 } else {
 sb.append(hand.get(i));
 sb.append('\n');
 }
 }
 return sb.toString();
 }
}

Card game in Java

So this is a card game, any feedback or tips on what I should remove from the code that is not needed is very appreciated.

Apparently I wasn't allowed to post this here.
Please delete the post if possible.
Thx.

Blackjack game in Java

So this is a blackjack game, any feedback or tips on what I should remove from the code that is not needed is very appreciated.

Note: some of the names of classes or variables are in Swedish but it should not interfere with understanding the code I think.

Blackjack.java

import java.util.Scanner;
class Card {
 private final Face face;
 private final Suit suit;
 public Card(Face face, Suit suit) {
 this.face = face;
 this.suit = suit;
 }
 public Face getFace() {
 return face;
 }
 public Suit getSuit() {
 return suit;
 }
 @Override
 public String toString() {
 return face + " of " + suit;
 }
}
enum Face {
 Ace(11), Deuce(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(10), Queen(10), King(10);
 private final int value;
 private Face(int value) {
 this.value = value;
 }
 
 public int getValue() {
 return value;
 }
}
enum Suit {
 hearts, spades, diamonds, clubs;
}
public class BlackJack {
 public static void main(String[] args) {
 int wins = 0;
 int losses = 0;
 Scanner scanner = new Scanner(System.in);
 String input;
 
 //Clear Terminal from file paths
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 
 //Start loop
 do {
 //Clear Terminal from last game
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 //Create players, shuffle deck and turn gameOver off
 Player player = new Player("You");
 Player dealer = new Player("Dealer");
 Deck deck = new Deck();
 deck.shuffle();
 boolean gameOver = false;
 //Get cards for player
 player.addCard(deck.draw());
 player.addCard(deck.draw());
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 //Get cards for dealer
 dealer.addCard(deck.draw());
 dealer.addCard(deck.draw());
 
 //Player turn
 do {
 if (player.getHandSum() == 21) {
 System.out.println("Super lucky Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 break;
 }
 if (player.getHandSum() > 21) {
 System.out.println("Super unlucky! You lost.");
 losses = losses + 1;
 gameOver = true;
 break;
 }
 System.out.println("");System.out.println("");
 System.out.println("Draw or stay?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Draw") && !input.equalsIgnoreCase("Stay"));
 //Draw
 if (input.equalsIgnoreCase("Draw")) {
 player.addCard(deck.draw());
 System.out.println("");
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 if (player.getHandSum() == 21) {
 System.out.println("Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 }
 if (player.getHandSum() > 21) {
 System.out.println("You busted with " + player.getHandSum() + " in your hand. Dealer wins!");
 losses = losses + 1;
 gameOver = true;
 }
 }
 //Stay
 if (input.equalsIgnoreCase("stay")) {
 System.out.println("You have chosen to stay. Your hand: " + player.getHandSum());
 }
 } while (input.equalsIgnoreCase("Draw") && !gameOver);
 //Dealer turn
 if (!gameOver) {
 System.out.println("");System.out.println("");System.out.println("");System.out.println("");System.out.println("");
 System.out.println("________________________________________________________________________");
 System.out.println("Dealers turn");
 System.out.println("________________________________________________________________________");
 
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 while (!gameOver) {
 if (dealer.getHandSum() <= 17) {
 //Draw card
 dealer.addCard(deck.draw());
 System.out.println(dealer.getVem() + " drew another card");
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 
 if (dealer.getHandSum() == 17) {
 if (player.getHandSum() == 17) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 18) {
 if (player.getHandSum() == 18) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 19) {
 if (player.getHandSum() == 19) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 
 if (dealer.getHandSum() == 20) {
 if (player.getHandSum() == 20) {
 System.out.println("It's a draw!");
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 if (dealer.getHandSum() > 21) {
 System.out.println("Dealer busted with " + dealer.getHandSum() + " in their hand. You win!");
 wins = wins + 1;
 gameOver = true;
 }
 } else {
 //Stay
 System.out.println("Dealer chose to stay!");
 System.out.println("");
 int totalDealerSum = dealer.getHandSum();
 int totalPlayerSum = player.getHandSum();
 if (totalDealerSum > totalPlayerSum) {
 System.out.println("Both players decided to stay. The Dealer won with a total of " + totalDealerSum + " in their hand.");
 losses = losses + 1;
 } else {
 System.out.println("Both players decided to stay. You win with a total of " + totalPlayerSum + " in your hand.");
 wins = wins + 1;
 }
 gameOver = true;
 }
 }
 //New game? And Score
 System.out.println("");System.out.println("");System.out.println("");
 
 if(wins==1 && losses==0){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 if(wins==0 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 
 if(wins>1 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins==0 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==0){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 if(wins==1 && losses==1){
 System.out.println("You have won " + wins + " time and lost " + losses + " time.");
 }
 if(wins==1 && losses>1){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 System.out.println("");
 System.out.println("Play again?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Yes") && !input.equalsIgnoreCase("No"));
 
 } while (input.equalsIgnoreCase("Yes"));
 
 scanner.close();
 }
}

Deck.java

import java.util.ArrayList;
import java.util.Collections;
public class Deck {
 private final ArrayList<Card> cards;
 public Deck() {
 cards = new ArrayList<Card>();
 // populate deck with cards
 for (Suit suit : Suit.values()) {
 for (Face face : Face.values()) {
 cards.add(new Card(face, suit));
 }
 }
 }
 public void shuffle() {
 Collections.shuffle(cards);
 }
 public Card draw() {
 return cards.remove(0);
 }
 @Override
 public String toString() {
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < cards.size(); i++) {
 sb.append(i + 1);
 sb.append('/');
 sb.append(cards.size());
 sb.append(' ');
 sb.append(cards.get(i));
 sb.append('\n');
 }
 return sb.toString();
 }
}

Player.java

import java.util.ArrayList;
//Player class
public class Player {
 private final String vem;
 private final ArrayList<Card> hand;
 public Player(String vem) {
 this.vem = vem;
 this.hand = new ArrayList<Card>();
 }
 public String getVem() {
 return vem;
 }
 public void addCard(Card card) {
 hand.add(card);
 }
 public int getHandSum() {
 int handSum = 0;
 for (Card card : hand) {
 handSum += card.getFace().getValue();
 }
 return handSum;
 }
 public String getHandAsString(boolean b) {
 StringBuilder sb = new StringBuilder();
 sb.append(vem); //'s'\
 sb.append('\n');
 for (int i = 0; i < hand.size(); i++) {
 if (i == 0 && b) {
 sb.append('\n');
 } else {
 sb.append(hand.get(i));
 sb.append('\n');
 }
 }
 return sb.toString();
 }
}
deleted 13763 characters in body; edited title
Source Link
Golden
  • 105
  • 6

Blackjack Card game in Java

So this is a blackjack game, any feedback or tips on what I should remove from the code that is not needed is very appreciated.

Note: some of the names of classes or variables are in Swedish but it should not interfere with understanding the code I think.

Blackjack.java

import java.util.Scanner;
class Card {
 private final Face face;
 private final Suit suit;
 public Card(Face face, Suit suit) {
 this.face = face;
 this.suit = suit;
 }
 public Face getFace() {
 return face;
 }
 public Suit getSuit() {
 return suit;
 }
 @Override
 public String toString() {
 return face + " of " + suit;
 }
}
enum Face {
 Ace(11), Deuce(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(10), Queen(10), King(10);
 private final int value;
 private Face(int value) {
 this.value = value;
 }
 
 public int getValue() {
 return value;
 }
}
enum Suit {
 hearts, spades, diamonds, clubs;
}
public class BlackJack {
 public static void main(String[] args) {
 int wins = 0;
 int losses = 0;
 Scanner scanner = new Scanner(System.in);
 String input;
 
 //Clear Terminal from file paths
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 
 //Start loop
 do {
 //Clear Terminal from last game
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 //Create players, shuffle deck and turn gameOver off
 Player player = new Player("You");
 Player dealer = new Player("Dealer");
 Deck deck = new Deck();
 deck.shuffle();
 boolean gameOver = false;
 //Get cards for player
 player.addCard(deck.draw());
 player.addCard(deck.draw());
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 //Get cards for dealer
 dealer.addCard(deck.draw());
 dealer.addCard(deck.draw());
 
 //Player turn
 do {
 if (player.getHandSum() == 21) {
 System.out.println("Super lucky Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 break;
 }
 if (player.getHandSum() > 21) {
 System.out.println("Super unlucky! You lost.");
 losses = losses + 1;
 gameOver = true;
 break;
 }
 System.out.println("");System.out.println("");
 System.out.println("Draw or stay?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Draw") && !input.equalsIgnoreCase("Stay"));
 //Draw
 if (input.equalsIgnoreCase("Draw")) {
 player.addCard(deck.draw());
 System.out.println("");
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 if (player.getHandSum() == 21) {
 System.out.println("Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 }
 if (player.getHandSum() > 21) {
 System.out.println("You busted with " + player.getHandSum() + " in your hand. Dealer wins!");
 losses = losses + 1;
 gameOver = true;
 }
 }
 //Stay
 if (input.equalsIgnoreCase("stay")) {
 System.out.println("You have chosen to stay. Your hand: " + player.getHandSum());
 }
 } while (input.equalsIgnoreCase("Draw") && !gameOver);
 //Dealer turn
 if (!gameOver) {
 System.out.println("");System.out.println("");System.out.println("");System.out.println("");System.out.println("");
 System.out.println("________________________________________________________________________");
 System.out.println("Dealers turn");
 System.out.println("________________________________________________________________________");
 
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 while (!gameOver) {
 if (dealer.getHandSum() <= 17) {
 //Draw card
 dealer.addCard(deck.draw());
 System.out.println(dealer.getVem() + " drew another card");
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 
 if (dealer.getHandSum() == 17) {
 if (player.getHandSum() == 17) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 18) {
 if (player.getHandSum() == 18) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 19) {
 if (player.getHandSum() == 19) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 
 if (dealer.getHandSum() == 20) {
 if (player.getHandSum() == 20) {
 System.out.println("It's a draw!");
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 if (dealer.getHandSum() > 21) {
 System.out.println("Dealer busted with " + dealer.getHandSum() + " in their hand. You win!");
 wins = wins + 1;
 gameOver = true;
 }
 } else {
 //Stay
 System.out.println("Dealer chose to stay!");
 System.out.println("");
 int totalDealerSum = dealer.getHandSum();
 int totalPlayerSum = player.getHandSum();
 if (totalDealerSum > totalPlayerSum) {
 System.out.println("Both players decided to stay. The Dealer won with a total of " + totalDealerSum + " in their hand.");
 losses = losses + 1;
 } else {
 System.out.println("Both players decided to stay. You win with a total of " + totalPlayerSum + " in your hand.");
 wins = wins + 1;
 }
 gameOver = true;
 }
 }
 //New game? And Score
 System.out.println("");System.out.println("");System.out.println("");
 
 if(wins==1 && losses==0){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 if(wins==0 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 
 if(wins>1 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins==0 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==0){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 if(wins==1 && losses==1){
 System.out.println("You have won " + wins + " time and lost " + losses + " time.");
 }
 if(wins==1 && losses>1){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 System.out.println("");
 System.out.println("Play again?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Yes") && !input.equalsIgnoreCase("No"));
 
 } while (input.equalsIgnoreCase("Yes"));
 
 scanner.close();
 }
}

Deck.java

import java.util.ArrayList;
import java.util.Collections;
public class Deck {
 private final ArrayList<Card> cards;
 public Deck() {
 cards = new ArrayList<Card>();
 // populate deck with cards
 for (Suit suit : Suit.values()) {
 for (Face face : Face.values()) {
 cards.add(new Card(face, suit));
 }
 }
 }
 public void shuffle() {
 Collections.shuffle(cards);
 }
 public Card draw() {
 return cards.remove(0);
 }
 @Override
 public String toString() {
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < cards.size(); i++) {
 sb.append(i + 1);
 sb.append('/');
 sb.append(cards.size());
 sb.append(' ');
 sb.append(cards.get(i));
 sb.append('\n');
 }
 return sb.toString();
 }
}

Player.java

So this is a card game, any feedback or tips on what I should remove from the code that is not needed is very appreciated.

import java.util.ArrayList;
//Player class
public class Player {
 private final String vem;
 private final ArrayList<Card> hand;
 public Player(String vem) {
 this.vem = vem;
 Apparently I wasn't allowed to post this.hand = new ArrayList<Card>();
 }
 public String getVem() {
 return vem;
 }
 public void addCard(Card card) {
 hand.add(card);
 }
 public int getHandSum() {
 int handSum = 0;
 for (Card card : hand) {
 handSum += card.getFace().getValue();
 }
 return handSum;
  }
 public String getHandAsString(boolean b) {
 StringBuilder sb = new StringBuilder();
 sb.append(vem); //'s'\
 sb.append('\n');
 for (int i = 0; i < handhere.size(); i++) {
Please delete the post if (i == 0 && b) {
  sb.append('\n');
 } else {
 sb.append(hand.get(i));
 sbpossible.append('\n');
 }
 }
 return sbThx.toString();
 }
}

Blackjack game in Java

So this is a blackjack game, any feedback or tips on what I should remove from the code that is not needed is very appreciated.

Note: some of the names of classes or variables are in Swedish but it should not interfere with understanding the code I think.

Blackjack.java

import java.util.Scanner;
class Card {
 private final Face face;
 private final Suit suit;
 public Card(Face face, Suit suit) {
 this.face = face;
 this.suit = suit;
 }
 public Face getFace() {
 return face;
 }
 public Suit getSuit() {
 return suit;
 }
 @Override
 public String toString() {
 return face + " of " + suit;
 }
}
enum Face {
 Ace(11), Deuce(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(10), Queen(10), King(10);
 private final int value;
 private Face(int value) {
 this.value = value;
 }
 
 public int getValue() {
 return value;
 }
}
enum Suit {
 hearts, spades, diamonds, clubs;
}
public class BlackJack {
 public static void main(String[] args) {
 int wins = 0;
 int losses = 0;
 Scanner scanner = new Scanner(System.in);
 String input;
 
 //Clear Terminal from file paths
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 
 //Start loop
 do {
 //Clear Terminal from last game
 System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");System.out.println(" ");
 //Create players, shuffle deck and turn gameOver off
 Player player = new Player("You");
 Player dealer = new Player("Dealer");
 Deck deck = new Deck();
 deck.shuffle();
 boolean gameOver = false;
 //Get cards for player
 player.addCard(deck.draw());
 player.addCard(deck.draw());
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 //Get cards for dealer
 dealer.addCard(deck.draw());
 dealer.addCard(deck.draw());
 
 //Player turn
 do {
 if (player.getHandSum() == 21) {
 System.out.println("Super lucky Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 break;
 }
 if (player.getHandSum() > 21) {
 System.out.println("Super unlucky! You lost.");
 losses = losses + 1;
 gameOver = true;
 break;
 }
 System.out.println("");System.out.println("");
 System.out.println("Draw or stay?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Draw") && !input.equalsIgnoreCase("Stay"));
 //Draw
 if (input.equalsIgnoreCase("Draw")) {
 player.addCard(deck.draw());
 System.out.println("");
 System.out.print(player.getHandAsString(false));
 System.out.println("Total: " + player.getHandSum());
 System.out.println("");System.out.println("");
 if (player.getHandSum() == 21) {
 System.out.println("Blackjack! You win.");
 wins = wins + 1;
 gameOver = true;
 }
 if (player.getHandSum() > 21) {
 System.out.println("You busted with " + player.getHandSum() + " in your hand. Dealer wins!");
 losses = losses + 1;
 gameOver = true;
 }
 }
 //Stay
 if (input.equalsIgnoreCase("stay")) {
 System.out.println("You have chosen to stay. Your hand: " + player.getHandSum());
 }
 } while (input.equalsIgnoreCase("Draw") && !gameOver);
 //Dealer turn
 if (!gameOver) {
 System.out.println("");System.out.println("");System.out.println("");System.out.println("");System.out.println("");
 System.out.println("________________________________________________________________________");
 System.out.println("Dealers turn");
 System.out.println("________________________________________________________________________");
 
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 while (!gameOver) {
 if (dealer.getHandSum() <= 17) {
 //Draw card
 dealer.addCard(deck.draw());
 System.out.println(dealer.getVem() + " drew another card");
 System.out.println("");
 System.out.print(dealer.getHandAsString(false));
 System.out.println(dealer.getHandSum());
 System.out.println("");System.out.println(""); 
 
 if (dealer.getHandSum() == 17) {
 if (player.getHandSum() == 17) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 18) {
 if (player.getHandSum() == 18) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 19) {
 if (player.getHandSum() == 19) {
 System.out.println("Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 }
 
 if (dealer.getHandSum() == 20) {
 if (player.getHandSum() == 20) {
 System.out.println("It's a draw!");
 gameOver = true;
 }
 }
 if (dealer.getHandSum() == 21) {
 System.out.println("Blackjack! Dealer won.");
 losses = losses + 1;
 gameOver = true;
 }
 if (dealer.getHandSum() > 21) {
 System.out.println("Dealer busted with " + dealer.getHandSum() + " in their hand. You win!");
 wins = wins + 1;
 gameOver = true;
 }
 } else {
 //Stay
 System.out.println("Dealer chose to stay!");
 System.out.println("");
 int totalDealerSum = dealer.getHandSum();
 int totalPlayerSum = player.getHandSum();
 if (totalDealerSum > totalPlayerSum) {
 System.out.println("Both players decided to stay. The Dealer won with a total of " + totalDealerSum + " in their hand.");
 losses = losses + 1;
 } else {
 System.out.println("Both players decided to stay. You win with a total of " + totalPlayerSum + " in your hand.");
 wins = wins + 1;
 }
 gameOver = true;
 }
 }
 //New game? And Score
 System.out.println("");System.out.println("");System.out.println("");
 
 if(wins==1 && losses==0){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 if(wins==0 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 
 if(wins>1 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins==0 && losses>1){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==0){
 System.out.println("You have won " + wins + " times and lost " + losses + " times.");
 }
 if(wins>1 && losses==1){
 System.out.println("You have won " + wins + " times and lost " + losses + " time.");
 }
 if(wins==1 && losses==1){
 System.out.println("You have won " + wins + " time and lost " + losses + " time.");
 }
 if(wins==1 && losses>1){
 System.out.println("You have won " + wins + " time and lost " + losses + " times.");
 }
 System.out.println("");
 System.out.println("Play again?");
 do {
 input = scanner.nextLine();
 } while (!input.equalsIgnoreCase("Yes") && !input.equalsIgnoreCase("No"));
 
 } while (input.equalsIgnoreCase("Yes"));
 
 scanner.close();
 }
}

Deck.java

import java.util.ArrayList;
import java.util.Collections;
public class Deck {
 private final ArrayList<Card> cards;
 public Deck() {
 cards = new ArrayList<Card>();
 // populate deck with cards
 for (Suit suit : Suit.values()) {
 for (Face face : Face.values()) {
 cards.add(new Card(face, suit));
 }
 }
 }
 public void shuffle() {
 Collections.shuffle(cards);
 }
 public Card draw() {
 return cards.remove(0);
 }
 @Override
 public String toString() {
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < cards.size(); i++) {
 sb.append(i + 1);
 sb.append('/');
 sb.append(cards.size());
 sb.append(' ');
 sb.append(cards.get(i));
 sb.append('\n');
 }
 return sb.toString();
 }
}

Player.java

import java.util.ArrayList;
//Player class
public class Player {
 private final String vem;
 private final ArrayList<Card> hand;
 public Player(String vem) {
 this.vem = vem;
  this.hand = new ArrayList<Card>();
 }
 public String getVem() {
 return vem;
 }
 public void addCard(Card card) {
 hand.add(card);
 }
 public int getHandSum() {
 int handSum = 0;
 for (Card card : hand) {
 handSum += card.getFace().getValue();
 }
 return handSum;
  }
 public String getHandAsString(boolean b) {
 StringBuilder sb = new StringBuilder();
 sb.append(vem); //'s'\
 sb.append('\n');
 for (int i = 0; i < hand.size(); i++) {
 if (i == 0 && b) {
  sb.append('\n');
 } else {
 sb.append(hand.get(i));
 sb.append('\n');
 }
 }
 return sb.toString();
 }
}

Card game in Java

So this is a card game, any feedback or tips on what I should remove from the code that is not needed is very appreciated.

Apparently I wasn't allowed to post this here.
Please delete the post if possible.
Thx.
Tweeted twitter.com/StackCodeReview/status/1352360353716445190
Loading
Source Link
Golden
  • 105
  • 6
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /