capim_tictactoe 0.1.17 → 1.0.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
  SHA256:
3
- metadata.gz: 9a47236f9c8f1d4dede27095e510553ce0ed450120e0271e0355a32e5498b393
4
- data.tar.gz: a58954d61b3b6dc78c72dbf9753492aa5c9eb930b91b07fd4025a98ff0735fa7
3
+ metadata.gz: fdf5bac7bf1e398ca35afb15af94f6e8998b718f16ac6c925f6afa496e7e84f2
4
+ data.tar.gz: c28147c0e3ca615dd0bfad0db4cf2856961c8736effbf23c555e77e6dc24534f
5
5
  SHA512:
6
- metadata.gz: 2f83d9e6bf6c68e4118d801585184cfa0fc598bd01c275d8963274ff8b9dc655483bbe1f4021ae897a867526fe2399633d816be3f5cf24f40538efd7ed02f4fb
7
- data.tar.gz: 4d6b5d47f5276272a3233aede9eb368dfff0ac0a8eaae54d0bff1c85c92466947fe8d72e97cd6b987d63256eff0dd813eb233a4293f3c3cfd3217e35f330049c
6
+ metadata.gz: acc7fe6e11b68bed57615d10ce0cd0b440d3803592c5398dd25d9b7b8366fba583bc0699330077b5c60b5aaa3d63c4c10bd3179dc0b8ad9394f0819511faa16a
7
+ data.tar.gz: e273149946bd88438facc9bf3f48317e36010ed95e0a29010166f359f3e96f57bb0be665d0039496e16d3db8d04474c4d3360109b271832e7f2b62518c7bf1aa
@@ -33,7 +33,7 @@ class Board
33
33
  end
34
34
 
35
35
  def game_over?(spots = @grid.spots)
36
- valid_line?(@grid.rows(spots)) || valid_line?(@grid.columns(spots)) || valid_line?(@grid.diagonals)
36
+ valid_line?(@grid.rows(spots)) || valid_line?(@grid.columns(spots)) || valid_line?(@grid.diagonals(spots))
37
37
  end
38
38
 
39
39
  def tie?
@@ -1,17 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Evaluator
4
- def eval_board(board, human_marker, computer_marker)
4
+ def eval_board(board, human_marker, computer_marker, game_mode)
5
+ # EASY behavior:
6
+ # 1 - Mark center if available
7
+ # 2 - Go through all available moves and checks if one of them is the winning move.
8
+ # 3 - If not, mark a random spot.
9
+ #
10
+ # HARD behavior:
11
+ # 1 - Mark center if available.
12
+ # 2 - Go through all available moves and checks if one of them is the winning move for both the machine or the player.
13
+ # 3 - If not, mark a random spot.
14
+
5
15
  unless board.center_marked?
6
16
  board.mark_center(computer_marker)
7
17
  return
8
18
  end
9
19
 
10
- spot = get_best_move(board, human_marker, computer_marker)
20
+ spot = get_best_move(board, human_marker, computer_marker, game_mode)
11
21
  board.grid.marked?(spot) ? nil : board.grid.spot_marker(spot, computer_marker)
12
22
  end
13
23
 
14
- def get_best_move(board, human_marker, computer_marker)
24
+ def get_best_move(board, human_marker, computer_marker, game_mode)
15
25
  best_move = nil
16
26
 
17
27
  board.available_spots.each do |available_spot|
@@ -20,7 +30,7 @@ module Evaluator
20
30
  return best_move
21
31
  end
22
32
 
23
- best_move = available_spot.to_i if winning_move?(board, human_marker, available_spot.to_i)
33
+ best_move = available_spot.to_i if game_mode == :hard && winning_move?(board, human_marker, available_spot.to_i)
24
34
  end
25
35
  return best_move if best_move
26
36
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Game
4
+ require 'optparse'
4
5
  require_relative 'board'
5
6
  require_relative 'evaluator'
6
7
  require_relative 'color'
@@ -16,6 +17,7 @@ class Game
16
17
  @board = Board.new
17
18
  @computer = x_marker
18
19
  @human = o_marker
20
+ parse_args
19
21
  end
20
22
 
21
23
  def start_game
@@ -24,7 +26,7 @@ class Game
24
26
  until @board.game_over? || @board.tie?
25
27
  fetch_human_spot(@board, @human)
26
28
 
27
- eval_board(@board, @human, @computer) unless @board.game_over? || @board.tie?
29
+ eval_board(@board, @human, @computer, @options[:game_mode]) unless @board.game_over? || @board.tie?
28
30
  @board.grid.display
29
31
  end
30
32
 
@@ -31,8 +31,8 @@ class Grid
31
31
  rows(spots).to_a.transpose
32
32
  end
33
33
 
34
- def diagonals
35
- [[@grid[0], @grid[4], @grid[8]], [@grid[2], @grid[4], @grid[6]]]
34
+ def diagonals(spots = grid)
35
+ [[spots[0], spots[4], spots[8]], [spots[2], spots[4], spots[6]]]
36
36
  end
37
37
 
38
38
  def spot_marker(spot, marker)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rainbow'
4
+
3
5
  module InputHandler
4
6
  def fetch_human_spot(board, human_marker)
5
7
  spot = nil
@@ -14,6 +16,23 @@ module InputHandler
14
16
  board.grid.spot_marker(spot, human_marker)
15
17
  end
16
18
 
19
+ def parse_args
20
+ @options = { game_mode: :hard, ascii_art: true, jokes: true }
21
+ OptionParser.new do |opts|
22
+ opts.banner = Rainbow('Usage: capim_tictactoe [options], the game will default to challenging mode.').red
23
+
24
+ opts.on('-e', '--easy', 'Runs the game in easy mode.') do
25
+ @options[:game_mode] = :easy
26
+ end
27
+ opts.on('-a', '--noart', 'Runs the game without ASCII art.') do
28
+ @options[:ascii_art] = false
29
+ end
30
+ opts.on('-j', '--nojokes', 'Runs the game without the silly lines.') do
31
+ @options[:jokes] = false
32
+ end
33
+ end.parse!
34
+ end
35
+
17
36
  private
18
37
 
19
38
  # to_i converts 'non integer' strings to 0: 'a'.to_i => 0.
@@ -8,8 +8,8 @@ module Presenter
8
8
  GREEN_GRASS = AsciiArt::GREEN_GRASS
9
9
 
10
10
  def present_game
11
- print GREEN_GRASS
12
- puts "\n#{SILLY_LINE}\n\n"
11
+ print GREEN_GRASS if @options[:ascii_art]
12
+ puts "\n#{SILLY_LINE}\n\n" if @options[:jokes]
13
13
  @board.grid.display
14
14
  puts 'Enter [0-8]'
15
15
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Capimtictactoe
2
- VERSION = '0.1.17'
4
+ VERSION = '1.0.0'
3
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capim_tictactoe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Maia