Write a Java program thats a card game that asks the user to

Write a Java program thats a card game that asks the user to pick a number from a normal 52 card deck. Next, the computer picks a random number between 0 to 51 (another card from the deck, but not the one the user chose). The program should then display whose card was highest.

As a second part to the program, create an “auto play” mode, where two computers play against each other. Like the first part, each computer randomly picks a card and a message declares the one with the highest card. Have the deck shuffled every turn, and allow the two computers to play 100 times before quitting. Once they have played 100 times, display the winner of the whole game (who had the most wins out of 100 turns), or if it ended in a draw.

Have each part in its own separate method, and allow the user to choose between Part 1 or Part 2, for testing purposes.  

Solution

// CardGame.java

import java.util.Scanner;
import java.util.*;

public class CardGame
{  
   public static void choice1()
   {
   Scanner sc=new Scanner(System.in);
   Random rand = new Random();
   int computer, user;

   while (true)
   {
      System.out.println(\"\ Pick a number(0-51): \");
       user = sc.nextInt();

       if(user < 0 || user > 51)
           System.out.println(\"Invalid Input\");
       else
           break;

   }
     
      while (true)
      {
          computer = rand.nextInt(51);
          if(computer != user)
              break;
      }

      if(computer > user)
      {
          System.out.println(\"Computers card was highest\");
      }
      else
      {
          System.out.println(\"Users card was highest\");
      }
   }

   public static void choice2()
   {
       Random rand = new Random();
   int computer1;
   int computer2;
   int countcomputer1 = 0;
   int countcomputer2 = 0;
   int count = 0;
   while (count < 100)
   {
      computer1 = rand.nextInt(51);
      computer2 = rand.nextInt(51);
      while(computer2 == computer1)
      {
          computer2 = rand.nextInt(51);
      }
      if (computer1 > computer2)
      {
          countcomputer1++;  
      }
      else
          countcomputer2++;

      count++;
   }

   System.out.println(\"Computer1: \" + countcomputer1);
   System.out.println(\"Computer2: \" + countcomputer2);
      if(countcomputer1 > countcomputer2)
      {
          System.out.println(\"Computer1 won\");
      }
      else if(countcomputer2 > countcomputer1)
      {
          System.out.println(\"Computer2 won\");
      }
       else
       {
           System.out.println(\"Draw\");
       }
   }


   public static void main(String[] args)
   {


       Scanner sc=new Scanner(System.in);
       int choice;

       while(true)
       {
           System.out.println(\"\ 1. Part1\ 2. Part2\ 3. exit\");
           System.out.println(\"Enter choice \");
           choice = sc.nextInt();
           if(choice == 1)
               choice1();

           else if (choice == 2)
               choice2();

           else
               break;
       }
   }
}

/*
output:

1. Part1
2. Part2
3. exit
Enter choice
1

Pick a number(0-51):
-1
Invalid Input

Pick a number(0-51):
23
Computers card was highest

1. Part1
2. Part2
3. exit
Enter choice
2
Computer1: 41
Computer2: 60
Computer2 won

1. Part1
2. Part2
3. exit
Enter choice
2
Computer1: 47
Computer2: 53
Computer2 won

1. Part1
2. Part2
3. exit
Enter choice
3


*/

Write a Java program thats a card game that asks the user to pick a number from a normal 52 card deck. Next, the computer picks a random number between 0 to 51
Write a Java program thats a card game that asks the user to pick a number from a normal 52 card deck. Next, the computer picks a random number between 0 to 51
Write a Java program thats a card game that asks the user to pick a number from a normal 52 card deck. Next, the computer picks a random number between 0 to 51

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site