bloodymines 0.1.3 → 0.1.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.
Files changed (4) hide show
  1. data/bin/bloodymines +8 -65
  2. data/lib/bloodymines.rb +4 -2
  3. data/lib/tui.rb +67 -0
  4. metadata +6 -5
data/bin/bloodymines CHANGED
@@ -2,74 +2,12 @@
2
2
  # An example of how to use the bloodymines game egnine within a curses program.
3
3
 
4
4
  require File.dirname(__FILE__) + "/../lib/bloodymines"
5
- require "curses"
6
- include Curses
7
-
8
- module Curses
9
- def self.program
10
- main_screen = init_screen # init curses
11
- noecho # don't show what you type
12
- trap(0) { echo } # reactivate echo on exit
13
- cbreak # turn off buffered input
14
- curs_set(0) # hide cursor
15
- main_screen.keypad = true # cursor keys are real events
16
- yield main_screen
17
- end
18
- end
5
+ require File.dirname(__FILE__) + "/../lib/tui"
19
6
 
20
7
  # Here I will extend the Bloodymines class with the displaying methods for a
21
8
  # text UI
22
9
  class Bloodymines
23
- SPACING, X_OFFSET, Y_OFFSET = 1, 1, 1
24
- attr_accessor :cursor_x, :cursor_y
25
-
26
- def draw_field
27
- clear
28
- 0.upto(@width - 1) do |x|
29
- 0.upto(@height - 1) do |y|
30
- # curses swaps x and y
31
- setpos(y + Y_OFFSET, x + X_OFFSET + spacing(x, SPACING))
32
- field = case @minefield.fields[x][y]
33
- when false, :mine: "."
34
- when :boom: "X"
35
- else @minefield.fields[x][y].to_s
36
- end
37
- addstr(field)
38
- end
39
- end
40
- draw_cursor
41
- setpos(0, 1)
42
- addstr("Score: #{score}")
43
- setpos(@height + 2, 1)
44
- addstr("Use the arrow keys to move around.\n Hit Space to reveal a field. Q or ESC quits the game.")
45
- refresh
46
- end
47
-
48
- def spacing(x, space)
49
- x * space
50
- end
51
-
52
- def draw_cursor
53
- x = @cursor_x + X_OFFSET + spacing(@cursor_x, SPACING)
54
- y = @cursor_y + Y_OFFSET
55
- setpos(y, x - 1)
56
- addstr("[")
57
- setpos(y, x + 1)
58
- addstr("]")
59
- end
60
-
61
- def move(params={})
62
- case params[:direction]
63
- when :up
64
- @cursor_y -= 1 if @cursor_y > 0
65
- when :down
66
- @cursor_y += 1 if @cursor_y < @height - 1
67
- when :left
68
- @cursor_x -= 1 if @cursor_x > 0
69
- when :right
70
- @cursor_x += 1 if @cursor_x < @width - 1
71
- end
72
- end
10
+ include TUI
73
11
  end
74
12
 
75
13
  # main app
@@ -106,5 +44,10 @@ Curses.program do |scr|
106
44
  end
107
45
 
108
46
  close_screen
109
- puts "Your final score was: #{game.score}"
47
+ if game.result[:finished]
48
+ puts "You completed the game on #{game.difficulty.to_s.upcase}!"
49
+ else
50
+ puts "You did not manage to finish the game."
51
+ end
52
+ puts "Your final score was: #{game.result[:score]}"
110
53
  end
data/lib/bloodymines.rb CHANGED
@@ -17,14 +17,16 @@ class Bloodymines
17
17
  @minefield.difficulty
18
18
  end
19
19
 
20
- def score
20
+ def result
21
21
  score = 0
22
+ finished = true
22
23
  @minefield.fields.each do |rows|
23
24
  rows.each do |cell|
24
25
  score += cell if cell.kind_of?(Integer)
26
+ finished = false if cell == false || cell == :boom
25
27
  end
26
28
  end
27
- score
29
+ {:score => score, :finished => finished}
28
30
  end
29
31
 
30
32
  # returns true if you triggered a mine or uncovered all save fields
data/lib/tui.rb ADDED
@@ -0,0 +1,67 @@
1
+ require "curses"
2
+ include Curses
3
+
4
+ module Curses
5
+ def self.program
6
+ main_screen = init_screen # init curses
7
+ noecho # don't show what you type
8
+ trap(0) { echo } # reactivate echo on exit
9
+ cbreak # turn off buffered input
10
+ curs_set(0) # hide cursor
11
+ main_screen.keypad = true # cursor keys are real events
12
+ yield main_screen
13
+ end
14
+ end
15
+
16
+ module TUI
17
+ SPACING, X_OFFSET, Y_OFFSET = 1, 1, 1
18
+ attr_accessor :cursor_x, :cursor_y
19
+
20
+ def draw_field
21
+ clear
22
+ 0.upto(@width - 1) do |x|
23
+ 0.upto(@height - 1) do |y|
24
+ # curses swaps x and y
25
+ setpos(y + Y_OFFSET, x + X_OFFSET + spacing(x, SPACING))
26
+ field = case @minefield.fields[x][y]
27
+ when false, :mine: "."
28
+ when :boom: "X"
29
+ else @minefield.fields[x][y].to_s
30
+ end
31
+ addstr(field)
32
+ end
33
+ end
34
+ draw_cursor
35
+ setpos(0, 1)
36
+ addstr("Score: #{result[:score]}")
37
+ setpos(@height + 2, 1)
38
+ addstr("Use the arrow keys to move around.\n Hit Space to reveal a field. Q or ESC quits the game.")
39
+ refresh
40
+ end
41
+
42
+ def spacing(x, space)
43
+ x * space
44
+ end
45
+
46
+ def draw_cursor
47
+ x = @cursor_x + X_OFFSET + spacing(@cursor_x, SPACING)
48
+ y = @cursor_y + Y_OFFSET
49
+ setpos(y, x - 1)
50
+ addstr("[")
51
+ setpos(y, x + 1)
52
+ addstr("]")
53
+ end
54
+
55
+ def move(params={})
56
+ case params[:direction]
57
+ when :up
58
+ @cursor_y -= 1 if @cursor_y > 0
59
+ when :down
60
+ @cursor_y += 1 if @cursor_y < @height - 1
61
+ when :left
62
+ @cursor_x -= 1 if @cursor_x > 0
63
+ when :right
64
+ @cursor_x += 1 if @cursor_x < @width - 1
65
+ end
66
+ end
67
+ 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: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - !binary |
@@ -17,12 +17,12 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-08-18 00:00:00 +02:00
20
+ date: 2010-08-19 00:00:00 +02:00
21
21
  default_executable:
22
22
  dependencies: []
23
23
 
24
24
  description: Play a minesweeper-like game in your terminal
25
- email: darkoem@gmal.com
25
+ email: darkoem@gmail.com
26
26
  executables:
27
27
  - bloodymines
28
28
  extensions: []
@@ -32,6 +32,7 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - lib/bloodymines.rb
34
34
  - lib/minefield.rb
35
+ - lib/tui.rb
35
36
  - test/test_minefield.rb
36
37
  - bin/bloodymines
37
38
  has_rdoc: true