An implementation of Bulls and Cows in Factor

Page 1 of 1

12 Replies - 2993 Views - Last Post: 25 February 2010 - 03:26 AM

#1 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

An implementation of Bulls and Cows in Factor

Posted 21 February 2010 - 08:14 AM

It's a relatively small command-line based application written in Factor. It could probably be smaller, but this is my first decent Factor app, and I think I did pretty good.

It's an implementation of the Bulls and Cows game.

```USING: accessors assocs combinators fry grouping hashtables kernel
locals math math.parser math.ranges random sequences strings
io ascii ;

IN: bullsncows

TUPLE: score bulls cows ;
: <score> ( -- score ) 0 0 score boa ;

TUPLE: cow ;
: <cow> ( -- cow ) cow new ;

TUPLE: bull ;
: <bull> ( -- bull ) bull new ;

: inc-bulls ( score -- score ) dup bulls>> 1 + >>bulls ;
: inc-cows ( score -- score ) dup cows>> 1 + >>cows ;

: random-nums ( -- seq ) 9 [1,b] 4 sample ;

: add-digits ( seq -- n ) 0 [ swap 10 * + ] reduce number>string ;

: new-number ( -- n narr ) random-nums dup add-digits ;

: narr>nhash ( narr -- nhash ) { 1 2 3 4 } swap zip ;

: num>hash ( n -- hash )
[ 1string string>number ] { } map-as narr>nhash ;

:: cow-or-bull ( n g -- arr )
{
{ [ n first g at n second = ] [ <bull> ] }
{ [ n second g value? ] [ <cow> ] }
[ f ]
} cond ;

: add-to-score ( arr -- score )
<score> [ bull? [ inc-bulls ] [ inc-cows ] if ] reduce ;

: check-win ( score -- ? ) bulls>> 4 = ;

: sum-score ( n g -- score ? )
'[ _ cow-or-bull ] map sift add-to-score dup check-win ;

: print-sum ( score -- str )
dup bulls>> number>string "Bulls: " swap append swap cows>> number>string
" Cows: " swap 3append "\n" append ;

: (validate-readln) ( str -- ? ) dup length 4 = not swap [ letter? ] all? or ;

: validate-readln ( -- str )
[ "Invalid input.\nPlease enter a valid 4 digit number: "
when ;

: win ( -- ) "\nYou've won! Good job. You're so smart." print flush ;

: main-loop ( x -- )
"Enter a 4 digit number: " write flush validate-readln num>hash swap
[ sum-score swap print-sum print flush ] keep swap not
[ main-loop ] [ drop win ] if ;

: main ( -- ) new-number drop narr>nhash main-loop ;

MAIN: main

```

Here are some binaries that I compiled on Ubuntu hardy. It will run standalone as you would expect. I can't offer a Windows version because I don't immediately have access to a box to compile them on.

Is This A Good Question/Topic? 1

Replies To: An implementation of Bulls and Cows in Factor

Reputation: 9
• Posts: 120
• Joined: 11-July 09

Re: An implementation of Bulls and Cows in Factor

Posted 21 February 2010 - 08:56 AM

The program works fine - obviously. I can't really judge you on the code though. (yet)

It seems good for a first project in Factor. How are you finding the language?

#3 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

Re: An implementation of Bulls and Cows in Factor

Posted 21 February 2010 - 07:34 PM

I think Factor is a really awesome language. It's fully featured, with a huge standard library, and all sorts of fun to code in. The only thing really hurting Factor right now is lack of a decent GUI toolkit. There are no bindings to any major toolkits, and the one shipped with Factor is missing a lot of common things. Developing a traditional GUI desktop application would be difficult at this point.

#4 dom96

• D.I.C Regular

Reputation: 4
• Posts: 256
• Joined: 29-December 08

Re: An implementation of Bulls and Cows in Factor

Posted 22 February 2010 - 04:16 PM

I think someone is working on GTK bindings, as for the GUI Factor includes i never tested it so i do not know if it's good or not.

