-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNUMBER GUESS GAME.c
More file actions
206 lines (153 loc) · 4.72 KB
/
NUMBER GUESS GAME.c
File metadata and controls
206 lines (153 loc) · 4.72 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
struct Node{
int data;
struct Node *next;
};
void append(struct Node** head_ref, int new_data){
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_node->data = new_data;
/* 3. This new node is going to be the last node, so make next
of it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
return;
}
/* function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b)
{
int temp = a->data;
a->data = b->data;
b->data = temp;
}
void bubbleSort(struct Node *start)
{
int swapped, i;
struct Node *ptr1;
struct Node *lptr = NULL;
/* Checking for empty list */
if (start == NULL)
return;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
{
if (ptr1->data > ptr1->next->data)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
void printList(struct Node* node)
{
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int Pow(int a, int b){ // returns a^b
int result = 1;
int i;
for ( i = 0; i < b; i++)
result = result * a;
return result;
}
int getRandomNumber(int digitCount){
int randomNumber = 0;
int digits[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int randomIndex;
srand(time(NULL));
randomIndex = (rand() % 9) + 1;
randomNumber = digits[randomIndex] * Pow(10,digitCount-1);
digits[randomIndex] = -1;
int i;
for (i = (digitCount - 2); i >= 0; i--){
while (1){
randomIndex = (rand() % 10);
if (digits[randomIndex] != -1){
randomNumber = randomNumber + (digits[randomIndex] * Pow(10, i));
digits[randomIndex] = -1;
break;
}
}
}
return randomNumber;
}
int main(void){
printf("==================================\n");
printf("========NUMBER GUESS GAME=========\n");
printf("==================================\n");
// kullanicidan basamak sayisini aliyoruz
int digitCount;
printf("Enter the number of digits for the game 4-10: ");
scanf("%d", &digitCount);
// random sayimizi uretiyoruz
int randomNumber = getRandomNumber(digitCount);
// karsilastirmasi kolay olsun diye random sayimizin string halini uretiyoruz
char randomNumberStringVersion[digitCount];
sprintf(randomNumberStringVersion, "%d", randomNumber);
// basamaklarin dogruluklarini kontrol eden degiskenleri tanimliyoruz
int correctLocation = 0;
int wrongLocation = 0;
// kullanicinin girecegi sayiyi string turunde tutuyoruz ki basamak karsilastirma islemini kolayca yapalim
int userGuess;
char userGuessStringVersion[digitCount];
// girilen tahminleri tutacagimiz adresleri isaret edecek pointer
struct Node* startAddress = (struct Node*) malloc(sizeof(struct Node));
// girilen tahmin sayisini tutacak degisken
int guessCount = 0;
while (1){
// kullanicidan tahminini aliyoruz
printf("Enter a guess: ");
scanf("%d", &userGuess);
guessCount = guessCount + 1;
append(&startAddress, userGuess);
sprintf(userGuessStringVersion, "%d", userGuess);
int i, j;
for ( i = 0; i < digitCount; i++){
for ( j = 0; j < digitCount; j++){
if (randomNumberStringVersion[i] == userGuessStringVersion[j]){
if(i == j)
correctLocation = correctLocation + 1;
else
wrongLocation = wrongLocation + 1;
}
}
}
printf("+%d-%d\n", correctLocation, wrongLocation);
if (correctLocation == digitCount){
printf("Congratz you won the game\n");
break;
}
correctLocation = 0;
wrongLocation = 0;
}
printf("total number of tries:%d\n", guessCount);
printf("all guesses from smallest to largest:\n");
bubbleSort(startAddress);
printList(startAddress);
return 0;
}