jeudi 27 mai 2021

Bird Rock Water using method in Java

So my task is to make a Bird Rock Water game using methods. My problem is how to

  • let the program accepts the integer value from the user that denotes Bird (1), Rock(2) or Water(3).
  • let user play 3 times in one round. (the output for my code keep repeating more than 3 times)
  • let the program count user’s ties, win and lose using method.

Any ideas on how to implement this? or what I need to change to my code.

This is my code:

import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
        public static Scanner   sc  = new Scanner(System.in);
        public static int score = 0;
        public static int gameCount = 0;

        public static void main(String[] args)
        {
            System.out.println("Welcome to Wan tu Som game");
            play();
            while (playAgain())
            {
                play();
            }
        }

        public static void play()
        {
             String computer = computerChoice();
             String user= userChoice();
             determineWinner(computer, user);
        }
        
        public static String computerChoice()
        {
            Random rand = new Random();
            int cinput = rand.nextInt(3) + 1;
            String computer = "thing";
            if (cinput == 1)
                computer = "Bird";
            if (cinput == 2)
                computer = "Rock";
            if (cinput == 3)
                computer = "Water";
            return computer;
        }

        public static boolean playAgain()
        {
            System.out.println("Would you like to play again?(y/n): ");
            String input = sc.nextLine();
            if (input.toLowerCase().equals("y"))
            {
                return true;
            } else if (input.toLowerCase().equals("n"))
            {
                return false;
            } else
            {
                System.out.println("Invalid Input");
                return playAgain();
            }

        }

        public static String userChoice()
        {

            String user = "default";
            do
            {
                System.out.println("Choose either Bird (1), Rock (2), Water (3) ");
                user = sc.nextLine();
            } while (!isValidChoice(user));
            return user;
        }

        public static boolean isValidChoice(String choice)
        {
            boolean status;
            if (choice.equals("Bird"))
                status = true;
            else if (choice.equals("Rock"))
                status = true;
            else if (choice.equals("Water"))
                status = true;
            else
            {
                status = false;
                System.out.println("Error! Make sure you are capitalizing your choices");
            }

            return status;
        }

        public static void determineWinner(String computer, String user)
        {
            gameCount++;
            System.out.println(computer + " vs " + user);
            if (computer.equals("Water") && user.equals("Rock"))
            {
                score--;
                System.out.println("You lost!");
            }
            if (computer.equals("Bird") && user.equals("Water"))
            {
                score--;
                System.out.println("You lost!");
            }
            if (computer.equals("Rock") && user.equals("Bird"))
            {
                score--;
                System.out.println("You lost!");
            }
            if (computer.equals("Bird") && user.equals("Rock"))
            {
                score++;
                System.out.println("You win!");
            }
            if (computer.equals("Rock") && user.equals("Water"))
            {
                score++;
                System.out.println("You win!");
            }
            if (computer.equals("Water") && user.equals("Bird"))
            {
                score++;
                System.out.println(" You win!");
            } else if (computer.equals(user))
            {
                System.out.println("Game ties!");
            }
            
            int ties = 0;
             ties += score;
             int userWins = 0;
             userWins += score;
             int userLost = 0;
             userLost += score;
             int compWins = 0;
             {
             for(int i = 0; i < 3; i ++){
                 computer = computerChoice();
                 user = userChoice();
                 determineWinner(computer, user);
             }
            System.out.println("You have ties for " + ties + " times");
            System.out.println("You have winning for " + userWins + " times");
            System.out.println("You have losing for " + userLost + " times");
            return;
        }

    }
}



Output:

Welcome to Wan tu Som game
Choose either Bird (1), Rock (2), Water (3) 
Bird
Bird vs Bird
Game ties!
Choose either Bird (1), Rock (2), Water (3) 
Water
Water vs Water
Game ties!
Choose either Bird (1), Rock (2), Water (3) 
Rock
Rock vs Rock
Game ties!
Choose either Bird (1), Rock (2), Water (3) 
Bird
Water vs Bird
 You win!
Choose either Bird (1), Rock (2), Water (3) 
Rock
Bird vs Rock
You win!
Choose either Bird (1), Rock (2), Water (3) 
Water
Water vs Water
Game ties!
Choose either Bird (1), Rock (2), Water (3) 

Expected output:

Welcome to Wan tu Som game                                                                                                  

Choose either Bird (1), Rock (2), or Water (3): 1                                                                               

You win!                                                                                                                        

Choose either Bird (1), Rock (2), or Water (3): 2                                                                               

Rock vs Bird: You win!                                                                                                          

Choose either Bird (1), Rock (2), or Water (3): 1                                                                               

Game ties!                                                                                                                      

Would you like to play again?: n                                                                                                

You have ties for 1 times                                                                                                       

You have winning for 2 times                                                                                                    

You have losing for 0 times




Aucun commentaire:

Enregistrer un commentaire