City Game

Remake of python game on old blog. Jupyter notebook has no image display and therefore image file name is displayed instead. Image functionality is done on local file though. Practices transition from python to java, combines concepts from rest of units to demonstrate understanding and practice java.

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
import java.awt.GridLayout;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.util.*;

class City {
    char firstLetter;
    String state;
    String county;
    String name;
    int population;
    String filename;
    public City (String filename){
        String[] information = filename.split("_");
        this.firstLetter = information[0].charAt(0);
        this.state = information[2];
        this.county = information[1];
        this.name = information[0];
        String pop = information[3];
        this.population = Integer.parseInt(pop.substring(0, pop.length() - 4));
        this.filename = filename;
    }

    public char getFirstLetter() {
        return firstLetter;
    }
    public String getState() {
        return state;
    }
    public String getCounty() {
        return county;
    }
    public String getName() {
        return name;
    }
    public int getPopulation() {
        return population;
    }
    public String getFilename() {
        return filename;
    }
    public void displayImage(String filename) {
        Main.showImage("./Cities/" + filename);
    }
}

public class Main {
    public static void main(String[] args) {
        int end = 0;
        int finaled = 0;
        float score = 0;
        float score_to_add;
        Scanner scanner = new Scanner(System.in);
        Random rand = new Random();

        File folder = new File("./Cities");
        File[] listOfFiles = folder.listFiles();
        City city;
        City[] cities = new City[listOfFiles.length];

        for(int i = 0;i<listOfFiles.length;i++) {
            city = new City(listOfFiles[i].getName());
            cities[i] = city;
        }

        List<Integer> done_cities = new ArrayList<Integer>();
        boolean done_yet;
        boolean is_same;
        int cities_done = 0;
        while (end == 0) {
            score_to_add = 1;
            finaled = 0;
            System.out.println("----------------------------------------");
            System.out.println("Here is your city!");
            done_yet = true;
            int city_to_add = 0;
            int randInt;
            while (done_yet == true) {
                is_same = false;
                randInt = rand.nextInt(cities.length);
                for(int i=0;i<done_cities.size();i++) {
                    if (randInt == done_cities.get(i)) {
                        is_same = true;
                    }
                }
                if (is_same == false) {
                    done_yet = false;
                }
                if (done_yet == false) {
                    city_to_add = randInt;
                }
            }
            City current_city = cities[city_to_add];
            cities_done += 1;
            done_cities.add(city_to_add);
            current_city.displayImage(current_city.getFilename());
            System.out.println("Cities done: " + cities_done);
            System.out.print("Enter Answer: ");
            String response = scanner.nextLine();
            while (finaled == 0) {
                if (response.equals("s")) {
                    if (score_to_add > 0) {
                        System.out.println("State: " + current_city.getState());
                        score_to_add -= 0.5;
                        System.out.print("Enter Answer: ");
                        response = scanner.nextLine();
                    } else {
                        System.out.println("Two hints have already been used, sorry!");
                        System.out.print("Enter Answer: ");
                        response = scanner.nextLine();
                    }
                } else if (response.equals("c")) {
                    if (score_to_add > 0) {
                        System.out.println("County: " + current_city.getCounty());
                        score_to_add -= 0.5;
                        System.out.print("Enter Answer: ");
                        response = scanner.nextLine();
                    } else {
                        System.out.println("Two hints have already been used, sorry!");
                    }
                } else if (response.equals("f")) {
                    if (score_to_add > 0) {
                        System.out.println("First Letter: " + current_city.getFirstLetter());
                        score_to_add -= 0.5;
                        System.out.print("Enter Answer: ");
                        response = scanner.nextLine();
                    } else {
                        System.out.println("Two hints have already been used, sorry!");
                    }
                } else if (response.equals("e")) {
                    System.out.println("Your final score was: " + score);
                    finaled = 1;
                    end = 1;
                } else {
                    finaled = 1;
                    if (response.equals(current_city.getName())) {
                        score += score_to_add;
                        System.out.println("Good job! The city was " + current_city.getName() + ", " + current_city.getCounty() + ", " + current_city.getState() + " (" + current_city.getPopulation() + ")");
                        System.out.println("Score: " + score);
                    } else {
                        score = 0;
                        System.out.println("I'm sorry, The city was " + current_city.getName() + ", " + current_city.getCounty() + ", " + current_city.getState() + " (" + current_city.getPopulation() + ")");
                        System.out.println("Score: " + score);
                    }
                }
            }
        }

    }
    static void showImage(final String fileName)
    {
        System.out.println(fileName);
    }
}

Main.main(null)
----------------------------------------
Here is your city!
./Cities/Temple City_Los Angeles County_CA_36494.jpg
Cities done: 1
Enter Answer: Good job! The city was Temple City, Los Angeles County, CA (36494)
Score: 1.0
----------------------------------------
Here is your city!
./Cities/Anaheim_Orange County_CA_346824.jpg
Cities done: 2
Enter Answer: First Letter: A
Enter Answer: Good job! The city was Anaheim, Orange County, CA (346824)
Score: 1.5
----------------------------------------
Here is your city!
./Cities/Santee_San Diego County_CA_60037.jpg
Cities done: 3
Enter Answer: I'm sorry, The city was Santee, San Diego County, CA (60037)
Score: 0.0
----------------------------------------
Here is your city!
./Cities/Orange_Orange County_CA_139911.jpg
Cities done: 4
Enter Answer: I'm sorry, The city was Orange, Orange County, CA (139911)
Score: 0.0
----------------------------------------
Here is your city!
./Cities/North Tustin_Orange County_CA_25718.jpg
Cities done: 5
Enter Answer: I'm sorry, The city was North Tustin, Orange County, CA (25718)
Score: 0.0
----------------------------------------
Here is your city!
./Cities/Hesperia_San Bernardino County_CA_99818.jpg
Cities done: 6
Enter Answer: Your final score was: 0.0