Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

117
LINES

< > BotCompany Repo | #1002516 // Java text game / QuestionMain

Java source code

// CSE 143 Homework 6: 20 Questions
//
// To use the jGRASP debugger with this program, set a breakpoint
// and once the execution breaks, open 'this' or 'tq' on the left,
// then look at its variable 'tree'.  That's your QuestionTree.
// Drag your 'tree' over to the right to see a visualization of it.
// 
// (Your QuestionTree is constructed by this file on line 30.
// The overall loop to play games is around line 68.)

import java.io.*;
import java.util.Scanner;

/** A basic text user interface for the 20 questions game. */
public class QuestionMain implements UserInterface {
    public static void main(String[] args) {
        QuestionMain tq = new QuestionMain();
        tq.run();
    }
    
    
    // fields
    private Scanner console;
    private QuestionTree tree;
    
    /** Constructs a text user interface and its question tree. */
    public QuestionMain() {
        console = new Scanner(System.in);
        tree = new QuestionTree(this);
    }
    
    /**
     * Returns the user's response as a String.
     */
    public String nextLine() {
        return console.nextLine();
    }

    /** Prints the given string to the console. */
    public void print(String message) {
        System.out.print(message);
        System.out.print(" ");
    }

    /** Prints the given string to the console. */
    public void println(String message) {
        System.out.println(message);
    }

    /** Prints a blank line to the console. */
    public void println() {
        System.out.println();
    }

    /**
     * Waits for the user to answer a yes/no question on the console and returns the
     * user's response as a boolean (true for anything that starts with "y" or "Y").
     */
    public boolean nextBoolean() {
        String answer = console.nextLine();
        return answer.trim().toLowerCase().startsWith("y");
    }
    
    // private helper for overall game(s) loop
    private void run() {
        println("Welcome to the game of 20 Questions!");
        load();
        
        // "Think of an item, and I will guess it in N tries."
        println("\n" + BANNER_MESSAGE);
            
        do {
            // play one complete game
            println();      // blank line between games
            tree.play();
            print(PLAY_AGAIN_MESSAGE);
        } while (nextBoolean());   // prompt to play again
        
        // print overall stats
        // Games played: N ...  I have won: M
        println("\n" + String.format(STATUS_MESSAGE,
                tree.totalGames(), tree.gamesWon()));

        save();
    }
    
    // common code for asking the user whether they want to save or load
    private void load() {
        print(LOAD_MESSAGE);
        if (nextBoolean()) {
            print(SAVE_LOAD_FILENAME_MESSAGE);
            String filename = nextLine();
            try {
                Scanner in = new Scanner(new File(filename));
                tree.load(in);
            } catch (FileNotFoundException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
    
    // common code for asking the user whether they want to save or load
    private void save() {
        print(SAVE_MESSAGE);
        if (nextBoolean()) {
            print(SAVE_LOAD_FILENAME_MESSAGE);
            String filename = nextLine();
            try {
                PrintStream out = new PrintStream(new File(filename));
                tree.save(out);
                out.close();
            } catch (FileNotFoundException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
}

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002516
Snippet name: Java text game / QuestionMain
Eternal ID of this version: #1002516/1
Text MD5: 676f602d8e74013e667716e92bdb335a
Author: stefan
Category:
Type: Java source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-29 18:34:07
Source code size: 3742 bytes / 117 lines
Pitched / IR pitched: No / No
Views / Downloads: 789 / 157
Referenced in: #1002517 - Java text game / QuestionTree
#3000189 - Answer for stefanreich(>> t bla)
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)