-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.cpp
115 lines (106 loc) · 2.08 KB
/
Helper.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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include "Game.h"
#include "Leaderboards.h"
#include "Movements.h"
const int LEADERBOARD_SIZE = 5;
const int MAX_NICKNAME = 101;
void clearConsole() {
std::cout << "\033[;H";
std::cout << "\033[J";
}
void printBoard(int** arr, int size) {
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
std::cout << std::setw(5) << arr[i][j];
}
std::cout << std::endl;
}
}
int score(int** arr, int size) {
int firstTile = 2; //since you initialize the board with a tile, you need to add it to the score
int score = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
score += arr[i][j];
}
}
score += firstTile;
return score;
}
bool emptyArray(int** arr, int size) {
int count = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (arr[i][j] == 0)
{
count++;
}
}
}
if (count == size * size)
{
return true;
}
return false;
}
bool checkPossibleMoves(int** arr, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (arr[i][j] == 0) {
return true;
}
if (j < size - 1 && arr[i][j] == arr[i][j + 1]) { // if you can merge horizontally
return true;
}
if (i < size - 1 && arr[i][j] == arr[i + 1][j]) { // if you can merge vertically
return true;
}
}
}
return false;
}
bool isEmptySpace(int** arr, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (arr[i][j] == 0)
return true;
}
}
return false;
}
void checkInput(int& num)
{
while (true) {
std::cin >> num;
if (std::cin.fail()) {
std::cout << "Invalid input. Please input a number. " << std::endl;
std::cin.clear();
std::cin.ignore(100000, '\n');
}
else {
break;
}
}
}
bool win(int** arr, int size) {
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (arr[i][j] == 2048)
{
return true;
}
}
}
return false;
}