snakegame 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af3fe354355283106bf69540e90b1538e9e6ed87
4
- data.tar.gz: 1adedd1a57e19db0f24366ddd8aaad1a4f63c749
3
+ metadata.gz: b6d873a1e55d734c98cce193abefce89357a3d78
4
+ data.tar.gz: ee7c745add2f13dfecf5f1df5477bd8134d18b8e
5
5
  SHA512:
6
- metadata.gz: 985048076a795e91b34e81d4691b64eae91ebb405545a633b535b947890f6b48e0e301284b8a62ac1da326d6ae2afd7d7b5044c035d3767631d80aab884e972b
7
- data.tar.gz: 547d516c9005449d8195e2410fef3cf41363603714a721ca97a936c02964cc0279d580dcc57b6c619891d839bfee265fed50c903cc7f6838dbd28eb8e6aa9693
6
+ metadata.gz: 16f9d5600ed64fc214c1498ba0d82a5aab2b8daa139cd19a7ce3016e896fd439a141bcb876a209f34192919ab07856e782f63ad0ae1132c78da1cd2310428980
7
+ data.tar.gz: 54f14c37395dfba240f5c25a4c06b7aadada8d15e34c1f6edf2782c07ade5c4549b60339b802cf0fd027260d0808330ff88d8da48333c850c1e0489ae50f8bc3
data/Rakefile CHANGED
@@ -4,3 +4,10 @@ Rake::TestTask.new do |t|
4
4
  t.test_files = FileList['test/*.rb']
5
5
  end
6
6
  task :default => :test
7
+
8
+ task :rebuild do
9
+ %x{gem uninstall snakegame
10
+ rm snakegame*.gem
11
+ gem build snakegame.gemspec
12
+ gem install ./snakegame*.gem}
13
+ end
@@ -46,7 +46,7 @@ class Board
46
46
  @win << 'o'
47
47
  end
48
48
  @win.setpos(*fruit.graphics_to_a)
49
- @win << '🍎'
49
+ @win << '🍏'
50
50
  @win.refresh
51
51
  end
52
52
 
@@ -6,23 +6,25 @@ require 'curses'
6
6
  class IllegalStateError < StandardError; end
7
7
 
8
8
  class Game
9
+ attr :board, :snake
9
10
  def initialize
10
11
  @board = Board.new
11
- @snake = Snake.new(@board.initialize_snake)
12
+ @snake = Snake.new(board.initialize_snake)
12
13
  end
14
+
13
15
  def play
14
- food = @board.random_position
15
- while true
16
+ food = board.random_position
17
+ Kernel.loop do
16
18
  change = Curses.getch
17
- @snake.process_direction(change)
19
+ snake.process_direction(change)
18
20
  begin
19
- food = @snake.tick(food)
20
- raise unless @board.is_legal?(@snake.positions)
21
+ food = snake.tick(food)
22
+ raise unless board.is_legal?(snake.positions)
21
23
  rescue IllegalStateError
22
- raise "You died!"
24
+ raise 'You died!'
23
25
  end
24
- food ||= @board.random_position
25
- @board.draw(snake: @snake, fruit: food)
26
+ food ||= board.random_position
27
+ board.draw(snake: snake, fruit: food)
26
28
  sleep 0.1
27
29
  end
28
30
  end
@@ -31,6 +31,7 @@ class Snake
31
31
  @vector = vector
32
32
  end
33
33
  end
34
+
34
35
  DIRECTION_HASH = {
35
36
  # We are in graphics land, so 0, 0 is the origin,
36
37
  # and the y axis goes down _positively_
@@ -64,7 +65,7 @@ class Snake
64
65
 
65
66
  def handle_food(food, next_head_position)
66
67
  if next_head_position.eql? food
67
- 5.times { add_body_part }
68
+ 2.times { add_body_part }
68
69
  return nil
69
70
  end
70
71
  food
@@ -13,5 +13,5 @@ Gem::Specification.new do |gem|
13
13
  gem.name = "snakegame"
14
14
  gem.add_runtime_dependency "curses"
15
15
  gem.require_paths = ["lib"]
16
- gem.version = "0.0.3"
16
+ gem.version = "0.0.4"
17
17
  end
@@ -4,6 +4,7 @@ class BoardTest < Minitest::Test
4
4
  def board
5
5
  @board ||= Board.new(width: 20, height: 20)
6
6
  end
7
+
7
8
  def test_is_legal?
8
9
  refute board.is_legal?([Point.new(21, 21)])
9
10
  assert board.is_legal?([Point.new(15, 15)])
@@ -4,11 +4,13 @@ describe Snake do
4
4
  let(:point_one) { Point.new(5, 5) }
5
5
  let(:point_two) { Point.new(3, 5) }
6
6
  let(:snake) { Snake.new(starting_position: Point.new(5, 5), vector: Point.new(1, 1)) }
7
+
7
8
  it 'tests handle_food with food' do
8
9
  food = snake.handle_food(point_one, point_one)
9
10
  assert food.nil?
10
11
  assert snake.body.length > 1
11
12
  end
13
+
12
14
  it 'tests handle_food without food' do
13
15
  food = snake.handle_food(point_one, point_two)
14
16
  assert food == point_one
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snakegame
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Steger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-12 00:00:00.000000000 Z
11
+ date: 2017-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses
@@ -37,7 +37,6 @@ files:
37
37
  - Gemfile.lock
38
38
  - Rakefile
39
39
  - bin/snake
40
- - board.rb
41
40
  - lib/board.rb
42
41
  - lib/game.rb
43
42
  - lib/point.rb
data/board.rb DELETED
@@ -1,61 +0,0 @@
1
- class Board
2
- class CursesBoard
3
- attr_accessor :win
4
- def initialize
5
- Curses.stdscr.keypad(true)
6
- @win = Curses::Window.new(height, width, 0, 0)
7
- Curses.stdscr.keypad(true)
8
- @win.box('|', '-')
9
- Curses.init_screen
10
- Curses.curs_set(0)
11
- Curses.crmode
12
- Curses.noecho
13
- Curses.stdscr.nodelay = true
14
- end
15
- end
16
- attr_accessor :width, :height
17
- def initialize(width: 60, height: 30)
18
- @top_right = Point.new(width, 0)
19
- @bottom_left = Point.new(0, height)
20
-
21
- @width = width
22
- @height = height
23
- end
24
-
25
- def initialize_snake
26
- x, y = @width / 2, @height / 2
27
- # TODO Make this adjust based on survival likelihood
28
- return { starting_position: Point.new(x, y), vector: Point.new(0, 1) }
29
- end
30
-
31
- def is_legal?(positions)
32
- positions.all? do |position|
33
- position.x > @bottom_left.x &&
34
- position.x < @top_right.x - 1 &&
35
- position.y > @top_right.y &&
36
- position.y < @bottom_left.y - 1
37
- end
38
- end
39
-
40
- def draw(snake:, fruit:)
41
- @curses_board ||= CursesBoard.new(@height, @width)
42
- @curses_board.win.clear
43
- @curses_board.win.box('|', '-')
44
- Curses.init_screen
45
-
46
- current_pos = snake.head
47
- @curses_board.win.setpos(*current_pos.graphics_to_a)
48
- @curses_board.win << snake.head_direction
49
- snake.body.each do |body_part|
50
- @curses_board.win.setpos(*body_part.graphics_to_a)
51
- @curses_board.win << 'o'
52
- end
53
- @curses_board.win.setpos(*fruit.graphics_to_a)
54
- @curses_board.win << '🍎'
55
- @curses_board.win.refresh
56
- end
57
-
58
- def random_position
59
- Point.new(Random.rand(width - 2) + 1, Random.rand(height - 2) + 1)
60
- end
61
- end