Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/tic_cat_toe/game.rb
Overview
The Game
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#current_player ⇒ Object
Returns the value of attribute current_player.
-
#player1 ⇒ Object
Returns the value of attribute player1.
-
#player2 ⇒ Object
Returns the value of attribute player2.
-
#winner ⇒ Object
Returns the value of attribute winner.
Instance Method Summary collapse
- #check_duplicate(abs, ord) ⇒ Object
- #check_game ⇒ Object
- #check_input(input) ⇒ Object
- #check_range(abs, ord) ⇒ Object
- #check_size(size) ⇒ Object
- #check_slots? ⇒ Boolean
-
#initialize(*args) ⇒ Game
constructor
A new instance of Game.
- #next_turn(mark) ⇒ Object
- #play ⇒ Object
- #player_move(mark) ⇒ Object
- #run ⇒ Object
- #show_game ⇒ Object
- #yes_not(input) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tic_cat_toe/game.rb', line 11 def initialize(*args) puts args @board = Board.new(args[0] || 3) @current_player = args[1] || 'O' @player1 = Player.new('X') @player2 = Player.new('O') @current_x = 0 @current_y = 0 @winner = false end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
9 10 11 |
# File 'lib/tic_cat_toe/game.rb', line 9 def board @board end |
#current_player ⇒ Object
Returns the value of attribute current_player.
9 10 11 |
# File 'lib/tic_cat_toe/game.rb', line 9 def current_player @current_player end |
#player1 ⇒ Object
Returns the value of attribute player1.
9 10 11 |
# File 'lib/tic_cat_toe/game.rb', line 9 def player1 @player1 end |
#player2 ⇒ Object
Returns the value of attribute player2.
9 10 11 |
# File 'lib/tic_cat_toe/game.rb', line 9 def player2 @player2 end |
#winner ⇒ Object
Returns the value of attribute winner.
9 10 11 |
# File 'lib/tic_cat_toe/game.rb', line 9 def winner @winner end |
Instance Method Details
#check_duplicate(abs, ord) ⇒ Object
43 44 45 |
# File 'lib/tic_cat_toe/game.rb', line 43 def check_duplicate(abs, ord) @board.check_slot(abs, ord) ? true : false end |
#check_game ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tic_cat_toe/game.rb', line 81 def check_game show_game case @winner when true @current_player = next_turn(@current_player) puts "#{@current_player} WINS! Do you want to play again? Y/N" yes_not(gets.chomp.downcase) when false puts 'DRAW! Do you want to play again? Y/N' yes_not(gets.chomp.downcase) end end |
#check_input(input) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tic_cat_toe/game.rb', line 22 def check_input(input) if /^([0-9]+)[,|.]\s*([0-9]+)$/.match(input) x = input.split(//)[0].to_i y = input.split(//)[2].to_i if check_range(x, y) && check_duplicate(x, y) @current_x = x @current_y = y return true end end false end |
#check_range(abs, ord) ⇒ Object
39 40 41 |
# File 'lib/tic_cat_toe/game.rb', line 39 def check_range(abs, ord) (0..@board.size).cover?(abs) && (0..@board.size).cover?(ord) ? true : false end |
#check_size(size) ⇒ Object
35 36 37 |
# File 'lib/tic_cat_toe/game.rb', line 35 def check_size(size) size.is_a?(Integer) && (3..9).cover?(size) ? true : false end |
#check_slots? ⇒ Boolean
47 48 49 |
# File 'lib/tic_cat_toe/game.rb', line 47 def check_slots? @board.available_slots ? true : false end |
#next_turn(mark) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/tic_cat_toe/game.rb', line 62 def next_turn(mark) case mark when 'X' then 'O' when 'O' then 'X' end end |
#play ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/tic_cat_toe/game.rb', line 94 def play puts 'Type an integer number=> ' new_dimention = gets.chomp.to_i if check_size(new_dimention) initialize(new_dimention, next_turn(@current_player)) run else play end end |
#player_move(mark) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tic_cat_toe/game.rb', line 69 def player_move(mark) print "Player #{mark}=> " player = gets.chomp case check_input(player) when true @winner = @board.put_mark(@current_x, @current_y, mark) @current_player = next_turn(mark) when false then player_move(mark) else puts 'Something has gone wrong' end end |
#run ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/tic_cat_toe/game.rb', line 105 def run while @winner != true break if check_slots? show_game case @current_player when player1.mark then player_move(player1.mark) when player2.mark then player_move(player2.mark) else puts 'Comun demonio! Lo que faltaba' end end check_game end |
#show_game ⇒ Object
55 56 57 58 59 60 |
# File 'lib/tic_cat_toe/game.rb', line 55 def show_game OS.windows? ? system('cls') : system('clear') # system('cls') # puts "Player X: #{player1.victories} Player O: #{player2.victories}" @board.to_print end |
#yes_not(input) ⇒ Object
51 52 53 |
# File 'lib/tic_cat_toe/game.rb', line 51 def yes_not(input) input.eql?('y') || input.eql?('yes') ? play : false end |