summaryrefslogtreecommitdiffstats
path: root/labb5/src/boggleplay.cpp
blob: 21b3172fd42ec3553b118aa6674f7ec1fb6617cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// You will edit and turn in this CPP file.
// Also remove these comments here and add your own.
// TODO remove this comment header and replace with your own

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "Boggle.h"
#include "bogglemain.h"
#include "strlib.h"

#include <string>

void setup_board(Boggle& boggle) {
    bool input_custom_board = yesOrNo("Input custom board? ");
    if (input_custom_board) {
        string input_board;
        while (true) {
            cout << "Input your board (16 characters): ";
            getline(cin, input_board);
            if (input_board.length() == 16) {
                if (boggle.letters_from_string(input_board)) {
                    break;
                } else {
                    cout << "Please input a valid string" << endl;
                }
            } else {
                cout << "Please input 16 characters only" << endl;
            }
        }
    } else {
        boggle.shuffle();
    }
}

void print_user_status(const Boggle& boggle) {
    cout << boggle.board_to_string() << endl;
    cout << "Your words (" << boggle.get_user_words_size() << "): " << endl;
    cout << boggle.user_words_to_string() << endl;
    cout << "Your score: " << boggle.get_user_score() << endl;
}

void ask_debug(Boggle& boggle) {
    if (yesOrNo("Enable debug mode? ")) {
        boggle.debug_mode = true;
    }
}

/*
 * Plays one game of Boggle using the given boggle game state object.
 */
void playOneGame(Boggle& boggle) {
    boggle.clear();
    setup_board(boggle);
    ask_debug(boggle);
    clearConsole();

    cout << endl;  // this is later replaced by the response to the prev word
    if (boggle.debug_mode) cout << endl;  // debug mode means two lines
    string user_input;
    while (true) {
        cout << "It's your turn!" << endl;
        print_user_status(boggle);
        cout << "Type a word (or press Enter to end your turn) ";
        getline(cin, user_input);
        clearConsole();
        if (user_input == "") {
            break;
        }
        if (!boggle.find_single_word(user_input)) {
            cout << "Your word is invalid" << endl;
        } else if (!boggle.word_is_unplayed(user_input)) {
            cout << "Your word has already been played!" << endl;
        } else if (user_input.length() < 4) {
            cout << "Your word is too short!" << endl;
        } else {
            cout << "You found a new word!" << endl;
            boggle.add_user_word(user_input);
        }
    }
    print_user_status(boggle);
    cout << endl;

    cout << "It's my turn!" << endl;
    boggle.do_computer_turn();
    cout << "My words (" << boggle.get_computer_words_size() << "): " << endl;
    cout << boggle.computer_words_to_string() << endl;
    cout << "My score: " << boggle.get_computer_score() << endl;

    cout << endl;
    if (boggle.get_computer_score() > boggle.get_user_score()) {
        cout << "Ha ha ha, I destroyed you. Better luck next time, puny human!" << endl;
    } else {
        cout << "WOW, you defeated me! Congratulations!" << endl;
    }
}

/*
 * Erases all currently visible text from the output console.
 */
void clearConsole() {
#if defined(_WIN32) || defined(_WIN64)
    std::system("CLS");
#else
    // assume POSIX
    std::system("clear");
#endif
}