In Java: use of a server to join two clients together for a modified game of Bla
ID: 3696442 • Letter: I
Question
In Java:
use of a server to join two clients together for a modified game of Blackjack.
Create exactly two classes. One class will act as the functional server. The main method of this server will setup the connection on a localhost, find the two clients, let them know which one is Player 1 and which one is Player 2 (depending on the order they connect), and then manage the game as it’s being played.
For each player’s turn they should start by displaying their current score to the user. Then, the user should be prompted to select whether they want to “Hit” or “Stand”. This option selection can be done any way you choose, but invalid entries must be accounted for*. If the player chooses to “Hit”, the server should deal a “card” to the player, which will consist of choosing a random number between 1 and 13. The server sends this “card” to the player, and the player receives the card and adds it to their score. If the card is above a 10, then add a 10 to the player’s score instead of the original value (In a standard Blackjack game, Jacks, Queens, and Kings still count as 10). After they receive the card or if they choose to “Stand”, the next player should take their turn. If either player takes a “Hit”, and the card they’re dealt raises their score total above 21, then the game should immediately end and the other player is the winner (this is known as a “Bust”). Likewise, if both players choose to “Stand”, then both players should be shown the final scores and the game is then over.
*for example, if you choose to say “Enter (1) For a Hit or enter (2) to Stand”, then any entry other than 1 or 2 should be counted as invalid and should loop to allow the player another entry. If you choose something like “Enter (H)it or (S)tand”, then valid entries should be “H”, “S”, and ideally “h” and “s”.
Get user input in any way you choose, though you must check for invalid entries and prompt a re-entry if invalid data is entered; you may choose to implement an additional feature that allows a 1 (Ace) to be worth either 1 or 11, however this is not necessary; you may choose to deal two cards to each player to begin the game, however this is not necessary; you may choose to display the first card drawn to each player to both players, however this is not necessary; you may use driver classes and additional methods other than the main method in each class, however this is not necessary to complete this program.
Explanation / Answer
Answer:
#include <iostream>
using namespace std;
class Blackjack
{
public:
void input();
void figureout();
void output();
Blackjack(int cards);
Blackjack();
private:
char card;
char output_statement;
int card_value;
int card_value2;
int counter;
int cards;
int number_of_cards;
};
void description();
int main()
{
int input;
char again;
Blackjack input1;
description();
do
{
input1= Blackjack(input);
input1.input();
input1.figureout();
input1.output();
cout << endl;
cout << "Want to run the program again?(type y for yes or n for no) ";
cin >> again;
cout << endl;
}while((again == 'y')||(again == 'Y'));
cout << endl << endl;
cout << "program ends ";
cout << endl << endl;
system("pause");
return 0;
}
void description()
{
//cout << endl << endl << endl;
}
void Blackjack::input()
{
counter = 0;
card_value = 0;
card_value2 = 0;
cout << "Enter the values of your cards:";
cout << endl;
cout << "Press enter after each card til the message iput end comes up";
cout << endl;
cout << "Number of cards are: "<< number_of_cards;
cout << endl;
cout << endl;
do
{
cout << "Enter value of the card: ";
cin >> card;
cout << endl;
counter++;
if (card == '2')
{
card_value = card_value + 2;
card_value2 = card_value2 + 2;
}
else if (card == '3')
{
card_value = card_value + 3;
card_value2 = card_value2 + 3;
}
else if (card == '4')
{
card_value = card_value + 4;
card_value2 = card_value2 + 4;
}
else if (card == '5')
{
card_value = card_value + 5;
card_value2 = card_value2 + 5;
}
else if (card == '6')
{
card_value = card_value + 6;
card_value2 = card_value2 + 6;
}
else if (card == '7')
{
card_value = card_value + 7;
card_value2 = card_value2 + 7;
}
else if (card == '8')
{
card_value = card_value + 8;
card_value2 = card_value2 + 8;
}
else if (card == '9')
{
card_value = card_value + 9;
card_value2 = card_value2 + 9;
}
else if ((card == 'A')||(card == 'a'))
{
card_value = card_value + 1;
card_value2 = card_value2 + 11;
}
else if ((card == 'Q')||(card == 'q'))
{
card_value = card_value + 10;
card_value2 = card_value2 + 10;
}
if ((card == 'K')||(card == 'k'))
{
card_value = card_value + 10;
card_value2 = card_value2 + 10;
}
else if ((card == 'J')||(card == 'j'))
{
card_value = card_value + 10;
card_value2 = card_value2 + 10;
}
else if ((card == 'T')||(card == 't'))
{
card_value = card_value + 10;
card_value2 = card_value2 + 10;
}
} while(counter != number_of_cards);
cout << "<Input end>";
cout << endl << endl;
}
void Blackjack::figureout()
{
if(card_value <= 21)
{
output_statement = 'A';
}
if(card_value2 <= 21)
{
output_statement = 'B';
}
cout << endl;
}
void Blackjack:: output()
{
switch(output_statement)
{
case 'A':
{
cout << "The Master hand is "<< card_value;
cout << endl;
break;
}
case 'B':
{
cout << "The Master hand is "<< card_value2;
cout << endl;
break;
}
default:
{
cout << "OUT OF GAME:: score is over 21!";
cout << endl;
break;
}
}
}
Blackjack :: Blackjack(int cards)
{
cout << "Enter the amount of cards in your hand: ";
cin >> cards;
cout << endl;
while ((cards < 2)||(cards > 5))
{
cout << "Invalid amount of cards in hand re- enter: ";
cin >> cards;
cout << endl;
}
number_of_cards = cards;
}
Blackjack :: Blackjack()
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.