yolo_tic_tac_toe 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/tictactoe/board.rb +57 -0
- data/lib/tictactoe/cell.rb +30 -0
- data/lib/tictactoe/cell_line.rb +32 -0
- data/lib/tictactoe/player.rb +12 -0
- data/lib/tictactoe/string_patch.rb +15 -0
- data/lib/yolo_tic_tac_toe.rb +94 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 561f176e8d80caf9c1f40501cddf63c5651d41a50e9760c19a02a95e484b9a87
|
4
|
+
data.tar.gz: e166cd9baef1a73805334e3b646dbe313ca02122a0cb7baf1d6d4899b3c9aaa6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65d17f649b662d1b7c3e13a2cedd2d1505a1d1d64a514c0eddb14c13616b0284c938af62771a0fe65eb36721a34c3fff7f714209999752286cb35852fc376416
|
7
|
+
data.tar.gz: 71fb6de016e3c6b8b2cd48e2d8c7b1f8a044ae98770fbb597f4958d83f4621d493e356d50e4e9da61993a8b0696f7fef9dff0e146b037b7f6b1ee6cdaae56504
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class Board
|
2
|
+
attr_reader :cells, :cell_lines
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@cells = create_cells
|
6
|
+
@cell_lines = create_cell_lines
|
7
|
+
end
|
8
|
+
|
9
|
+
def filled?
|
10
|
+
cells.all?(&:filled?)
|
11
|
+
end
|
12
|
+
|
13
|
+
def someone_won?
|
14
|
+
cell_lines.any?(&:filled_with_same_symbol?)
|
15
|
+
end
|
16
|
+
|
17
|
+
def winner
|
18
|
+
cell_lines.find(&:filled_with_same_symbol?).cells.first.symbol
|
19
|
+
end
|
20
|
+
|
21
|
+
def find(x, y)
|
22
|
+
cells.find { |cell| cell.x == x && cell.y == y }
|
23
|
+
end
|
24
|
+
|
25
|
+
def display!
|
26
|
+
display = ''
|
27
|
+
# top row
|
28
|
+
display << cell_lines.find { |cell_line| cell_line.cells.all?(&:in_top_row?) }.to_s
|
29
|
+
display << "\n"
|
30
|
+
|
31
|
+
# middle row
|
32
|
+
display << cell_lines.find { |cell_line| cell_line.cells.all?(&:in_middle_row?) }.to_s
|
33
|
+
display << "\n"
|
34
|
+
|
35
|
+
# bottom row
|
36
|
+
display << cell_lines.find { |cell_line| cell_line.cells.all?(&:in_bottom_row?) }.to_s
|
37
|
+
|
38
|
+
puts display
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def create_cells
|
44
|
+
[-1, 0, 1].repeated_permutation(2).map do |point|
|
45
|
+
Cell.new(x: point[0], y: point[1])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_cell_lines
|
50
|
+
[
|
51
|
+
CellLine.new(cells.select { |cell| cell.x == cell.y }), # right diagonal
|
52
|
+
CellLine.new(cells.select { |cell| cell.x == -cell.y }), # left diagonal
|
53
|
+
*[-1, 0, 1].map { |i| CellLine.new(cells.select { |cell| cell.x == i }) }, # cols
|
54
|
+
*[-1, 0, 1].map { |i| CellLine.new(cells.select { |cell| cell.y == i }) } # rows
|
55
|
+
]
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Cell
|
2
|
+
attr_accessor :x, :y, :filled, :symbol
|
3
|
+
|
4
|
+
def initialize(x:, y:)
|
5
|
+
@x = x
|
6
|
+
@y = y
|
7
|
+
@filled = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def fill(symbol)
|
11
|
+
@symbol = symbol
|
12
|
+
@filled = true
|
13
|
+
end
|
14
|
+
|
15
|
+
def filled?
|
16
|
+
filled
|
17
|
+
end
|
18
|
+
|
19
|
+
def in_bottom_row?
|
20
|
+
x.negative?
|
21
|
+
end
|
22
|
+
|
23
|
+
def in_middle_row?
|
24
|
+
x.zero?
|
25
|
+
end
|
26
|
+
|
27
|
+
def in_top_row?
|
28
|
+
x.positive?
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class CellLine
|
2
|
+
attr_reader :cells
|
3
|
+
|
4
|
+
def initialize(cells)
|
5
|
+
raise unless cells.size == 3
|
6
|
+
|
7
|
+
@cells = cells
|
8
|
+
end
|
9
|
+
|
10
|
+
def filled?
|
11
|
+
cells.all?(&:filled?)
|
12
|
+
end
|
13
|
+
|
14
|
+
def filled_with
|
15
|
+
cells.map(&:symbol)
|
16
|
+
end
|
17
|
+
|
18
|
+
def filled_with_same_symbol?
|
19
|
+
filled? && filled_with.compact.uniq.size == 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
" _____ _____ _____ \n" \
|
24
|
+
"| || || |\n" \
|
25
|
+
"#{middle_row_of_s}" \
|
26
|
+
'|_____||_____||_____|'
|
27
|
+
end
|
28
|
+
|
29
|
+
def middle_row_of_s
|
30
|
+
"#{cells.map(&:symbol).map { |s| "| #{s || ' '} |" }.join}\n"
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class String
|
2
|
+
def to_coord
|
3
|
+
return 1 if self == 't' || self == 'r'
|
4
|
+
return 0 if self == 'm'
|
5
|
+
return -1 if self == 'b' || self == 'l'
|
6
|
+
|
7
|
+
raise ToCoordError
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ToCoordError < StandardError
|
12
|
+
def message
|
13
|
+
"Can not transform #{self} into a coord. Valid values are t, m and b, r, l"
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative 'tictactoe/string_patch'
|
2
|
+
require_relative 'tictactoe/board'
|
3
|
+
require_relative 'tictactoe/cell'
|
4
|
+
require_relative 'tictactoe/cell_line'
|
5
|
+
require_relative 'tictactoe/player'
|
6
|
+
|
7
|
+
class TicTacToe
|
8
|
+
def initialize
|
9
|
+
@board = Board.new
|
10
|
+
@cross_player = Player.new(symbol: :x, game: self)
|
11
|
+
@circle_player = Player.new(symbol: :o, game: self)
|
12
|
+
@current_player = @cross_player
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.play!
|
16
|
+
new.play!
|
17
|
+
end
|
18
|
+
|
19
|
+
def play!
|
20
|
+
welcome!
|
21
|
+
|
22
|
+
until finished?
|
23
|
+
begin
|
24
|
+
board.display!
|
25
|
+
puts 'Please choose a square e.g.(t_l, t_m, b_m): '
|
26
|
+
move = gets.chomp.split('_').map(&:to_coord)
|
27
|
+
cell = board.find(*move)
|
28
|
+
if cell.filled?
|
29
|
+
puts 'This one is filled!'
|
30
|
+
else
|
31
|
+
current_player.fill(cell)
|
32
|
+
next_player!
|
33
|
+
end
|
34
|
+
rescue ToCoordError
|
35
|
+
puts 'Invalid move!'
|
36
|
+
end
|
37
|
+
end if gets.chomp == 'y'
|
38
|
+
|
39
|
+
if board.someone_won?
|
40
|
+
board.display!
|
41
|
+
puts "The winner is #{winner}!!"
|
42
|
+
puts 'Do you want to play again?(y/n)'
|
43
|
+
if gets.chomp == 'y'
|
44
|
+
reset!
|
45
|
+
else
|
46
|
+
puts 'Thanks for playing!'
|
47
|
+
end
|
48
|
+
elsif board.filled?
|
49
|
+
puts 'It\'s a draw!'
|
50
|
+
else
|
51
|
+
puts 'Bye then!'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def reset!
|
56
|
+
self.class.play!
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
attr_reader :board, :current_player
|
62
|
+
|
63
|
+
def winner
|
64
|
+
case board.winner
|
65
|
+
when :x
|
66
|
+
'Crosses player'
|
67
|
+
when :o
|
68
|
+
'Circles player'
|
69
|
+
else
|
70
|
+
'What'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def welcome!
|
75
|
+
puts "Welcome to Tic-Tac-Toe. \n" \
|
76
|
+
"The bottom is 'b', the top is 't', the middle is 'm', " \
|
77
|
+
"the left is 'l', and the right is, you guessed it, 'r'.\n" \
|
78
|
+
"So in order to mark the bottom left square you have to put in 'b_l', "\
|
79
|
+
"And for the centre one use 'm_m'.\nEasy.\n" \
|
80
|
+
'Are you ready to play?(y/n)'
|
81
|
+
end
|
82
|
+
|
83
|
+
def players
|
84
|
+
[@cross_player, @circle_player]
|
85
|
+
end
|
86
|
+
|
87
|
+
def finished?
|
88
|
+
board.filled? || board.someone_won?
|
89
|
+
end
|
90
|
+
|
91
|
+
def next_player!
|
92
|
+
@current_player = players.find { |player| player != current_player }
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yolo_tic_tac_toe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Barseek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: I hate this already
|
14
|
+
email: sergey.b@hey.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/tictactoe/board.rb
|
20
|
+
- lib/tictactoe/cell.rb
|
21
|
+
- lib/tictactoe/cell_line.rb
|
22
|
+
- lib/tictactoe/player.rb
|
23
|
+
- lib/tictactoe/string_patch.rb
|
24
|
+
- lib/yolo_tic_tac_toe.rb
|
25
|
+
homepage: https://rubygems.org/gems/hola
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.3.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Play tic tac toe in the console
|
48
|
+
test_files: []
|