-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.h
124 lines (107 loc) · 4.18 KB
/
io.h
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
#ifndef _IO_H
#define _IO_H
#include "board.h"
#include "move.h"
#include "window.h"
#include "difficultylevel.h"
#include <iostream>
#include <memory>
class Input;
class Output;
class TextInput;
/**
* IO is a meta-object, to hold the Outputs [observers] and
* the singular Input [subject], that follow the Observer pattern
*/
class IO {
// [Observer Pattern] the subject:
std::unique_ptr<Input> input;
// [Observer Pattern] the observers:
std::vector<std::unique_ptr<Output>> outputs;
std::ostream& out;
bool basicPieces = true; // Whether pieces are drawn with text [eg. K] or ascii pieces [eg. ♔]
bool showCheckers = false; // Whether to differentiate the dark and light squares
bool boardPerspective = false; // Whether to print the board from black's perspective when appropriate
bool wideBoard = false; // Whether to print the board with wide style
// Helper functions for the main `fullDisplay()`:
void initialize(bool setup, std::string white, std::string black, int game);
void display(Board& board, GameState state, bool setup = false, bool firstSetup = false);
public:
IO(std::istream& in, std::ostream& out);
void makeTextOutput(std::ostream& out);
void makeGraphicOutput(int size);
/**
* Closes the most recently opened graphical output.
* This is fully functional; however, it is impractical to allow our user to create
* multiple graphical displays at once, so the input handler implements a cap-at-one system.
*/
void closeGraphicOutput();
/** For the input handler's cap-at-one system. */
bool hasGraphicsOpen();
// [Observer Pattern] notifyObservers():
void fullDisplay(Board& board, GameState state, int game, std::pair<int, int> players, bool setup = false, bool firstSetup = false);
void toggleSetting(int setting);
bool getSetting(int setting);
/**
* Get this whole big baby ROLLIN'!!!
*/
void runProgram();
};
// [Observer Pattern] Observer
class Output {
protected:
Input* toFollow;
public:
Output(Input* toFollow): toFollow{toFollow} {};
virtual void initialize(bool setup, std::string white, std::string black, int game) = 0;
// [Observer Pattern] notify():
virtual void display(Board& board, std::array<bool, 4> settings, GameState state, bool setup, bool firstSetup) = 0;
virtual ~Output() = default;
};
// [Observer Pattern] Concrete Observer #1
class TextOutput: public Output {
std::ostream& out;
public:
TextOutput(Input* toFollow, std::ostream& out);
void initialize(bool setup, std::string white, std::string black, int game) override {};
// [Observer Pattern] notify();
void display(Board& board, std::array<bool, 4> settings, GameState state, bool setup, bool firstSetup) override;
};
// [Observer Pattern] Concrete Observer #2
class GraphicalOutput: public Output {
std::unique_ptr<Xwindow> window;
int size;
public:
GraphicalOutput(Input* toFollow, int size);
void initialize(bool setup, std::string white, std::string black, int game) override;
void display(Board& board, std::array<bool, 4> settings, GameState state, bool setup, bool firstSetup) override;
};
// [Observer Pattern] Subject
class Input {
protected:
std::vector<Output*> outputs;
public:
// [Observer Pattern] attach(), detach(), and notifyObservers():
virtual void attach(Output* output) = 0;
virtual void detach(Output* output) = 0;
virtual void notifyOutputs(Board& board, std::array<bool, 4> settings, GameState state, bool setup, bool firstSetup) = 0;
/**
* Get this whole big baby ROLLIN'!!!
*/
virtual void runProgram(IO& io, std::ostream& out) = 0;
};
// [Observer Pattern] Concrete Subject #1
class TextInput: public Input {
std::istream& in;
public:
TextInput(std::istream& in): in{in} {};
// [Observer Pattern] attach(), detach(), and notifyObservers():
void attach(Output* output) override;
void detach(Output* output) override;
void notifyOutputs(Board& board, std::array<bool, 4> settings, GameState state, bool setup, bool firstSetup) override;
/**
* Get this whole big baby ROLLIN'!!!
*/
void runProgram(IO& io, std::ostream& out) override;
};
#endif