repl.it
@ericqweinstein/

tic_tac_toe.rb

Ruby

A little CLI Tic-Tac-Toe game written in Ruby.

fork
loading
Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Tic-Tac-Toe in Ruby!
#
# Based on work by Clederson Cruz.
# @see https://github.com/Cledersonbc/tic-tac-toe-minimax
# Distributed under the GNU General Public License (GPL).

HUMAN = -1
COMP = 1
INFINITY = 1.0 / 0.0

@board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

def evaluate(state)
  if wins(state, COMP)
    1
  elsif wins(state, HUMAN)
    -1
  else
    0
  end
end


def wins(state, player)
  win_state = [
    [state[0][0], state[0][1], state[0][2]],
    [state[1][0], state[1][1], state[1][2]],
    [state[2][0], state[2][1], state[2][2]],
    [state[0][0], state[1][0], state[2][0]],
    [state[0][1], state[1][1], state[2][1]],
    [state[0][2], state[1][2], state[2][2]],
    [state[0][0], state[1][1], state[2][2]],
    [state[2][0], state[1][1], state[0][2]]
  ]

  win_state.include? [player, player, player]
end


def game_over(state)
  wins(state, HUMAN) || wins(state, COMP)
end


def empty_cells(state)
  [].tap do |cells|
    state.each_with_index do |row, x|
      row.each_with_index do |cell, y|
        cells << [x, y] if cell.zero?
      end
    end
  end
end


def set_move(x, y, player)
  if empty_cells(@board).include? [x, y]
    @board[x][y] = player
    return true
  end

  false
end


def minimax(state, depth, player)
  if player == COMP
    best = [-1, -1, -INFINITY]
  else
    best = [-1, -1, INFINITY]
  end

  if depth.zero? || game_over(state)
    score = evaluate state
    return [-1, -1, score]
  end

  empty_cells(state).each do |cell|
    x, y = cell[0], cell[1]
    state[x][y] = player
    score = minimax state, depth - 1, -player
    state[x][y] = 0
    score[0], score[1] = x, y

    if player == COMP
      if score[2] > best[2]
        best = score
      end
    else
      if score[2] < best[2]
        best = score
      end
    end
  end

  best
end


def render(state, c_choice, h_choice)
  state.each do |row|
    puts "\n---------------"
    row.each do |cell|
      if cell == COMP
        print "| #{c_choice} |"
      elsif cell == HUMAN
        print "| #{h_choice} |"
      else
        print "|   |"
      end
    end
  end
  puts "\n---------------"
end


def clear_screen
  puts "\n" * 40
end


def ai_turn(c_choice, h_choice)
  depth = empty_cells(@board).size
  return if depth.zero? || game_over(@board)

  clear_screen
  puts "Computer turn [#{c_choice}]"
  render @board, c_choice, h_choice

  if depth == 9
    x = [0, 1, 2].sample
    y = [0, 1, 2].sample
  else
    move = minimax @board, depth, COMP
    x, y = move[0], move[1]
  end

  set_move x, y, COMP
  sleep 1
end


def human_turn(c_choice, h_choice)
  depth = empty_cells(@board).size
  return if depth.zero? || game_over(@board)

  # Hash of valid moves
  move = -1
  moves = {
    1 => [0, 0], 2 => [0, 1], 3 => [0, 2],
    4 => [1, 0], 5 => [1, 1], 6 => [1, 2],
    7 => [2, 0], 8 => [2, 1], 9 => [2, 2]
  }

  clear_screen
  puts "Human turn [#{h_choice}]"
  render @board, c_choice, h_choice

  while move < 1 || move > 9
    begin
      print 'Use numpad (1..9): '
      move = gets.chomp.to_i
      coord = moves[move]
      try_move = set_move coord[0], coord[1], HUMAN

      if !try_move
        puts 'Invalid move'
        move = -1
      end
    rescue SignalException
      puts 'Bye!'
      exit
    rescue StandardError
      puts 'Invalid choice'
    end
  end
end


def main
  h_choice = ''
  c_choice = ''
  first = ''

  while h_choice != 'O' && h_choice != 'X'
    begin
      puts 'Choose X or O:'
      h_choice = gets.chomp.upcase
      puts "Chosen: #{h_choice}"
    rescue SignalException
      puts 'Bye!'
      exit
    rescue StandardError
      puts 'Invalid choice'
    end
  end

  if h_choice == 'X'
    c_choice = 'O'
  else
    c_choice = 'X'
  end

  while first != 'Y' && first != 'N'
    begin
      puts 'First to start? [y/n]: '
      first = gets.chomp.upcase
    rescue SignalException
      puts 'Bye!'
      exit
    rescue StandardError
      puts 'Invalid choice'
    end
  end

  while empty_cells(@board).size > 0 && !game_over(@board)
    if first == 'N'
      ai_turn c_choice, h_choice
      first = ''
    end

    human_turn c_choice, h_choice
    ai_turn c_choice, h_choice
  end

  if wins @board, HUMAN
    clear_screen
    puts "Human turn [#{h_choice}]"
    render @board, c_choice, h_choice
    puts 'You win!'
  elsif wins @board, COMP
    clear_screen
    puts "Computer turn [#{c_choice}]"
    render @board, c_choice, h_choice
    puts 'Computer wins!'
  else
    clear_screen
    render @board, c_choice, h_choice
    puts 'It\'s a tie!'
  end

  exit 0
end

main
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]