-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer.rb
70 lines (60 loc) · 1.5 KB
/
computer.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
# frozen_string_literal: true
require './helper'
# Is used to determine the computer plays
class Computer
def initialize(computer)
@computer = computer
end
def eval_board(board, difficulty)
spot = nil
return board[4] = @computer if board[4] == '4'
until spot
spot = get_best_move(board, @computer, difficulty)
if board[spot] != 'X' && board[spot] != 'O'
board[spot] = @computer
else
spot = nil
end
end
end
def get_best_move(board, computer, difficulty)
available_spaces = board.select { |b| Helper.place_empty?(b) }
case difficulty
when '0'
random_move(available_spaces)
when '1'
if rand < 0.5
random_move(available_spaces)
else
ideal_move(available_spaces, board, computer)
end
else
ideal_move(available_spaces, board, computer)
end
end
def random_move(available_spaces)
n = rand(0..available_spaces.count)
available_spaces[n].to_i
end
def ideal_move(available_spaces, board, _computer)
best_move = nil
available_spaces.each do |as|
board[as.to_i] = @computer
if Helper.end_game(board)
best_move = as.to_i
board[as.to_i] = as
best_move
else
board[as.to_i] = @player
if Helper.end_game(board)
best_move = as.to_i
board[as.to_i] = as
best_move
else
board[as.to_i] = as
end
end
end
best_move || random_move(available_spaces)
end
end