-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
129 lines (111 loc) · 2.82 KB
/
board.cpp
File metadata and controls
129 lines (111 loc) · 2.82 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
#include "board.h"
// initialize the board with coins
Board::Board()
{
board.push_back(Coin(0));
board.push_back(Coin(0));
board.push_back(Coin(0));
board.push_back(Coin(2));
board.push_back(Coin(1));
board.push_back(Coin(1));
board.push_back(Coin(1));
}
// print the coins in the board
void Board::PrintBoard()
{
for (int i = 0; i < board.size(); i++)
{
cout << board[i].GetColor() << " ";
}
cout << endl;
}
// swap two coins with their index
void Board::SwapCoins(int initialPos, int finalPos)
{
Coin tempCoin = board[finalPos];
board[finalPos] = board[initialPos];
board[initialPos] = tempCoin;
}
// move coins with a vector having init and final positions
void Board::MoveCoins(std::vector<int> pos)
{
MoveCoins(pos[0], pos[1]);
}
// Move a coin with its init. and final index
void Board::MoveCoins(int initialPos, int finalPos)
{
if (!isInBounds(initialPos) || !isInBounds(finalPos))
{
cout << "Positions out of bounds" << endl;
return;
}
if (GetCoinFromIndex(initialPos).GetEnumColor() == Empty)
{
cout << "Choose a non empty Coin" << endl;
return;
}
int dist = abs(finalPos - initialPos);
if (dist == 1)
{
if (isFinalDestinationEmpty(finalPos))
{
SwapCoins(initialPos, finalPos);
}
else
{
cout << "Choose a valid final position (TYPE 1)" << endl;
}
}
else if (dist == 2)
{
if (isValidJump(initialPos, finalPos))
{
SwapCoins(initialPos, finalPos);
}
else
{
cout << "Choose a valid final position (TYPE 2)" << endl;
}
}
else
{
cout << "Final coin too far" << endl;
}
}
Coin Board::GetCoinFromIndex(int index)
{
return board[index];
}
// takes in a vector of positions, return a vector of indices
std::vector<int> Board::ConvertPositionsToIndex(std::vector<sf::Vector2i> vec, int size)
{
std::vector<int> vec2;
for (auto i : vec)
{
vec2.push_back(i.x / size);
}
return vec2;
}
int Board::ConvertPositionToIndex(int pos, int size)
{
return pos/ size;
}
// checks if a coin can jump over a coin of opposite color
bool Board::isValidJump(int initPos, int finalPos)
{
Coin firstCoin = GetCoinFromIndex(initPos);
Coin middleCoin = GetCoinFromIndex((initPos + finalPos) / 2);
Coin lastCoin = GetCoinFromIndex(finalPos);
return firstCoin.GetEnumColor() != middleCoin.GetEnumColor() && middleCoin.GetEnumColor() != Empty && lastCoin.GetEnumColor() == Empty;
}
bool Board::isFinalDestinationEmpty(int dest)
{
return GetCoinFromIndex(dest).GetEnumColor() == Empty;
}
bool Board::isInBounds(int index)
{
if (index >= 0 && index < board.size())
return true;
else
return false;
}