Class: TTT::Interface::CLI::Views

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/interface/cli/views.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli) ⇒ Views

Returns a new instance of Views.



11
12
13
# File 'lib/ttt/interface/cli/views.rb', line 11

def initialize(cli)
  self.cli = cli
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



15
16
17
18
# File 'lib/ttt/interface/cli/views.rb', line 15

def method_missing(meth, *args, &block)
  super unless cli.respond_to? meth
  cli.send meth, *args, &block
end

Instance Attribute Details

#cliObject

Returns the value of attribute cli.



9
10
11
# File 'lib/ttt/interface/cli/views.rb', line 9

def cli
  @cli
end

Instance Method Details

#backward_diagonal_winner?(square) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/ttt/interface/cli/views.rb', line 93

def backward_diagonal_winner?(square)
  return false unless winner? square
  [[3, 5], [5, 7], [7, 5]].any? do |s1, s2|
    square == s1 && winner?(s2)
  end
end

#char_for(position) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/ttt/interface/cli/views.rb', line 110

def char_for(position)
  case game[position]
  when nil
    ' '
  when 1
    player1.marker
  when 2
    player2.marker
  end
end

#display_boardObject



20
21
22
23
24
# File 'lib/ttt/interface/cli/views.rb', line 20

def display_board
  fileout.print row(0) << horizontal_separator <<
                row(1) << horizontal_separator <<
                row(2) << "\n\n"
end

#forward_diagonal_winner?(square) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/ttt/interface/cli/views.rb', line 86

def forward_diagonal_winner?(square)
  return false unless winner? square
  [[1, 5], [5, 9], [9, 5]].any? do |s1, s2|
    square == s1 && winner?(s2)
  end
end

#horizontal_separatorObject



38
39
40
# File 'lib/ttt/interface/cli/views.rb', line 38

def horizontal_separator
  "-----|-----|-----\n"
end

#horizontal_winner?(square) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/ttt/interface/cli/views.rb', line 105

def horizontal_winner?(square)
  return false unless winner? square
  winner?(square+1) || winner?(square-1)
end

#line(n) ⇒ Object



31
32
33
34
35
36
# File 'lib/ttt/interface/cli/views.rb', line 31

def line(n)
  offset, col = (n/3)*3, (n%3)
  square(offset+1, col) << vertical_separator <<
  square(offset+2, col) << vertical_separator <<
  square(offset+3, col) << "\n"
end

#line0_for(square) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ttt/interface/cli/views.rb', line 50

def line0_for(square)
  if forward_diagonal_winner?(square)
    "\\    "
  elsif backward_diagonal_winner?(square)
    "    /"
  elsif vertical_winner? square
    "  |  "
  else
    "     "
  end
end

#line1_for(square) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/ttt/interface/cli/views.rb', line 62

def line1_for(square)
  if horizontal_winner? square
    "--%s--"
  else
    "  %s  "
  end % char_for(square)
end

#line2_for(square) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ttt/interface/cli/views.rb', line 70

def line2_for(square)
  if forward_diagonal_winner?(square)
    "    \\"
  elsif backward_diagonal_winner?(square)
    "/    "
  elsif vertical_winner? square
    "  |  "
  else
    "     "
  end
end

#row(n) ⇒ Object



26
27
28
29
# File 'lib/ttt/interface/cli/views.rb', line 26

def row(n)
  n *= 3
  line(n) << line(n+1) << line(n+2)
end

#square(num, line) ⇒ Object



46
47
48
# File 'lib/ttt/interface/cli/views.rb', line 46

def square(num, line)
  send "line#{line}_for", num
end

#vertical_separatorObject



42
43
44
# File 'lib/ttt/interface/cli/views.rb', line 42

def vertical_separator
  "|"
end

#vertical_winner?(square) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/ttt/interface/cli/views.rb', line 100

def vertical_winner?(square)
  return false unless winner? square
  winner? (square + 2) % 9 + 1
end

#winner?(square) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ttt/interface/cli/views.rb', line 82

def winner?(square)
  game.over? && !game.tie? && game.winning_positions.include?(square)
end