-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
75 lines (59 loc) · 1.87 KB
/
functions.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
//
// Created by Rafael on 13/11/2019.
//
#include "functions.h"
#include <random>
#include <ctime>
int bagRandomizer(vector<int> &pool) {
const unsigned int seed = time(0);
mt19937_64 rng(seed);
uniform_int_distribution<int> range(0, pool.size() - 1);
// Choose a random tetromino from pool
int pos=range(rng);
int piece = pool.at(pos);
pool.erase(pool.begin() + pos);
if (pool.empty())
pool = {0, 1, 2, 3, 4, 5, 6};
return piece;
}
int rotate(int px, int py, int r) {
//Rotation matrix in 1d vector format
int pi = 0;
switch (r % 4) {
case 0: //0
pi = py * 4 + px;
break;
case 1: //90
pi = 12 + py - (px * 4);
break;
case 2: //180
pi = 15 - (py * 4) - px;
break;
case 3: //270
pi = 3 - py + (px * 4);
break;
}
return pi;
}
bool collisionCheck(int &nTetromino, int nRotation, int nPosX, int nPosY, const unsigned char *pField) {
// All Field cells >0 are occupied
for (int px = 0; px < 4; px++)
for (int py = 0; py < 4; py++) {
// Get index into piece
int pi = rotate(px, py, nRotation);
// Get index into field
int fi = (nPosY + py ) * nFieldWidth + (nPosX + px);
// In Bounds so do collision check
if (tetromino[nTetromino][pi] != L'.' && pField[fi] != 0)
return false; // fail on first hit
}
return true;
}
bool gameOverCheck(const unsigned char *pFieldRef, int currentFigure) {
int nCurrentX = (nFieldWidth / 2) - 2;
int nCurrentY = 0;
int nCurrentRotation = 0;
// If piece does not fit straight away, game over!
bool bGameOver = !collisionCheck(currentFigure, nCurrentRotation, nCurrentX, nCurrentY, pFieldRef);
return bGameOver;
}