This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.cpp
127 lines (111 loc) · 3.25 KB
/
gui.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
#include "gui.h"
#include <deque>
using namespace std;
deque<Pt> opMovesBuffer;
void commitOpsMove(DrawingBoard &me)
{
bool shotMade = false;
bool success;
while (!shotMade)
for (int i = 0; i < BoardSize; ++i)
for (int j = 0; j < BoardSize; ++j)
{
int cellState = me.getCellState(Pt(i, j));
if (cellState == CellState::wounded)
{
for (int k = 0; k < 4; ++k)
{
Pt shot = Pt(i + DX[k], j + DY[k]);
if (me.getCellState(shot) == CellState::alive || me.getCellState(shot) == CellState::gratis)
shotMade = me.makeShot(shot, success);
}
}
}
if (shotMade)
return;
while (!shotMade)
{
Pt shot = me.getFreeRandPoint();
shotMade = me.makeShot(shot, success);
}
}
void init(DrawingBoard &me, DrawingBoard &op)
{
sf::Vector2f position(0.05 * WindowWidth / 2.0, 0.05 * WindowHeight / 2.0);
sf::Vector2u size(int(WindowWidth / 2.0 * 0.9), int(WindowHeight / 2.0 * 0.9));
me.fillRandom();
me.setIsOpen(true);
me.setPosition(position);
me.setSize(size);
position.x += float(WindowWidth / 2.0);
op.fillRandom();
op.setIsOpen(false);
op.setPosition(position);
op.setSize(size);
}
void closeGame(sf::RenderWindow &window, DrawingBoard &me, DrawingBoard &op)
{
//TODO are you sure you want to exit game?
window.close();
}
GameResult getGameResult(DrawingBoard &me, DrawingBoard &op)
{
if (me.getShipsLeft() != 0 && op.getShipsLeft() != 0)
return GameResult::none;
if (me.getShipsLeft() != 0)
return GameResult::meWin;
if (op.getShipsLeft() != 0)
return GameResult::opWin;
return GameResult::draw;
}
void drawGameState(sf::RenderWindow &window, DrawingBoard &me, DrawingBoard &op)
{
window.clear();
drawBackGround(window);
window.draw(me);
window.draw(op);
window.display();
sf::sleep(PauseDuration);
}
void drawGameOverState(sf::RenderWindow &window, GameResult gameResult)
{
window.clear();
showMessage(window, getStringGameResult(gameResult));
window.display();
}
void showMessage(sf::RenderTarget &target, const std::string msg)
{
sf::Font font;
if (!font.loadFromFile("font.ttf"))
throw "font is not loaded";
sf::Text text;
text.setString(msg);
text.setFont(font);
text.setCharacterSize(60);
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width / 2, textRect.top + textRect.height / 2);
text.setPosition(WindowWidth / 2, WindowHeight / 2);
target.draw(text);
}
std::string getStringGameResult(const GameResult &gameResult)
{
//enum GameResult { draw, opWin, meWin, none };
switch (gameResult)
{
case none:
return "none";
case opWin:
return "opWin";
case meWin:
return "meWin";
case draw:
return "draw";
}
return "none";
}
bool isLeftMouseButtonPressed(sf::Event event)
{
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Button::Left)
return true;
return false;
}