-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
93 lines (79 loc) · 1.77 KB
/
matrix.cpp
File metadata and controls
93 lines (79 loc) · 1.77 KB
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
#include "matrix.h"
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;
Matrix::Matrix()
{
/* initialize random seed: */
srand(time(NULL));
for(int y = 0; y < HEIGHT; y++)
for(int x = 0; x < WIDTH; x++)
matrix[x][y] = 0;
this->AddRandom();
this->AddRandom();
}
void Matrix::SumToLeft()
{
}
void Matrix::SumToRight()
{
for(int y = 0; y < HEIGHT; y++)
{
for(int x = WIDTH-2; x >= 0; x--)
{
// se diff de 0
if(matrix[x][y] != 0)
{
// se o vizinho é 0 entao move
// animacao move
if(matrix[x+1][y] == 0)
{
matrix[x+1][y] = matrix[x][y];
matrix[x][y] = 0;
}
// se igual ao vizinho entao soma e zera
// animacao soma
if(matrix[x][y] == matrix[x+1][y])
{
matrix[x+1][y] += matrix[x][y];
matrix[x][y] = 0;
}
}
}
}
this->AddRandom();
}
void Matrix::SumToTop()
{
}
void Matrix::SumToBottom()
{
}
void Matrix::AddRandom()
{
int rx, ry;
/* encontra uma posicao aleatoria com 0 */
do {
rx = (rand() % WIDTH) + 1;
ry = (rand() % HEIGHT) + 1;
}while(matrix[rx][ry] != 0);
/* cria um valor 2 ou 4 */
int v = ((rand() % 2) + 1) * 2;
matrix[rx][ry] = v;
}
void Matrix::Show()
{
cout<<"---------------------"<<endl;
for(int y = 0; y < HEIGHT; y++)
{
cout<<"|";
for(int x = 0; x < WIDTH; x++)
{
cout<<setw(4)<<matrix[x][y]<<"|";
}
cout<<endl;
}
cout<<"---------------------"<<endl;
}