Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In the game that you created below, enter the name of the player and add it to a file. The program should be able to
In the game that you created below, enter the name of the player and add it to a file. The program should be able to read the file and pull a list of all the users who have played with the computer. import java.io.*; import java.util.Scanner; public class MatchStickGame { private int mSticks; private int choice; public void playComputer(Scanner sc) { switch (choice) { case 1: choice = 4; break; case 2: choice = 3; break; case 3: choice = 2; break; case 4: choice = 1; break; } mSticks -= choice; System.out.println("Computer picked:" + choice); System.out.println("No. of matchsticks remaining:" + mSticks); playHuman(sc); } public void playHuman(Scanner sc) { System.out.print("Your turn->Pick the matchsticks:"); choice = sc.nextInt(); if (choice > 4) { System.out.println("You can pick 1,2 3 or 4 sticks"); playHuman(sc); } if ((mSticks - choice) == 0) { System.out.println("You lose"); return; } mSticks -= choice; System.out.println("No. of matchsticks remaining:" + mSticks); playComputer(sc); } public static void main(String args[]) { MatchStickGame m = new MatchStickGame(); Scanner sc=new Scanner(System.in); System.out.println("You can pick 1,2 3 or 4 sticks"); System.out.println("Number of matchsticks to be used:"); m.mSticks = sc.nextInt(); int turn = (m.mSticks % 5) - 1; if (turn > 0) { m.mSticks -= turn; System.out.println("Computer picked:" + turn); System.out.println("No. of matchsticks remaining:" + m.mSticks); m.playHuman(sc); } else { m.playHuman(sc); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started