dimanche 20 mars 2016

Improving Java Midterm

I have written a game of black jack for my Java midterm. The deck can be unlimited so there is no need to create an actual deck. Anyways, I was wondering if I should make some improvements to my code or if I should leave it be. Also, when I compile my code, I get the Java error: actual and formal argument lists differ in length, which refers to the main method at the bottom. As I am newer to Java, I don't understand what this means. In addition, are the if statements in my Game method correct or do I need to make some adjustments?

import java.util.Scanner;
import java.util.Random;

public class BlackJack
{
   String type;
   int currentCard;    //Holds the value of current card
   int total = 0;      //Holds value of sum of cards drawn
   static int dealerTotal = 0;   //Holds total for dealer
   static int playerTotal = 0;   //Holds total for player
   Random ran = new Random();    //Creates new instance of random
   Scanner s = new Scanner(System.in);   //Creates new scanner for input

   //This method will introduce the game and call the other methods.
   public void BlackJack(String player)
   {
      type = player;
      System.out.println("     Welcome to a game of 21!  ");
      System.out.println("  Your opponent will be Ninja Cat!");
      System.out.println("        Prepare to lose!!!   ");
      Deal();
      Game();
   }

   //This method will generate random numbers to represent cards in a
   //regular deck. It will then give a value to player's or dealer's total.
   void displayCard()
   {
      currentCard = ran.nextInt(14);

      //This while loop forgoes the number 0 to improve 
      //readability and calculations
      while(currentCard == 0)
      {
         currentCard = ran.nextInt(14);
      }

      switch(currentCard)
      {
         //Case 1 sets the number 1 equal to an Ace
         case 1:
            System.out.println(type + " Drew an Ace");
            total = total + 11;
            System.out.println(type + " Current Total: " + total);
            break;

         //Case 11 sets randomly generated #11 equal to a Jack
         case 11:
            System.out.println(type + " Drew a Jack");
            total = total + 10;
            System.out.println(type + " Current Total: " + total);
            break;

         //Case 12 sets randomly generated #12 equal to a Queen
         case 12:
            System.out.println(type + " Drew a Queen");
            total = total + 10;
            System.out.println(type + " Current Total: " + total);
            break;

         //Case 13 sets randomly generated #13 equal to a King
         case 13:
            System.out.println(type + " Drew a King");
            total = total + 10;
            System.out.println(type + " Current Total: " + total);
            break;

         //Sets total to actual value of any current cards #2-10
         default:
            System.out.println(type + " Drew a " + currentCard);
            total = total + currentCard;
            System.out.println(type + " Current Total: " + total);
      }
   }

   //This method will start the dealing phase and call upon the
   //displayCard method
   void Deal()
   {
      System.out.println("First card is: ");
      displayCard();
      System.out.println("Second Card is: ");
      displayCard();
   }

   void Game()
   {
      if(total <= 21)
      {
         if(!(type.equals("Ninja Cat")))
         {
            System.out.println("Would you like to Hit?");
            if(s.nextLine().toLowerCase().equals("yes"))
            {
               displayCard();
               Game();
            }
            else
            {
               System.out.println("Your Total: " + total);
               playerTotal = total;
            }
         }
      }
      else
      {
         System.out.println(type + " Busted! @ " + total);
         System.exit(0);
      }

      if(type.equals("Ninja Cat"))
      {
         if(total < 17)
         {
            if(playerTotal <= 21 && total < playerTotal)
            {
               displayCard();
               Game();
            }
         }
         else if(total >= 17 && total < 21)
         {
            dealerTotal = total;
         }
         else
         {
            System.out.println(" ");
         }
      }
   }

   void Winner()
   {
      if(dealerTotal == 0)
      {
         System.out.println("Ninja Cat Busted!");
      }
      if(playerTotal == 0)
      {
         System.out.println("Player Busted!");
      }
      if(dealerTotal > playerTotal)
      {
         System.out.println("Ninja Cat Wins");
      }
      if(dealerTotal < playerTotal)
      {
         System.out.println("Player Wins!");
      }
      if(dealerTotal == playerTotal)
      {
         System.out.println("Push (Draw)");
      }
   }

   public static void main(String[] args)
   {
      BlackJack player = new BlackJack("Player");
      BlackJack dealer = new BlackJack("Ninja Cat");
      player.Winner();
   }
}

Aucun commentaire:

Enregistrer un commentaire