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

#1 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • 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 )
    readln dup (validate-readln)
    [ "Invalid input.\nPlease enter a valid 4 digit number: "
      write flush drop validate-readln ]
    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

#2 YamNad  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 02:23 AM

View Postdom96, 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.
Was This Post Helpful? 0
  • +
  • -

#6 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • 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 :P

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

Was This Post Helpful? 0
  • +
  • -

#7 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: An implementation of Bulls and Cows in Factor

Posted 23 February 2010 - 03:46 PM

...Stack-based is a paradigm.
Was This Post Helpful? 1
  • +
  • -

#10 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • 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'.
Was This Post Helpful? 0
  • +
  • -

#11 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#12 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • 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

Paradigm multi-paradigm: concatenative, stack-oriented


Why do you insist on arguing with me?
Was This Post Helpful? 0
  • +
  • -

#13 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1