summaryrefslogtreecommitdiff
path: root/src/Model/Grid.cpp
blob: 92837f6b240fbeed086631b0c6c1d3e8e8754e46 (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
#include "Grid.hpp"

//Constructor
Grid::Grid(): m_size(4), m_grid(4){

	//Init all cells
	for(int i=0;i<m_size;i++){
		for(int j=0;j<m_size;j++){
			m_grid.at(i).push_back(0);
		}
	}

}

//Destructor
Grid::~Grid(){
}


std::string Grid::description(){

	//Init stringstream description
	std::stringstream description;

	//Start to write description
	description << "-----------------" << std::endl;
	for(int i=0;i<m_size;i++){
		for(int j=0;j<m_size;j++){
			if(m_grid.at(i).at(j) == 0)
				description << "| " << " " << " ";
			else
				description << "| " << m_grid.at(i).at(j) << " ";
		}
		description << "|";
		description << std::endl;
	}
	description << "-----------------" << std::endl << std::endl;

	//Return description
	return description.str();
}

bool Grid::isEmpty(int i, int j){
	if(m_grid.at(i).at(j) == 0)
		return true;
	return false;
}

std::tuple<int, int> Grid::getRandomEmptyCellCoord(){

	//Init list of candidate
	std::vector<std::tuple<int, int> > candidates;

	//Construct list of candidates
	for(int i=0;i<m_size;i++){
		for(int j=0;j<m_size;j++){
			if(this->isEmpty(i,j)){
				std::tuple<int, int> currentCandidate(i,j);
				candidates.push_back(currentCandidate);
			}
		}
	}

	//If no candidate available
	if(candidates.size() == 0)
		return std::tuple<int, int>(-1, -1);

	//Select the candidates
	int winnerIs(rand() % candidates.size());

	//Return the candidate
	return candidates.at(winnerIs);

}



//Change value of cell
bool Grid::setCell(std::tuple<int, int> coord, int value){
	int i=std::get<0>(coord); 
	int j=std::get<1>(coord); 

	if(i>=0 && i<m_size && j>=0 && j<m_size){
		m_grid.at(i).at(j)=value;
		return true;
	}

	return false;
}

//Another setCell method
bool Grid::setCell(int i, int j, int value){
	std::tuple<int, int> coord(i,j);
	return this->setCell(coord, value);
}