-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.cpp
124 lines (120 loc) · 2.47 KB
/
display.cpp
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
/**
*
* @Name : MinesweeperCLI/display.cpp
* @Version : 1.0
* @Programmer : Max
* @Date : 2018-07-12
* @Released under : https://github.com/BaseMax/MinesweeperCLI/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/MinesweeperCLI
*
**/
#include <algorithm>
// #include <stdlib.h>
// #include <stdio.h>
// #include <time.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;
const unsigned int size=8;// 8 * 8 = 64
// enum Type {BOMB, EMPTY};
#define BOMB -1
void boardRender(int board[size][size]) {
cout << size << "\n";
cout << " ";
for(int index=0; index < size; index++) {
cout << index << " ";
}
cout << "\n";
for(int rowIndex=0; rowIndex < size; rowIndex++) {
cout << rowIndex;
for(int columnIndex=0; columnIndex < size; columnIndex++) {
cout << "|";
if(board[rowIndex][columnIndex] == -1) {
cout << "*";
}
else {
cout << " ";
}
}
cout << "\n";
}
}
void trim(std::string& input) {
if(input.empty())
return;
int first = 0, last = input.size() - 1;
while(first < input.size() && std::isspace(input[first++]));
while(last >= 0 && std::isspace(input[last--]));
if(first > last)
input = "";
else {
first--;
last++;
int wi = 0;
while(first <= last)
input[wi++] = input[first++];
input.erase(wi);
}
return;
}
int main(int argc, char const *argv[]) {
// unsigned int width=8;
// unsigned int height=8;
srand(time(0)); // Seed
unsigned int bomb;
// cout << "How many bomb do you want?";
// cin >> bomb;
/*
E.g: Plans:
- Easy (bomb=10)
- Medium (bomb=15)
- Hard (bomb=20)
- Very Hard (bomb=50)
*/
string ask;
cout << "Choose your level? (easy, medium, hard, veryhard)\n > ";
cin >> ask;
// trim(ask);
transform(ask.begin(), ask.end(), ask.begin(), ::tolower);
if(ask == "easy") {
bomb=10;
}
else if(ask == "medium") {
bomb=20;
}
else if(ask == "hard") {
bomb=30;
}
else if(ask == "veryhard") {
bomb=50;
}
else {
bomb=atoi(ask.c_str());
if(bomb >= (size*size)) {
bomb=size*size-1;
}
}
cout << bomb << endl;
int board[size][size]={0};
int bombFill=0;
int randomRow;
int randomColumn;
// We can optimize it...
while(bombFill <= bomb) {
randomRow=rand() % size;
randomColumn=rand() % size;
if(board[randomRow][randomColumn] == 0) {
board[randomRow][randomColumn]=BOMB;
// We can make it with a better way...
bombFill++;
}
else {
// Aleady there is a bomb at here...
}
}
cout << "Render:\n";
boardRender(board);
return 0;
}