tic_tac_toe_master 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 5e3659bac6c92e015436335be6b70d1871568c397c45ce3d41a6e2dec0466cfd
4
- data.tar.gz: 1903f663f4a5a694ff3d89068311d9c3194d9244baa5a11279ebf7738a47edec
3
+ metadata.gz: 9232d960106fae766f2e5d13f4963e7694e34590eb8b726510b78d1088770c69
4
+ data.tar.gz: a1bbe1379141080c951f6c93b5c9028bc32a7bd2f88c73864e1c04b70ec332d8
5
5
  SHA512:
6
- metadata.gz: 57ad2bf0255629b7fc84a68acfdad2e6e3c1baabc32be573c4c08ab77dafe985acfe87cd998670c0404369c0f6ec6a01af1f9958985b08a69a815c94c734799e
7
- data.tar.gz: b0a23baae0a35103a57b077cccdfe2020e37ade52e817fe6c7a786d8b6b973eafb79c0f7c30e6bf6b265f0d8fa45466d30c7bff17d13349e7df20374553c1eb8
6
+ metadata.gz: b4f18a926b5e2f5c98907a2894baf81429f8f4349998ca5c9cfe300db3c0217aa017658527fc0c5c78616e8e6a812af27ca3f584763112ca9b65f0defb8c2346
7
+ data.tar.gz: 56e978fd0fdb40830d571d4956a95678b641567eacd1494cc77aef52720e3c4c1003da82f155d5228f7a560cd106a35c87a45821658bb54f9224d707896a6e24
@@ -7,25 +7,10 @@ module TicTacToeMaster
7
7
 
8
8
  FIELD_SIZE = (1..9)
9
9
 
10
- WIN_CONBINATIONS = [
11
- [0, 1, 2], [3, 4, 5], [6, 7, 8],
12
- [0, 3, 6], [1, 4, 7], [2, 5, 8],
13
- [0, 4, 8], [2, 4, 6]
14
- ].freeze
15
-
16
10
  def initialize
17
11
  @grid = FIELD_SIZE.to_a
18
12
  end
19
13
 
20
- def draw
21
- puts '-----------'
22
-
23
- grid.each_slice(3) do |row|
24
- puts " #{row.join(' | ')} "
25
- puts '-----------'
26
- end
27
- end
28
-
29
14
  def place(position, symbol)
30
15
  return false unless valid_position?(position)
31
16
 
@@ -40,11 +25,5 @@ module TicTacToeMaster
40
25
  def full?
41
26
  grid.none?(Integer)
42
27
  end
43
-
44
- def winner?(symbol)
45
- WIN_CONBINATIONS.any? do |combo|
46
- combo.all? { |index| grid[index] == symbol }
47
- end
48
- end
49
28
  end
50
29
  end
@@ -5,6 +5,12 @@ module TicTacToeMaster
5
5
  class Game
6
6
  attr_reader :player1, :player2, :current_player, :board
7
7
 
8
+ WIN_CONBINATIONS = [
9
+ [0, 1, 2], [3, 4, 5], [6, 7, 8],
10
+ [0, 3, 6], [1, 4, 7], [2, 5, 8],
11
+ [0, 4, 8], [2, 4, 6]
12
+ ].freeze
13
+
8
14
  def initialize(player1_name:, player2_name:)
9
15
  @player1 = TicTacToeMaster::Player.new(player1_name, 'X')
10
16
  @player2 = TicTacToeMaster::Player.new(player2_name, 'O')
@@ -21,7 +27,7 @@ module TicTacToeMaster
21
27
  def make_move(position)
22
28
  return :invalid unless board.place(position, current_player.symbol)
23
29
 
24
- if board.winner?(current_player.symbol)
30
+ if winner?(current_player.symbol)
25
31
  :win
26
32
  elsif board.full?
27
33
  :draw
@@ -31,6 +37,12 @@ module TicTacToeMaster
31
37
  end
32
38
  end
33
39
 
40
+ def winner?(symbol)
41
+ WIN_CONBINATIONS.any? do |combo|
42
+ combo.all? { |index| board.grid[index] == symbol }
43
+ end
44
+ end
45
+
34
46
  private
35
47
 
36
48
  def switch_player
@@ -12,17 +12,17 @@ module TicTacToeMaster
12
12
  puts '🎮 Welcome to Tic Tac Toe!'
13
13
 
14
14
  loop do
15
- @game.board.draw
15
+ puts draw_board(@game.board)
16
16
 
17
17
  case @game.make_move(ask_for_move)
18
18
  when :invalid
19
19
  puts '❌ Invalid move, try again.'
20
20
  when :win
21
- @game.board.draw
21
+ puts draw_board(@game.board)
22
22
  puts "🏆 #{@game.current_player.name} wins!"
23
23
  break
24
24
  when :draw
25
- @game.board.draw
25
+ puts draw_board(@game.board)
26
26
  puts "🤝 It's a draw!"
27
27
  break
28
28
  end
@@ -32,6 +32,27 @@ module TicTacToeMaster
32
32
 
33
33
  private
34
34
 
35
+ def draw_board(board)
36
+ rows = board.grid.each_slice(3).map do |row|
37
+ "║ #{row[0]} ║ #{row[1]} ║ #{row[2]} ║"
38
+ end
39
+
40
+ top = '╔═════╦═════╦═════╗'
41
+ middle = '╠═════╬═════╬═════╣'
42
+ bottom = '╚═════╩═════╩═════╝'
43
+
44
+ [
45
+ top,
46
+ rows[0],
47
+ middle,
48
+ rows[1],
49
+ middle,
50
+ rows[2],
51
+ bottom,
52
+ ''
53
+ ].join("\n")
54
+ end
55
+
35
56
  def ask_for_move
36
57
  print "\n#{@game.current_player.name} #{@game.current_player.symbol}, " \
37
58
  "choose a position #{TicTacToeMaster::Board::FIELD_SIZE}: "
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TicTacToeMaster
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tic_tac_toe_master
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitalii Matseiko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-11-13 00:00:00.000000000 Z
11
+ date: 2025-11-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: TicTacToeMaster is a gem that allows you to play Tic Tac Toe in the console.
14
14
  email: