Class: RubySnake::Server
- Inherits:
-
Object
- Object
- RubySnake::Server
- Defined in:
- lib/ruby_snake/server.rb
Class Attribute Summary collapse
-
.message ⇒ Object
Returns the value of attribute message.
-
.receive ⇒ Object
Returns the value of attribute receive.
Class Method Summary collapse
Class Attribute Details
.message ⇒ Object
Returns the value of attribute message.
6 7 8 |
# File 'lib/ruby_snake/server.rb', line 6 def @message end |
.receive ⇒ Object
Returns the value of attribute receive.
6 7 8 |
# File 'lib/ruby_snake/server.rb', line 6 def receive @receive end |
Class Method Details
.start ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruby_snake/server.rb', line 8 def start @message = '0' server = TCPServer.new 2000 UI::Two.init_windows Ncurses.nodelay UI::Two.window, true begin system 'clear' puts '=' * 40 puts 'Waiting for a connection...' puts '=' * 40 loop do client = server.accept if client = client.recv(4096) client.send @message, 0 case when '0' @message = ([Game.snake.food] + Game.snake.body).join(',').to_s if Game.snake when '1' Game.win when '2' Game.lose else @message = ([Game.snake.food] + Game.snake.body).join(',').to_s if Game.snake @receive = .split(',').map(&:to_i).each_slice(2).to_a if end UI::Two.draw_map operation = Game.listen_operation if Game.lose? Game.lose! break elsif Game.win? Game.win! break elsif !UI::Two.snake.move(operation) @message = '1' elsif UI::Two.score == 0 @message = '2' end end end ensure Ncurses.endwin end end |