Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How do I write a toContinue method in blackjack class?

Posted on 2011-09-14
Medium Priority
301 Views
Could someone help me write the toContinue method in the TwoPlayerBlackJack class?

``````
``````
public class Card
{

private String rank;//card rank: a number between 2 and 10, or Jack, Queen, King or Ace
private char suit;//card suit: S,C,H,or D
private static String[] Ranks={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
private static char[] Suites={'C','H','D','S'};

/**
* Default constructor: sets card rank to 2 and card suit to H
*/
public Card()
{
rank="2";
suit='H';

}

/**
* Class constructor: sets card rank and suit to provided values.
*/
public Card(String rank, char suit)
{
this.rank=rank;
this.suit=suit;
}

/**
* Gets the rank
*/
public String getRank()
{
return rank;
}

/**
* Gets the suit
*/
public char getSuit()
{
return suit;
}

/**
* Sets the rank
*/
public void setRank(String rank)
{
this.rank=rank;
}

/**
* Sets the suit
*/
public void setSuit(char suit)
{
this.suit=suit;
}
/**
* Returns an array of possible card ranks
*/
public static String[] getPossibleRanks()
{
return Ranks;
}
/**
* Returns an array of possible card suits
*/

public static char[] getPossibleSuits()
{
return Suites;
}

/**
* Returns string representation of the card that looks as follows:
* 2 of Hearts, 3 of Spades, Jack of Diamonds, etc.
*/
public String toString()
{String suitName = "";
switch (suit){
case 'H':
suitName = rank + " of Hearts";
break;
case 'S':
suitName = rank + " of Spades";
break;
case 'D':
suitName = rank + " of Diamonds";
break;
case 'C':
suitName = rank + " of Clubs";

}
return suitName;
}
}

DeckOfCards class:

import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
private ArrayList<Card> deck;
Random getRandom = new Random();

/**
* Default constructor creates an Array List of Cards and populates it with 52 different cards
*/
public DeckOfCards()
{
deck = new ArrayList<Card>();
resetDeckOfCards();
}

/**
* Remove all leftover cards from the ArrayList of Cards called deck
* Populate an empty deck with 52 new cards...
*/
public void resetDeckOfCards()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();

for (int i = 0; i < Card.getPossibleRanks().length ; i++)
{
for (int j = 0; j < Card.getPossibleSuits().length; j++)
{
}
}
}

/**
* Remove random card of arraylist of cards and return value from method
*/
public Card getCard()
{
if (deck.isEmpty())
resetDeckOfCards();
return deck.remove(getRandom.nextInt(deck.size()));
}

/**
* Return an ArrayList of specified size (i.e. returned cards are being removed from the deck).
*/
public ArrayList<Card> getHand(int size)
{
ArrayList<Card> hand = new ArrayList<Card>();
for (int i = 0; i < size; i++)
{
}
return hand;

}

}

TwoPlayerBlackJack class:

import java.util.ArrayList;
/**
* Write a description of class TwoPlayerBlackJack here.
*
* @version (a version number or a date)
*/
public class TwoPlayerBlackJack
{

private String rank;
private ArrayList<Card> blackJack;
private char suit;

/**
* Constructor for objects of class TwoPlayerBlackJack
*/
public TwoPlayerBlackJack()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();
blackJack = new ArrayList<Card>();
for (int i = 0; i < Card.getPossibleRanks().length ; i++)
{
for (int j = 0; j < Card.getPossibleSuits().length; j++)
{
}
}
}

/**
* The method takes an object of class Card and return an integer that represents the value of that card.
* The values of the cards are determines as follows:
* two is 2, three is 3, ...., nine is 9, ten is 10,  jacks, queens and kings are all 10.
* Ace could be either 1 or 11 but for this method we will assume that the value Ace is always 1
*/
public int getCardValue(Card c){
String rank = c.getRank();

int value = -1;
try {
value = new Integer(rank);
}catch(Exception ex){
if(rank.equals("Ace"))value = 1;
else{ value = 10;

}

}
return value;
}

/**
* Given a hand cards (i.e. ArrayList of Cards) return an integer value that corresponds to the hand value
* (i.e. sum of all card values).
*/
public int computeHandValue(ArrayList <Card> hand)
{

int sum = 0;
int numAces = 0;

for(Card c: hand){
int value = getCardValue(c);
sum += value;
if(c.getRank().equals("Ace"))
numAces++;

}
if(sum<12 && numAces >0)sum += 10;

return sum;

}

/**
* Method display hand value display the value of each card followed by the hand value
*/
public void printHandValue(ArrayList <Card> hand)
{
System.out.println(computeHandValue(hand));
}

