1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators fry grouping hashtables
4 kernel locals math math.parser math.ranges random sequences
6 IN: rosetta-code.bulls-and-cows
8 ! http://rosettacode.org/wiki/Bulls_and_cows
10 ! This is an old game played with pencil and paper that was
11 ! later implemented on computer.
13 ! The task is for the program to create a four digit random
14 ! number from the digits 1 to 9, without duplication. The program
15 ! should ask for guesses to this number, reject guesses that are
16 ! malformed, then print the score for the guess.
18 ! The score is computed as:
20 ! 1. The player wins if the guess is the same as the randomly
21 ! chosen number, and the program ends.
23 ! 2. A score of one bull is accumulated for each digit in the
24 ! guess that equals the corresponding digit in the randomly
25 ! chosen initial number.
27 ! 3. A score of one cow is accumulated for each digit in the
28 ! guess that also appears in the randomly chosen number, but in
31 TUPLE: score bulls cows ;
32 : <score> ( -- score ) 0 0 score boa ;
35 : <cow> ( -- cow ) cow new ;
38 : <bull> ( -- bull ) bull new ;
40 : inc-bulls ( score -- score ) dup bulls>> 1 + >>bulls ;
41 : inc-cows ( score -- score ) dup cows>> 1 + >>cows ;
43 : random-nums ( -- seq ) 9 [1,b] 4 sample ;
45 : add-digits ( seq -- n ) 0 [ swap 10 * + ] reduce number>string ;
47 : new-number ( -- n narr ) random-nums dup add-digits ;
49 : narr>nhash ( narr -- nhash ) { 1 2 3 4 } swap zip ;
51 : num>hash ( n -- hash )
52 [ digit> ] { } map-as narr>nhash ;
54 :: cow-or-bull ( n g -- arr )
56 { [ n first g at n second = ] [ <bull> ] }
57 { [ n second g value? ] [ <cow> ] }
61 : add-to-score ( arr -- score )
62 <score> [ bull? [ inc-bulls ] [ inc-cows ] if ] reduce ;
64 : check-win ( score -- ? ) bulls>> 4 = ;
66 : sum-score ( n g -- score ? )
67 '[ _ cow-or-bull ] map sift add-to-score dup check-win ;
69 : print-sum ( score -- str )
70 dup bulls>> number>string "Bulls: " swap append swap cows>> number>string
71 " Cows: " swap 3append "\n" append ;
73 : (validate-readln) ( str -- ? ) dup length 4 = not swap [ letter? ] all? or ;
75 : validate-readln ( -- str )
76 readln dup (validate-readln)
77 [ "Invalid input.\nPlease enter a valid 4 digit number: "
78 write flush drop validate-readln ]
81 : win ( -- ) "\nYou've won! Good job. You're so smart." print flush ;
84 "Enter a 4 digit number: " write flush validate-readln num>hash swap
85 [ sum-score swap print-sum print flush ] keep swap not
86 [ main-loop ] [ drop win ] if ;
88 : bulls-and-cows-main ( -- ) new-number drop narr>nhash main-loop ;
90 MAIN: bulls-and-cows-main