-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumwordle.c
76 lines (72 loc) · 1.46 KB
/
numwordle.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
65
66
67
68
69
70
71
72
73
74
75
76
/*
12345
13789
21000
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int dig(int n, int num, int i)
{
return (int)(num / pow(10, n - i - 1)) % 10;
}
void wordlegame(int n, int num, int guess)
{
for(int i=0; i<n; i++)
{
int digG = dig(n, guess, i), count=1;
for(int j=0; j<n; j++)
{
int digN = dig(n, num, j);
if(digG==digN && i==j)
{
printf("2");
count=0;
break;
}
else if(digG==digN && i!=j)
{
printf("1");
count=0;
break;
}
}
if(count==1) printf("0");
}
printf("\n");
}
int playWordle(int n, int attempts)
{
int num;
while(num<(int)pow(10, n-1) || num>=(int)pow(10, n))
{
num = rand()%(int)(pow(10, n));
}
int guess;
while (attempts > 0)
{
printf("Guess the number:\n");
scanf("%d", &guess);
if (guess == num)
{
for(int i=0; i<n; i++) {
printf("2");
}
printf("\n");
return 1;
}
wordlegame(n, num, guess);
attempts--;
}
printf("The number was %d\n", num);
return 0;
}
int main()
{
int n = 5, attempts = 5, resWordle;
resWordle = playWordle(n, attempts);
if (resWordle == 1)
printf("GG!\n");
else if (resWordle == 0)
printf("You Lose!\n");
}