Class: RobotsFindKitten::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/robotsfindkitten/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/robotsfindkitten/client.rb', line 8

def initialize
  @message = ''
  @things = []
  @exit = false
  @move_up = false
  @move_down = false
  @move_left = false
  @move_right = false
  @local_service = nil
  @server = nil
  @robot = nil
end

Instance Method Details

#connectObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/robotsfindkitten/client.rb', line 21

def connect
  host = nil
  until host
    print 'Enter host: '
    host = gets
  end
  host.chomp!
  puts "Connecting to #{host}..."
  @local_service = DRb.start_service
  @server = DRbObject.new_with_uri("druby://#{host}:50293")
  puts "Connected."
  until @robot
    name = nil
    until name
      print 'Enter name: '
      name = gets
    end
    puts 'Joining...'
    @robot = @server.join(name.chomp)
    puts 'Name in use' unless @robot
  end
end

#startObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/robotsfindkitten/client.rb', line 44

def start
  Curses.init_screen
  Curses.start_color
  Curses.crmode
  Curses.noecho
  Curses.stdscr.keypad = true
  Curses.curs_set(0)
  Curses.clear
  Curses.setpos(0, 0)
  Curses.addstr(<<eos)
robotsfindkitten #{RobotsFindKitten::VERSION}
By the illustrious Rob Steward (C) 2014
Based on robotfindskitten by Leonard Richardson

In this game, you are robot (#). Your job is to find kitten. This task
is complicated by the existence of various things which are not kitten.
Robot must touch items to determine if they are kitten or not. The game
does not end when robotfindskitten. You may end the game by hitting the
q key or a good old-fashioned Ctrl-C.

See the documentation for more information.

Press any key to start.
eos
  Curses.getch
  Curses.clear
  Curses.timeout = 0

  update_interval = 1.0 / 60.0
  until @exit
    start_time = Time.now
    update
    draw
    input
    end_time = Time.now
    diff = (end_time - start_time)
    if start_time <= end_time && diff < update_interval
      sleep(update_interval - diff)
    end
  end
ensure
  @server.leave(@robot)
  Curses.close_screen
end