Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Game play Object Creation:
- package com.company;
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- import java.lang.*;
- public class FinalProject {
- //Initialize the score
- public static int score = 0;
- public static void GamePlay(Scanner input) throws IOException {
- //Creates an array with all the words in the dictionary
- Scanner inputFile = new Scanner(new File("C:\\Users\\Arush Adabala\\Desktop\\Dictionary.txt"));
- String st = "";
- while (inputFile.hasNext()){
- st= st + " " +inputFile.next();
- }
- String s = st;
- String[] words = s.split("\\s+");
- for( int i = 0; i<words.length; i++)
- {
- words[i] = words[i].replaceAll("[^\\w]", "");
- }
- input = new Scanner(System.in);
- // Generate two random letters.
- Random rand = new Random();
- char firstLetter = (char) (rand.nextInt(26) + 'A');
- char secondLetter = (char) (rand.nextInt(26) + 'A');
- System.out.println("First letter = " + firstLetter + " Last letter = " + secondLetter);
- //User types in a word
- System.out.println("Type a word");
- String typedWord = input.next();
- String checkingWord = typedWord.toUpperCase();
- char letter1 = checkingWord.charAt(0);
- char letter2 = checkingWord.charAt(typedWord.length() - 1);
- //Checks if the first and last letter match
- if (letter1 == firstLetter && letter2 == secondLetter) {
- for(int i = 0; i < 58110; i++) {
- //Checks if the word is in the dictionary.
- if (typedWord.equalsIgnoreCase(words[i])) {
- System.out.println("Correct");
- score++;
- }
- }
- } else {
- System.out.println("Incorrect");
- score--;
- }
- System.out.println("Your score is " + score);
- }
- }
- Level Object Creation:
- package com.company;
- public class FinalProjectLevel {
- private int level;
- public FinalProjectLevel() //Default constructor
- {
- level = 0;
- }
- public FinalProjectLevel(int level1) //Default constructor
- {
- level = level1;
- }
- public void setLevel(int level1)
- {
- level = level1;
- }
- public int getLevel()
- {
- return level;
- }
- public String toString()
- {
- return "("+level+")";
- }
- }
- Object Client Class:
- package com.company;
- import java.io.IOException;
- import java.util.*;
- public class FinalProjectClient {
- public static void main(String[] args)throws IOException {
- System.out.println("Type in a word that matches the first and last letter given to you.");
- System.out.println("If you type in a word that doesn't match the first and letter you lose a point");
- System.out.println("You don't lose or gain any points for typing in a word that doesn't exist.");
- System.out.println("You gain a point for every word you get correct. You go up a commitment level for every game you play.");
- // Create a variable to increment the number of games played.
- int playcounter = 0;
- //Create level object.
- FinalProjectLevel level = new FinalProjectLevel();
- //Output your initial level.
- System.out.println("Your level currently is "+level.toString());
- // Create scanner
- Scanner input = new Scanner(System.in);
- //Create variable needed to keep playing
- String UserContinuityStatus = "";
- // Create the main Game object.
- FinalProject main = new FinalProject();
- while (!UserContinuityStatus.equalsIgnoreCase("no")) {
- //Start the Game.
- main.GamePlay(input);
- //Increment the number of games played
- playcounter++;
- //Ask the User if he wants to keep playing.
- System.out.println("Do you want to keep playing (yes/no)");
- UserContinuityStatus = input.next();
- }
- level.setLevel(playcounter);
- System.out.println("Your commitment level is "+level.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement