console-reversi 0.2.0 → 0.3.0

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: 4bcbac06de802b63e98972131d7ebf469edcc6fa
4
- data.tar.gz: f30dd8d7bc3fcc79c25749ae9cef1b3091981788
3
+ metadata.gz: b8aaa514166ac6d465b762165a48f4bc2f4de07d
4
+ data.tar.gz: 8c3d5eaeff2bb5bfc69e92ec5845a3e8e2f09116
5
5
  SHA512:
6
- metadata.gz: 0e4f20249315424dee6a5a71fc3b3277cd83fb5c6ab1b664ac10cef8e0c7da14c6be81a4f846cec7ffbad0db80f4cbd0bf85982ded7e33085b9ddcdb42f63002
7
- data.tar.gz: 170595a981e487b5a299d13df920118a126c8f41bbc6f02b77f2a77cd994f831b55084f0f502060a7a9dfee498d7bfc79e2f22069d573b1663199d92b9702927
6
+ metadata.gz: 6a1280bc05ef0c6cf0528fbe7b149169b02a86999ad953ac01030fb24e45f923f214d19083c0ce8b0c39cd8f0b31752212afd1bc6b85e796bfc843264094a083
7
+ data.tar.gz: 4efb45d543155874cb5074ceb7094b48f435102cb6584f4426be29938417d476aaac2f1d94556162bfed601f29b96dc3b0c1e1aebcf7e9d33d92444a70aacc20
@@ -4,8 +4,11 @@ require 'console_reversi/searcher'
4
4
  require 'console_reversi/piece'
5
5
  require 'console_reversi/player'
6
6
  require 'console_reversi/ascii_art'
7
+ require 'console_reversi/cursor_operatable'
7
8
 
8
9
  class ConsoleReversi
10
+ include CursorOperatable
11
+
9
12
  def initialize
10
13
  @board = Board.new
11
14
  @black_player = Player.new(piece_color: :black)
@@ -13,6 +16,7 @@ class ConsoleReversi
13
16
  end
14
17
 
15
18
  def game_start
19
+ @board.plot_putable_point!(now_player(0))
16
20
  @board.pretty_print
17
21
 
18
22
  loop.with_index do |_, turn_number|
@@ -36,6 +40,8 @@ class ConsoleReversi
36
40
  now_player(turn_number).put_piece_on!(@board, x: board_position[:x], y: board_position[:y])
37
41
  now_player(turn_number).turn_pieces!(@board, x: board_position[:x], y: board_position[:y])
38
42
 
43
+ @board.refresh_putable_point!
44
+ @board.plot_putable_point!(next_player(turn_number))
39
45
  @board.pretty_print
40
46
 
41
47
  # NOTE back a cursor
@@ -48,7 +54,7 @@ class ConsoleReversi
48
54
 
49
55
  print_result
50
56
 
51
- print "\e[8;1H"
57
+ print "\e[9;1H"
52
58
  end
53
59
 
54
60
  private
@@ -61,52 +67,6 @@ class ConsoleReversi
61
67
  now_player(turn_number) == @black_player ? @white_player : @black_player
62
68
  end
63
69
 
64
- def move_cursor(&block)
65
- while key = STDIN.getch
66
- # NOTE 一時的に ctrl c で中断できるようにしてる
67
- exit if key == "\C-c"
68
-
69
- if key == "\e" && STDIN.getch == "["
70
- key = STDIN.getch
71
-
72
- direction =
73
- case key
74
- when 'A', 'B'
75
- key
76
- when 'C', 'D'
77
- "2#{key}"
78
- else
79
- nil
80
- end
81
-
82
- print "\e[#{direction}" if direction
83
- end
84
-
85
- block.call(key)
86
- end
87
- end
88
-
89
- def cursor_position
90
- stdout = ''
91
-
92
- $stdin.raw do |stdin|
93
- $stdout << "\e[6n"
94
- $stdout.flush
95
-
96
- while (c = stdin.getc) != 'R'
97
- stdout << c
98
- end
99
- end
100
-
101
- positions = stdout.match /(\d+);(\d+)/
102
-
103
- {x: positions[2].to_i, y: positions[1].to_i}
104
- end
105
-
106
- def type_enter?(key)
107
- key == "\r"
108
- end
109
-
110
70
  def print_pass
111
71
  print "\e[2;1H"
112
72
  print AsciiArt::PASS
@@ -9,7 +9,7 @@ class ConsoleReversi
9
9
  end
10
10
 
11
11
  def putable_piece?(direction:, piece_color:, x:, y:)
12
- return false unless at(x, y) == 0
12
+ return false unless at(x, y).is_a?(Numeric)
13
13
 
14
14
  have_turnable_piece?(direction: direction, piece_color: piece_color, x: x, y: y)
15
15
  end
@@ -18,12 +18,12 @@ class ConsoleReversi
18
18
  searcher = create_searcher(x, y)
19
19
 
20
20
  p = searcher.search(direction, 1)
21
- return false if p.nil? || p == 0 || p.send("#{piece_color}?")
21
+ return false if !p.is_a?(Piece) || p.send("#{piece_color}?")
22
22
 
23
23
  loop.with_index(2) do |_, distance|
24
24
  p = searcher.search(direction, distance)
25
25
 
26
- return false if p.nil? || p == 0
26
+ return false if !p.is_a?(Piece)
27
27
 
