robotsfindkitten 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,125 @@
1
+ require 'drb/drb'
2
+ require 'curses'
3
+ require_relative 'version'
4
+ require_relative 'thing_info'
5
+
6
+ module RobotsFindKitten
7
+ class Client
8
+ def initialize
9
+ @message = ''
10
+ @things = []
11
+ @exit = false
12
+ @move_up = false
13
+ @move_down = false
14
+ @move_left = false
15
+ @move_right = false
16
+ @local_service = nil
17
+ @server = nil
18
+ @robot = nil
19
+ end
20
+
21
+ def connect
22
+ host = nil
23
+ until host
24
+ print 'Enter host: '
25
+ host = gets
26
+ end
27
+ host.chomp!
28
+ puts "Connecting to #{host}..."
29
+ @local_service = DRb.start_service
30
+ @server = DRbObject.new_with_uri("druby://#{host}:50293")
31
+ puts "Connected."
32
+ until @robot
33
+ name = nil
34
+ until name
35
+ print 'Enter name: '
36
+ name = gets
37
+ end
38
+ puts 'Joining...'
39
+ @robot = @server.join(name.chomp)
40
+ puts 'Name in use' unless @robot
41
+ end
42
+ end
43
+
44
+ def start
45
+ Curses.init_screen
46
+ Curses.start_color
47
+ Curses.crmode
48
+ Curses.noecho
49
+ Curses.stdscr.keypad = true
50
+ Curses.curs_set(0)
51
+ Curses.clear
52
+ Curses.setpos(0, 0)
53
+ Curses.addstr(<<eos)
54
+ robotsfindkitten #{RobotsFindKitten::VERSION}
55
+ By the illustrious Rob Steward (C) 2014
56
+ Based on robotfindskitten by Leonard Richardson
57
+
58
+ In this game, you are robot (#). Your job is to find kitten. This task
59
+ is complicated by the existence of various things which are not kitten.
60
+ Robot must touch items to determine if they are kitten or not. The game
61
+ does not end when robotfindskitten. You may end the game by hitting the
62
+ q key or a good old-fashioned Ctrl-C.
63
+
64
+ See the documentation for more information.
65
+
66
+ Press any key to start.
67
+ eos
68
+ Curses.getch
69
+ Curses.clear
70
+ Curses.timeout = 0
71
+
72
+ update_interval = 1.0 / 60.0
73
+ until @exit
74
+ start_time = Time.now
75
+ update
76
+ draw
77
+ input
78
+ end_time = Time.now
79
+ diff = (end_time - start_time)
80
+ if start_time <= end_time && diff < update_interval
81
+ sleep(update_interval - diff)
82
+ end
83
+ end
84
+ ensure
85
+ @server.leave(@robot)
86
+ Curses.close_screen
87
+ end
88
+
89
+ private
90
+
91
+ def input
92
+ @move_up = false
93
+ @move_down = false
94
+ @move_left = false
95
+ @move_right = false
96
+ case Curses.getch
97
+ when 'w', Curses::KEY_UP then @move_up = true
98
+ when 'a', Curses::KEY_LEFT then @move_left = true
99
+ when 's', Curses::KEY_DOWN then @move_down = true
100
+ when 'd', Curses::KEY_RIGHT then @move_right = true
101
+ when 'q' then @exit = true
102
+ end
103
+ end
104
+
105
+ def update
106
+ @robot.move_up if @move_up
107
+ @robot.move_down if @move_down
108
+ @robot.move_left if @move_left
109
+ @robot.move_right if @move_right
110
+ @things = @server.things
111
+ @message = @robot.message
112
+ end
113
+
114
+ def draw
115
+ Curses.clear
116
+ @things.each do |thing|
117
+ Curses.setpos(thing.y, thing.x)
118
+ Curses.addch(thing.symbol)
119
+ end
120
+ Curses.setpos(23, 0)
121
+ Curses.addstr(@message)
122
+ Curses.refresh
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'thing'
2
+
3
+ module RobotsFindKitten
4
+ class Kitten < Thing
5
+ def initialize
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'thing'
2
+
3
+ module RobotsFindKitten
4
+ def self.load_nkis(filename)
5
+ array = File.readlines(filename)
6
+ array.delete_if {|line| line.start_with?('#')}
7
+ end
8
+
9
+ NKIS = load_nkis(File.expand_path(File.join(File.dirname(__FILE__),
10
+ '..',
11
+ '..',
12
+ 'data',
13
+ 'vanilla.nki')))
14
+
15
+ class NKI < Thing
16
+ attr_reader :message
17
+
18
+ def initialize
19
+ super
20
+ @message = NKIS.sample
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,48 @@
1
+ require 'drb/drb'
2
+ require_relative 'thing'
3
+
4
+ module RobotsFindKitten
5
+ class Robot < Thing
6
+ include DRb::DRbUndumped
7
+
8
+ attr_reader :message
9
+ attr_accessor :on_find_kitten
10
+
11
+ def initialize(server)
12
+ super()
13
+ @symbol = '#'
14
+ @message = ''
15
+ @server = server
16
+ end
17
+
18
+ def move_up
19
+ move(0, -1)
20
+ end
21
+
22
+ def move_down
23
+ move(0, 1)
24
+ end
25
+
26
+ def move_left
27
+ move(-1, 0)
28
+ end
29
+
30
+ def move_right
31
+ move(1, 0)
32
+ end
33
+
34
+ private
35
+
36
+ def move(dx, dy)
37
+ new_x = @x + dx
38
+ new_y = @y + dy
39
+ message = @server.occupied?(new_x, new_y)
40
+ if message
41
+ @message = message
42
+ else
43
+ @x = new_x % WIDTH
44
+ @y = new_y % HEIGHT
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,83 @@
1
+ require 'thread'
2
+ require 'drb/drb'
3
+ require_relative 'version'
4
+ require_relative 'thing_info'
5
+ require_relative 'nki'
6
+ require_relative 'robot'
7
+ require_relative 'kitten'
8
+
9
+ module RobotsFindKitten
10
+ class Server
11
+ def initialize
12
+ @robots_mutex = Mutex.new
13
+ @robots = {}
14
+ @kitten = nil
15
+ @nkis = []
16
+ puts <<eos
17
+ robotsfindkitten #{RobotsFindKitten::VERSION}
18
+ By the illustrious Rob Steward (C) 2014
19
+ Based on robotfindskitten by Leonard Richardson
20
+
21
+ eos
22
+ puts 'Server started.'
23
+ reset
24
+ end
25
+
26
+ def join(name)
27
+ puts "Join attempt from #{name}"
28
+ robot = Robot.new(self)
29
+ @robots_mutex.synchronize do
30
+ if @robots.has_key?(name)
31
+ puts "Robot named #{name} already in game."
32
+ return
33
+ end
34
+ @robots[name] = robot
35
+ puts "#{name} joined successfully."
36
+ end
37
+ robot
38
+ end
39
+
40
+ def leave(robot)
41
+ @robots_mutex.synchronize do
42
+ name = @robots.key(robot)
43
+ @robots.delete_if {|k, v| v == robot}
44
+ puts "#{name} left the game."
45
+ end
46
+ end
47
+
48
+ def things
49
+ things_internal.map do |thing|
50
+ ThingInfo.new(thing.x, thing.y, thing.symbol)
51
+ end
52
+ end
53
+
54
+ def occupied?(x, y)
55
+ thing = things_internal.find do |thing|
56
+ thing.x == x && thing.y == y
57
+ end
58
+ message = nil
59
+ case thing
60
+ when Kitten
61
+ message = 'You found kitten! Way to go, robot!'
62
+ reset
63
+ when Robot
64
+ message = 'It\'s another robot looking for kitten. Good luck, robot!'
65
+ when NKI
66
+ message = thing.message
67
+ end
68
+ message
69
+ end
70
+
71
+ private
72
+
73
+ def things_internal
74
+ (@robots.values + [@kitten] + @nkis)
75
+ end
76
+
77
+ def reset
78
+ @kitten = Kitten.new
79
+ @nkis = (0...20).map {|i| NKI.new}
80
+ puts 'Game reset.'
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,17 @@
1
+ module RobotsFindKitten
2
+ WIDTH = 80
3
+ HEIGHT = 23
4
+ ALPHABET = ('!'..'~').to_a
5
+
6
+ class Thing
7
+ attr_reader :symbol, :x, :y
8
+
9
+ def initialize
10
+ symbol = '#'
11
+ symbol = ALPHABET.sample while symbol == '#'
12
+ @symbol = symbol
13
+ @x = rand(WIDTH)
14
+ @y = rand(HEIGHT)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module RobotsFindKitten
2
+ class ThingInfo
3
+ attr_reader :x, :y, :symbol
4
+
5
+ def initialize(x, y, symbol)
6
+ @x = x
7
+ @y = y
8
+ @symbol = symbol
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module RobotsFindKitten
2
+ VERSION = '0.0.2'
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robotsfindkitten
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Rob Steward
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curses
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: |-
28
+ In this game, you are robots (#). Your job is to find kitten. This task
29
+ is complicated by the existence of various things which are not kitten.
30
+ Robots must touch items to determine if they are kitten or not. The game
31
+ ends when robotfindskitten.
32
+ email: bobert_1234567890@hotmail.com
33
+ executables:
34
+ - rfk-client
35
+ - rfk-server
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - LICENSE
40
+ - README.md
41
+ - bin/rfk-client
42
+ - bin/rfk-server
43
+ - data/vanilla.nki
44
+ - lib/robotsfindkitten/client.rb
45
+ - lib/robotsfindkitten/kitten.rb
46
+ - lib/robotsfindkitten/nki.rb
47
+ - lib/robotsfindkitten/robot.rb
48
+ - lib/robotsfindkitten/server.rb
49
+ - lib/robotsfindkitten/thing.rb
50
+ - lib/robotsfindkitten/thing_info.rb
51
+ - lib/robotsfindkitten/version.rb
52
+ homepage: https://github.com/filialpails/robotsfindkitten
53
+ licenses:
54
+ - GPL-3.0
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.2.1
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Multi-user zen simulation.
76
+ test_files: []
77
+ has_rdoc: