Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with Java Programming Project Project outcomes: Develop a Java progr

ID: 3817918 • Letter: P

Question

Please help with Java Programming Project Project outcomes: Develop a Java program that reads input from the keyboard using a Scanner object and its methods uses selection and branching if and if else) statements uses iteration (while, do while, or for statements uses String comparison methods. follows standard acceptable programming practices. .handles integer overflow errors Prep Readings: Absolute Java Chapter 1 3 and Security Injection Labs (Integer Error) Project Requirements: Write a Java program that plays a mathematical game called "Taking Stones" which is based on the Chinese game of Tsyan-shiz. In this game, the person who runs your program will be the human player and your program will be a simple Artificial Intelligence (Al that will serve as the other player, as well as provide the narrative for the game and keep score. Your program must keep score impartially while participating in the game, but this is not a difficult task for the computer. 2. The rules of the game are: a The human player and the computer will role a (virtual pair of dice at the beginning of each game to see who goes first. b. The player who goes first provides the number of stones that will be in the pile. The number must be between 10 and 50 Each player then removes some number (between l and 3) of stones from the pile until one player removes the final stone. The player who goes first

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
/* Name of the class has to be "Main" only if the class is public. */
class Takinggame
{
   public static void main (String[] args) throws java.lang.Exception
   {
       Scanner scanner = new Scanner(System.in);
       System.out.print("The game consists of doing a toss in the begining to decides who gets tochoose the number of stones in the pile and the first turn. User and computer takes alternate turns where in each turn theychoose a number in 1 to 3 less than number of stones left in the pile. The last one to take the turn wins the game.");      
       System.out.print("Please enter your name? ");
       String username = scanner.next();
       username = username.toLowerCase();
       username = username.substring(0, 1).toUpperCase() + username.substring(1);
       int letsplay=1;
       while(letsplay==1){      
           System.out.print("Welcome " + username + " to the game of taking stones.");
           int dice_user= 1 + (int)(Math.random() * ((6 - 1) + 1));
           int dice_ai= 1 + (int)(Math.random() * ((6 - 1) + 1));
           int chance =1 ; //1 ==> user's chance 0==>ai's chance         
           while(dice_ai==dice_user)    dice_ai= 1 + (int)(Math.random() * ((6 - 1) + 1));
           int pile_count=0;
           if(dice_user>dice_ai){
               System.out.print("Please enter the number of stones in the pile. ");
               pile_count = scanner.nextInt();
           }
           else{
               pile_count = 10 + (int)(Math.random() * ((50 - 10) + 1));
           }
           System.out.printf("Number of stones in the pile : %d ",pile_count);
           while(pile_count!=0){
               if(chance==1){
                   System.out.print("pick a number in (1-3): ");
                   int userinput = scanner.nextInt();
                   if(userinput<1||userinput>3){
                       System.out.printf("INVALID input : ");
                       }
                   else if(userinput>pile_count){System.out.printf("enter a number less than pile_count : "); }
                   else {
                       pile_count=pile_count-userinput;
                       chance=0;
                       if(pile_count==0) System.out.printf("You have won the game. ");
                   }
               }
               else{
                   int pick_ai = 1 + (int)(Math.random() * ((3 - 1) + 1));
                   while(pick_ai>pile_count) pick_ai = 1 + (int)(Math.random() * ((3 - 1) + 1));
                   pile_count = pile_count-pick_ai;
                   chance=1;
                   if(pile_count==0) System.out.printf("You have lost the game. ");
                }              
              
           }
           System.out.printf("Do you want to play again ? enter (Y or y, N or n) ");
           String text = "Z";
           while(!text.equals("Y")&&!text.equals("N")){
               text = scanner.next();          
               text=text.toUpperCase();
              
               if(!text.equals("Y")&&!text.equals("N")) System.out.printf("please enter (Y or y, N or n) ");
           }
               if(text.equals("Y")) letsplay=1;
               else letsplay=0;
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote