This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geodesic_y_sfml.cpp
175 lines (147 loc) · 4.46 KB
/
geodesic_y_sfml.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <stdint.h>
#include <math.h>
#include "board.h"
#include "geodesic_y.h"
const int winw = 800;
const int winh = 800;
const int scale = 720;
sf::Color black(0, 0, 0);
sf::Color grey(128, 128, 128);
sf::Color red(200, 0, 0);
sf::Color blue(0, 0, 200);
sf::Color c = grey;
float radius = 24.0f;
struct vertex {
float X;
float Y;
};
/*
std::vector<vertex> geodesic_cords(int n) {
if (n < 2) {
throw "bruh";
}
float l = 1.0;
float x1, y1;
std::vector<uint16_t> top;
std::vector<uint16_t> right;
for (int i = 1; i < n; i++) {
x1 = l * cos(11 * M_PI / 12);
y1 = l * sin(11 * M_PI / 12);
for (int d = 0; d < 3; d++) {
}
}
}
*/
geodesic_y::State state(geodesic_y::n, 1);
std::vector<vertex> vertices(geodesic_y::n);
std::vector<sf::Vertex> edges;
float convertX(float x) {
x -= 0.5f;
x *= scale;
x += winw / 2;
return x;
}
float convertY(float y) {
y = 0.5f - y;
y *= scale;
y += winh / 2;
return y;
}
void load_cords() {
for (size_t i = 0; i < vertices.size(); i++) {
vertices[i].X = convertX(cords[i][0]);
vertices[i].Y = convertY(cords[i][1]);
// .at() for saftey.
}
}
sf::CircleShape circle(float x, float y, sf::Color color) {
sf::CircleShape circle(radius, 32);
circle.setOrigin(radius, radius);
circle.setPosition(x, y);
circle.setFillColor(color);
return circle;
}
void draw(sf::RenderWindow* window) {
window->clear();
window->draw(edges.data(), edges.size(), sf::Lines);
for (size_t v = 0; v < vertices.size(); v++) {
switch (state.board_[v].player) {
case 1:
window->draw(circle(vertices[v].X, vertices[v].Y, blue));
break;
case -1:
window->draw(circle(vertices[v].X, vertices[v].Y, red));
break;
}
}
window->display();
}
void run() {
//geodesic_y::State state(geodesic_y::n, 1);
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(winw, winh), "Geodesic Y", sf::Style::Default, settings);
load_cords();
for (size_t i = 0; i < geodesic_y::graph.size(); i++) {
for (size_t j = 0; j < geodesic_y::graph[i].size(); j++) {
if (i < geodesic_y::graph[i][j]) {
sf::Vertex vert1;
sf::Vertex vert2;
vert1.position = {vertices[i].X, vertices[i].Y};
vert2.position = {vertices[geodesic_y::graph[i][j]].X, vertices[geodesic_y::graph[i][j]].Y};
vert1.color = c;
vert2.color = c;
edges.push_back(vert1);
edges.push_back(vert2);
}
}
}
bool refreshed = false;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf:: Mouse::Left) {
int mx = event.mouseButton.x;
int my = event.mouseButton.y;
float cmin = 1000;
int cv = -1;
for (size_t v = 0; v < vertices.size(); v++) {
float distance = sqrt(pow(vertices[v].X - mx, 2) + pow(vertices[v].Y - my, 2));
if (distance < radius && distance < cmin) {
cmin = distance;
cv = v;
}
}
if (cv > -1) {
if (state.board_[cv].player == 0) {
state.Move(cv);
if (state.CheckWin() != 0) {
print(int(state.win));
print("won");
draw(&window);
std::cin.get();
return; // wtf
}
//refreshed = false;
}
}
}
}
}
if (!refreshed) {
draw(&window);
//refreshed = true;
}
}
}
int main() {
run();
}