-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveGame.cpp
More file actions
92 lines (79 loc) · 2.18 KB
/
InteractiveGame.cpp
File metadata and controls
92 lines (79 loc) · 2.18 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
#include "InteractiveGame.h"
#include <iostream>
InteractiveGame::InteractiveGame(sf::RenderWindow* window)
{
dartboard1 = new DartBoard(300, 300, 200);
player1 = Player();
player2 = Player();
currentPlayer = &player1;
this->window = window;
dartboard1->ProvideWindow(window);
// Start game text
std::cout << std::endl << "New player vs player game" << std::endl;
std::cout << "Player 1's points: " << player1.GetPoints() << std::endl;
std::cout << "Player 2's points: " << player2.GetPoints() << std::endl << std::endl;
std::cout << "Player " << player + 1 << "'s turn" << std::endl;
while (window->isOpen())
{
Update();
if (gameOver)
return;
}
}
void InteractiveGame::Update()
{
sf::Event event;
while (window->pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window->close();
break;
case sf::Event::MouseButtonReleased:
if (event.mouseButton.button == sf::Mouse::Left) // If event is left mouse button
{
if (dartboard1->GetSegment() != Null) // If aiming at a board piece
{
// Throw at the board
currentPlayer->ThrowAtBoard(dartboard1->GetSectorPoints(), dartboard1->GetSegment());
std::cout << "Current points for player " << player + 1 << ": " << currentPlayer->GetPoints() << std::endl;
currentThrow++;
// If player won
if (currentPlayer->GetPoints() == 0)
{
// Won
std::cout << "Player " << player + 1 << " has won!" << std::endl;
gameOver = true;
return;
}
// If score is an impossible score
if (currentPlayer->GetPoints() <= 1)
{
currentPlayer->SetPoints(startOfThrowsScore);
std::cout << "Illegal amount of points" << std::endl;;
currentThrow = 3;
}
// Switch player
if (currentThrow == 3)
{
player = !player;
if (currentPlayer == &player1)
currentPlayer = &player2;
else
currentPlayer = &player1;
currentThrow = 0;
startOfThrowsScore = player1.GetPoints();
std::cout << std::endl << "Player " << player + 1 << "'s turn" << std::endl;
}
}
}
break;
}
}
// Clear window
window->clear();
dartboard1->UpdateDisplay();
// Display window
window->display();
}