├── Game.java ├── Main.java ├── MovieList.java ├── README.md └── tests.zip /Game.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Game { 4 | /** 5 | * Class that represents a "Guess The Movie" Game: 6 | * The rules are simple, the computer randomly picks a movie title, and shows the player how many letters it's made 7 | * up of. The goal is to try to figure out the movie by guessing one letter at a time. If a letter is indeed in the 8 | * title the computer will reveal its correct position in the word, if not, the player loses a point. If the player 9 | * loses 10 points, the game is over. 10 | */ 11 | 12 | private String movieToGuess; 13 | private int pointsLost; 14 | private String wrongLetters; 15 | private String rightLetters; 16 | private boolean gameWon; 17 | 18 | /** 19 | * Class constructor that initializes a {@link MovieList} object from a file containing the movies' titles, and 20 | * all other attributes nof the game: 21 | * pointsLost = points lost so far; 22 | * rightLetters = letters guessed that are actually in the movie title (in upper and lower case); 23 | * wrongLetters = letters guessed that are not in the movie title; 24 | * gameWon = whether the player has already won the game. 25 | * 26 | * @param pathname Path to a file containing the movies' titles. 27 | */ 28 | public Game(String pathname) { 29 | MovieList movieList = new MovieList(pathname); 30 | movieToGuess = movieList.getRandomMovie().trim(); 31 | pointsLost = 0; 32 | rightLetters = ""; 33 | wrongLetters = ""; 34 | gameWon = false; 35 | } 36 | 37 | /** 38 | * Method that returns the title of the movie to be guessed. 39 | * 40 | * @return title of the movie to be guessed. 41 | */ 42 | public String getMovieTitle() { 43 | return movieToGuess; 44 | } 45 | 46 | /** 47 | * Method that replaces all the letters in the movie title with underscores, if no letters have been correctly 48 | * guessed yet, and all the letters except the ones guessed, if any letter was already correctly guessed. 49 | * 50 | * @return {@link String} with the movie title with the letters not guessed hidden. 51 | */ 52 | public String getHiddenMovieTitle() { 53 | if(rightLetters.equals("")){ 54 | return movieToGuess.replaceAll("[a-zA-Z]", "_"); 55 | } 56 | else{ 57 | return movieToGuess.replaceAll("[a-zA-Z&&[^" + rightLetters +"]]", "_"); 58 | } 59 | } 60 | 61 | /** 62 | * Method that returns letters guessed that are not in the movie title. 63 | * 64 | * @return {@link String} with letters guessed that are not in the movie title separated by blank spaces. 65 | */ 66 | public String getWrongLetters() { 67 | return wrongLetters; 68 | } 69 | 70 | /** 71 | * Method that returns true if the game was won and false otherwise. 72 | * 73 | * @return true if the game was won and false otherwise. 74 | */ 75 | public boolean WonGame() { 76 | return gameWon; 77 | } 78 | 79 | /** 80 | * Method that returns that the game has ended and the player did not win if number of points lost is at least 10, 81 | * and returns that the game has ended and the player won if the previous is not true and there are no underscores 82 | * left in the hidden version of the movie title. 83 | * 84 | * @return true if the game has ended and false otherwise. 85 | */ 86 | public boolean gameEnded() { 87 | if (pointsLost >= 10) { 88 | return true; 89 | } 90 | 91 | if(!getHiddenMovieTitle().contains("_")) { 92 | gameWon = true; 93 | return true; 94 | } 95 | return false; 96 | } 97 | 98 | /** 99 | * Method that (1) asks the player to input a letter; (2) converts it to lower case; (3) asks him to input another 100 | * letter (implemented recursively) if (a) the {@link String} inputted is not a letter or (b) if the letter was 101 | * already guessed and so is included in the {@link String} objects containing the letters guessed wrongly and 102 | * correctly, respectively; (4) if the {@link String} inputted is a letter not guessed yet, the method returns the 103 | * letter. 104 | * 105 | * @return Letter not guessed yet. 106 | */ 107 | private String inputLetter(){ 108 | 109 | System.out.println("Guess a letter:"); 110 | Scanner scanner = new Scanner(System.in); 111 | String letter = scanner.nextLine().toLowerCase(); 112 | 113 | if(!letter.matches("[a-z]")){ 114 | System.out.println("That is not a letter."); 115 | return inputLetter(); 116 | } 117 | else if(wrongLetters.contains(letter) || rightLetters.contains(letter)){ 118 | System.out.println("You already guessed that letter."); 119 | return inputLetter(); 120 | } 121 | else{ 122 | return letter; 123 | } 124 | } 125 | 126 | /** 127 | * Method that (1) asks the player for a letter not guessed and (a) if the guess is correct, adds it to the 128 | * {@link String} that contains the letters guessed correctly in upper and lower case, (b) otherwise increases 129 | * the number of points lost by 1 and adds the letter to the {@link String} that contains the letters guessed 130 | * wrongly. 131 | */ 132 | public void guessLetter() { 133 | 134 | String guessedLetter = inputLetter(); 135 | 136 | if (movieToGuess.toLowerCase().contains(guessedLetter)) { 137 | rightLetters += guessedLetter + guessedLetter.toUpperCase(); 138 | } 139 | else { 140 | pointsLost++; 141 | wrongLetters += " " + guessedLetter; 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | Game game = new Game("movies.txt"); 5 | 6 | System.out.println("Welcome to Guess the Movie!"); 7 | System.out.println("The rules are simple, the computer randomly picks a movie title, and shows you how " + 8 | "many " + "letters it's made up of."); 9 | System.out.println("Your goal is to try to figure out the movie by guessing one letter at a time."); 10 | System.out.println("If a letter is indeed in the title the computer will reveal its correct position in" + 11 | " the " + "word, if not, you lose a point."); 12 | System.out.println("If you lose 10 points, game over!"); 13 | System.out.println("Let's start!"); 14 | System.out.println("The movie title has " + game.getMovieTitle().length() + " characters (including spaces " 15 | + "and punctuation)."); 16 | 17 | while(!game.gameEnded()){ 18 | System.out.println("You are guessing:" + game.getHiddenMovieTitle()); 19 | System.out.println("You have guessed (" + game.getWrongLetters().length()/2 + ") wrong letters:" 20 | + game.getWrongLetters()); 21 | game.guessLetter(); 22 | } 23 | if(game.WonGame()){ 24 | System.out.println("You win!"); 25 | System.out.println("You have guessed " + game.getMovieTitle() + " correctly."); 26 | } 27 | else{ 28 | System.out.println("You have guessed (" + game.getWrongLetters().length()/2 + ") wrong letters:" + 29 | game.getWrongLetters()); 30 | System.out.println("You lost!"); 31 | System.out.println("The movie title was " + game.getMovieTitle()); 32 | System.out.println("Better luck next time."); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /MovieList.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | import java.util.ArrayList; 3 | import java.util.Scanner; 4 | import java.io.FileNotFoundException; 5 | 6 | public class MovieList { 7 | /** 8 | * Class representing a list of movies. 9 | */ 10 | 11 | private ArrayList movies; 12 | 13 | /** 14 | * Class constructor that stores the movie titles contained a file in an {@link ArrayList}, until it scans all lines 15 | * in the file, if a valid path to the file exists or warns the user otherwise. 16 | * 17 | * @param pathname Path to a file containing the movies' titles. 18 | */ 19 | public MovieList(String pathname) { 20 | movies = new ArrayList(); 21 | File file = new File(pathname); 22 | try { 23 | Scanner scanner = new Scanner(file); 24 | while (scanner.hasNextLine()) { 25 | movies.add(scanner.nextLine()); 26 | } 27 | } catch (FileNotFoundException e) { 28 | System.out.println("File does not exist!"); 29 | } 30 | } 31 | 32 | /** 33 | * Method that generates a random {@link Integer} from 0 to the number of movie titles in the list minus 1, and 34 | * returns the movie title in the movies {@link ArrayList} with that index. 35 | * 36 | * @return random movie title from the list. 37 | */ 38 | public String getRandomMovie() { 39 | //Returns a string with a random movie title from the list. 40 | int movieIndex = (int) (Math.random() * movies.size()); 41 | return movies.get(movieIndex); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Object Oriented Programming in Java - Guess The Movie Project** (Udacity) 2 | 3 | **Main.java:** Main class 4 | **MovieList.java**: MovieList class 5 | **Game.java:** Game class 6 | **movies.txt:** Text database of movie titles 7 | 8 | **Project Summary** 9 | 10 | The goal of this project was to build a simple text game in which the player gets to guess a movie title given the number of letters in it (pretty much like hangman but with movies). The rules are simple, the computer randomly picks a movie title, and shows the player how many letters it's made up of. The goal is to try to figure out the movie by guessing one letter at a time. 11 | If a letter is indeed in the title the computer will reveal its correct position in the word, if not, the player loses a point. If the player loses 10 points, the game is over. 12 | -------------------------------------------------------------------------------- /tests.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ADNovo/OOPJava/7013ebde84cc9a810f8f28d98ee487b317b883f0/tests.zip --------------------------------------------------------------------------------