-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprinter.h
More file actions
129 lines (110 loc) · 3.38 KB
/
Copy pathprinter.h
File metadata and controls
129 lines (110 loc) · 3.38 KB
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
// vim:filetype=cpp:textwidth=120:shiftwidth=2:softtabstop=2:expandtab
// Copyright 2017 Christoph Schwering
#ifndef EXAMPLES_SUDOKU_PRINTER_H_
#define EXAMPLES_SUDOKU_PRINTER_H_
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <limbo/io/output.h>
#include "game.h"
#include "kb.h"
typedef std::string Color;
class Colors {
public:
virtual ~Colors() {}
virtual std::string fill(int) const = 0;
virtual Color reset() const = 0;
virtual Color dim() const = 0;
virtual Color black() const = 0;
virtual Color red() const = 0;
virtual Color green() const = 0;
};
class TerminalColors : public Colors {
public:
std::string fill(int n) const override { return std::string(n, ' '); }
Color reset() const override { return s(0); }
Color dim() const override { return s(2); }
Color black() const override { return s(30); }
Color red() const override { return s(31); }
Color green() const override { return s(32); }
private:
static Color s(int c) {
std::stringstream ss;
ss << "\033[" << c << "m";
return ss.str();
}
};
class HtmlColors : public Colors {
public:
std::string fill(int n) const override { return std::string(n, '_'); }
Color reset() const override { return s("reset"); }
Color dim() const override { return s("dim"); }
Color black() const override { return s("black"); }
Color red() const override { return s("red"); }
Color green() const override { return s("green"); }
private:
static Color s(const std::string& cls) {
std::stringstream ss;
ss << "</span><span class='"+ cls +"'>";
return ss.str();
}
};
class Printer {
public:
typedef std::string Label;
Printer(const Colors* colors, std::ostream* os) : colors_(colors), os_(os) {}
virtual ~Printer() = default;
void Print(const Game& g) {
const std::string DASH = "\u2550";
const std::string PIPE = "\u2551";
const std::string CROSS = "\u256c";
*os_ << colors_->fill(3);
for (int x = 1; x <= 9; ++x) {
*os_ << colors_->dim() << colors_->dim() << colors_->fill(1) << x << colors_->fill(1) << colors_->reset();
if (x == 3 || x == 6) {
*os_ << colors_->dim() << colors_->fill(1) << colors_->reset();
}
}
*os_ << std::endl;
for (int y = 1; y <= 9; ++y) {
*os_ << colors_->dim() << colors_->fill(1) << y << colors_->fill(1) << colors_->reset();
for (int x = 1; x <= 9; ++x) {
Label l = label(g, Point(x, y));
*os_ << colors_->fill(1) << l << colors_->fill(1);
if (x == 3 || x == 6) {
*os_ << colors_->dim() << PIPE << colors_->reset();
}
}
*os_ << std::endl;
if (y == 3 || y == 6) {
*os_ << colors_->fill(1) << colors_->dim() << colors_->fill(2);
for (int x = 1; x <= 9; ++x) {
*os_ << DASH << DASH << DASH;
if (x == 3 || x == 6) {
*os_ << CROSS;
}
}
*os_ << colors_->reset() << std::endl;
}
}
}
virtual Label label(const Game&, Point) = 0;
protected:
const Colors* colors_;
std::ostream* os_;
};
class SimplePrinter : public Printer {
public:
using Printer::Printer;
Label label(const Game& g, const Point p) override {
if (g.get(p) > 0) {
std::stringstream ss;
ss << g.get(p);
return Label(ss.str());
} else {
return Label(" ");
}
}
};
#endif // EXAMPLES_SUDOKU_PRINTER_H_