• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <time.h>
20 
main()21 int main() {
22   srand(time(NULL));
23 
24   unsigned int ans = rand() % 100;
25   unsigned int user = 100;
26   unsigned int left = 0;
27   unsigned int right = 99;
28 
29   printf("Hello, droid!  Let's play a number guessing game!\n");
30 
31   while (user != ans) {
32     printf("Please input a number [%d-%d]:\n", left, right);
33 
34     if (scanf("%u", &user) != 1) {
35       break;
36     }
37 
38     if (user < left || user > right) {
39       /* Out of range, ignore this answer. */
40       continue;
41     } else if (user == ans) {
42       printf("You got it!\n");
43       break;
44     } else if (user < ans) {
45       left = user;
46     } else {
47       right = user;
48     }
49   }
50 
51   return 0;
52 }
53