console-reversi 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8aaa514166ac6d465b762165a48f4bc2f4de07d
4
- data.tar.gz: 8c3d5eaeff2bb5bfc69e92ec5845a3e8e2f09116
3
+ metadata.gz: ad490db6ee92e754a57d3ce9cddc7fdd8b595e96
4
+ data.tar.gz: cfab7ab8f235b14d98a5497942929ae96d0e931a
5
5
  SHA512:
6
- metadata.gz: 6a1280bc05ef0c6cf0528fbe7b149169b02a86999ad953ac01030fb24e45f923f214d19083c0ce8b0c39cd8f0b31752212afd1bc6b85e796bfc843264094a083
7
- data.tar.gz: 4efb45d543155874cb5074ceb7094b48f435102cb6584f4426be29938417d476aaac2f1d94556162bfed601f29b96dc3b0c1e1aebcf7e9d33d92444a70aacc20
6
+ metadata.gz: 9870ef7398fecbc8deadff6cbb2e663d31f0e5eb450b130bee6fe1e41c3c55ec4c6f7d6a2ebbf89ad269d7b8cd275a68e5970616e4f8d854cd9b32b76e0609f1
7
+ data.tar.gz: 5d3cf1e7db40adf99603640e05602b57b4bbf9d875a633f2e93afb6f8760e05fe812340d197667bcf8ac2f05abb20f2ca5b797d2b966970eae3c19f5b92445aa
data/README.md CHANGED
@@ -24,6 +24,16 @@ Or install it yourself as:
24
24
  $ console-reversi
