summaryrefslogtreecommitdiff
path: root/src/Model/Grid.cpp
blob: 6fc41c67db758b0fbd152a982c4218265e579c48 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "Grid.hpp"

//Constructor
Grid::Grid(int size)
{
	//Create Vector
	m_size = size;
	m_table = std::vector<std::vector<Cell<StringElement>*> >(size);

	//Init all of line and cell
	for(int i = 0 ; i < size ; i++)
	{
		m_table[i] = std::vector<Cell<StringElement>*>(size);
		for (int j = 0 ; j < size ; j++)
		{
			Cell<StringElement> * cell = new Cell<StringElement>("");
			m_table[i][j] = cell;
		}
	}
}

//Destructor
Grid::~Grid()
{
	for(int i = 0 ; i < m_size ; i++)
	{
		for(int j = 0 ; j < m_size ; j++)
			delete m_table[i][j];
	}
}

void Grid::show()
{
	std::cout << "_________________" << std::endl;
	std::cout << std::endl;
	for(int i = 0 ; i < m_size ; i++)
	{
		std::cout << "|";
		for(int j = 0 ; j < m_size ; j++)
		{
			std::cout << " " << m_table[i][j]->description() << " |";
		}
		std::cout << std::endl;

		if (i != m_size -1)
			std::cout << std::endl;
	}
	std::cout << "_________________" << std::endl;
}

bool Grid::isEmpty(int i, int j)
{
	if (i >= 0 && i < m_size && j >= 0 && j < m_size)
		return m_table[i][j]->isEmpty();

	return false;
}

bool Grid::gridIsFull()
{

	for (int i = 0; i < m_size ; i++)
	{
		for (int j = 0; j < m_size ; j++)
		{
			if (m_table[i][j]->isEmpty())
				return false;
		}
	}

	return true;
}

void Grid::setCell(int i, int j, Cell<StringElement> *cell)
{
	if (i >= 0 && i < m_size && j >= 0 && j < m_size)
	{
		delete m_table[i][j];
		m_table[i][j] = cell;
	}
}

Cell<StringElement>* Grid::getCell(short i, short j){
	return m_table[i][j];
}

int Grid::getNRows(){
	return m_table[0].size();
}

int Grid::getNCols(){
	return m_table.size();
}


std::vector<Cell<StringElement>* > Grid::swipeLine(std::vector<Cell<StringElement>* > line){
	std::vector<Cell<StringElement>*> newLine = std::vector<Cell<StringElement>*>(4);


	for (int j = 0 ; j < 3 ; j++)
	{
		if(j>3)
			break;
		Cell<StringElement> * cell = new Cell<StringElement>(line.at(j)->getElementValue());
		Cell<StringElement> * cellp1 = new Cell<StringElement>(line.at(j+1)->getElementValue());

		int a=atoi(cell->getElementValue().c_str());
		int ap1=atoi(cellp1->getElementValue().c_str());

		std::string s=std::to_string(a);
		std::string sp1=std::to_string(ap1);

		if(a==ap1 && a!=0){
			s="";
			sp1=std::to_string(a+ap1);
			if(ap1 == 0)
				newLine[j+1] = new Cell<StringElement>("");
			else
				newLine[j+1] = new Cell<StringElement>(sp1);
			newLine[j] = new Cell<StringElement>(s);
			j++;
		}
		else{
			if(ap1==0)
				newLine[j+1] = new Cell<StringElement>("");
			else
				newLine[j+1] = new Cell<StringElement>(sp1);
			if(a==0)
				newLine[j] = new Cell<StringElement>("");
			else
				newLine[j] = new Cell<StringElement>(s);

		}
		delete cell;
		delete cellp1;
		
	}


	for (int j = 0 ; j < 3 ; j++){

		if(!newLine[j]->isEmpty()){
			if(newLine[j+1]->isEmpty()){
				newLine[j+1]=new Cell<StringElement>(newLine[j]->getElementValue());
				newLine[j]=new Cell<StringElement>("");
			}
		}
	}

	for(int i=0; i<4;i++){

		std::cout << "|" << newLine[i]->description() << "|";
	}
	std::cout << "done";
	return newLine;
}

void Grid::swipeRight(){
	std::vector<Cell<StringElement>*> a=this->swipeLine(m_table.at(0));
	m_table[0]=a;
}