-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSnake.cpp
112 lines (89 loc) · 2.94 KB
/
Snake.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
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
#include "Snake.h"
#include "Ladder.h"
#include "Player.h"
#include <iostream>
using namespace std;
Snake::Snake(const CellPosition& startCellPos, const CellPosition& endCellPos) : GameObject(startCellPos)
{
this->endCellPos = endCellPos;
///TODO: Do the needed validation
if (Validity == VALID_OBJECT)
{
if (position.HCell() != endCellPos.HCell() && endCellPos.VCell() <= position.VCell())
Validity = COLUMN_AND_REVERSING_ERROR;
else if (position.HCell() != endCellPos.HCell())
Validity = NOT_SAME_COLUMN;
else if (endCellPos.VCell() <= position.VCell())
Validity = START_END_REVERSED;
if (!endCellPos.IsValidCell())
Validity = INVALID_END;
}
else if (!endCellPos.IsValidCell())
Validity = INVALID_START_END;
}
bool Snake::IsOverlapping(GameObject* newObj) const
{
Snake* pSnake = dynamic_cast<Snake*>(newObj);
if (pSnake)
{
int HPosition1 = position.HCell();
int HPosition2 = newObj->GetPosition().HCell();
int VStartPosition1 = position.VCell();
int VEndPosition1 = endCellPos.VCell();
int VStartPosition2 = newObj->GetPosition().VCell();
int VEndPosition2 = pSnake->GetEndPosition().VCell();
if (HPosition1 == HPosition2 && ((VStartPosition2 > VStartPosition1 && VStartPosition2 <= VEndPosition1) || (VStartPosition1 > VStartPosition2 && VStartPosition1 <= VEndPosition2)))
return true;
}
else {
Ladder* pLadder = dynamic_cast<Ladder*>(newObj);
if (pLadder) {
int LadderVStartCell = pLadder->GetPosition().VCell();
int LadderHStartCell = pLadder->GetPosition().HCell();
int SnakeVEndCell = endCellPos.VCell();
int SnakeHEndCell = endCellPos.HCell();
if (LadderVStartCell == SnakeVEndCell && LadderHStartCell == SnakeHEndCell)
return true;
}
}
return false;
}
void Snake::Draw(Output* pOut) const
{
pOut->DrawSnake(position, endCellPos);
}
void Snake::Apply(Grid* pGrid, Player* pPlayer)
{
///TODO: Implement this function as mentioned in the guideline steps (numbered below) below
int x, y;
int move;
Player* currPlayer;
// == Here are some guideline steps (numbered below) to implement this function ==
// 1- Print a message "You have reached a snake. Click to continue ..." and wait mouse click
pGrid->PrintErrorMessage("You have reached a snake. Click to continue ...");
// 2- Apply the snake's effect by moving the player to the endCellPos
// Review the "pGrid" functions and decide which function can be used for that
move = endCellPos.GetCellNum() - position.GetCellNum();
currPlayer = pGrid->GetCurrentPlayer();
currPlayer->Move(pGrid, move);
}
CellPosition Snake::GetEndPosition() const
{
return endCellPos;
}
void Snake::Save(ofstream& OutFile)
{
OutFile << position.GetCellNum() << "\t" << endCellPos.GetCellNum() << endl;
}
void Snake::Load(ifstream& InFile)
{
int startPos, endPos;
InFile >> startPos >> endPos;
CellPosition StartCellPosition(startPos);
CellPosition EndCellPosition(endPos);
position = StartCellPosition;
endCellPos = EndCellPosition;
}
Snake::~Snake()
{
}