25
25
  ```
26
26
 
27
+ You can select a human player or computer player
28
+
29
+ ```
30
+ Choose black player's type (Use arrow keys, press Enter to select)
31
+ ‣ human
32
+ computer
33
+ ```
34
+
35
+ please select by using arrow keys
36
+
27
37
  ## Commands
28
38
 
29
39
  * move a cursor : `direction keys`
data/bin/console-reversi CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'console_reversi'
5
+ require 'tty'
5
6
  require 'pry'
6
7
 
7
- ConsoleReversi.new.game_start
8
+ prompt = TTY::Prompt.new
9
+ black = prompt.select("Choose black player's type", ConsoleReversi::Player::TYPES)
10
+ white = prompt.select("Choose white player's type", ConsoleReversi::Player::TYPES)
11
+
12
+ ConsoleReversi.new(black, white).game_start
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.13"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "pry"
25
+ spec.add_dependency "tty"
25
26
  end
@@ -9,10 +9,10 @@ require 'console_reversi/cursor_operatable'
9
9
  class ConsoleReversi
10
10
  include CursorOperatable
11
11
 
12
- def initialize
12
+ def initialize(black_player_type, white_player_type)
13
13
  @board = Board.new
14
- @black_player = Player.new(piece_color: :black)
15
- @white_player = Player.new(piece_color: :white)
14
+ @player1 = Player.new(piece_color: :black, type: black_player_type)
15
+ @player2 = Player.new(piece_color: :white, type: white_player_type)
16
16
  end
17
17
 
18
18
  def game_start
@@ -30,24 +30,34 @@ class ConsoleReversi
30
30
  end
31
31
  end
32
32
 
33
- move_cursor do |key|
34
- if type_enter?(key)
35
- position = cursor_position
36
- board_position = {x: position[:x] / 2, y: position[:y] - 1}
33
+ if now_player(turn_number).type == :computer
34
+ @board.plot_putable_point!(now_player(turn_number))
35
+ sleep 0.5
36
+ now_player(turn_number).put_piece_randomly!(@board)
37
37
 
38
- next if Searcher::DIRECTIONS.none? {|d| @board.putable_piece?(direction: d, piece_color: now_player(turn_number).piece_color, x: board_position[:x], y: board_position[:y]) }
38
+ @board.refresh_putable_point!
39
+ @board.plot_putable_point!(next_player(turn_number))
40
+ @board.pretty_print
41
+ else
42
+ move_cursor do |key|
43
+ if type_enter?(key)
44
+ position = cursor_position
45
+ board_position = {x: position[:x] / 2, y: position[:y] - 1}
39
46
 
40
- now_player(turn_number).put_piece_on!(@board, x: board_position[:x], y: board_position[:y])
41
- now_player(turn_number).turn_pieces!(@board, x: board_position[:x], y: board_position[:y])
47
+ next if Searcher::DIRECTIONS.none? {|d| @board.putable_piece?(direction: d, piece_color: now_player(turn_number).piece_color, x: board_position[:x], y: board_position[:y]) }
42
48
 
43
- @board.refresh_putable_point!
44
- @board.plot_putable_point!(next_player(turn_number))
45
- @board.pretty_print
49
+ now_player(turn_number).put_piece_on!(@board, x: board_position[:x], y: board_position[:y])
50
+ now_player(turn_number).turn_pieces!(@board, x: board_position[:x], y: board_position[:y])
46
51
 
47
- # NOTE back a cursor
48
- print "\e[#{position[:y]};#{position[:x]}H"
52
+ @board.refresh_putable_point!
53
+ @board.plot_putable_point!(next_player(turn_number))
54
+ @board.pretty_print
49
55
 
50
- break
56
+ # NOTE back a cursor
57
+ print "\e[#{position[:y]};#{position[:x]}H"
58
+
59
+ break
60
+ end
51
61
  end
52
62
  end
53
63
  end
@@ -60,11 +70,11 @@ class ConsoleReversi
60
70
  private
61
71
 
62
72
  def now_player(turn_number)
63
- turn_number.even? ? @black_player : @white_player
73
+ turn_number.even? ? @player1 : @player2
64
74
  end
65
75
 
66
76
  def next_player(turn_number)
67
- now_player(turn_number) == @black_player ? @white_player : @black_player
77
+ now_player(turn_number) == @player1 ? @player2 : @player1
68
78
  end
69
79
 
70
80
  def print_pass
@@ -76,6 +76,14 @@ class ConsoleReversi
76
76
  end
77
77
  end
78
78
 
79
+ def putable_positions
80
+ @board.each_with_object([]).with_index {|(col, positions), i|
81
+ col.each_with_index {|s, j|
82
+ positions << [i, j] if s == 1
83
+ }
84
+ }
85
+ end
86
+
79
87
  private
80
88
 
81
89
  def initialize_board
@@ -1,9 +1,12 @@
1
1
  class ConsoleReversi
2
2
  class Player
3
- attr_accessor :piece_color
3
+ attr_accessor :piece_color, :type
4
4
 
5
- def initialize(piece_color:)
5
+ TYPES = %i(human computer)
6
+
7
+ def initialize(piece_color:, type:)
6
8
  @piece_color = piece_color.to_sym
9
+ @type = type
7
10
  end
8
11
 
9
12
  def put_piece_on!(board, x:, y:)
@@ -34,5 +37,13 @@ class ConsoleReversi
34
37
  end
35
38
  end
36
39
  end
40
+
41
+ def put_piece_randomly!(board)
42
+ random_position = board.putable_positions.sample
43
+
44
+ put_piece_on!(board, x: random_position[1], y: random_position[0])
45
+
46
+ turn_pieces!(board, x: random_position[1], y: random_position[0])
47
+ end
37
48
  end
38
49
  end
@@ -1,3 +1,3 @@
1
1
  class ConsoleReversi
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console-reversi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pekepek
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: You can play the reversi in terminal
56
70
  email:
57
71
  - ishihata@33i.co.jp
@@ -74,7 +88,6 @@ files:
74
88
  - lib/console_reversi/piece.rb
75
89
  - lib/console_reversi/player.rb
76
90
  - lib/console_reversi/searcher.rb
77
- - lib/console_reversi/square.rb
78
91
  - lib/console_reversi/version.rb
79
92
  homepage: https://github.com/pekepek/console-reversi
80
93
  licenses: []
@@ -1,13 +0,0 @@
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