And as for this program, nice job, i have no idea how to play Bulls and cows but still lol

#5 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 02:23 AM

dom96, on 22 February 2010 - 03:16 PM, said:

I think someone is working on GTK bindings, as for the GUI Factor includes i never tested it so i do not know if it's good or not.

And as for this program, nice job, i have no idea how to play Bulls and cows but still lol

I'm pretty sure that nobody is working on GTK bindings. I remember there was somebody working on something to automatically generate Factor FFI bindings to C libraries in order to generate bindings for Qt, but that just disappeared. Would have been nice though. Factor really needs a good GUI toolkit.

The Factor UI toolkit isn't bad, as a matter of fact, it's pretty good. The problem is that Slava only develops what he needs for the developer tools, and forgets about everything else. He doesn't feel it's important at this point, I imagine, and expects others to contribute and do it if they care enough. I would do it, but I'm not sure I'll ever be proficient enough in Factor to feel confident in doing it.

As an example of what is missing in the Factor UI toolkit: menubars and radio buttons.

#6 dom96

• D.I.C Regular

Reputation: 4
• Posts: 256
• Joined: 29-December 08

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 09:35 AM

I'm pretty sure someone is working on GTK Bindings, http://github.com/ex...lo-world.factor

But to tell you the truth, i think Factor is way too 'alien' in other words, the syntax is too confusing, all that stack swapping and dupping and all that, it just confuses me.

This post has been edited by dom96: 23 February 2010 - 09:35 AM

#7 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 12:34 PM

It's a different paradigm than you're used to. It's not "too alien", it's just unfamiliar. You have to learn this stuff over time. I think it's pretty fun to learn how to program in an entirely different way than I'm used to. Though, it's not that unfamiliar to me, as Factor hosts some pretty nice FP features and I've done point-free stuff in Clojure and Haskell before.

I mean, I don't expect Factor to ever be anywhere near mainstream, if even well-known. However, I expect it to be a perfectly viable, and useful language once it has bindings to a good UI toolkit. I mean, it's already useful for most things now, and you can even do SOME GUI stuff in it. However, a traditional desktop application is really not in the scope of things at the moment.

As for the GTK bindings, I'll believe it when I see it. The guy hasn't even announced it, so I wouldn't expect it to get anywhere. If it does, I'll be ecstatic.

Thanks for the link, I'll talk to the guy when I can, see if he's serious. If so, he's my hero.

This post has been edited by Raynes: 23 February 2010 - 12:41 PM

#8 dom96

• D.I.C Regular

Reputation: 4
• Posts: 256
• Joined: 29-December 08

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 12:47 PM

Yeah, but still i find other programming languages like Python easier to get used to then Factor.
Also i don't think its the paradigm that's unfamilliar to me, i think that stack based languages are

This post has been edited by dom96: 23 February 2010 - 12:51 PM

#9 programble

Reputation: 51
• Posts: 1,315
• Joined: 21-February 09

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 03:46 PM

#10 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 06:45 PM

What Curtis said is right. Python is easier to you, as it is the sort of thing you're used to. You're sticking Factor in an unfair comparison against languages that you find 'normal'.

#11 dom96

• D.I.C Regular

Reputation: 4
• Posts: 256
• Joined: 29-December 08

Re: An implementation of Bulls and Cows in Factor

Posted 24 February 2010 - 12:52 PM

I'm pretty sure that's not a programming paradigm, nothing is said about it here

#12 programble

Reputation: 51
• Posts: 1,315
• Joined: 21-February 09

Re: An implementation of Bulls and Cows in Factor

Posted 24 February 2010 - 05:21 PM

http://en.wikipedia....ing_language%29

Quote

Why do you insist on arguing with me?

#13 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

Re: An implementation of Bulls and Cows in Factor

Posted 25 February 2010 - 03:26 AM

The point is, stack-oriented programming is a way of thinking. You have to think of programs as a changing data stack.

But, that's beside the point, Factor even has lexical variables.