bloodymines 0.1.2 → 0.1.3

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.
Files changed (3) hide show
  1. data/bin/bloodymines +30 -54
  2. data/lib/bloodymines.rb +1 -2
  3. metadata +3 -3
data/bin/bloodymines CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- # bloodymines -- the executable terminal script for the bloodymines gem
3
- require File.dirname(__FILE__) + "/../lib/minefield"
2
+ # An example of how to use the bloodymines game egnine within a curses program.
3
+
4
+ require File.dirname(__FILE__) + "/../lib/bloodymines"
4
5
  require "curses"
5
6
  include Curses
6
7
 
@@ -16,46 +17,22 @@ module Curses
16
17
  end
17
18
  end
18
19
 
19
- class Game
20
+ # Here I will extend the Bloodymines class with the displaying methods for a
21
+ # text UI
22
+ class Bloodymines
23
+ SPACING, X_OFFSET, Y_OFFSET = 1, 1, 1
20
24
  attr_accessor :cursor_x, :cursor_y
21
- attr_reader :mines, :x_max, :y_max
22
- def initialize(space = 1, x_offset = 1, y_offset = 1)
23
- @space, @x_offset, @y_offset = space, x_offset, y_offset
24
- @cursor_x, @cursor_y = 0, 0
25
-
26
- puts "Welcome to bloody mines!"
27
- print "Select your difficulty: [1]beginner, [2]intermediate, [3]expert => "
28
- difficulty = gets.chomp.to_i
29
- difficulty = case difficulty
30
- when 2: :intermediate
31
- when 3: :expert
32
- else :beginner
33
- end
34
- @mines = MineField.new(:difficulty => difficulty)
35
- @x_max = @mines.fields.size - 1
36
- @y_max = @mines.fields[0].size - 1
37
- end
38
-
39
- def score
40
- score = 0
41
- @mines.fields.each do |rows|
42
- rows.each do |cell|
43
- score += cell if cell.kind_of?(Integer)
44
- end
45
- end
46
- score
47
- end
48
25
 
49
26
  def draw_field
50
27
  clear
51
- 0.upto(@x_max) do |x|
52
- 0.upto(@y_max) do |y|
28
+ 0.upto(@width - 1) do |x|
29
+ 0.upto(@height - 1) do |y|
53
30
  # curses swaps x and y
54
- setpos(y + @y_offset, x + @x_offset + spacing(x, @space))
55
- field = case @mines.fields[x][y]
31
+ setpos(y + Y_OFFSET, x + X_OFFSET + spacing(x, SPACING))
32
+ field = case @minefield.fields[x][y]
56
33
  when false, :mine: "."
57
34
  when :boom: "X"
58
- else @mines.fields[x][y].to_s
35
+ else @minefield.fields[x][y].to_s
59
36
  end
60
37
  addstr(field)
61
38
  end
@@ -63,7 +40,7 @@ class Game
63
40
  draw_cursor
64
41
  setpos(0, 1)
65
42
  addstr("Score: #{score}")
66
- setpos(@y_max + 3, 1)
43
+ setpos(@height + 2, 1)
67
44
  addstr("Use the arrow keys to move around.\n Hit Space to reveal a field. Q or ESC quits the game.")
68
45
  refresh
69
46
  end
@@ -73,8 +50,8 @@ class Game
73
50
  end
74
51
 
75
52
  def draw_cursor
76
- x = @cursor_x + @x_offset + spacing(@cursor_x, @space)
77
- y = @cursor_y + @y_offset
53
+ x = @cursor_x + X_OFFSET + spacing(@cursor_x, SPACING)
54
+ y = @cursor_y + Y_OFFSET
78
55
  setpos(y, x - 1)
79
56
  addstr("[")
80
57
  setpos(y, x + 1)
@@ -86,29 +63,27 @@ class Game
86
63
  when :up
87
64
  @cursor_y -= 1 if @cursor_y > 0
88
65
  when :down
89
- @cursor_y += 1 if @cursor_y < @y_max
66
+ @cursor_y += 1 if @cursor_y < @height - 1
90
67
  when :left
91
68
  @cursor_x -= 1 if @cursor_x > 0
92
69
  when :right
93
- @cursor_x += 1 if @cursor_x < @x_max
70
+ @cursor_x += 1 if @cursor_x < @width - 1
94
71
  end
95
72
  end
73
+ end
96
74
 
97
- def ended?
98
- uncovered = 0
99
- @mines.fields.each do |rows|
100
- rows.each do |cell|
101
- uncovered += 1 if cell == false
102
- return true if cell == :boom
103
- end
104
- end
105
- return false if uncovered > 0
106
- true
107
- end
75
+ # main app
76
+ puts "Welcome to bloodymines!"
77
+ print "Choose your difficulty: [1]Beginner, [2]Intermediate, [3]Expert > "
78
+
79
+ case gets.chomp.to_i
80
+ when 2: game = Bloodymines.new(:difficulty => :intermediate)
81
+ when 3: game = Bloodymines.new(:difficulty => :expert)
82
+ else game = Bloodymines.new
108
83
  end
109
84
 
110
- # curses app
111
- game = Game.new
85
+ game.cursor_x = 0
86
+ game.cursor_y = 0
112
87
 
113
88
  Curses.program do |scr|
114
89
  while true
@@ -118,7 +93,7 @@ Curses.program do |scr|
118
93
  when Key::DOWN : game.move(:direction => :down)
119
94
  when Key::LEFT : game.move(:direction => :left)
120
95
  when Key::RIGHT : game.move(:direction => :right)
121
- when 32 : game.mines.uncover_field(game.cursor_x, game.cursor_y)
96
+ when 32 : game.uncover(game.cursor_x, game.cursor_y)
122
97
  when 27, ?q, ?Q : break
123
98
  end
124
99
  if game.ended?
@@ -129,6 +104,7 @@ Curses.program do |scr|
129
104
  break
130
105
  end
131
106
  end
107
+
132
108
  close_screen
133
109
  puts "Your final score was: #{game.score}"
134
110
  end
data/lib/bloodymines.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "minefield"
1
+ require File.dirname(__FILE__) + "/minefield"
2
2
 
3
3
  class Bloodymines
4
4
  attr_reader :minefield, :x_max, :y_max
@@ -39,5 +39,4 @@ class Bloodymines
39
39
  return false if uncovered > 0
40
40
  true
41
41
  end
42
-
43
42
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bloodymines
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - !binary |