-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPosition.cpp
More file actions
145 lines (141 loc) · 4.58 KB
/
Position.cpp
File metadata and controls
145 lines (141 loc) · 4.58 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
139
140
141
142
143
144
145
#include "Board.h"
#include "Position.h"
//-------------------------------------------------------------------------------------------------------------------------------------
Position::Position(int row, int col) : m_row(row), m_col(col)
{
}
//-------------------------------------------------------------------------------------------------------------------------------------
bool Position::isInBoard(int size) const
{
if (m_row < 0 || m_row >= size || m_col < 0 || m_col >= size)
return false;
else return true;
}
//-------------------------------------------------------------------------------------------------------------------------------------
Position Position::getPosFromDirection(const DIRECTION& direction) const
{
Position newPos;
switch (direction)
{
case DIRECTION::UP_LEFT:
newPos.setRow(getRow() + 1);
newPos.setCol(getCol() - 1);
break;
case DIRECTION::UP_RIGHT:
newPos.setRow(getRow() + 1);
newPos.setCol(getCol() + 1);
break;
case DIRECTION::DOWN_LEFT:
newPos.setRow(getRow() - 1);
newPos.setCol(getCol() - 1);
break;
case DIRECTION::DOWN_RIGHT:
newPos.setRow(getRow() - 1);
newPos.setCol(getCol() + 1);
break;
case DIRECTION::UP:
newPos.setRow(getRow() + 1);
newPos.setCol(getCol());
break;
case DIRECTION::DOWN:
newPos.setRow(getRow() - 1);
newPos.setCol(getCol());
break;
case DIRECTION::LEFT:
newPos.setRow(getRow());
newPos.setCol(getCol() - 1);
break;
case DIRECTION::RIGHT:
newPos.setRow(getRow());
newPos.setCol(getCol() + 1);
break;
}
return newPos;
}
//-------------------------------------------------------------------------------------------------------------------------------------
Position Position::getPosFromDirection(const KNIGHT_DIRECTION& direction) const
{
Position newPos;
switch (direction)
{
case KNIGHT_DIRECTION::LEFT_DOWN:
newPos.setRow(getRow() - 1);
newPos.setCol(getCol() - 2);
break;
case KNIGHT_DIRECTION::LEFT_UP:
newPos.setRow(getRow() + 1);
newPos.setCol(getCol() - 2);
break;
case KNIGHT_DIRECTION::UP_LEFT:
newPos.setRow(getRow() + 2);
newPos.setCol(getCol() - 1);
break;
case KNIGHT_DIRECTION::UP_RIGHT:
newPos.setRow(getRow() + 2);
newPos.setCol(getCol() + 1);
break;
case KNIGHT_DIRECTION::RIGHT_UP:
newPos.setRow(getRow() + 1);
newPos.setCol(getCol() + 2);
break;
case KNIGHT_DIRECTION::RIGHT_DOWN:
newPos.setRow(getRow() - 1);
newPos.setCol(getCol() + 2);
break;
case KNIGHT_DIRECTION::DOWN_RIGHT:
newPos.setRow(getRow() - 2);
newPos.setCol(getCol() + 1);
break;
case KNIGHT_DIRECTION::DOWN_LEFT:
newPos.setRow(getRow() - 2);
newPos.setCol(getCol() - 1);
break;
}
return newPos;
}
//-------------------------------------------------------------------------------------------------------------------------------------
bool Position::operator==(const Position& other)
{
return ((getRow() == other.getRow()) && (getCol() == other.getCol()));
}
//-------------------------------------------------------------------------------------------------------------------------------------
bool Position::operator!=(const Position& other)
{
return ((getRow() != other.getRow()) || (getCol() != other.getCol()));
}
//-------------------------------------------------------------------------------------------------------------------------------------
Position::DIRECTION Position::checkOppositeDirection(const Position& from, const Position& to)
{
int fromRow, fromCol, toRow, toCol;
fromRow = from.getRow();
fromCol = from.getCol();
toRow = to.getRow();
toCol = to.getCol();
if (fromRow - toRow < 0)
{
if (fromCol - toCol > 0)
return Position::DIRECTION::DOWN_RIGHT;
if (fromCol - toCol < 0)
return Position::DIRECTION::DOWN_LEFT;
}
else
{
if (fromCol - toCol > 0)
return Position::DIRECTION::UP_RIGHT;
if (fromCol - toCol < 0)
return Position::DIRECTION::UP_LEFT;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
void Position::save(ostream& out) const
{
out.write(rcastcc(&m_row), sizeof(m_row));
out.write(rcastcc(&m_col), sizeof(m_col));
}
//-------------------------------------------------------------------------------------------------------------------------------------
void Position::load(istream& in)
{
in.read(rcastc(&m_row), sizeof(m_row));
in.read(rcastc(&m_col), sizeof(m_col));
}
//-------------------------------------------------------------------------------------------------------------------------------------