-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle.cpp
More file actions
129 lines (100 loc) · 2.89 KB
/
puzzle.cpp
File metadata and controls
129 lines (100 loc) · 2.89 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
#ifndef __PUZZLE_CPP__
#define __PUZZLE_CPP__
#include "puzzle.h"
#include <iostream>
Puzzle::Puzzle(int Rows, int Cols) : rows(Rows), cols(Cols)
{
for (int i = 0; i < this->rows; i++)
{
std::vector<int> _temp;
for (int i = 0; i < this->cols; i++)
{
_temp.push_back(-1);
}
this->puzzle.push_back(_temp);
}
}
Puzzle::Puzzle(int Rows, int Cols, std::vector<int> oVals) : rows(Rows), cols(Cols)
{
for (int i = 0; i < this->rows; i++)
{
std::vector<int> _temp;
for (int i = 0; i < this->cols; i++)
{
_temp.push_back(-1);
}
this->puzzle.push_back(_temp);
}
unsigned int Counter = 0;
for (int i = 0; i < this->rows; i++)
{
for (int j = 0; j < this->cols; j++)
{
if(Counter < oVals.size()) this->setTile(j, i, oVals[Counter++]);
}
}
}
const std::vector<std::vector<int>> Puzzle::getPuzzle() const {
return this->puzzle;
}
int Puzzle::getTile(int tile_x, int tile_y) const
{
if (tile_x >= 0 && tile_x < this->cols)
{
if (tile_y >= 0 && tile_y < this->rows)
{
return this->puzzle[tile_y][tile_x];
}
}
return -2; // Out of bounds
}
int Puzzle::setTile(int tile_x, int tile_y, int tile_value)
{
if (this->getTile(tile_x, tile_y) != -2)
{
this->puzzle[tile_y][tile_x] = tile_value;
return 1;
}
return -2;
}
int Puzzle::isValidMove(int tile_x, int tile_y, int move_x, int move_y) const
{
int tile_value = this->getTile(tile_x, tile_y);
int tile_dest_value = this->getTile(tile_x + move_x, tile_y + move_y);
return ((tile_value != -2) && (tile_dest_value != -2)) && ((tile_value == -1) || (tile_dest_value == -1));
}
int Puzzle::moveTile(int tile_x, int tile_y, int move_x, int move_y)
{
if (!this->isValidMove(tile_x, tile_y, move_x, move_y))
return 0;
// Swap tiles
int temp = this->puzzle[tile_y + move_y][tile_x + move_x];
this->puzzle[tile_y + move_y][tile_x + move_x] = this->puzzle[tile_y][tile_x];
this->puzzle[tile_y][tile_x] = temp;
return 1;
}
void Puzzle::print()
{
const std::vector<std::vector<int>> p = this->getPuzzle();
unsigned int max_width = 0;
for (int i = 0; i < this->rows; i++)
{
for (int j = 0; j < this->cols; j++)
{
if(std::to_string(p[i][j]).length() > max_width) max_width = std::to_string(p[i][j]).length();
}
}
for (int i = 0; i < this->rows; i++)
{
for (int j = 0; j < this->cols; j++)
{
int curr = p[i][j];
int buffer = max_width - std::to_string((curr == -1) ? 0 : curr).length();
for(int k = 0; k < buffer; k++) std::cout << " ";
(curr == -1) ? std::cout << '*' : std::cout << curr;
std::cout << " ";
}
std::cout << std::endl;
}
}
#endif