-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
117 lines (106 loc) · 2.82 KB
/
main.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
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int board[4][4];
int dirLine[] = {1, 0, -1, 0};
int dirColumn[] = {0, 1, 0, -1};
pair<int,int> generateUnoccupiedPosition() {
int occupied = 1, line, column;
while (occupied) {
line = rand() % 4;
column = rand() % 4;
if (board[line][column] == 0) {
occupied = 0;
}
}
return make_pair(line, column);
}
void addPiece() {
pair<int, int> pos = generateUnoccupiedPosition();
board[pos.first][pos.second] = 2;
}
void newGame() {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
board[i][j] = 0;
}
}
addPiece();
}
void printUI() {
system("clear");
; for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (board[i][j] == 0) {
cout << setw(4) << ".";
} else {
cout << setw(4) << board[i][j];
}
}
cout << "\n";
}
cout << "n: new game, w: up, s: down, d: right, a: left, q: quit\n";
}
bool canDoMove (int line, int column, int nextLine, int nextColumn) {
if (nextLine < 0 || nextColumn < 0 || nextLine >= 4 || nextColumn >= 4 || (board[line][column] != board[nextLine][nextColumn] && board[nextLine][nextColumn] != 0)) {
return false;
}
return true;
}
void applyMove (int direction) {
int startLine = 0, startColumn = 0, lineStep = 1, columnStep = 1;
if (direction == 0){
startLine = 3;
lineStep = -1;
}
if (direction == 1) {
startColumn = 3;
columnStep = -1;
}
int movePossible, canAddPiece = 0;
do {
movePossible = 0;
for (int i = startLine; i >= 0 && i < 4; i += lineStep)
{
for (int j = startColumn; j >= 0 && j < 4; j += columnStep) {
int nextI = i + dirLine[direction], nextJ = j + dirColumn[direction];
if (board[i][j] && canDoMove(i, j, nextI, nextJ)) {
board[nextI][nextJ] += board [i][j];
board [i][j] = 0;
movePossible = canAddPiece = 1;
}
}
printUI();
}
} while (movePossible);
if (canAddPiece) {
addPiece();
}
}
int main()
{
srand(time(0));
char commandToDir[128];
commandToDir['s'] = 0;
commandToDir['d'] = 1;
commandToDir['w'] = 2;
commandToDir['a'] = 3;
newGame();
while (true) {
printUI();
char command;
cin >> command;
if (command == 'n') {
newGame();
} else if (command == 'q') {
break;
} else {
int currentDirection = commandToDir[command];
applyMove(currentDirection);
}
}
return 0;
}