使用命令行界面创建 Black Jack 游戏
Create a Black Jack game with a command line Interface
我的 Java 1 class 中有一个作业。它是创建一个带有命令行界面的 Black-Jack 游戏。
但是我的书只有一页专门介绍命令行参数。这个项目是一个额外的功劳,所以我想我们应该去别处寻找这个信息。我已经完成了我的程序。它运行并工作。我敢肯定我的代码对于已经这样做了一段时间的任何人来说都会显得笨拙。
任何人都可以就这个主题启发我,主题是命令行界面吗?
这是我的代码:
package blackjack;
import java.util.Scanner;
public class BlackJack {
public static void main(String[] args) {
boolean play = true;
while (play == true) {
Game game1 = new Game();
Scanner keyboard = new Scanner(System.in);
Deck deck1 = new Deck();
deck1.createDeck();
deck1.shuffleDeck(deck1.getDeck1());
game1.setDeck(deck1.getDeck2());
System.out.println("Welcome to BLACKJACK!");
System.out.println("------------------------");
System.out.println("Enter 1 to Start: ");
int start = keyboard.nextInt();
if (start == 1) {
deck1.createDeck();
System.out.println("Shuffling Deck");
deck1.shuffleDeck(deck1.getDeck1());
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Lets Play!");
game1.dealOneCardPlayer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.dealOneCardPlayer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.dealoneCardDealer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.dealoneCardDealer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.check();
}
while (game1.isEndGame() == false) {
game1.displayPlayer();
game1.turnLoop();
}
if (game1.isEndGame() == true) {
System.out.println("FINAL HANDS");
System.out.println("");
game1.displayPlayer();
System.out.println("");
System.out.println("Do you want to Play again? 1 = yes or 2 = no");
int answer = keyboard.nextInt();
if (answer == 1) {
play = true;
} else if (answer == 2) {
System.out.println("Fine, Go do something else.");
play = false;
}
} else {
System.out.println("Why won't you play with me billy?");
}
}
}
}
public class Card {
private int value;
private String suit;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card(int value, String suit) {
this.value = value;
this.suit = suit;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public class Deck {
private Card[] Deck1 = new Card[52];
private Card[] Deck2 = new Card[52];
public Card[] getDeck2() {
return Deck2;
}
// this is important... I think.
public void setDeck2(Card[] Deck2) {
this.Deck2 = Deck2;
}
public Card[] getDeck1() {
return Deck1;
}
public void setDeck1(Card[] Deck1) {
this.Deck1 = Deck1;
}
public Card getOneCard2(int i) {
System.out.println("your drew the " + Deck2[i].getName() + " of " + Deck2[i].getSuit());
return Deck1[2];
}
public Card getOneCard(int i) {
System.out.println("your drew the " + Deck1[i].getName() + " of " + Deck1[i].getSuit());
return Deck1[i];
}
public void setDeck(Card Card, int position) {
Deck1[position] = Card;
}
public void shuffleDeck(Card[] Deck){
for(int i = 0; i < 52 ; i++){
Random rand = new Random();
int position = rand.nextInt((51 - 0) + 1);
Deck2[i] = Deck1[position];
}
}
public void printDeck1(){
for(int i = 0; i < 52 ; i++){
System.out.println(Deck1[i].getName() + " of " + Deck1[i].getSuit() + " In Game Value: " + Deck1[i].getValue() );
}
}
public void createDeck(){
int B;
int value;
String suit;
int deckPosition = 0;
for( B = 1 ; B <= 4 ; B++){
if( B == 1){
suit = "Spades";
}
else if (B == 2){
suit = "Hearts";
}
else if (B == 3){
suit = "Clubs";
}
else {
suit = "Diamonds";
}
for (int i = 1; i <= 13; i++){
value = i;
Card card = new Card(value, suit);
Deck1[deckPosition] = card;
if(Deck1[deckPosition].getValue() == 11){
Deck1[deckPosition].setName("Jack");
Deck1[deckPosition].setValue(10);
}
else if(Deck1[deckPosition].getValue() == 12){
Deck1[deckPosition].setName("Queen");
Deck1[deckPosition].setValue(10);
}
else if(Deck1[deckPosition].getValue() == 13){
Deck1[deckPosition].setName("King");
Deck1[deckPosition].setValue(10);
}
else if(Deck1[deckPosition].getValue() == 1){
Deck1[deckPosition].setName("Ace");
Deck1[deckPosition].setValue(11);
}
else{
Deck1[deckPosition].setName(Integer.toString(Deck1[deckPosition].getValue()));
}
deckPosition++;
}
}
}
}
public class Game {
private Card[] Deck = new Card[52];
private ArrayList<Card> playerHand = new ArrayList<>();
private ArrayList<Card> dealerHand = new ArrayList<>();
private int pValue = 0;
private int dValue = 0;
private int cardCounter = 0;
private boolean endGame = false;
public boolean isEndGame() {
return endGame;
}
public int getCardCounter() {
return cardCounter;
}
public void setCardCounter(int cardCounter) {
this.cardCounter = cardCounter;
}
public int getpValue() {
return pValue;
}
public int getdValue() {
return dValue;
}
public ArrayList<Card> getPlayerHand() {
return playerHand;
}
public ArrayList<Card> getDealerHand() {
return dealerHand;
}
public void dealOneCardPlayer(int i) {
playerHand.add(Deck[i]);
}
public void dealoneCardDealer(int i) {
dealerHand.add(Deck[i]);
}
public void setDealerHand(ArrayList<Card> dealerHand) {
this.dealerHand = dealerHand;
}
public void setDeck(Card[] Deck) {
this.Deck = Deck;
}
public void displayPlayer(){
pValue = 0;
dValue = 0;
System.out.println("---------------------------------------------");
System.out.println("DEALER HAND");
for( int i = 0 ; i < dealerHand.size() ; i++){
System.out.println(dealerHand.get(i).getName() + " of " + dealerHand.get(i).getSuit());
dValue = dValue + dealerHand.get(i).getValue();
}
System.out.println("Dealer Hand Value = " + dValue);
System.out.println("---------------------------------------------");
System.out.println("PLAYER HAND");
for( int i = 0 ; i < playerHand.size() ; i++){
System.out.println(playerHand.get(i).getName() + " of " + playerHand.get(i).getSuit());
pValue = pValue + playerHand.get(i).getValue();
}
System.out.println("Player Hand Value = " + pValue);
System.out.println("");
System.out.println("");
}
public void turnLoop() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Do you want to (H)it, (S)tay, or (C)all?");
String hitstaycall = keyboard.nextLine();
if("h".equals(hitstaycall.toLowerCase())){
playerHand.add(Deck[cardCounter]);
pValue = pValue + Deck[cardCounter].getValue();
cardCounter++;
if ( dValue < 16 ){
dealerHand.add(Deck[cardCounter]);
dValue = dValue + Deck[cardCounter].getValue();
cardCounter++;
}
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
else if("s".equals(hitstaycall.toLowerCase())){
if ( dValue < 16 ){
dealerHand.add(Deck[cardCounter]);
dValue = dValue + Deck[cardCounter].getValue();
cardCounter++;
}
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
else if("c".equals(hitstaycall.toLowerCase())){
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue < pValue && pValue < 21){
dealerHand.add(Deck[cardCounter]);
dValue = dValue + Deck[cardCounter].getValue();
cardCounter++;
}
if ( pValue > dValue && pValue < 22){
System.out.print("YOU WIN!!!!");
endGame = true;
}
if ( dValue > pValue && dValue < 22){
System.out.println("Dealer Wins!");
endGame = true;
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
else { System.out.println("Hey you.");
}
}
public void check(){
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
}
我认为这里 "command line interface" 的意思是命令行参数和从 System.in 读取并写入 System.out 的混合。也就是说,用户可以通过命令行终端或 window 与程序交互。
此外,它可能暗示用户通过 window 发出命令,程序在从 System.in 读取后解释这些命令。
我想这里的术语 "command" 可以按照您喜欢的方式进行解释,但您可能知道您的导师会期望什么。
我的 Java 1 class 中有一个作业。它是创建一个带有命令行界面的 Black-Jack 游戏。
但是我的书只有一页专门介绍命令行参数。这个项目是一个额外的功劳,所以我想我们应该去别处寻找这个信息。我已经完成了我的程序。它运行并工作。我敢肯定我的代码对于已经这样做了一段时间的任何人来说都会显得笨拙。
任何人都可以就这个主题启发我,主题是命令行界面吗?
这是我的代码:
package blackjack;
import java.util.Scanner;
public class BlackJack {
public static void main(String[] args) {
boolean play = true;
while (play == true) {
Game game1 = new Game();
Scanner keyboard = new Scanner(System.in);
Deck deck1 = new Deck();
deck1.createDeck();
deck1.shuffleDeck(deck1.getDeck1());
game1.setDeck(deck1.getDeck2());
System.out.println("Welcome to BLACKJACK!");
System.out.println("------------------------");
System.out.println("Enter 1 to Start: ");
int start = keyboard.nextInt();
if (start == 1) {
deck1.createDeck();
System.out.println("Shuffling Deck");
deck1.shuffleDeck(deck1.getDeck1());
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Shuffling Deck");
System.out.println("Lets Play!");
game1.dealOneCardPlayer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.dealOneCardPlayer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.dealoneCardDealer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.dealoneCardDealer(game1.getCardCounter());
game1.setCardCounter(game1.getCardCounter() + 1);
game1.check();
}
while (game1.isEndGame() == false) {
game1.displayPlayer();
game1.turnLoop();
}
if (game1.isEndGame() == true) {
System.out.println("FINAL HANDS");
System.out.println("");
game1.displayPlayer();
System.out.println("");
System.out.println("Do you want to Play again? 1 = yes or 2 = no");
int answer = keyboard.nextInt();
if (answer == 1) {
play = true;
} else if (answer == 2) {
System.out.println("Fine, Go do something else.");
play = false;
}
} else {
System.out.println("Why won't you play with me billy?");
}
}
}
}
public class Card {
private int value;
private String suit;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card(int value, String suit) {
this.value = value;
this.suit = suit;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public class Deck {
private Card[] Deck1 = new Card[52];
private Card[] Deck2 = new Card[52];
public Card[] getDeck2() {
return Deck2;
}
// this is important... I think.
public void setDeck2(Card[] Deck2) {
this.Deck2 = Deck2;
}
public Card[] getDeck1() {
return Deck1;
}
public void setDeck1(Card[] Deck1) {
this.Deck1 = Deck1;
}
public Card getOneCard2(int i) {
System.out.println("your drew the " + Deck2[i].getName() + " of " + Deck2[i].getSuit());
return Deck1[2];
}
public Card getOneCard(int i) {
System.out.println("your drew the " + Deck1[i].getName() + " of " + Deck1[i].getSuit());
return Deck1[i];
}
public void setDeck(Card Card, int position) {
Deck1[position] = Card;
}
public void shuffleDeck(Card[] Deck){
for(int i = 0; i < 52 ; i++){
Random rand = new Random();
int position = rand.nextInt((51 - 0) + 1);
Deck2[i] = Deck1[position];
}
}
public void printDeck1(){
for(int i = 0; i < 52 ; i++){
System.out.println(Deck1[i].getName() + " of " + Deck1[i].getSuit() + " In Game Value: " + Deck1[i].getValue() );
}
}
public void createDeck(){
int B;
int value;
String suit;
int deckPosition = 0;
for( B = 1 ; B <= 4 ; B++){
if( B == 1){
suit = "Spades";
}
else if (B == 2){
suit = "Hearts";
}
else if (B == 3){
suit = "Clubs";
}
else {
suit = "Diamonds";
}
for (int i = 1; i <= 13; i++){
value = i;
Card card = new Card(value, suit);
Deck1[deckPosition] = card;
if(Deck1[deckPosition].getValue() == 11){
Deck1[deckPosition].setName("Jack");
Deck1[deckPosition].setValue(10);
}
else if(Deck1[deckPosition].getValue() == 12){
Deck1[deckPosition].setName("Queen");
Deck1[deckPosition].setValue(10);
}
else if(Deck1[deckPosition].getValue() == 13){
Deck1[deckPosition].setName("King");
Deck1[deckPosition].setValue(10);
}
else if(Deck1[deckPosition].getValue() == 1){
Deck1[deckPosition].setName("Ace");
Deck1[deckPosition].setValue(11);
}
else{
Deck1[deckPosition].setName(Integer.toString(Deck1[deckPosition].getValue()));
}
deckPosition++;
}
}
}
}
public class Game {
private Card[] Deck = new Card[52];
private ArrayList<Card> playerHand = new ArrayList<>();
private ArrayList<Card> dealerHand = new ArrayList<>();
private int pValue = 0;
private int dValue = 0;
private int cardCounter = 0;
private boolean endGame = false;
public boolean isEndGame() {
return endGame;
}
public int getCardCounter() {
return cardCounter;
}
public void setCardCounter(int cardCounter) {
this.cardCounter = cardCounter;
}
public int getpValue() {
return pValue;
}
public int getdValue() {
return dValue;
}
public ArrayList<Card> getPlayerHand() {
return playerHand;
}
public ArrayList<Card> getDealerHand() {
return dealerHand;
}
public void dealOneCardPlayer(int i) {
playerHand.add(Deck[i]);
}
public void dealoneCardDealer(int i) {
dealerHand.add(Deck[i]);
}
public void setDealerHand(ArrayList<Card> dealerHand) {
this.dealerHand = dealerHand;
}
public void setDeck(Card[] Deck) {
this.Deck = Deck;
}
public void displayPlayer(){
pValue = 0;
dValue = 0;
System.out.println("---------------------------------------------");
System.out.println("DEALER HAND");
for( int i = 0 ; i < dealerHand.size() ; i++){
System.out.println(dealerHand.get(i).getName() + " of " + dealerHand.get(i).getSuit());
dValue = dValue + dealerHand.get(i).getValue();
}
System.out.println("Dealer Hand Value = " + dValue);
System.out.println("---------------------------------------------");
System.out.println("PLAYER HAND");
for( int i = 0 ; i < playerHand.size() ; i++){
System.out.println(playerHand.get(i).getName() + " of " + playerHand.get(i).getSuit());
pValue = pValue + playerHand.get(i).getValue();
}
System.out.println("Player Hand Value = " + pValue);
System.out.println("");
System.out.println("");
}
public void turnLoop() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Do you want to (H)it, (S)tay, or (C)all?");
String hitstaycall = keyboard.nextLine();
if("h".equals(hitstaycall.toLowerCase())){
playerHand.add(Deck[cardCounter]);
pValue = pValue + Deck[cardCounter].getValue();
cardCounter++;
if ( dValue < 16 ){
dealerHand.add(Deck[cardCounter]);
dValue = dValue + Deck[cardCounter].getValue();
cardCounter++;
}
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
else if("s".equals(hitstaycall.toLowerCase())){
if ( dValue < 16 ){
dealerHand.add(Deck[cardCounter]);
dValue = dValue + Deck[cardCounter].getValue();
cardCounter++;
}
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
else if("c".equals(hitstaycall.toLowerCase())){
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue < pValue && pValue < 21){
dealerHand.add(Deck[cardCounter]);
dValue = dValue + Deck[cardCounter].getValue();
cardCounter++;
}
if ( pValue > dValue && pValue < 22){
System.out.print("YOU WIN!!!!");
endGame = true;
}
if ( dValue > pValue && dValue < 22){
System.out.println("Dealer Wins!");
endGame = true;
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
else { System.out.println("Hey you.");
}
}
public void check(){
dValue = 0;
pValue = 0;
for( int i = 0 ; i < dealerHand.size() ; i++){
dValue = dValue + dealerHand.get(i).getValue();
}
for( int i = 0 ; i < playerHand.size() ; i++){
pValue = pValue + playerHand.get(i).getValue();
}
if ( dValue > 21 && pValue > 21){
System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
endGame = true;
}
if (pValue == 21 || dValue == 21 ){
System.out.println("BLACKJACK!");
endGame = true;
}
if ( dValue > 21 && pValue < 22){
System.out.println("Dealer BUST. You Win");
endGame = true;
}
if ( pValue > 21 && dValue <22){
System.out.println("You BUST. Dealer Win");
endGame = true;
}
}
}
我认为这里 "command line interface" 的意思是命令行参数和从 System.in 读取并写入 System.out 的混合。也就是说,用户可以通过命令行终端或 window 与程序交互。
此外,它可能暗示用户通过 window 发出命令,程序在从 System.in 读取后解释这些命令。
我想这里的术语 "command" 可以按照您喜欢的方式进行解释,但您可能知道您的导师会期望什么。