28
28
  return true if p.send("#{piece_color}?")
29
29
  end
@@ -45,6 +45,8 @@ class ConsoleReversi
45
45
 
46
46
  if p == 0
47
47
  print ' '
48
+ elsif p == 1
49
+ print "\e[37m〇"
48
50
  else
49
51
  p.pretty_print
50
52
  end
@@ -58,15 +60,31 @@ class ConsoleReversi
58
60
  @board.at(y)&.at(x)
59
61
  end
60
62
 
63
+ def plot_putable_point!(player)
64
+ 8.times do |i|
65
+ 8.times do |j|
66
+ @board[j][i] = 1 if Searcher::DIRECTIONS.any? {|d| putable_piece?(direction: d, piece_color: player.piece_color, x: i, y: j) }
67
+ end
68
+ end
69
+ end
70
+
71
+ def refresh_putable_point!
72
+ 8.times do |i|
73
+ 8.times do |j|
74
+ @board[j][i] = 0 if @board[j][i] == 1
75
+ end
76
+ end
77
+ end
78
+
61
79
  private
62
80
 
63
81
  def initialize_board
64
82
  board = 8.times.map { [0].cycle(8).to_a }
65
83
 
66
- board[3][3] = Piece.new(type: :black)
67
- board[3][4] = Piece.new(type: :white)
68
- board[4][3] = Piece.new(type: :white)
69
- board[4][4] = Piece.new(type: :black)
84
+ board[3][3] = Piece.new(color: :black)
85
+ board[3][4] = Piece.new(color: :white)
86
+ board[4][3] = Piece.new(color: :white)
87
+ board[4][4] = Piece.new(color: :black)
70
88
 
71
89
  board
72
90
  end
@@ -0,0 +1,49 @@
1
+ class ConsoleReversi
2
+ module CursorOperatable
3
+ def move_cursor(&block)
4
+ while key = STDIN.getch
5
+ # NOTE enable to cancel a game by ctrl-c
6
+ exit if key == "\C-c"
7
+
8
+ if key == "\e" && STDIN.getch == "["
9
+ key = STDIN.getch
10
+
11
+ direction =
12
+ case key
13
+ when 'A', 'B'
14
+ key
15
+ when 'C', 'D'
16
+ "2#{key}"
17
+ else
18
+ nil
19
+ end
20
+
21
+ print "\e[#{direction}" if direction
22
+ end
23
+
24
+ block.call(key)
25
+ end
26
+ end
27
+
28
+ def cursor_position
29
+ stdout = ''
30
+
31
+ $stdin.raw do |stdin|
32
+ $stdout << "\e[6n"
33
+ $stdout.flush
34
+
35
+ while (c = stdin.getc) != 'R'
36
+ stdout << c
37
+ end
38
+ end
39
+
40
+ positions = stdout.match /(\d+);(\d+)/
41
+
42
+ {x: positions[2].to_i, y: positions[1].to_i}
43
+ end
44
+
45
+ def type_enter?(key)
46
+ key == "\r"
47
+ end
48
+ end
49
+ end
@@ -2,22 +2,22 @@ class ConsoleReversi
2
2
  class Piece
3
3
  COLORS = {black: "\e[30m", white: "\e[37m"}
4
4
 
5
- attr_accessor :type
5
+ attr_accessor :color
6
6
 
7
- def initialize(type:)
8
- @type = type
7
+ def initialize(color:)
8
+ @color = color
9
9
  end
10
10
 
11
11
  def pretty_print
12
- print "#{COLORS[@type.to_sym]}⬤️⃝ "
12
+ print "#{COLORS[@color.to_sym]}⬤️⃝ "
13
13
  end
14
14
 
15
15
  def black?
16
- @type == :black
16
+ @color == :black
17
17
  end
18
18
 
19
19
  def white?
20
- @type == :white
20
+ @color == :white
21
21
  end
22
22
  end
23
23
  end
@@ -7,7 +7,7 @@ class ConsoleReversi
7
7
  end
8
8
 
9
9
  def put_piece_on!(board, x:, y:)
10
- board.put_piece!(piece: Piece.new(type: @piece_color), x: x, y: y)
10
+ board.put_piece!(piece: Piece.new(color: @piece_color), x: x, y: y)
11
11
  end
12
12
 
13
13
  def putable_piece?(board)
@@ -0,0 +1,13 @@
1
+ class ConsoleReversi
2
+ class Square
3
+ attr_reader :piece
4
+
5
+ def put(piece)
6
+ @piece = piece
7
+ end
8
+
9
+ def empty?
10
+ @piece.nil?
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  class ConsoleReversi
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console-reversi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pekepek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-28 00:00:00.000000000 Z
11
+ date: 2017-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,9 +70,11 @@ files:
70
70
  - lib/console_reversi.rb
71
71
  - lib/console_reversi/ascii_art.rb
72
72
  - lib/console_reversi/board.rb
73
+ - lib/console_reversi/cursor_operatable.rb
73
74
  - lib/console_reversi/piece.rb
74
75
  - lib/console_reversi/player.rb
75
76
  - lib/console_reversi/searcher.rb
77
+ - lib/console_reversi/square.rb
76
78
  - lib/console_reversi/version.rb
77
79
  homepage: https://github.com/pekepek/console-reversi
78
80
  licenses: []