Class: Display

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

Instance Method Summary collapse

Constructor Details

#initializeDisplay

Returns a new instance of Display.



6
7
8
# File 'lib/Display.rb', line 6

def initialize
  @TicTacToe = Gato.new
end

Instance Method Details

#get_column_to_moveObject



52
53
54
55
56
# File 'lib/Display.rb', line 52

def get_column_to_move
  print "Column [1-3]: "
  new_column = gets.chomp
  new_column.to_i
end

#get_row_to_moveObject



46
47
48
49
50
# File 'lib/Display.rb', line 46

def get_row_to_move
  print "Row [1-3]: "
  new_row = gets.chomp
  new_row.to_i
end

#playObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/Display.rb', line 10

def play
   print_rules
  while not @TicTacToe.isFinished
    print @TicTacToe.currentPlayer, "'s turn >>>"
    print_board
    new_row = get_row_to_move
    new_column = get_column_to_move
    move_result =  @TicTacToe.intended_move(new_row, new_column)
    put_result_operation(move_result)
  end
  print_board
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/Display.rb', line 31

def print_board
puts ''
puts "_______ _______ _______"   
  for row in (0..2)
    puts "       |       |       |"
    for column in (0..2)
      casilla = @TicTacToe.board[row][column]
      print "   ", casilla, "   |"
    end
    puts ''
    puts "_______|_______|_______|"
  end
  puts ''
end


23
24
25
26
27
28
29
# File 'lib/Display.rb', line 23

def print_rules
  puts "******TIC TAC TOE********"
  puts "Objective of the game: get 3 X's or O's in a row, column or diagonal"
  puts "Each player will make a move and then, the other player will play until"
  puts "one of the players win or a draw is reached."
puts "The X will go first."
end

#put_result_operation(operation_number) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/Display.rb', line 58

def put_result_operation(operation_number)
  case operation_number
  when -2#Gato:BOX_TAKEN
    puts "That box is taken, try another one"
  when -1 #Gato:INVALID_POSITION
    puts "The Position you choose is out of the board, try another one"
  when 0 #Gato:SUCCESSFUL_MOVE
    puts "The move was made."
  when 1 #Gato:GAME_FINISHED
    puts "The game ended"
    print "The winner was player ", @TicTacToe.currentPlayer
  when 2 #Gato:GAME_TIED
    puts "The game resulted in a tie, better luck next time"
  end
end