Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/tic_cat_toe/game.rb

Overview

The Game

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#boardObject

Returns the value of attribute board.



9
10
11
# File 'lib/tic_cat_toe/game.rb', line 9

def board
  @board
end

#current_playerObject

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

#player1Object

Returns the value of attribute player1.



9
10
11
# File 'lib/tic_cat_toe/game.rb', line 9

def player1
  @player1
end

#player2Object

Returns the value of attribute player2.



9
10
11
# File 'lib/tic_cat_toe/game.rb', line 9

def player2
  @player2
end

#winnerObject

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_gameObject



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

Returns:

  • (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

#playObject



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

#runObject



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_gameObject



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