/**
* Method that takes two Array Lists of Cards (i.e. two card hands) and determines the winner
* of the BlackJack game.
* 1 – the first hand won the game
* 0 – nobody won, it is a push
* -1 – the first hand lost the game
*/
public int isWinner(ArrayList <Card> hand01, ArrayList <Card> hand02)
{

if(computeHandValue(hand01) > 21) return -1;

if(computeHandValue(hand02)  > 21) return 1;

if(computeHandValue(hand01) > computeHandValue(hand02)) return 1;
else  if(computeHandValue(hand01) == computeHandValue(hand02)) return 0;
else return -1;
}

/**
* Method that returns true if players would like to play another game, and false otherwise.
*/

public boolean toContinue()
{

do{
System.out.println("Would you like to play again? Answer yes or no");
String game = blackJack.equalsIgnoreCase("yes");
while(true){
resetDeckOfCards();

}
}
0
Question by:ryanbecker24
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 10

LVL 47

Expert Comment

ID: 36539439

I guess something like that should work:

``````public boolean toContinue()
{
Scanner keyboard = new Scanner(System.in);

while(true){
String line = keyboard.nextLine();
if(line.trim().equals("N"))break;
resetDeckOfCards();

}

System.exit(0);

}
``````
0

LVL 47

Expert Comment

ID: 36539446
Rather like that:

``````public boolean toContinue()
{
Scanner keyboard = new Scanner(System.in);

while(true){

System.out.println("Would you like to play again? Answer yes or no");

String line = keyboard.nextLine();
if(line.trim().toLoawerCase().equals("no"))break;
resetDeckOfCards();

}

System.exit(0);

}
``````
0

LVL 47

Expert Comment

ID: 36539453
If you want boolean then differently.
0

LVL 47

Expert Comment

ID: 36539465
This is with boolean return

``````public boolean toContinue()
{
Scanner keyboard = new Scanner(System.in);

while(true){

System.out.println("Would you like to play again? Answer yes or no");

String line = keyboard.nextLine();
if(line.trim().toLoawerCase().equals("no"))break;
resetDeckOfCards();
return true;

}

return false;

}
``````
0

LVL 47

Expert Comment

ID: 36539471
But if you want to return boolean you don't nedd the loop and you don't need to resetDeck in this method - at lest it is not logical
0

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36539484
if you just want boolean to return true or false if they want to play further or not
you don't want any loop here
- you make a loop elsewhere where you call this method
and reshufflisng of the deck should be also there where you start a a new
game.

``````public boolean toContinue()
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Would you like to play again? Answer yes or no");

String line = keyboard.nextLine();
if(line.trim().toLowerCase().equals("no"))return false;

return true;

}
``````
0

LVL 47

Expert Comment

ID: 36539524
Then somehwre where your main action is happeinig
you should say

while(toContinue) {
resetDeckOfCards();
//.... and intiate the gaeme
} else System.exit(0);

There is no need in the toContinue() method
to ask them repeatedly if they explicitly told you that
they don't want to play any longer or if they want to play, you just call this method
in the loop.

0

LVL 47

Expert Comment

ID: 36539548
You don't need "else" here of course:

while(toContinue) {
resetDeckOfCards();
//.... and intiate the gaeme
}
//else <---remove this

System.exit(0);

0

LVL 47

Expert Comment

ID: 36539571

This is how it works:

``````   public static  boolean toContinue()
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Would you like to play again? Answer yes or no");

String line = keyboard.nextLine();
if(line.trim().toLowerCase().equals("no"))return false;

return true;

}
``````

``````  public static void main(String[] args) {

while(toContinue()){
System.out.println("starting new game now");

}
System.out.println("exiting");
}
``````

Dialog:
``````Would you like to play again? Answer yes or no
yes
starting new game now
Would you like to play again? Answer yes or no
yes
starting new game now
Would you like to play again? Answer yes or no
no
exiting
``````
0

LVL 47

Expert Comment

ID: 36539781
The only problem with the above setup is
thet it woill ask them if they want to continue also
before the firs game.

In order to avoid it in the main method it is better to use do while construction in main()
(no changes in toContinue() method are required:

``````   public static void main(String[] args) {

do {
System.out.println("starting new game now");
//initiate game
}while(toContinue());
System.out.println("exiting");
}

``````

Ouput:
``````starting new game now
Would you like to play again? Answer yes or no
yes
starting new game now
Would you like to play again? Answer yes or no
yes
starting new game now
Would you like to play again? Answer yes or no
no
exiting
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll