aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/output/state_printer.cpp
blob: bfb6c7dde0750ab9dec9ebe8f09abb095465dac1 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Created by thecharlesblake on 1/3/18.
//

#include "state_printer.h"

using namespace std;

typedef sol_rules::stock_deal_type sdt;

ostream& state_printer::print(ostream& stream, const game_state& gs) {
    if (gs.rules.foundations) {
        state_printer::print_header(stream, "Foundations");
        state_printer::print_top_of_piles(stream, gs.foundations, gs);
    }
    if (gs.rules.cells) {
        state_printer::print_header(stream, "Cells");
        state_printer::print_piles(stream, gs.cells, gs);
    }
    if (gs.rules.tableau_pile_count > 0) {
        state_printer::print_header(stream, "Tableau Piles");
        state_printer::print_piles(stream, gs.tableau_piles, gs);
    }
    if (gs.rules.reserve_size > 0) {
        if (gs.rules.reserve_stacked) {
            state_printer::print_header(stream, "Reserve (Stacked)");
        } else {
            state_printer::print_header(stream, "Reserve");
        }
        state_printer::print_piles(stream, gs.reserve, gs);
    }
    if (gs.rules.stock_size > 0) {
        if (gs.rules.stock_deal_t == sdt::WASTE) {
            state_printer::print_header(stream, "Stock | Waste");
            state_printer::print_piles(stream, {gs.stock, gs.waste}, gs);
        } else {
            state_printer::print_header(stream, "Stock");
            state_printer::print_piles(stream, {gs.stock}, gs);
        }
    }
    if (gs.rules.hole) {
        state_printer::print_header(stream, "Hole Card");
        state_printer::print_top_of_pile(stream, gs.hole, gs);
    }
    return stream << "===================================";
}

void state_printer::print_header(ostream& stream, const char* header) {
    stream << "--- " << header << " ";
    size_t pad = 20 - strlen(header);
    for (size_t i = 0; i < pad; i++) {
        stream << '-';
    }
    stream << "\n";
}

void state_printer::print_piles(ostream& stream,
                                const vector<game_state::pile_ref>& pile_rs,
                                const game_state& gs) {
    bool empty_row = false;
    pile::size_type row_idx = 0;

    // Loops through the rows in each pile, starting from the bottom, until
    // one is empty
    while (!empty_row) {
        // Loops through the current row to determine if it is empty
        empty_row = true;
        for (const game_state::pile_ref pile_r : pile_rs) {
            if (gs.piles[pile_r].size() > row_idx) {
                empty_row = false;
                break;
            }
        }
        if (!empty_row || row_idx == 0) {
            // Loops through the current (non-empty) row, and outputs the values
            for (const game_state::pile_ref pile_r : pile_rs) {
                const pile& p = gs.piles[pile_r];
                if (p.size() > row_idx) {
                    print_card(stream, p[p.size() - 1 - row_idx]);
                } else if (row_idx == 0) {
                    stream << "[]";
                }
                stream << "\t";
            }

            stream << "\n";
            row_idx++;
        }
    }
}

void state_printer::print_top_of_piles(ostream& stream,
                                       const vector<game_state::pile_ref>& vp,
                                       const game_state& gs) {
    vector<game_state::pile_ref> top(vp);

    for (game_state::pile_ref p : top) {
        if (gs.piles[p].empty()) {
            stream << "[]";
        } else {
            print_card(stream, gs.piles[p].top_card());
        }
        stream << "\t";
    }
    stream << "\n";
}

void state_printer::print_top_of_pile(ostream& stream,
                                      const game_state::pile_ref pile_r,
                                      const game_state& gs) {
    print_top_of_piles(stream, {pile_r}, gs);
}

void state_printer::print_card(std::ostream& s, const card c) {
    switch(c.get_rank()) {
        case 1 : s << "A"; break;
        case 11: s << "J"; break;
        case 12: s << "Q"; break;
        case 13: s << "K"; break;
        default: s << int(c.get_rank());
    }
    switch(c.get_suit()) {
        case card::suit_t::Spades  : s << "S"; break;
        case card::suit_t::Hearts  : s << "H"; break;
        case card::suit_t::Clubs   : s << "C"; break;
        case card::suit_t::Diamonds: s << "D"; break;
    }
}