+1 (315) 557-6473 

Program To Create a Simple Word Game Using Java Programming Language Assignment Solutions.


Instructions

Objective
Write a program to create a simple word game in Java programming language.

Requirements and Specifications

Program to create a simple word game in Java programming language

Source Code

GAME PLAYER

// CMPINF 401 Fall 2022

//Pranav Rajupalepu

// GamePlayer Class

/*This class you must implement yourself. It is a fairly simple class but it is very

important to your game, as it will encapsulate the functionality of a player of your game. A GamePlayer

will represent each player of the game and should have data to represent the

player's name,

number of rounds played, and

other information.

It should also have methods to update the values as needed and to

represent the data in a form that can be saved into a file. For more specific details on the GamePlayer

class and its requirements, see the program GamePlayerTest.java. Read the comments in this program

carefully and see the output in GPT-out.txt. Your GamePlayer class should run with GamePlayerTest

without change and should generate the same output as shown in GPT-out.txt.*/

import java.io.*;

import java.util.*;

public class GamePlayer {

    // initialize variables

    private String userName;

    private String password;

    private int rounds;

    private int successes;

    private int minDists;

    private int attempts;

    // Name:

    public GamePlayer(String userName, String password, int rounds, int successes, int minDists, int attempts) {

        this.userName = userName;

        this.password = password;

        this.rounds = rounds;

        this.successes = successes;

        this.minDists = minDists;

        this.attempts = attempts;

    }

    public GamePlayer(String userName, String password) {

        this(userName, password, 0, 0, 0, 0);

    }

    public GamePlayer(String userName) {

        this(userName, null);

    }

    public int getRounds() {

        return rounds;

    }

    public int getSuccesses() {

        return successes;

    }

    public int getMinDists() {

        return minDists;

    }

    public int getAttempts() {

        return attempts;

    }

    public void success(int tries, int optTries) {

        if (tries == optTries) {

            minDists += tries;

            this.successes += 1;

        }

        else {

            attempts += (tries - optTries);

        }

        this.rounds += 1;

    }

    public void failure() {

        this.rounds += 1;

    }

    public String toStringFile() {

        return userName + "," + password + "," + rounds + "," + successes + "," + minDists + "," + attempts;

    }

    @Override

    public String toString() {

        StringBuilder builder = new StringBuilder();

        double ratio = 1.0;

        if (rounds > 0) {

            ratio = 1.0 * attempts / minDists;

        }

        builder.append("\t").append("Name: ").append(userName).append(System.lineSeparator());

        builder.append("\t").append("Rounds: ").append(rounds).append(System.lineSeparator());

        builder.append("\t").append("Successes: ").append(successes).append(System.lineSeparator());

        builder.append("\t").append("Failures: ").append(rounds - successes).append(System.lineSeparator());

        builder.append("\t").append("Ratio (successes only): ").append(ratio);

        return builder.toString();

    }

// A getName() accessor will return the name of the GamePlayer

    public String getName() {

        return this.userName;

    }

//– Each GamePlayer will now have a password in addition to a name. This will be stored in a

// separate data field and will require the constructors to initialize this field. A setPass()

// method will also be implemented to allow a user to set (or update) their password.

    public void setPass(String password) {

        this.password = password;

    }

    //An equals() method will return true if an argument GamePlayer has the same name and

    // password (both must match) as the current GamePlayer and false otherwise.

    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        GamePlayer player = (GamePlayer) o;

        return Objects.equals(userName, player.userName) && Objects.equals(password, player.password);

    }

    @Override

    public int hashCode() {

        return Objects.hash(userName, password);

    }

}

PLAYER LIST

// CMPINF 0401 Fall 2022

// Shell of the PlayerList class.

// This class represents a collection of players -- a very simple database. The

// collection can be represented in various ways. However, for this assignment

// you are required to use an array of GamePlayer.

// Note the imports below. java.util.* is necessary for a Scanner and java.io.* is

// necessary for various File classes.

import java.util.*;

import java.io.*;

public class PlayerList {

    private final List<GamePlayer> players; // list of GamePlayer

    private final String fileName;

    private int capacity = 6;

    // Initialize the list from a file. Note that the file name is a parameter. You

    // must open the file, read the data, making a new GamePlayer object for each line

    // and putting them into the array. Your initial size for the array MUST be 3 and

    // if you fill it should resize by doubling the array size. You will need to write

    // a resize() method to do the resizing.

    // Note that this method throws IOException. Because of this, any method that CALLS

    // this method must also either catch IOException or throw IOException. Note that

    // the main() in PlayerListTest.java throws IOException. Keep this in mind for your

    // main program (Assig3.java). Note that your saveList() method will also need

    // throws IOException in its header, since it is also accessing a file.

    public PlayerList(String fileName) throws IOException {

        this.fileName = fileName;

        players = new ArrayList<>();

        try (Scanner scanner = new Scanner(new File(fileName))) {

            while (scanner.hasNextLine()) {

                String[] parts = scanner.nextLine().split(",");

                String userName = parts[0];

                String password = parts[1];

                int rounds = Integer.parseInt(parts[2]);

                int successes = Integer.parseInt(parts[3]);

                int minDists = Integer.parseInt(parts[4]);

                int attempts = Integer.parseInt(parts[5]);

                addPlayer(new GamePlayer(userName, password, rounds, successes, minDists, attempts));

            }

        }

    }

    // See program PlayerListTest.java to see the other methods that you will need for

    // your PlayerList class. There are a lot of comments in that program explaining

    // the required effects of the methods. Read that program very carefully before

    // completing the PlayerList class. You will also need to complete the modified

    // GamePlayer class before the PlayerList class will work, since your array is an

    // array of GamePlayer objects.

    // You may also want to add some methods that are not tested in PlayerListTest.java.

    // Think about what you need to do to a PlayerList in your Assig3 program and write

    // the methods to achieve those tasks. However, make sure you are always thinking

    // about encapsulation and data abstraction.

    public int size() {

        return players.size();

    }

    public int capacity() {

        return capacity;

    }

    public boolean containsName(String s) {

        for (GamePlayer player : players) {

            if (player.getName().equals(s)) {

                return true;

            }

        }

        return false;

    }

    public boolean addPlayer(GamePlayer onePlayer) {

        if (containsName(onePlayer.getName())) {

            return false;

        }

        if (size() == capacity) {

            capacity *= 2;

        }

        players.add(onePlayer);

        return true;

    }

    public GamePlayer authenticate(GamePlayer temp) {

        for (GamePlayer player : players) {

            if (player.equals(temp)) {

                return player;

            }

        }

        return null;

    }

    public void saveList() throws IOException {

        try (PrintWriter printWriter = new PrintWriter(fileName)) {

            for (GamePlayer player : players) {

                printWriter.println(player.toStringFile());

            }

        }

    }

    @Override

    public String toString() {

        StringBuilder builder = new StringBuilder("Total Players: " + size());

        builder.append(System.lineSeparator());

        for (int i = 0; i < size(); i++) {

            builder.append(players.get(i));

            builder.append(System.lineSeparator());

            builder.append(System.lineSeparator());

        }

        return builder.toString();

    }

}