Question
Project should contain: documented source code, record of a user playing the first time and the second time, the file created in the first session
Project should contain: documented source code, record of a user playing the first time and the second time, the file created in the first session and the file updated when the player played again.
STRUCTURE OF THE PROGRAM: // fourth game EXPLAIN THE PROGRAM // to the user ASK THE USER NAME // part of project four CHECK TO SEE IF USER HAS A FILE AND IF NOT CREATE // project four ELSE READ THE DATA FROM THE FILE INTO AN ARRAY // record of previous games PLAY THE GAME USER INPUT // already written this COMPUTER CHOICE // already written EVALUATE THE WINNER // already written RECORD THE DATA // this part needs to be added STORE THE DATA IN THE FILE // needs to be added if you are going to read the data you have to know how it is stored data types in the file: store number of games store number won store number tied store number lost char -- to store moves//record of each game WHEN PLAYER PLAYS AGAIN// retrieve data from file and update with results of the second session.
PREVIOUS PROJECT
import java.util.*; public class Game { // open class Game public static void main(String[] args) { // main method Scanner console = new Scanner(System.in); // ask user for a name explain(); // function call or method call char choice = console.next().charAt(0); char comChoice = computer(); // computer selects System.out.println(" computer chose: "+comChoice); System.out.println(" user chose "+ choice); declareWinner(choice,comChoice);
} // close main
public static void explain() { //prompt with explanation of the game System.out.println("Welcome to the game."); System.out.println("You are playing against the computer."); System.out.println("Select rock 'r' paper 'p' or scissors 's'"); } // closes method explain
public static char computer() { // computer selects the move Random move = new Random(); int move1= move.nextInt(3)+1;//so actual values with 1,2,or3 int[] record = new int[4]; // declare an array of integer for (int i = 0; i<3000; i++){ //System.out.println("the number is "+ move1); record[move1]++; move1=move.nextInt(3)+1; } //for (int i =1;i<=3; i++) used to test the random generator //System.out.print(" there are "+record[i]+" hits on "+i); System.out.println(" there are "+record[1]+" hits on r "); System.out.println(" there are "+record[2]+" hits on p "); System.out.println(" there are "+record[3]+" hits on s "); if (move1 == 1) return 'r'; else if (move1== 2) return 'p'; else return 's'; } // closes method computer public static void declareWinner(char user, char machine){ if ((user == 'r' && machine == 's') ||(user == 'p' && machine == 'r') ||(user == 's' && machine == 'p')) System.out.println(" The human player won."); else if (user == machine) System.out.println("Tied"); else System.out.println(" The machine won."); }// close method
}// close class Game /* Microsoft Windows [Version 10.0.19042.804] (c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\Karla>cd C:\Java
C:\Java>javac Game.java
C:\Java>java Game Welcome to the game. You are playing against the computer. Select rock 'r' paper 'p' or scissors 's' r there are 1010 hits on r there are 1015 hits on p there are 975 hits on s computer chose: r user chose r Tied
C:\Java>java Game Welcome to the game. You are playing against the computer. Select rock 'r' paper 'p' or scissors 's' p there are 995 hits on r there are 1021 hits on p there are 984 hits on s computer chose: r user chose p The human player won.
C:\Java>java Game Welcome to the game. You are playing against the computer. Select rock 'r' paper 'p' or scissors 's' s there are 1034 hits on r there are 958 hits on p there are 1008 hits on s computer chose: r user chose s The machine won.
C:\Java> */
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