Repository navigation
Expand file tree
/
Copy pathSetTile.cpp
More file actions
112 lines (98 loc) · 2.91 KB
/
Copy pathSetTile.cpp
File metadata and controls
112 lines (98 loc) · 2.91 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
/*
* File: SetTile.cpp
* Author: jester
*
* Created on June 8, 2015, 6:34 PM
*/
#include "SetTile.h"
#include "Tile.h"
#include "Position.h"
#include "Exceptions.h"
#include <vector>
SetTile::SetTile() : Command("settile")
{
}
SetTile::~SetTile()
{
}
int SetTile::execute(Game &board, std::vector<std::string> ¶ms)
{
Position pos;
//check number of parameters and (if available) evaluate first parameter
if (params.size() != 2 || !pos.parse(params.at(0)))
{
throw TraxException(TraxException::INVALID_PARAMS);
}
//preserve old data in case of screw up
std::vector<std::vector<Tile *> > game_board = board.getBoard();
std::pair<Position, Position> min_max_save(
Position(board.getMinX(), board.getMinY()),
Position(board.getMaxX(), board.getMaxY()));
std::vector<Position> buffer;
buffer.push_back(pos);
//evaluate second parameter
Tile::Type tile_type;
std::string tile_type_str = params.at(1);
if (tile_type_str == "+")
tile_type = Tile::TYPE_CROSS;
else if (tile_type_str == "/")
tile_type = Tile::TYPE_CURVE_1;
else if (tile_type_str == "\\")
tile_type = Tile::TYPE_CURVE_2;
else
{
throw TraxException(TraxException::INVALID_PARAMS);
}
bool first_tile = board.getStartTile() == nullptr;
//first tile must be at (0,0)
// if (first_tile && (pos.getX() != 0 || pos.getY() != 0))
// {
// throw TraxException(TraxException::POSITION_MUST_BE_ZERO);
// }
//check if space is taken
if (board.getTileAtPosition(pos) != nullptr)
{
throw TraxException(TraxException::FIELD_NOT_FREE);
}
Tile *tile_pointer;
//handle first tile because further calculations are not related to it
if (first_tile)
{
tile_pointer = new Tile(tile_type, Color::COLOR_RED);
board.setStartTile(tile_pointer);
board.setTileAtPosition(pos, tile_pointer);
board.togglePlayer();
return 1;
}
// //calculate top color
// int top_color_integer;
// switch (tile_type)
// {
// case Tile::TYPE_CROSS:
// top_color_integer = colors_for_tile.getTop() |
// Game::getInvertedColor(colors_for_tile.getRight()) |
// colors_for_tile.getBottom() |
// Game::getInvertedColor(colors_for_tile.getLeft());
// break;
// case Tile::TYPE_CURVE_1:
// top_color_integer = colors_for_tile.getTop() |
// Game::getInvertedColor(colors_for_tile.getRight()) |
// Game::getInvertedColor(colors_for_tile.getBottom()) |
// colors_for_tile.getLeft();
// break;
// case Tile::TYPE_CURVE_2:
// top_color_integer = colors_for_tile.getTop() |
// colors_for_tile.getRight() |
// Game::getInvertedColor(colors_for_tile.getBottom()) |
// Game::getInvertedColor(colors_for_tile.getLeft());
// break;
// }
//
tile_pointer = new Tile(tile_type, static_cast<Color>(1));
board.setTileAtPosition(pos, tile_pointer);
return 0;
}
unsigned int SetTile::getParamCnt()
{
return 2;
}