Changeset 185

Show
Ignore:
Timestamp:
11/18/04 09:05:44 (4 years ago)
Author:
ged
Message:

-- Oops. Misunderstood epic3 code. Re-arranging, now with the power of coffee!

Location:
branches/simplest-thing/acceptance
Files:
1 removed
1 moved

Legend:

Unmodified
Added
Removed
  • branches/simplest-thing/acceptance/lib/game.rb

    r184 r185  
    11#!/usr/bin/evn ruby 
     2 
     3BEGIN { 
     4    $basedir = File::dirname( File::dirname(__FILE__) ) 
     5    %w{acceptance/lib lib}.each do |dir| 
     6        $LOAD_PATH.unshift File::join( $basedir, dir ) 
     7    end 
     8} 
     9 
     10require "textui" 
     11require "character" 
    212 
    313DESCRIPTION = <<END 
     
    515 
    616END 
    7 PROMPT      = "FaerieMUD>" 
    8 NEW_CHAR    = "Starting a new character." 
     17PROMPT      = "FaerieMUD>" 
     18NEW_CHAR    = "Starting a new character." 
    919 
    1020RACE_CHOOSE = "Please choose from dwarf, human, pixie" 
    11 RACE        = "dwarf" 
    12 CHOSEN      = "Your character is a %s." 
    13 INVALID     = "Invalid species." 
     21RACE        = "dwarf" 
     22CHOSEN      = "Your character is a %s." 
     23INVALID     = "Invalid species." 
    1424 
    15 ASK_NAME    = "What is your character's name?"    #" 
    16 PUNCHBAG    = "Punchbag" 
     25ASK_NAME    = "What is your character's name?"    #" 
     26PUNCHBAG    = "Punchbag" 
    1727NO_PUNCHBAG = "You may not be named 'Punchbag'." 
    18 NAME        = "Bob" 
     28NAME        = "Bob" 
    1929 
    20 RESUME      = "Resuming play as %s, the %s." % [NAME, RACE] 
     30RESUME      = "Resuming play as %s, the %s." % [NAME, RACE] 
    2131 
    22 LOOK        = "look" 
    23 SEE_WHAT    = %q[You are locked in the formless void with an ugly orc.  He says, "Hi, I'm Punchbag".] 
    24 WHOAMI      = "who am I?" 
    25 WHOAMME     = "me?" 
    26 IAM         = "You are %s." % NAME 
    27 OTHER       = "meow" 
    28 OTHER_WHAT  = "The formless void stares vacantly back at you.  The orc's stare is more hungry than vacant." 
     32LOOK        = "look" 
     33SEE_WHAT    = %q[You are locked in the formless void with an ugly orc.  He says, "Hi, I'm Punchbag".] 
     34WHOAMI      = "who am I?" 
     35WHOAMME     = "me?" 
     36IAM         = "You are %s." % NAME 
     37OTHER       = "meow" 
     38OTHER_WHAT  = "The formless void stares vacantly back at you.  The orc's stare is more hungry than vacant." 
    2939 
    30 QUIT        = "quit" 
    31 GOODBYE     = "Goodbye." 
     40QUIT        = "quit" 
     41GOODBYE     = "Goodbye." 
    3242 
    33 HIT1        = "hit Punchbag" 
    34 HIT2        = "hit punchbag" 
    35 HIT3        = "hit orc" 
    36 SMACK       = "You smack the orc." 
     43HIT1        = "hit Punchbag" 
     44HIT2        = "hit punchbag" 
     45HIT3        = "hit orc" 
     46SMACK       = "You smack the orc." 
    3747UNCONSCIOUS = "The orc collapses into unconsciousness." 
    3848 
     49 
    3950class Game 
     51    include TextUI 
    4052 
    41     def initialize  
    42         @io = IO::popen('ruby bin/faeriemud.rb', IO::NONBLOCK | IO::RDWR) 
    43     end 
     53    def initialize  
     54        IO::popen("-", IO::NONBLOCK | IO::RDWR) {|io| 
    4455 
    45     attr :io 
     56            if io # Parent 
     57                @io = io 
     58            else # Child 
     59                Game.play 
     60            end 
     61        } 
     62    end 
    4663 
    47     def puts(line) 
    48         @io.puts(line) 
    49     end 
     64    attr :io 
    5065 
    51     def gets  
    52         @io.gets 
    53     end 
     66    def puts(line) 
     67        @io.puts(line) 
     68    end 
    5469 
    55     def read  
    56         @io.read 
    57     end 
     70    def gets  
     71        @io.gets 
     72    end 
    5873 
    59     def close  
    60         if select([],[@io],[],1) 
    61             self.puts(RACE) 
    62         end 
    63         if select([],[@io],[],1) 
    64             self.puts(NAME) 
    65         end 
    66         if select([],[@io],[],1) 
    67             self.puts(QUIT) 
    68         end 
    69         if select([@io],[],[],1) 
    70             self.read 
    71         end 
    72         @io.close 
    73     end 
     74    def read  
     75        @io.read 
     76    end 
    7477 
    75     def Game.preset  
    76         Game.reset 
    77         game = Game.new 
    78         game.close 
    79         return Game.new 
    80     end 
     78    def close  
     79        if select([],[@io],[],1) 
     80            self.puts(RACE) 
     81        end 
     82        if select([],[@io],[],1) 
     83            self.puts(NAME) 
     84        end 
     85        if select([],[@io],[],1) 
     86            self.puts(QUIT) 
     87        end 
     88        if select([@io],[],[],1) 
     89            self.read 
     90        end 
     91        @io.close 
     92    end 
    8193 
    82     def Game.reset 
    83         `ruby bin/faeriemud.rb --reset` 
    84     end 
     94    def Game.preset  
     95        Game.reset 
     96        game = Game.new 
     97        game.close 
     98        return Game.new 
     99    end 
    85100 
     101    def Game.reset 
     102        Character.reset 
     103        $stderr.puts "Game reset." 
     104    end 
     105 
     106    def Game.play 
     107        message <<END 
     108        You are in the formless void known as FaerieMUD 1.0. 
     109 
     110        END 
     111 
     112        character = Character.load 
     113 
     114        if character 
     115            message "Resuming play as %s, the %s." % [character.name, character.race] 
     116        else 
     117            message "Starting a new character." 
     118            race = prompt( "Please choose from dwarf, human, pixie", 
     119                              "Invalid species." ) {|s| 
     120                %w[dwarf human pixie].include?( s ) 
     121            } 
     122            message "Your character is a %s." % race 
     123            name = prompt( "What is your character's name?",    #" 
     124                           "You may not be named 'Punchbag'." ) {|n| 
     125                ! %w[Punchbag].include?( n ) 
     126            } 
     127            character = Character.new(race, name) 
     128            character.save 
     129        end 
     130 
     131        playing = true 
     132        orc = 10 
     133        while playing 
     134            command = prompt( "FaerieMUD>" ) 
     135            case command 
     136            when "who am I?", "me?"             #" 
     137                message "You are %s." % character.name 
     138            when "look" 
     139                message %q[You are locked in the formless void with an ugly orc.  He says, "Hi, I'm Punchbag".] 
     140            when %r{hit ([pP]unchbag|orc)} 
     141                if orc > 0 
     142                    message "You smack the orc." 
     143                    orc -= 1 
     144                else 
     145                    message "The orc collapses into unconsciousness." 
     146                end 
     147            when "quit" 
     148                message "Goodbye." 
     149                playing = false 
     150            else 
     151                message "The formless void stares vacantly back at you.  The orc's stare is more hungry than vacant." 
     152            end 
     153        end 
     154 
     155    end 
    86156end