-
Notifications
You must be signed in to change notification settings - Fork 1
/
SaveGrid.cpp
68 lines (54 loc) · 1.62 KB
/
SaveGrid.cpp
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
#include "SaveGrid.h"
#include<fstream>
SaveGrid::SaveGrid(ApplicationManager* pApp) : Action(pApp)
{
// Initializes the pManager pointer of Action with the passed pointer
pManager = pApp;
}
SaveGrid::~SaveGrid()
{
}
void SaveGrid::ReadActionParameters()
{
// Get a Pointer to the Input / Output Interfaces
Grid* pGrid = pManager->GetGrid();
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
// Read the GameObjCell parameter
pOut->PrintMessage("Saving the grid: Please enter the filename...");
Filename = pIn->GetSrting(pOut);
// Clear messages
pOut->ClearStatusBar();
}
// Execute the action
void SaveGrid::Execute()
{
// The first line of any Action Execution is to read its parameter first
// and hence initializes its data members
ReadActionParameters();
Grid* pGrid = pManager->GetGrid(); // We get a pointer to the Grid from the ApplicationManager
int Ladders, Snakes, CoinSet, Cards;
ofstream OutFile(Filename,ios:: trunc);
if (OutFile.is_open())
{
Ladders = pGrid->HowManyGameObj(1);
OutFile << Ladders << endl;
pGrid->SaveAll(OutFile, pGrid, 1);
Snakes = pGrid->HowManyGameObj(2);
OutFile << Snakes << endl;
pGrid->SaveAll(OutFile, pGrid, 2);
CoinSet = pGrid->HowManyGameObj(3);
OutFile << CoinSet << endl;
pGrid->SaveAll(OutFile, pGrid, 3);
Cards = pGrid->HowManyGameObj(4);
OutFile << Cards << endl;
pGrid->SaveAll(OutFile, pGrid, 4);
OutFile.close();
}
else
{
return;
}
// Here, the grid is successfull saved , so we finished executing the SaveGridAction
pGrid->UpdateInterface();
}