ttt_gem_8thlight 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/game.rb +1 -3
- data/lib/ttt.rb +44 -36
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca1b97f0a33561487272e06ca4f1a0b72780ffbb
|
4
|
+
data.tar.gz: 28499f2ae30528728c6598e16da377bc8d71f0fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac96d3e8c1065a638dc095a178a8267dd740fc7dd06f92f682c1480f81ec422e14ca1b167e55e91c63d5c3bb32888404878e538a9694c0a1a55ee10795c1123c
|
7
|
+
data.tar.gz: f78b39ef0d0a0b7491c6f45d4036c97544922d8f12704ac80e80b3a99b4c3f1f0e45c38541304f808533453fc54be5ddacf2431987018c4d2721eb87efbe8e2b
|
data/lib/game.rb
CHANGED
data/lib/ttt.rb
CHANGED
@@ -2,71 +2,79 @@ class TTT
|
|
2
2
|
PLAYER_ONE = 'x'
|
3
3
|
PLAYER_TWO = 'o'
|
4
4
|
|
5
|
-
attr_accessor :board, :
|
5
|
+
attr_accessor :board, :current_player
|
6
6
|
|
7
|
-
def initialize(board = Array.new(9) {"-"},
|
7
|
+
def initialize(board = Array.new(9) {"-"}, current_player = PLAYER_ONE)
|
8
8
|
@board = board
|
9
|
-
@
|
9
|
+
@current_player = current_player
|
10
10
|
end
|
11
11
|
|
12
12
|
def make_move(space)
|
13
|
-
@board[space] = @
|
14
|
-
@
|
13
|
+
@board[space] = @current_player
|
14
|
+
@current_player = @current_player == PLAYER_ONE ? PLAYER_TWO : PLAYER_ONE
|
15
15
|
end
|
16
16
|
|
17
17
|
def available_spaces
|
18
18
|
@board.map.with_index { |space, index| index if space == '-' }.compact
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
def new_move(space)
|
26
|
-
new_ttt = deep_copy
|
27
|
-
new_ttt.make_move(space)
|
28
|
-
new_ttt
|
21
|
+
def new_board_with_move(space)
|
22
|
+
board_with_current_player = deep_copy
|
23
|
+
board_with_current_player.make_move(space)
|
24
|
+
board_with_current_player
|
29
25
|
end
|
30
26
|
|
31
27
|
def deep_copy
|
32
28
|
copy = dup
|
33
29
|
copy.board = board.dup
|
34
|
-
copy.
|
30
|
+
copy.current_player = current_player.dup
|
35
31
|
copy
|
36
32
|
end
|
37
33
|
|
38
|
-
def
|
39
|
-
available_spaces.map { |space|
|
34
|
+
def possible_boards
|
35
|
+
available_spaces.map { |space| new_board_with_move(space) }
|
40
36
|
end
|
41
37
|
|
42
38
|
def possible_values
|
43
|
-
|
39
|
+
possible_boards.map { |board| board.minimax }
|
40
|
+
end
|
41
|
+
|
42
|
+
def best_move
|
43
|
+
return available_spaces.max_by { |space| new_board_with_move(space).minimax } if @current_player == PLAYER_ONE
|
44
|
+
return available_spaces.min_by { |space| new_board_with_move(space).minimax } if @current_player == PLAYER_TWO
|
44
45
|
end
|
45
46
|
|
46
47
|
def minimax
|
47
|
-
return 100 if
|
48
|
-
return -100 if
|
49
|
-
return 0 if
|
50
|
-
|
51
|
-
|
52
|
-
return possible_values.max + count_empty_spaces if @turn == PLAYER_ONE
|
53
|
-
return possible_values.min - count_empty_spaces if @turn == PLAYER_TWO
|
48
|
+
return 100 if x_won
|
49
|
+
return -100 if o_won
|
50
|
+
return 0 if tie
|
51
|
+
return possible_values.max + available_spaces.count if @current_player == PLAYER_ONE
|
52
|
+
return possible_values.min - available_spaces.count if @current_player == PLAYER_TWO
|
54
53
|
end
|
55
54
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
55
|
+
def x_won
|
56
|
+
winning_player?(PLAYER_ONE)
|
57
|
+
end
|
58
|
+
|
59
|
+
def o_won
|
60
|
+
winning_player?(PLAYER_TWO)
|
61
|
+
end
|
62
|
+
|
63
|
+
def tie
|
64
|
+
!winning_player?(PLAYER_ONE) &&
|
65
|
+
!winning_player?(PLAYER_TWO) &&
|
66
|
+
available_spaces.count == 0
|
59
67
|
end
|
60
68
|
|
61
|
-
def winning_player?(
|
62
|
-
return true if @board[0..2] == [
|
63
|
-
return true if @board[3..5] == [
|
64
|
-
return true if @board[6..8] == [
|
65
|
-
return true if [@board[0], @board[3], @board[6]] == [
|
66
|
-
return true if [@board[1], @board[4], @board[7]] == [
|
67
|
-
return true if [@board[2], @board[5], @board[8]] == [
|
68
|
-
return true if [@board[0], @board[4], @board[8]] == [
|
69
|
-
return true if [@board[2], @board[4], @board[6]] == [
|
69
|
+
def winning_player?(current_player)
|
70
|
+
return true if @board[0..2] == [current_player] * 3
|
71
|
+
return true if @board[3..5] == [current_player] * 3
|
72
|
+
return true if @board[6..8] == [current_player] * 3
|
73
|
+
return true if [@board[0], @board[3], @board[6]] == [current_player] * 3
|
74
|
+
return true if [@board[1], @board[4], @board[7]] == [current_player] * 3
|
75
|
+
return true if [@board[2], @board[5], @board[8]] == [current_player] * 3
|
76
|
+
return true if [@board[0], @board[4], @board[8]] == [current_player] * 3
|
77
|
+
return true if [@board[2], @board[4], @board[6]] == [current_player] * 3
|
70
78
|
end
|
71
79
|
end
|
72
80
|
|