-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovementPhase.cpp
More file actions
182 lines (126 loc) · 4.91 KB
/
Copy pathMovementPhase.cpp
File metadata and controls
182 lines (126 loc) · 4.91 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
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
176
177
178
179
180
181
182
#include "MovementPhase.h"
#include <random>
#include "Input.h"
#include <algorithm>
MovementPhase::MovementPhase(Grid * pGrid)
:currentPlayer(pGrid->GetCurrentPlayer())
{
this->pGrid = pGrid;
pOut = pGrid->GetOutput();
player0 = pGrid->GetPlayer(0);
player1 = pGrid->GetPlayer(1);
}
void MovementPhase::displayRandomCommands(Player * currentPlayer)
{
////random number generator
std::random_device engine;
std::uniform_int_distribution<int> distrib(1, 8);
///
int comsize = min(5,(currentPlayer->GetHealth()));
pOut->PrintMessage(std::to_string((currentPlayer->GetHealth())));
int x,y;
Input * pIn;
pIn = pOut->CreateInput();
pIn->GetPointClicked(x,y);
Command* savedCommands = new Command[comsize];
//Command savedCommands[5];
for (int i = 0; i < comsize; i++)
{
savedCommands[i] = NO_COMMAND;
}
Command availableCommands[10];
for (int i = 0; i < 10; i++)
{
//
availableCommands[i] = Command(distrib(engine));
}
pOut->CreateCommandsBar(savedCommands, comsize, availableCommands, 10);
// delete[] savedCommands;
// savedCommands = NULL;
/////
}
void MovementPhase::selectCommands(Player* currentPlayer)
{
// Determine the size of commands to be selected (at most 5 commands, or player's health)
int comsize = min(5, currentPlayer->GetHealth());
// Initialize savedCommands to NO_COMMAND
Command* savedCommands = new Command[comsize];
for (int i = 0; i < comsize; i++) {
savedCommands[i] = NO_COMMAND;
}
// Generate 10 random commands using random_device and uniform_int_distribution
int availableSize = 10;
Command* availableCommands = new Command[availableSize];
std::random_device engine;
std::uniform_int_distribution<int> distrib(1, 8); // Assuming 1 to 8 maps to Command enums
// Fill availableCommands with random commands
for (int i = 0; i < availableSize; i++) {
availableCommands[i] = static_cast<Command>(distrib(engine));
}
// Input for selection
Input* pIn = pOut->CreateInput();
int selectedIndex = 0;
// Allow the player to select 'comsize' number of commands
for (int i = 0; i < comsize; i++) {
// Display the available commands
pOut->CreateCommandsBar(savedCommands, comsize, availableCommands, availableSize);
// Prompt the player for a selection
pOut->PrintMessage("Select a command: ");
selectedIndex = pIn->GetSelectedCommandIndex();
// Ensure valid selection
if (selectedIndex >= 0 && selectedIndex < availableSize) {
// Save the selected command
savedCommands[i] = availableCommands[selectedIndex];
// Remove the selected command from availableCommands
// Shift the remaining commands to the left
for (int j = selectedIndex; j < availableSize - 1; j++) {
availableCommands[j] = availableCommands[j + 1];
}
// Set the last element in the availableCommands to NO_COMMAND (so that it's not reused)
availableCommands[availableSize - 1] = NO_COMMAND;
// Decrease the size of availableCommands
availableSize--;
} else {
// Invalid selection, retry for this slot
pOut->PrintMessage("Invalid selection. Try again.");
i--; // Retry the same command slot
}
pOut->CreateCommandsBar(savedCommands, comsize, availableCommands, availableSize);
}
//////////explicitly set the last selected ccommand "if removed ui becomes crazy":
// Final display of saved commands for debugging
std::string debugMessage = "Saved Commands: ";
for (int i = 0; i < comsize; i++) {
debugMessage += std::to_string(savedCommands[i]) + " ";
}
pOut->PrintMessage(debugMessage);
////////////////////////////////////////////////////
//savedMoves[i] = savedCommands[i];
////////////////////////////////////////////////////////////////////////
//////////////Execution at instant, نص العمى ولا العمى كله
////////////////////////////////////////////////////////////////////////
currentPlayer->Move(pGrid,savedCommands);
// dead function :(
delete[] savedCommands;
delete[] availableCommands;
}
void MovementPhase::execute(Player* currentPlayer)
{
if (!(currentPlayer->CanMove()))
{
pGrid->PrintErrorMessage("This player cant move this round");
return;
}
Input * pIn;
pIn = pOut->CreateInput();
int x,y;
////display random commands:
//displayRandomCommands(currentPlayer);
pIn->GetPointClicked(x,y);
////select commands:
selectCommands(currentPlayer);
////---/////logic will be implemented later.
////execute commands:
/////clear commands bar:
// pOut->ClearCommandsBar();
}