-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPosition.cpp
More file actions
65 lines (54 loc) · 1.3 KB
/
Position.cpp
File metadata and controls
65 lines (54 loc) · 1.3 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
//
// Position.cpp
// MazeProject
//
// Created by Toby Dragon on 11/7/14.
// Copyright (c) 2014 Toby Dragon. All rights reserved.
//
#include "stdafx.h"
#include "Position.h"
Position::Position(){
this->x = -1;
this->y = -1;
}
Position::Position( int x, int y){
this->x = x;
this->y = y;
}
int Position::getX()const{
return x;
}
int Position::getY()const{
return y;
}
Position* Position::getNextMoves() const{
Position* moves = new Position[4];
moves[0] = Position(x, y+1); //up
moves[1] = Position(x+1, y); //right
moves[2] = Position(x-1, y); //left
moves[3] = Position(x, y-1); //down
return moves;
}
bool Position::isAdjacent(Position toCheck) const{
Position* nextMoves = getNextMoves();
for (int i=0; i<4; i++){
if (nextMoves[i] == toCheck){
delete nextMoves;
nextMoves = nullptr;
return true;
}
}
delete nextMoves;
nextMoves = nullptr;
return false;
}
bool Position::operator==(const Position& otherPos) const{
return x==otherPos.x && y==otherPos.y;
}
bool Position::operator!=(const Position& otherPos) const{
return ! (x==otherPos.x && y==otherPos.y);
}
ostream& operator<< (ostream &out, const Position& pos){
out << "(" << pos.x << ", " << pos.y << ")";
return out;
}