-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectCommandAction.cpp
More file actions
138 lines (109 loc) · 3.27 KB
/
Copy pathSelectCommandAction.cpp
File metadata and controls
138 lines (109 loc) · 3.27 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
#include "SelectCommandAction.h"
#include "ApplicationManager.h"
#include "Grid.h"
#include "Input.h"
#include "Output.h"
#include "GameState.h"
#include "Player.h"
#include <cstdlib>
#include <ctime>
// Constructor: Initializes the action with the ApplicationManager
SelectCommandAction::SelectCommandAction(ApplicationManager* pApp) : Action(pApp)
{
}
// Reads parameters: checks if player exists and has health
bool SelectCommandAction::ReadActionParameters()
{
Grid* pGrid = pManager->GetGrid();
GameState* pState = pManager->GetGameState();
Player* pPlayer = pState->GetCurrentPlayer();
if (pPlayer == NULL || pPlayer->GetHealth() <= 0)
{
pGrid->PrintErrorMessage("Error: No current player or health. Click to continue ...");
return false;
}
return true;
}
// Executes the action: allows player to select commands and saves them
void SelectCommandAction::Execute()
{
if (!ReadActionParameters())
{
return;
}
Grid* pGrid = pManager->GetGrid();
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
GameState* pState = pManager->GetGameState();
Player* pPlayer = pState->GetCurrentPlayer();
// Clear saved commands
pPlayer->ClearSavedCommands();
int maxCommandsToSelect = pPlayer->GetMaxCommands();
int savedCount = 0;
Command savedCommands[MaxSavedCommandsWithExtendedMemory];
// Initialize saved commands
for (int i = 0; i < maxCommandsToSelect; i++)
{
savedCommands[i] = NO_COMMAND;
}
Command availableCommands[MaxAvailableCommands];
int availableCommandsCount = pPlayer->GetAvailableCommandCount();
for (int i = 0; i < availableCommandsCount; i++)
{
availableCommands[i] = pPlayer->GetAvailableCommand(i);
}
// Display available commands
pOut->CreateCommandsBar(savedCommands, maxCommandsToSelect, availableCommands, availableCommandsCount);
// Allow player to select commands
int selectedIndices[MaxAvailableCommands];
int selectedIndicesCount = 0;
while (savedCount < maxCommandsToSelect)
{
pOut->PrintMessage("Select a command. Click outside to stop.");
int cmdIndex = pIn->GetSelectedCommandIndex();
// Stop if clicked outside the commands
if (cmdIndex == -1)
{
break;
}
bool foundMatch = false;
for (int i = 0; i < selectedIndicesCount; i++)
{
if (selectedIndices[i] == cmdIndex)
{
pGrid->PrintErrorMessage("Command already selected. Click to continue ...");
foundMatch = true;
break;
}
}
if (foundMatch) continue;
// Check for invalid selection
if (cmdIndex < 0 || cmdIndex >= availableCommandsCount)
{
pGrid->PrintErrorMessage("Invalid selection. Click to continue ...");
continue;
}
// Save selected command
Command selectedCmd = availableCommands[cmdIndex];
selectedIndices[selectedIndicesCount] = cmdIndex;
selectedIndicesCount++;
pPlayer->AddSavedCommand(selectedCmd);
savedCommands[savedCount] = selectedCmd;
savedCount++;
// Update displayed saved commands
pOut->CreateCommandsBar(savedCommands, maxCommandsToSelect, availableCommands, availableCommandsCount);
}
// Inform player about selected commands
if (savedCount == 0)
{
pGrid->PrintErrorMessage("No commands selected. Movement skipped.");
}
else
{
pGrid->PrintErrorMessage("Commands saved successfully.");
}
}
// Destructor: Cleans up the action
SelectCommandAction::~SelectCommandAction()
{
}