Changeset 187

Show
Ignore:
Timestamp:
11/18/04 10:12:13 (4 years ago)
Author:
ged
Message:

-- Made epic3 code pass its tests again.

Location:
branches/simplest-thing/acceptance
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/simplest-thing/acceptance/epic3.tests.rb

    r186 r187  
    11#!/usr/bin/env ruby -w 
    22 
     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 
    310require 'test/unit' 
    4 $: << File.dirname(__FILE__) 
    511require 'game' 
    612 
     
    814 
    915    def setup  
    10         Game.reset 
    1116        @game = Game.preset 
    1217    end 
    1318 
    1419    def teardown  
    15         @game.close 
     20        @game = nil 
    1621    end 
    1722 
     
    2328    def test_010_hit  
    2429        data = "" 
    25         data = @game.gets until data.include?( PROMPT ) 
     30        @game.play {|player| 
     31            data = player.gets until data.include?( PROMPT ) 
    2632 
    27         #    - if i type "hit Punchbag", "hit orc", or "hit punchbag", i am told 
    28         #      "You smack the orc." 
    29         @game.puts( HIT1 ) 
    30         assert                            @game.gets.include?( SMACK ), 
    31                                           "Hitting Punchbag did not work." 
    32         @game.gets # prompt 
    33         @game.puts( HIT2 ) 
    34         assert                            @game.gets.include?( SMACK ), 
    35                                           "Hitting Punchbag did not work." 
    36         @game.gets # prompt 
    37         @game.puts( HIT3 ) 
    38         assert                            @game.gets.include?( SMACK ), 
    39                                           "Hitting Punchbag did not work." 
    40         @game.gets # prompt 
    41          
     33            #    - if i type "hit Punchbag", "hit orc", or "hit punchbag", i am told 
     34            #      "You smack the orc." 
     35            player.puts( HIT1 ) 
     36            assert                            player.gets.include?( SMACK ), 
     37                                              "Hitting Punchbag did not work." 
     38            player.gets # prompt 
     39            player.puts( HIT2 ) 
     40            assert                            player.gets.include?( SMACK ), 
     41                                              "Hitting Punchbag did not work." 
     42            player.gets # prompt 
     43            player.puts( HIT3 ) 
     44            assert                            player.gets.include?( SMACK ), 
     45                                              "Hitting Punchbag did not work." 
     46            player.gets # prompt 
    4247 
    43         #    - after typing 'hit orc' over and over, i eventually want to be 
    44         #      told "The orc collapses into unconsciousness." 
    45         limit = 10 
    46         unconscious = false 
    47         limit.times do 
    48             @game.puts( HIT1 ) 
    49             if @game.gets.include?( UNCONSCIOUS ) 
    50                 unconscious = true 
    51                 break 
    52             end 
    53             @game.gets # prompt 
    54         end 
    55         assert                            unconscious, 
    56                                           "The orc did not fall unconscious, even after #{limit} hits." 
     48 
     49            #    - after typing 'hit orc' over and over, i eventually want to be 
     50            #      told "The orc collapses into unconsciousness." 
     51            limit = 10 
     52            unconscious = false 
     53            limit.times do 
     54                player.puts( HIT1 ) 
     55                if player.gets.include?( UNCONSCIOUS ) 
     56                    unconscious = true 
     57                    break 
     58                end 
     59                player.gets # prompt 
     60            end 
     61            assert                            unconscious, 
     62                                              "The orc did not fall unconscious, even after #{limit} hits." 
     63        } 
    5764    end 
    5865 
  • branches/simplest-thing/acceptance/lib/game.rb

    r185 r187  
    1010require "textui" 
    1111require "character" 
     12require "forwardable" 
    1213 
    1314DESCRIPTION = <<END 
     
    5152    include TextUI 
    5253 
    53     def initialize  
    54         IO::popen("-", IO::NONBLOCK | IO::RDWR) {|io| 
     54    class Player 
     55        extend Forwardable 
    5556 
    56             if io # Parent 
    57                 @io = io 
    58             else # Child 
    59                 Game.play 
    60             end 
    61         } 
    62     end 
     57        def initialize( io ) 
     58            @io = io 
     59        end 
    6360 
    64     attr :io 
     61        def_delegators :@io, :puts, :gets, :read, :close, :closed? 
    6562 
    66     def puts(line) 
    67         @io.puts(line) 
    68     end 
     63    end # class Game::Player 
    6964 
    70     def gets  
    71         @io.gets 
    72     end 
    73  
    74     def read  
    75         @io.read 
    76     end 
    77  
    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 
    9365 
    9466    def Game.preset  
    95         Game.reset 
    96         game = Game.new 
    97         game.close 
    98         return Game.new 
     67        Character.reset 
     68        character = Character.new(RACE, NAME) 
     69        character.save 
     70        Game.new 
    9971    end 
    10072 
     
    10476    end 
    10577 
    106     def Game.play 
    107         message <<END 
     78 
     79    def initialize 
     80        @character = Character::load 
     81    end 
     82 
     83    attr_accessor :character 
     84 
     85    def play 
     86        IO::popen("-", IO::RDWR) {|io| 
     87            if io # Parent 
     88                player = Game::Player::new(io) 
     89                yield( player ) 
     90            else # Child 
     91                self.run 
     92            end 
     93        } 
     94    end 
     95 
     96    def run 
     97        message <<-END 
    10898        You are in the formless void known as FaerieMUD 1.0. 
    10999