-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntenna.cpp
More file actions
127 lines (95 loc) · 4.06 KB
/
Copy pathAntenna.cpp
File metadata and controls
127 lines (95 loc) · 4.06 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
#include"Antenna.h"
#include "Input.h"
#include "Output.h"
#include "Player.h"
#include <cmath>
#include "Antenna.h"
#include"CellPosition.h"
Antenna::Antenna(const CellPosition & antennaPosition):GameObject(antennaPosition)
{
}
void Antenna::Draw(Output* pOut) const
{
pOut->DrawAntenna(position);
}
void Antenna::Apply(Grid* pGrid, Player* pPlayer)
{
///TODO: Implement this function as mentioned in the guideline steps (numbered below) below
// == Here are some guideline steps (numbered below) to implement this function ==
// 1- Print a message "the antenna will decide the turn of players. Click to continue ..." and wait mouse click
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
pOut->PrintMessage("The antenna will decide the turn of players. Click to continue ...");
int x, y;
pIn->GetPointClicked(x, y);
// 2- Apply the antenna effect by following these notes, check grid class and decide which function to use
//The antenna determines the turn order for players.
//The distance of each player from the antenna determines his turn order in each rount.
//Player with the minimum distance[ from the antenna will have the first turn in that round.
//If there is a tie, player number will break it, for example if player 1 & 2 are in the same cell and they have the same distance from the antenna, player 1 will play first in that round.
// 3- After deciding the turn of player Print a message indicating which player will play first example: "Player 1 will play first"
// Step 1: Identify the current and other players using GetCurrentPlayer
Player* currentPlayer = pPlayer;
pGrid->AdvanceCurrentPlayer(); // Move to the next player
Player* otherPlayer = pGrid->GetCurrentPlayer();
pGrid->AdvanceCurrentPlayer(); // Reset to the original player
// Step 2: Get player positions
CellPosition player1Pos = currentPlayer->GetCell()->GetCellPosition();
CellPosition player2Pos = otherPlayer->GetCell()->GetCellPosition();
int v1 = position.VCell(); // Antenna's vertical position
int h1 = position.HCell(); // Antenna's horizontal position
int v2_1 = player1Pos.VCell(); // Current player's vertical position
int h2_1 = player1Pos.HCell(); // Current player's horizontal position
int v2_2 = player2Pos.VCell(); // Other player's vertical position
int h2_2 = player2Pos.HCell(); // Other player's horizontal position
// Step 3: Calculate distances
double distance1 = sqrt(pow(v1 - v2_1, 2) + pow(h1 - h2_1, 2));
double distance2 = sqrt(pow(v1 - v2_2, 2) + pow(h1 - h2_2, 2));
// Step 4: Determine the turn order
int firstPlayer;
if (distance1 < distance2) {
if (currentPlayer == pPlayer) {
firstPlayer = 0; // Current player is closer
}
else {
firstPlayer = 1; // Other player is closer
}
}
else if (distance1 > distance2) {
if (currentPlayer == pPlayer) {
firstPlayer = 1; // Other player is closer
}
else {
firstPlayer = 0; // Current player is closer
}
}
else if (distance1 == distance2) { // Distances are equal
if (currentPlayer == pPlayer) {
firstPlayer = 0; // Current player wins the tie
}
else {
firstPlayer = 1; // Other player wins the tie
}
}
pGrid->setCurrentplayer(firstPlayer);
// Step 5: Display the result
pOut->PrintMessage("Player " + to_string(firstPlayer + 1) + " will play first.");
pGrid->UpdateInterface();
}
void Antenna::Save(ofstream& OutFile, string file)
{
if (!OutFile.is_open())
OutFile.open(file, ios::out);
OutFile << position.GetCellNum() << endl;
}
void Antenna::Load(ifstream& Infile, string file)
{
if (!Infile.is_open())
Infile.open(file, ios::in);
int cellNum;
Infile >> cellNum;
position = CellPosition::GetCellPositionFromNum(cellNum);
}
Antenna::~Antenna()
{
}