Class: TicTacToe

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

Instance Method Summary collapse

Constructor Details

#initializeTicTacToe

Returns a new instance of TicTacToe.



2
3
4
5
6
7
8
9
10
# File 'lib/gamelib/ttt/ttt.rb', line 2

def initialize
  system('clear')
  @@row = @@col = 3
  @@p_turn  = '1'  # which player's turn is it
  @@players = {'1': {name: nil, mark: 'O'}, '2': {name: nil, mark: 'X'}}
  @board    = [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ]    # 3x3 board
  @playing  = true
  set_players_name
end

Instance Method Details

#display_boardObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gamelib/ttt/ttt.rb', line 35

def display_board
  puts "===================================="
  puts "%10s vs. %-10s" % [@@players[:'1'][:name], @@players[:'2'][:name]]
  puts "===================================="
  puts "\t %s | %s | %s" % [color(@board[0][0]), color(@board[0][1]), color(@board[0][2])]
  puts "\t___________"
  puts "\t %s | %s | %s" % [color(@board[1][0]), color(@board[1][1]), color(@board[1][2])]
  puts "\t___________"
  puts "\t %s | %s | %s" % [color(@board[2][0]), color(@board[2][1]), color(@board[2][2])]
  puts
end

#playObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gamelib/ttt/ttt.rb', line 20

def play
  for i in 0...9 do
    display_board
    turn
    return if !@playing
    if i==8 
      system('clear')
      display_board
      puts "%15s" % "Draw!"
      break
    end
    system('clear')
  end
end

#set_players_nameObject



12
13
14
15
16
17
18
# File 'lib/gamelib/ttt/ttt.rb', line 12

def set_players_name
  puts "Enter player1's name: "
  @@players[:'1'][:'name'] = gets.chomp
  puts "Enter player2's name: "
  @@players[:'2'][:'name'] = gets.chomp
  system('clear');
end

#turnObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gamelib/ttt/ttt.rb', line 47

def turn
  print "#{@@players[@@p_turn.to_sym][:name]}'s turn: " 
  cell = gets.to_i - 1
  while cell < 0 or cell > 8 do
    print "#{@@players[@@p_turn.to_sym][:name]}'s turn: " 
    cell = gets.to_i - 1
  end

  row = cell / @@row
  col = cell % @@col

  while @board[row][col] == 'O' or @board[row][col] == 'X'
    print "#{@@players[@@p_turn.to_sym][:name]}'s turn: " 
    cell = gets.to_i - 1
    row = cell / @@row
    col = cell % @@col
  end

  @board[row][col] = @@players[@@p_turn.to_sym][:mark]

  validate

  @@p_turn = @@p_turn == '1' ? '2' : '1'
end

#validateObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gamelib/ttt/ttt.rb', line 72

def validate
  # horiznotal and vertical check
  0.upto(2) do |i|
    if (@board[i][0] == @board[i][1] and @board[i][1] == @board[i][2]) or
        (@board[0][i] == @board[1][i] and @board[1][i] == @board[2][i]) 
      system('clear')
      display_board
      puts "%15s" % "#{@@players[@@p_turn.to_sym][:name]} won"
      @playing = false;
    end
  end

  # diagonal
  if (@board[0][0] == @board[1][1] and @board[1][1] == @board[2][2]) or
      (@board[0][2] == @board[1][1] and @board[1][1] == @board[2][0])
    system('clear')
    display_board
    puts "%15s" % "#{@@players[@@p_turn.to_sym][:name]} won"
    @playing = false;
  end

  # draw
end