-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomizer.c
64 lines (50 loc) · 1017 Bytes
/
randomizer.c
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
//
// Created by Avihoo on 25/06/2020.
//
#include "randomizer.h"
#include <stdlib.h>
#include <time.h>
void initRandomizer()
{
currentRecentIndex = 0;
for (int i = 0; i < RECENT_PIECES_SIZE; i++)
{
recentPieces[i] = 7;
}
srand((unsigned int) time(0));
}
unsigned int incrementRecentIndex()
{
unsigned int returnedIndex = currentRecentIndex;
currentRecentIndex = (currentRecentIndex + 1) % RECENT_PIECES_SIZE;
return returnedIndex;
}
void addRecentPiece(unsigned int pieceType)
{
recentPieces[incrementRecentIndex()] = pieceType;
}
unsigned char inRecentPieces(unsigned int pieceType)
{
for (int i = 0; i < RECENT_PIECES_SIZE; i++)
{
if (pieceType == recentPieces[i])
{
return 1;
}
}
return 0;
}
unsigned int getNewPiece()
{
unsigned int attemptedPiece = 7;
for (int i = 0; i < RANDOMIZER_MAX_ATTEMPTS; i++)
{
attemptedPiece = (unsigned int) (rand() % 7);
if (!inRecentPieces(attemptedPiece))
{
break;
}
}
addRecentPiece(attemptedPiece);
return attemptedPiece;
}