Upcoming SlideShare
×

Like this presentation? Why not share!

# 4 Digit Number Guessing Game

## on Mar 20, 2014

• 709 views

### Views

Total Views
709
Views on SlideShare
377
Embed Views
332

Likes
0
1
0

### 10 Embeds332

 http://aragornyeh.blogspot.tw 279 http://aragornyeh.blogspot.com 26 http://www.slideee.com 17 http://aragornyeh.blogspot.in 3 http://aragornyeh.blogspot.de 2 http://aragornyeh.blogspot.kr 1 http://aragornyeh.blogspot.com.es 1 http://aragornyeh.blogspot.no 1 http://aragornyeh.blogspot.co.uk 1 http://www.google.co.th 1
More...

### Report content

• Comment goes here.
Are you sure you want to

## 4 Digit Number Guessing GamePresentation Transcript

• aragornyeh.blogspot.com
• Watch
• • Bulls and Cows1 • Strategy2 • Cheating3
• Bulls and Cows
• Bulls and Cows 1. an old code-breaking paper and pencil game for two players 2. the players each write a 4-digit secret number 3. Moo
• Rule 1. Players take turns to guess each other’s secret number. 2. For each guess, players respond “bulls” and “cows” for correct digit and orders.
• Demo 数当てゲームMOOの最小質問戦略と 最強戦略
• C Programming Homework #2 1. Generate a secret 4-digit number: – Random numbers between 0~9 without repetition 2. Compare your guess with the secret number: – Output *A*B
• rand function int i = 0; while(i < 10) { printf("%dn", rand()); i++; }
• srand function int i = 0; srand(12); while(i < 10) { printf("%dn", rand()); i++; }
• time function #include <time.h> int i = 0; srand(time(NULL)); while(i < 10) { printf("%dn", rand()); i++; }
• Array my_Guess com_Ans
• Calculate ? A for (i=0; i<4; i++) { if (my_Guess[i]==com_Ans[i]) { a_Count++; } }
• Calculate ? B for (i=0; i<4; i++) { if (my_Guess[i]!=com_Ans[i]) { for (j=0; j<4; j++) { if (i!=j && my_Guess[i]==com_Ans[j]) { b_Count++; } } } }
• Combination 5040 Possibilities 14 Categories
• Logic Tree 応答は 4A, 3A, 2A2B, 2A1B, 2A, 1A3B, 1A2B, 1A1B, 1A, 4B, 3B, 2B, 1B, 0A0B の14種
• 2 Strategy Stylize, edit, and animate your media
• Question 1. Is there an algorithm that can identify what guesses should be made? 2. What is the best strategy?
• Research One giving an optimal strategy requiring an average number of guesses of 4.34, with a maximum of 6 is given by Koyama and Lai in 1993. A slight modification, reduces the maximum number of guesses to 5, but increases the average to 4.341. 数当てゲームMOOの最小 質問戦略と最強戦略
• Approach (1) Random Method: – Blind Random – Human Instinct
• Example: Blind Random
• Approach (2) Improved Progress: Ø Eliminate Impossible
• Eliminate Impossible 1234 3A0B 24 1235 1236 1237… 1534 1634 1734… 5234 6234 7234… 3456 7890 1357… 6890 9781 2579… 4567 1568 8312… ……………………………
• Approach(3) Fixed Algorithm: – Smallest Number – Frequency of Occurrence – Weight Based Method
• Example: Smallest Number 1234 3A0B 24 1235 1236 1237… 1534 1634 1734… 5234 6234 7234…
• Example: Frequency of Occurrence How many times each digit occurs in the list of possible solutions? Possible Results: 782、876、951、436 0 1 2 3 4 5 6 7 8 9 0 1 2 1 1 1 2 2 2 1 782 2+2+1 = 5 876 2+2+2 = 6 951 1+1+1 = 3 436 1+1+2 = 4
• Weight Based Method 0 1 2 3 4 5 6 7 8 9 Provide each number a weightage based on the bulls and cows that the number has generated
• Computer Vs. Human
• Human Thinking 1234 0A0B 5678 0A3B 6789 2A2B 7689 ------- 6987 ------- 8769 -------
• Human Thinking 1234 0A2B 5678 0A1B 9032 0A2B 3724 0A1B 9321 1A2B New Guess: 9613
• Computer Thinking 1023 1024 1025… 2013 2014 2015… 3012 3014 3015… 4012 4013 4015… 5012 5013 5014… …………………………… ……………………………
• Cheating Design, organize, and collaborate
• Change Result Predefined Result: ‘1203’ Change to other possible result Ex.’1863’
• Give the Worst Response 1B 1440 2B 1260 1A1B 720 1A0B 480 ……..
• Logic Tree 応答は 4A, 3A, 2A2B, 2A1B, 2A, 1A3B, 1A2B, 1A1B, 1A, 4B, 3B, 2B, 1B, 0A0B の14種
• Escape to the Crowds 681、724、539、137 Player Guess ‘123’ Respond “0A1B”
• DEMO
• C PROGRAM
• C Program Design
• main function int main () { Secret(); Guess(); Compare(); return 0; }
• Function Design
• Secret function void Secret() { srand ( time(NULL) ); ... ... ... ... }
• Guess function void Guess(){ printf("nPlease Input Your Guess:"); scanf("%i", &my_Guess_Temp); my_Guess[0] = ...; my_Guess[1] = ...; my_Guess[2] = ...; my_Guess[3] = ...; }
• Compare function void Compare(){ a_Count=0; b_Count=0; for (i=0; i<4; i++) { if (my_Guess[i]==com_Ans[i]) { a_Count=a_Count++; }} .........
• C Program Design
• Function Design
• int answerBase[5040][4]; For(i=0; i<10000; i++) ………………………………………
• Check Anwer with Guess 1023 1024 1025… 2013 2014 2015… 3012 3014 3015… 4012 4013 4015… 5012 5013 5014… …………………………… ……………………………
• Eliminate Impossible Answer from Database 1234 3A0B 24 1235 1236 1237… 1534 1634 1734… 5234 6234 7234… 3456 7890 1357… 6890 9781 2579… 4567 1568 8312… ……………………………
• MIDTERM PROJECT
• Content of Report 1. 遊戲獲勝的策略和方法 2. 遊戲畫面的設計圖 3. 其它增加遊戲的樂趣的新創意?
• New Idea
• If at first, the idea is not absurd, then there is no hope for it. Albert Einsten
• Delivers 1. C Programming Code 2. Design Report
• Design Report Strategy Description Visual Design Your Creative Ideas 1 + 1 + 1 = 3 Pages
• Design an Mobile App
• Visual Sketch
• Bulls and Cows
• Guess
• Hot Bike Code Breaking
• Design Report Strategy Description Visual Design Your Creative Ideas 1 + 1 + 1 = 3 Pages
• Content of Report 1. 遊戲獲勝的策略和方法 2. 遊戲畫面的設計圖 3. 其它增加遊戲的樂趣的新創意?
• Midterm Project Design Report Contest C code
• Midterm Project: 4/25 3 students design 1 C program
• Teamwork 分工合作： 3個⼈的Project, 最後只有一個人在做事。 何謂負責？ Team Members：溝通、互相鼓舞
• C Programming is Easy! aragornyeh@gmail .com
• Textbook