-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmastermind.h
More file actions
101 lines (71 loc) · 2.04 KB
/
mastermind.h
File metadata and controls
101 lines (71 loc) · 2.04 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
93
94
95
96
97
98
99
100
101
#include "mastermind.h"
int coloursRightPos(struct Row * guess)
{
struct Row * scrt = mastermind->secret;
int clrsRightPos = 0;
if( guess->colours[0] == scrt->colours[0] ) clrsRightPos++;
if( guess->colours[1] == scrt->colours[1] ) clrsRightPos++;
if( guess->colours[2] == scrt->colours[2] ) clrsRightPos++;
return clrsRightPos;
};
int coloursWrongPos(struct Row * guess)
{
struct Row * scrt = mastermind->secret;
int clrsWrongPos = 0;
for (size_t i = 0; i < 3; i++)
{
bool isRepeat = false;
for (size_t k = i - 1; 0 >= k; k--)
{
if ( guess->colours[i] == guess->colours[k] ) isRepeat = true;
}
if ( isRepeat ) continue;
for (size_t j = 0; j < 3; j++)
{
isRepeat = false;
for (size_t k = j - 1; 0 >= k; k--)
{
if ( scrt->colours[j] == scrt->colours[k] ) isRepeat = true;
}
if ( isRepeat ) break;
if ( i == j ) continue;
if ( scrt->colours[j] == guess->colours[i])
{
clrsWrongPos++;
break;
}
}
}
return clrsWrongPos;
};
void writeRow(struct Row * row, int colours[3])
{
row->colours[0] = colours[0];
row->colours[1] = colours[1];
row->colours[2] = colours[2];
};
void startGame()
{
mastermind = (struct Mastermind *) malloc(sizeof(struct Mastermind));
mastermind->guesses = (struct Row *) malloc(sizeof(struct Row) * 3);
mastermind->secret = (struct Row *) malloc(sizeof(struct Row));
int temp[3], i = 0;
srand(time(0));
while (i < 3)
{
int j = i, rnd = (rand() % 3) + 1;
while ( j >= 0 )
{
if ( rnd == temp[j] )
{
j = i;
rnd = (rand() % 3) + 1;;
}
j--;
}
temp[i] = rnd;
i++;
}
struct Row * scrt = mastermind->secret;
writeRow(scrt, temp);
};