-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddSnake.cpp
More file actions
88 lines (67 loc) · 2.71 KB
/
AddSnake.cpp
File metadata and controls
88 lines (67 loc) · 2.71 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
#include "AddSnake.h"
#include "Input.h"
#include "Output.h"
#include "Snake.h"
AddSnake::AddSnake(ApplicationManager* pApp) : Action(pApp)
{
// Initializes the pManager pointer of Action with the passed pointer
}
AddSnake::~AddSnake()
{
}
void AddSnake::ReadActionParameters()
{
// Get a Pointer to the Input / Output Interfaces
Grid* pGrid = pManager->GetGrid();
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
// Read the startPos parameter
pOut->PrintMessage("New Snake: Click on its Start Cell ...");
startPos = pIn->GetCellClicked();
// Read the endPos parameter
pOut->PrintMessage("New Snake: Click on its End Cell ...");
endPos = pIn->GetCellClicked();
Snake snake(startPos, endPos);
///TODO: Make the needed validations on the read parameters
if (endPos.GetCellNum() == -1 || startPos.GetCellNum() == -1)
{
pGrid->PrintErrorMessage("cancel operation:invalid cell position,click any where to continue");
}
else if (startPos.GetCellNum() == 99 || endPos.GetCellNum() == 1) {
pGrid->PrintErrorMessage("cancel operation:start cell cannot be last cell and the end cell cannot be the first cell,click any where to continue");
}
else if (!snake.validsnake())
{
pGrid->PrintErrorMessage("cancel operation:end cell can not be larger than start cell,click any where to continue");
}
else if (!snake.isvertical())
{
pGrid->PrintErrorMessage("cancel operation:snake must be vertiacl,click any where to continue");
}
else if (pGrid->isoverlapping(&snake)) {
pGrid->PrintErrorMessage("cancel operation : two overlapping gameobjects,click any where to continue");
}
// Clear messages
pOut->ClearStatusBar();
}
// Execute the action
void AddSnake::Execute()
{
// The first line of any Action Execution is to read its parameter first
// and hence initializes its data members
ReadActionParameters();
// Create a card object with the parameters read from the user
Snake* psnake = new Snake(startPos, endPos);
Grid* pGrid = pManager->GetGrid(); // We get a pointer to the Grid from the ApplicationManager
// Add the snake object to the GameObject of its Cell:
if (!(startPos.IsValidCell() && endPos.IsValidCell() && psnake->validsnake() && psnake->isvertical() && !pGrid->isoverlapping(psnake)&& startPos.GetCellNum() != 99 && endPos.GetCellNum() != 1))
return;
bool added = pGrid->AddObjectToCell(psnake);
// if the GameObject cannot be added
if (!added)
{
// Print an appropriate message
pGrid->PrintErrorMessage("Error: Cell already has an object ! Click to continue ...");
}
// Here, the snake is created and added to the GameObject of its Cell, so we finished executing the addsnakeaction
}