So I\'ve got to convert this program to this prompt, help? Original Program: /*
ID: 3676542 • Letter: S
Question
So I've got to convert this program to this prompt, help?
Original Program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication9;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
*/
public class JavaApplication9 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
int input = 3;
int exit = 3;
//0-Loop Start
do {
//1- Show Options
System.out.printf("***************************************** * Select one of the options * * 1.Play Game ");
System.out.printf("* 2.Show Scores ");
System.out.printf("* 3.Exit ");
//2-Get input
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
//3-Act on input
//for start game run gameplay
if (input == 1) {
gamePlay();
}
//for scores run show scores
if (input == 2) {
scoreView();
}
//for exit exit
// goto first step
} while (input != exit);
}
private static void gamePlay() throws IOException {
// TODO code application logic here
String word = new String("apple");
char[] guessWord;
String guess = "";
String[] pName = new String[2];
int[] score = new int[2];
boolean wordIsGuessed = false;
boolean correctGuess = false;
int player = 0;
///////Getting Player Names/////////////////////
Scanner input = new Scanner(System.in);
/////////////Player One//////////////////
System.out.println("Please enter first player name: ");
pName[0] = input.next();
/////////////////////Player Two/////////////////
System.out.println("Please enter second player name: ");
pName[1] = input.next().toLowerCase();
char[] temp = pName[1].toCharArray();
temp[0] = Character.toUpperCase(temp[0]);
pName[1] = String.valueOf(temp);
/////////////////Game Code////
guessWord = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
guessWord[i] = '-';
}
do {
System.out.println(guessWord);
System.out.println(word + " " + pName[player] + " please enter a letter:");
guess = input.next();
correctGuess = false;
for (int i = 0; i < word.length(); i++) {
if (guess.toLowerCase().charAt(0) == word.charAt(i)) {
guessWord[i] = guess.toLowerCase().charAt(0);
score[player]++;
correctGuess = true;
}
}
if (correctGuess == false) {
player++;
if (player == 2) {
player = 0;
}
}
} while (!word.equals(String.valueOf(guessWord)));
System.out.printf("%s your score is: %d ", pName[0], score[0]);
System.out.printf("%s your score is: %d ", pName[1], score[1]);
/////////////////////Scores Code
FileWriter fw = new FileWriter("scores.txt", true);
PrintWriter myFile = new PrintWriter(fw);
myFile.println(pName[0] + ": " + score[0]);
myFile.println(pName[1] + ": " + score[1]);
myFile.close();
}
private static void scoreView() throws IOException {
System.out.printf("Welcome to Scores! ");
File file = new File("scores.txt");
Scanner scoresFile = new Scanner(file);
// Read until the end of the file.
while (scoresFile.hasNext()) {
String str = scoresFile.nextLine();
System.out.println(str);
}
scoresFile.close();//
}
}
Here are the changes that are needed to be made:
1- Your gameController method for every player will call a userController method from a “Data” class and will get a name from userController.
2- userController method will ask for user name if it doesn’t exist in your usersInfo file it will ask for a password to create a new user, else it will ask for your previously saved password to let you play the game with that username. If password was correct it will return the username to gameController.
3- Create a “Game” class and call its playGame method from your gameController. Your gameController will pass usernames to your playGame method, and after finishing one round of game, it will return an array of scores to gameController (containing two scores for player 1 and player 2).
4- gameController method then will announce the winner and ask users if they want to continue the game or not if yes, it will call playGame again else it will send the winner and the total score of the winner to setScores method of your “Data” class.
5- Your “Data” class will have two methods to set and get highscores from usersInfo file and userController method.
Explanation / Answer
WordGame3.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class WordGame3 {
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner input = new Scanner(System.in);
int selection;
do {
//menu for start of application
System.out.println("Chose one of the options" + " 1. Start Game " + " 2. Add Word" + " 3. View Scores" + " 4. Exit");
//gamer menu input
selection = input.nextInt();
// Menu options
if (selection == 1) {
gameController();
} else if (selection == 2) {
addWords();
} else if (selection == 3) {
viewScores();
}
} while (selection != 4);
//Your gameController method for every player will call a
//userController method from a “Data” class and will get a name
//from userController.
//Your “Data” class will have two methods to set and get highscores
//from usersInfo file and userController method.
}
private static void gameController() throws FileNotFoundException, IOException{
//Create a “Game” class and call its playGame method from your
//gameController. Your gameController will pass usernames to your
//playGame method, and after finishing one round of game, it will
//return an array of scores to gameController
//(containing two scores for player 1 and player 2).
//gameController method then will announce the winner and ask
//users if they want to continue the game or not if yes, it will
//call playGame again else it will send the winner and the total
//score of the winner to setScores method of your “Data” class.
String nextRound;
Scanner scan = new Scanner(System.in);
String[] pNames = new String[2];
int[] scores = new int[2];
Data myDB = new Data(); // creates user database and connects to the data class
pNames[0] = myDB.userController();
scores[0] = myDB.getScores(pNames[0]);
pNames[1] = myDB.userController();
scores[1] = myDB.getScores(pNames[1]);
Game myGame = new Game(pNames);
do
{
int[] gScores = myGame.playGame();
scores[0] = gScores[0] + scores[0]; // converts gScores int integers and scores into integers to add together
scores[1] = gScores[1] + scores[1];
System.out.println("Do you want another round? "); //ask them to see if they want another round
nextRound = new String(scan.next());
}while (nextRound.charAt(0) == 'y'); //if yes go to a new word
myDB.setScores(scores); //return the scores
}
private static void addWords() throws FileNotFoundException, IOException
{
int input = 0;
String word = new String();
String answer;
String gamewords = "words.txt"; // filename
Scanner ip = new Scanner(System.in);
Scanner checkWord = new Scanner(System.in);
do {
System.out.println("Choose one of the options" + " 1. Add Word" + " 2. Show added words" + " 3. exit");
input = ip.nextInt();
word = (ip.nextLine());
if (input == 1) {
do{
FileWriter fw = new FileWriter(gamewords, true);
PrintWriter outputFile = new PrintWriter(fw);
// to add word to the hangman game
System.out.println("Add word to the the Hangman game: ");
word = (ip.nextLine());
outputFile.println(word);
outputFile.close();
System.out.println("Do you want to add another word to the Game: ");
answer = new String(checkWord.next());
}while(answer.charAt(0) == 'y');
}else if(input == 2) {
File file = new File(gamewords);
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext()){
String line = inputFile.nextLine();
System.out.println(line);
}
inputFile.close();
}
}while(input != 3);
}
private static void viewScores() throws FileNotFoundException
{
String filename = "userInfo.txt"; // filename
File file = new File(filename); // open the file
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext()){
String line = inputFile.nextLine();
System.out.println(line);
}
inputFile.close(); // close the file
}
}
Game.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class Game {
int[] scores;
String[] pNames;
public Game(String[] names){
pNames = names;
}
public String[] playGame() throws FileNotFoundException, IOException{
int w = 0;
Random rand = new Random();
Scanner scan = new Scanner(System.in);
boolean correctGuess = false;
boolean notRevealed = true;
int playerController = -1; // player1 == -1 and player2 == +1
//play the wordGame
System.out.printf("Hello %s and %s", pNames[0], pNames[1]);
String wordList[] = new String[100]; //to get a random word
String word = new String();
//read a file into an array of strings
File myFile = new File("words.txt");
Scanner inputFile = new Scanner(myFile);
while (inputFile.hasNext() && w < wordList.length) {
wordList[w] = inputFile.nextLine();
w++;
}
word = wordList[rand.nextInt(w)]; //select a random number in that array range
//System.out.println(word); //test if the word was printing out from the file
//select the word from the array
char[] guessWord = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
guessWord[i] = '-';
}
System.out.println(word); // prints out word that you are guessing in game for testing
System.out.println(guessWord); // changes letter of words to dashes so the user cant see the word
while (notRevealed) {
if (correctGuess == false) {
playerController++;
if (playerController == 2) {
playerController = 0;
}
}
correctGuess = false;
System.out.printf("%s, please guess:", pNames[playerController]);
String guess = scan.next(); //asking for their guess
notRevealed = false;
for (int i = 0; i < word.length(); i++) { //check to see if it is in the word
if (word.charAt(i) == guess.charAt(0)) {
guessWord[i] = guess.charAt(0); //replace the dashes with correct letter
correctGuess = true;
scores[playerController]++;
}
if (guessWord[i] == '-') {
notRevealed = true;
}
}
System.out.println(guessWord);
}//ask again (loop)
// System.out.printf("%s: %d %s: %d ", pNames[0], scores[0], pNames[1], scores[1]);
//
// if (scores[0] > scores[1]) {
// System.out.println(pNames[0] + " wins");
// //outputfile.println(pNames[0] + " wins");
// } else {
// System.out.println(pNames[1] + " wins");
// //outputfile.println(pNames[1] + " wins");
//
// outputfile.printf("%s: %d %s: %d ", pNames[0], scores[0], pNames[1], scores[1]);
// outputfile.close();
inputFile.close();
return new String[2];
}
//return the scores
}
Data.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Data {
int numberOfRecords;
String[] pNames;
String[][] _userInfoG;
public Data() throws FileNotFoundException {
getInfoFromFile();
setInfoToFile();
}
private void getInfoFromFile() throws FileNotFoundException {
//read in your userInfo file
ArrayList<String> _userInfo = new ArrayList<>();
File myFile = new File("userInfo.txt");
Scanner inputFile = new Scanner(myFile);
//load all your data from text file
while (inputFile.hasNext()) {
_userInfo.add(inputFile.nextLine());
}
inputFile.close();
numberOfRecords = _userInfo.size();
//String[][] dBase = new String[100][]; // logic from the basic read and write to database game to create an array of usersInfo
_userInfoG = new String[100][];
//put the userinfo into a gloabl array
for (int i = 0; i < numberOfRecords; i++) {
System.out.println(_userInfo.get(i));
//dBase[i] = userInfo.get(i).split(" "); // logic from the basic read and write to database game to create users with passwords in database
_userInfoG[i] = _userInfo.get(i).split(" ");
}
}
private void setInfoToFile() {
//write inserInfo into the file
// String filename = "userInfo.txt";
// FileWriter fw = new FileWriter(filename, true);
// PrintWriter outputfile = new PrintWriter(fw);
}
public String userController() {
Scanner scan = new Scanner(System.in);
int flag = -1;
String pNames = new String("");
do {
System.out.println("Enter a username:"); //userController method will ask for user name
String userName = new String(scan.nextLine());
//String pNames = new String("");
for (int i = 0; i < numberOfRecords; i++) {
if (_userInfoG[i][0].equalsIgnoreCase(userName)) {
flag = i; //you play the game with that username.
}
}
if (flag == -1) { //if it doesn’t exist in your usersInfo Array
System.out.println("Enter a password:"); //it will ask for a password to create a new user,
String password = scan.nextLine();
_userInfoG[numberOfRecords] = new String[3];
_userInfoG[numberOfRecords][0] = userName;
_userInfoG[numberOfRecords][1] = password;
_userInfoG[numberOfRecords][2] = "0";
flag = numberOfRecords;
numberOfRecords++;
} else { //else it will ask for your previously saved password to let
System.out.println("Enter a password:");
String password = new String(scan.nextLine());
if (password.equals(_userInfoG[flag][1])) { //If password was correct it
System.out.println("Correct");
} else {
System.out.println("False");
}
pNames = userName;
}
} while (flag == -1);
return(pNames); //will return the username to gameController.
}
public void setScores(String[] winner) {
//replace new high score with old one
setInfoToFile();
}
public String[][] getScores() {
//remove the password colomn from userInfo and return the names and the scores
return new String[1][];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.