console-reversi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/bin/console-reversi +7 -0
- data/console-reversi.gemspec +25 -0
- data/lib/console_reversi.rb +156 -0
- data/lib/console_reversi/ascii_art.rb +186 -0
- data/lib/console_reversi/board.rb +76 -0
- data/lib/console_reversi/piece.rb +23 -0
- data/lib/console_reversi/player.rb +38 -0
- data/lib/console_reversi/searcher.rb +41 -0
- data/lib/console_reversi/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a9124dd42d2f7f2890d817ef95072bc844fbd9f3
|
4
|
+
data.tar.gz: 7fa555bd4cf02126d13350c1a95f4cd410d0fcc0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3efa6472749127c8e4feb9b38da403c93b64428bfa9c7890cf1bc2500b3c671072f073bcea22628e4c486428cde8e17eab015c0a4d87997859ef5e484a31cc75
|
7
|
+
data.tar.gz: ee30d979bc03d01172432763b2a6a94c38e2d191db08f5e4179b85062887283b12fd95fd78a6747f1b9115a9e11a5786534444e806b7310c98b8561d69e99c62
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at ishihata@33i.co.jp. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# ConsoleReversi
|
2
|
+
|
3
|
+
You can play the reversi in your terminal!!
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'console-reversi'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install console-reversi
|
20
|
+
|
21
|
+
## Start Game
|
22
|
+
|
23
|
+
```
|
24
|
+
$ console-reversi
|
25
|
+
```
|
26
|
+
|
27
|
+
## Commands
|
28
|
+
|
29
|
+
* move a cursor : `direction keys`
|
30
|
+
* put a piece : `Enter`
|
data/Rakefile
ADDED
data/bin/console-reversi
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'console_reversi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "console-reversi"
|
8
|
+
spec.version = ConsoleReversi::VERSION
|
9
|
+
spec.authors = ["pekepek"]
|
10
|
+
spec.email = ["ishihata@33i.co.jp"]
|
11
|
+
|
12
|
+
spec.summary = %q{You can play the reversi in terminal}
|
13
|
+
spec.description = %q{You can play the reversi in terminal}
|
14
|
+
spec.homepage = "https://github.com/pekepek/console-reversi"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
require 'console_reversi/version'
|
2
|
+
require 'console_reversi/board'
|
3
|
+
require 'console_reversi/searcher'
|
4
|
+
require 'console_reversi/piece'
|
5
|
+
require 'console_reversi/player'
|
6
|
+
require 'console_reversi/ascii_art'
|
7
|
+
|
8
|
+
class ConsoleReversi
|
9
|
+
def initialize
|
10
|
+
@board = Board.new
|
11
|
+
@black_player = Player.new(piece_color: :black)
|
12
|
+
@white_player = Player.new(piece_color: :white)
|
13
|
+
end
|
14
|
+
|
15
|
+
def game_start
|
16
|
+
@board.pretty_print
|
17
|
+
|
18
|
+
loop.with_index do |_, turn_number|
|
19
|
+
unless now_player(turn_number).putable_piece?(@board)
|
20
|
+
if next_player(turn_number).putable_piece?(@board)
|
21
|
+
print_pass
|
22
|
+
|
23
|
+
next
|
24
|
+
else
|
25
|
+
break
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
move_cursor do |key|
|
30
|
+
if type_enter?(key)
|
31
|
+
position = cursor_position
|
32
|
+
board_position = {x: position[:x] / 2, y: position[:y] - 1}
|
33
|
+
|
34
|
+
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]) }
|
35
|
+
|
36
|
+
now_player(turn_number).put_piece_on!(@board, x: board_position[:x], y: board_position[:y])
|
37
|
+
now_player(turn_number).turn_pieces!(@board, x: board_position[:x], y: board_position[:y])
|
38
|
+
|
39
|
+
@board.pretty_print
|
40
|
+
|
41
|
+
# NOTE back a cursor
|
42
|
+
print "\e[#{position[:y]};#{position[:x]}H"
|
43
|
+
|
44
|
+
break
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
print_result
|
50
|
+
|
51
|
+
print "\e[8;1H"
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def now_player(turn_number)
|
57
|
+
turn_number.even? ? @black_player : @white_player
|
58
|
+
end
|
59
|
+
|
60
|
+
def next_player(turn_number)
|
61
|
+
now_player(turn_number) == @black_player ? @white_player : @black_player
|
62
|
+
end
|
63
|
+
|
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
|
+
def print_pass
|
111
|
+
print "\e[2;1H"
|
112
|
+
print AsciiArt::PASS
|
113
|
+
|
114
|
+
sleep 1
|
115
|
+
|
116
|
+
@board.pretty_print
|
117
|
+
print "\e[1;1H"
|
118
|
+
end
|
119
|
+
|
120
|
+
def print_result
|
121
|
+
print "\e[2;1H"
|
122
|
+
print AsciiArt::FINISH
|
123
|
+
|
124
|
+
sleep 1
|
125
|
+
|
126
|
+
@board.pretty_print
|
127
|
+
|
128
|
+
print "\e[1;1H"
|
129
|
+
|
130
|
+
black_count = @board.count_pieces(color: :black)
|
131
|
+
white_count = @board.count_pieces(color: :white)
|
132
|
+
|
133
|
+
print ConsoleReversi::AsciiArt.to_aa('black', black_count, margin: 20)
|
134
|
+
|
135
|
+
sleep 1
|
136
|
+
|
137
|
+
print "\n"
|
138
|
+
print ConsoleReversi::AsciiArt.to_aa('white', white_count, margin: 20)
|
139
|
+
|
140
|
+
sleep 1
|
141
|
+
|
142
|
+
@board.pretty_print
|
143
|
+
|
144
|
+
result =
|
145
|
+
if black_count > white_count
|
146
|
+
['black', 'win']
|
147
|
+
elsif black_count == white_count
|
148
|
+
['draw']
|
149
|
+
else
|
150
|
+
['white', 'win']
|
151
|
+
end
|
152
|
+
|
153
|
+
print "\e[2;1H"
|
154
|
+
print ConsoleReversi::AsciiArt.to_aa(*result, margin: 20)
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ConsoleReversi
|
4
|
+
module AsciiArt
|
5
|
+
PASS = <<-'EOS'
|
6
|
+
____
|
7
|
+
| _ \ __ _ ___ ___
|
8
|
+
| |_) / _` / __/ __|
|
9
|
+
| __/ (_| \__ \__ \
|
10
|
+
|_| \__,_|___/___/
|
11
|
+
EOS
|
12
|
+
|
13
|
+
FINISH = <<-'EOS'
|
14
|
+
_____ _ _ _
|
15
|
+
| ___(_)_ __ (_)___| |__
|
16
|
+
| |_ | | '_ \| / __| '_ \
|
17
|
+
| _| | | | | | \__ \ | | |
|
18
|
+
|_| |_|_| |_|_|___/_| |_|
|
19
|
+
EOS
|
20
|
+
|
21
|
+
WIN = <<-'EOS'
|
22
|
+
__ ___
|
23
|
+
\ \ / (_)_ __
|
24
|
+
\ \ /\ / /| | '_ \
|
25
|
+
\ V V / | | | | |
|
26
|
+
\_/\_/ |_|_| |_|
|
27
|
+
EOS
|
28
|
+
|
29
|
+
DRAW = <<-'EOS'
|
30
|
+
____
|
31
|
+
| _ \ _ __ __ ___ __
|
32
|
+
| | | | '__/ _` \ \ /\ / /
|
33
|
+
| |_| | | | (_| |\ V V /
|
34
|
+
|____/|_| \__,_| \_/\_/
|
35
|
+
EOS
|
36
|
+
|
37
|
+
BLACK = <<-'EOS'
|
38
|
+
____ _ _
|
39
|
+
| __ )| | __ _ ___| | __
|
40
|
+
| _ \| |/ _` |/ __| |/ /
|
41
|
+
| |_) | | (_| | (__| <
|
42
|
+
|____/|_|\__,_|\___|_|\_\
|
43
|
+
EOS
|
44
|
+
|
45
|
+
WHITE = <<-'EOS'
|
46
|
+
__ ___ _ _
|
47
|
+
\ \ / / |__ (_) |_ ___
|
48
|
+
\ \ /\ / /| '_ \| | __/ _ \
|
49
|
+
\ V V / | | | | | || __/
|
50
|
+
\_/\_/ |_| |_|_|\__\___|
|
51
|
+
EOS
|
52
|
+
|
53
|
+
SPACE = ' '
|
54
|
+
|
55
|
+
ONE = <<-'EOS'
|
56
|
+
__
|
57
|
+
/_ |
|
58
|
+
| |
|
59
|
+
| |
|
60
|
+
| |
|
61
|
+
|_|
|
62
|
+
EOS
|
63
|
+
|
64
|
+
TWO = <<-'EOS'
|
65
|
+
___
|
66
|
+
|__ \
|
67
|
+
) |
|
68
|
+
/ /
|
69
|
+
/ /_
|
70
|
+
|____|
|
71
|
+
EOS
|
72
|
+
|
73
|
+
THREE = <<-'EOS'
|
74
|
+
____
|
75
|
+
|___ \
|
76
|
+
__) |
|
77
|
+
|__ <
|
78
|
+
___) |
|
79
|
+
|____/
|
80
|
+
EOS
|
81
|
+
|
82
|
+
FOUR = <<-'EOS'
|
83
|
+
_ _
|
84
|
+
| || |
|
85
|
+
| || |_
|
86
|
+
|__ _|
|
87
|
+
| |
|
88
|
+
|_|
|
89
|
+
EOS
|
90
|
+
|
91
|
+
FIVE = <<-'EOS'
|
92
|
+
_____
|
93
|
+
| ____|
|
94
|
+
| |__
|
95
|
+
|___ \
|
96
|
+
___) |
|
97
|
+
|____/
|
98
|
+
EOS
|
99
|
+
|
100
|
+
SIX = <<-'EOS'
|
101
|
+
__
|
102
|
+
/ /
|
103
|
+
/ /_
|
104
|
+
| '_ \
|
105
|
+
| (_) |
|
106
|
+
\___/
|
107
|
+
EOS
|
108
|
+
|
109
|
+
SEVEN = <<-'EOS'
|
110
|
+
______
|
111
|
+
|____ |
|
112
|
+
/ /
|
113
|
+
/ /
|
114
|
+
/ /
|
115
|
+
/_/
|
116
|
+
EOS
|
117
|
+
|
118
|
+
EIGHT = <<-'EOS'
|
119
|
+
___
|
120
|
+
/ _ \
|
121
|
+
| (_) |
|
122
|
+
> _ <
|
123
|
+
| (_) |
|
124
|
+
\___/
|
125
|
+
EOS
|
126
|
+
|
127
|
+
NINE = <<-'EOS'
|
128
|
+
___
|
129
|
+
/ _ \
|
130
|
+
| (_) |
|
131
|
+
\__, |
|
132
|
+
/ /
|
133
|
+
/_/
|
134
|
+
EOS
|
135
|
+
|
136
|
+
ZERO = <<-'EOS'
|
137
|
+
___
|
138
|
+
/ _ \
|
139
|
+
| | | |
|
140
|
+
| | | |
|
141
|
+
| |_| |
|
142
|
+
\___/
|
143
|
+
EOS
|
144
|
+
|
145
|
+
NUMBER_TO_ENGLISH = {
|
146
|
+
1 => 'one',
|
147
|
+
2 => 'two',
|
148
|
+
3 => 'three',
|
149
|
+
4 => 'four',
|
150
|
+
5 => 'five',
|
151
|
+
6 => 'six',
|
152
|
+
7 => 'seven',
|
153
|
+
8 => 'eight',
|
154
|
+
9 => 'nine',
|
155
|
+
0 => 'zero'
|
156
|
+
}.freeze
|
157
|
+
|
158
|
+
module_function
|
159
|
+
|
160
|
+
def to_aa(*words, margin: nil)
|
161
|
+
aas = words.map {|w|
|
162
|
+
if w.is_a?(Numeric)
|
163
|
+
number_to_aa(w)
|
164
|
+
else
|
165
|
+
const_get(w.upcase)
|
166
|
+
end
|
167
|
+
}.flatten
|
168
|
+
|
169
|
+
splitted_aas = aas.map {|n| n.split("\n") }
|
170
|
+
max_aa_len = splitted_aas.map {|aas| aas.count }.max
|
171
|
+
|
172
|
+
max_aa_len.times.map {|i|
|
173
|
+
spcase = "\e[#{margin}G" || ''
|
174
|
+
|
175
|
+
spcase + splitted_aas.map {|aa|
|
176
|
+
max_length = aa.map(&:size).max
|
177
|
+
aa[i].to_s.ljust(max_length)
|
178
|
+
}.join('')
|
179
|
+
}.join("\n")
|
180
|
+
end
|
181
|
+
|
182
|
+
def number_to_aa(number)
|
183
|
+
number.to_s.chars.map {|c| const_get(NUMBER_TO_ENGLISH[c.to_i].upcase) }
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class ConsoleReversi
|
2
|
+
class Board
|
3
|
+
def initialize
|
4
|
+
@board = initialize_board
|
5
|
+
end
|
6
|
+
|
7
|
+
def put_piece!(piece:, x:, y:)
|
8
|
+
@board[y][x] = piece
|
9
|
+
end
|
10
|
+
|
11
|
+
def putable_piece?(direction:, piece_color:, x:, y:)
|
12
|
+
return false unless at(x, y) == 0
|
13
|
+
|
14
|
+
have_turnable_piece?(direction: direction, piece_color: piece_color, x: x, y: y)
|
15
|
+
end
|
16
|
+
|
17
|
+
def have_turnable_piece?(direction:, piece_color:, x:, y:)
|
18
|
+
searcher = create_searcher(x, y)
|
19
|
+
|
20
|
+
p = searcher.search(direction, 1)
|
21
|
+
return false if p.nil? || p == 0 || p.send("#{piece_color}?")
|
22
|
+
|
23
|
+
loop.with_index(2) do |_, distance|
|
24
|
+
p = searcher.search(direction, distance)
|
25
|
+
|
26
|
+
return false if p.nil? || p == 0
|
27
|
+
|
28
|
+
return true if p.send("#{piece_color}?")
|
29
|
+
end
|
30
|
+
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
def count_pieces(color:)
|
35
|
+
@board.map {|col| col.count {|p| p != 0 && p.send("#{color}?") } }.inject(:+)
|
36
|
+
end
|
37
|
+
|
38
|
+
def pretty_print
|
39
|
+
print "\e[2J"
|
40
|
+
print "\e[1;1H"
|
41
|
+
|
42
|
+
@board.each_with_index do |row, i|
|
43
|
+
row.each_with_index do |p, j|
|
44
|
+
if p == 0
|
45
|
+
print (i + j) % 2 == 0 ? "\e[48;5;22m " : "\e[48;5;232m "
|
46
|
+
else
|
47
|
+
p.pretty_print
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
print "\e[0m\n"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def at(x, y)
|
56
|
+
@board.at(y)&.at(x)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def initialize_board
|
62
|
+
board = 8.times.map { [0].cycle(8).to_a }
|
63
|
+
|
64
|
+
board[3][3] = Piece.new(type: :black)
|
65
|
+
board[3][4] = Piece.new(type: :white)
|
66
|
+
board[4][3] = Piece.new(type: :white)
|
67
|
+
board[4][4] = Piece.new(type: :black)
|
68
|
+
|
69
|
+
board
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_searcher(x, y)
|
73
|
+
Searcher.new(@board, x, y)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class ConsoleReversi
|
2
|
+
class Piece
|
3
|
+
COLORS = {black: "\e[37;40m", white: "\e[30;47m"}
|
4
|
+
|
5
|
+
attr_accessor :type
|
6
|
+
|
7
|
+
def initialize(type:)
|
8
|
+
@type = type
|
9
|
+
end
|
10
|
+
|
11
|
+
def pretty_print
|
12
|
+
print "#{COLORS[@type.to_sym]} ⃝ "
|
13
|
+
end
|
14
|
+
|
15
|
+
def black?
|
16
|
+
@type == :black
|
17
|
+
end
|
18
|
+
|
19
|
+
def white?
|
20
|
+
@type == :white
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class ConsoleReversi
|
2
|
+
class Player
|
3
|
+
attr_accessor :piece_color
|
4
|
+
|
5
|
+
def initialize(piece_color:)
|
6
|
+
@piece_color = piece_color.to_sym
|
7
|
+
end
|
8
|
+
|
9
|
+
def put_piece_on!(board, x:, y:)
|
10
|
+
board.put_piece!(piece: Piece.new(type: @piece_color), x: x, y: y)
|
11
|
+
end
|
12
|
+
|
13
|
+
def putable_piece?(board)
|
14
|
+
8.times.any? {|i|
|
15
|
+
8.times.any? {|j|
|
16
|
+
Searcher::DIRECTIONS.any? {|d| board.putable_piece?(direction: d, piece_color: @piece_color, x: i, y: j) }
|
17
|
+
}
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def turn_pieces!(board, x:, y:)
|
22
|
+
searcher = Searcher.new(board, x, y)
|
23
|
+
|
24
|
+
Searcher::DIRECTIONS.each do |direction|
|
25
|
+
next unless board.have_turnable_piece?(direction: direction, piece_color: piece_color, x: x, y: y)
|
26
|
+
|
27
|
+
loop.with_index(1) do |_, distance|
|
28
|
+
pos = searcher.position(direction, distance)
|
29
|
+
piece = board.at(pos[:x], pos[:y])
|
30
|
+
|
31
|
+
break if piece.send("#{piece_color}?")
|
32
|
+
|
33
|
+
put_piece_on!(board, x: pos[:x], y: pos[:y])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class ConsoleReversi
|
2
|
+
class Searcher
|
3
|
+
DIRECTIONS = %i(bottom_right bottom bottom_left right left upper_right upper upper_left)
|
4
|
+
|
5
|
+
def initialize(board, x, y)
|
6
|
+
@board = board
|
7
|
+
@x = x
|
8
|
+
@y = y
|
9
|
+
end
|
10
|
+
|
11
|
+
def search(direction, distance)
|
12
|
+
pos = position(direction, distance)
|
13
|
+
|
14
|
+
@board.at(pos[:y])&.at(pos[:x]) if !pos[:y].negative? && !pos[:x].negative?
|
15
|
+
end
|
16
|
+
|
17
|
+
def position(direction, distance)
|
18
|
+
y_distance =
|
19
|
+
case direction
|
20
|
+
when /\Abottom/
|
21
|
+
-1 * distance
|
22
|
+
when /\Aupper/
|
23
|
+
distance
|
24
|
+
else
|
25
|
+
0
|
26
|
+
end
|
27
|
+
|
28
|
+
x_distance =
|
29
|
+
case direction
|
30
|
+
when /left\z/
|
31
|
+
-1 * distance
|
32
|
+
when /right\z/
|
33
|
+
distance
|
34
|
+
else
|
35
|
+
0
|
36
|
+
end
|
37
|
+
|
38
|
+
{x: @x + x_distance, y: @y + y_distance}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: console-reversi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pekepek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: You can play the reversi in terminal
|
56
|
+
email:
|
57
|
+
- ishihata@33i.co.jp
|
58
|
+
executables:
|
59
|
+
- console-reversi
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console-reversi
|
69
|
+
- console-reversi.gemspec
|
70
|
+
- lib/console_reversi.rb
|
71
|
+
- lib/console_reversi/ascii_art.rb
|
72
|
+
- lib/console_reversi/board.rb
|
73
|
+
- lib/console_reversi/piece.rb
|
74
|
+
- lib/console_reversi/player.rb
|
75
|
+
- lib/console_reversi/searcher.rb
|
76
|
+
- lib/console_reversi/version.rb
|
77
|
+
homepage: https://github.com/pekepek/console-reversi
|
78
|
+
licenses: []
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.5.1
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: You can play the reversi in terminal
|
100
|
+
test_files: []
|