tic-tac-toe-with-ai 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/computer_player/computer_player.rb +7 -10
- data/lib/tic_tac_toe/board.rb +12 -12
- data/lib/tic_tac_toe/game.rb +15 -24
- data/lib/tic_tac_toe/rules.rb +13 -15
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e62b808f2c145609626467665cc40bd318d0d39f
|
4
|
+
data.tar.gz: 362ad948c77f086b98acd00001c9c319ff73cb31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 476ca0249d34210f61b2152f5ef6b0a33fbbf358ea073ef438ebc3cce8a40cf6742dfc742d20cb36ef64e2d7c127fc175c5d2b362409e8b3e020cce15b1d4cf8
|
7
|
+
data.tar.gz: b4b32d055f8139f0d9a3d269dbfc4d5662e1228435d9c8b8628d8bf42aae54c3a4ad2d46336e8839155fbe6aaecf7c141a51a830f0d0cf17fa0306742bb9dfbe
|
@@ -1,18 +1,18 @@
|
|
1
1
|
module TicTacToe
|
2
|
-
|
2
|
+
|
3
3
|
class ComputerPlayer
|
4
4
|
|
5
5
|
def initialize
|
6
6
|
@triples = [ [1,2,3], [4,5,6], [7,8,9], [1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7] ]
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def decide_move(board)
|
10
10
|
return find_winning_cell(board, "O") if find_winning_cell(board, "O") != nil
|
11
11
|
return find_winning_cell(board, "X") if find_winning_cell(board, "X") != nil
|
12
12
|
return random_move(board)
|
13
13
|
end
|
14
14
|
|
15
|
-
def find_winning_cell(board, mark)
|
15
|
+
def find_winning_cell(board, mark)
|
16
16
|
return find_cell(board, mark) if find_cell(board, mark) != nil
|
17
17
|
return nil
|
18
18
|
end
|
@@ -27,7 +27,7 @@ module TicTacToe
|
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
30
|
-
|
30
|
+
|
31
31
|
def find_cell(board, mark)
|
32
32
|
@triples.each do |triplet|
|
33
33
|
define_permutation(triplet[0], triplet[1], triplet[2]).each do |triple|
|
@@ -36,7 +36,7 @@ module TicTacToe
|
|
36
36
|
end
|
37
37
|
return nil
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def define_permutation(cell_one, cell_two, cell_three)
|
41
41
|
return [cell_one, cell_two, cell_three].permutation.to_a
|
42
42
|
end
|
@@ -50,12 +50,9 @@ module TicTacToe
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def spot_is_empty?(board, position)
|
53
|
-
[
|
54
|
-
return false if board[position] == mark
|
55
|
-
end
|
56
|
-
return true
|
53
|
+
board[position].nil?
|
57
54
|
end
|
58
55
|
|
59
56
|
end
|
60
|
-
|
57
|
+
|
61
58
|
end
|
data/lib/tic_tac_toe/board.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
module TicTacToe
|
2
|
-
|
2
|
+
|
3
3
|
class Board
|
4
|
-
|
4
|
+
|
5
5
|
def initialize
|
6
6
|
@board_positions = []
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def set_mark(mark, position)
|
10
10
|
@board_positions[position] = mark
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def get_mark_at(position)
|
14
14
|
@board_positions[position]
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def return_entire_board
|
18
18
|
return [get_mark_at(1), get_mark_at(2), get_mark_at(3),
|
19
19
|
get_mark_at(4), get_mark_at(5), get_mark_at(6),
|
20
20
|
get_mark_at(7), get_mark_at(8), get_mark_at(9)]
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def rows
|
24
24
|
position_groups_to_marks([
|
25
25
|
[1, 2, 3],
|
@@ -27,7 +27,7 @@ module TicTacToe
|
|
27
27
|
[7, 8, 9]
|
28
28
|
])
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def columns
|
32
32
|
position_groups_to_marks([
|
33
33
|
[1, 4, 7],
|
@@ -35,16 +35,16 @@ module TicTacToe
|
|
35
35
|
[3, 6, 9]
|
36
36
|
])
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def diagonals
|
40
40
|
position_groups_to_marks([
|
41
41
|
[1, 5, 9],
|
42
42
|
[3, 5, 7]
|
43
43
|
])
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
private
|
47
|
-
|
47
|
+
|
48
48
|
def position_groups_to_marks(position_groups)
|
49
49
|
position_groups.collect do |positions|
|
50
50
|
positions.collect do |position|
|
@@ -52,7 +52,7 @@ module TicTacToe
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
end
|
data/lib/tic_tac_toe/game.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
module TicTacToe
|
2
|
+
|
2
3
|
class Game
|
3
|
-
|
4
|
+
|
4
5
|
attr_reader :computer_player, :board
|
5
|
-
|
6
|
+
|
6
7
|
def initialize
|
7
8
|
@board = Board.new
|
8
9
|
@rules = Rules.new(@board)
|
@@ -11,47 +12,37 @@ module TicTacToe
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def in_progress?
|
14
|
-
return false if
|
15
|
-
return false if tie_exists?
|
15
|
+
return false if winner || tie
|
16
16
|
return true
|
17
17
|
end
|
18
18
|
|
19
19
|
def move(position)
|
20
|
-
board.set_mark(current_mark, position)
|
20
|
+
@board.set_mark(current_mark, position)
|
21
21
|
change_current_mark
|
22
22
|
end
|
23
23
|
|
24
24
|
def get_mark_at(position)
|
25
|
-
board.get_mark_at(position)
|
25
|
+
@board.get_mark_at(position)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def winner
|
29
|
-
rules.winner
|
29
|
+
@rules.winner
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def tie
|
33
|
-
rules.tie
|
33
|
+
@rules.tie
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
private
|
37
|
-
|
38
|
-
attr_reader :marks, :rules
|
39
37
|
|
40
38
|
def current_mark
|
41
|
-
marks.first
|
39
|
+
@marks.first
|
42
40
|
end
|
43
|
-
|
41
|
+
|
44
42
|
def change_current_mark
|
45
|
-
marks.reverse!
|
46
|
-
end
|
47
|
-
|
48
|
-
def winner_exists?
|
49
|
-
!winner.nil?
|
50
|
-
end
|
51
|
-
|
52
|
-
def tie_exists?
|
53
|
-
tie
|
43
|
+
@marks.reverse!
|
54
44
|
end
|
55
45
|
|
56
46
|
end
|
47
|
+
|
57
48
|
end
|
data/lib/tic_tac_toe/rules.rb
CHANGED
@@ -1,41 +1,39 @@
|
|
1
1
|
module TicTacToe
|
2
|
+
|
2
3
|
class Rules
|
3
|
-
|
4
|
+
|
4
5
|
def initialize(board)
|
5
6
|
@board = board
|
6
7
|
end
|
7
|
-
|
8
|
+
|
8
9
|
def winner
|
9
10
|
return "tie" if tie
|
10
|
-
["X", "O"].detect
|
11
|
-
winning_mark?(mark)
|
12
|
-
end
|
11
|
+
["X", "O"].detect { |mark| winning_mark?(mark) }
|
13
12
|
end
|
14
|
-
|
13
|
+
|
15
14
|
def tie
|
16
15
|
(1..9).each do |position|
|
17
|
-
return false if @board.get_mark_at(position)
|
16
|
+
return false if @board.get_mark_at(position).nil?
|
18
17
|
end
|
19
18
|
return true
|
20
19
|
end
|
21
|
-
|
20
|
+
|
22
21
|
private
|
23
|
-
|
22
|
+
|
24
23
|
def winning_mark?(mark)
|
25
24
|
potential_winning_combinations.any? do |marks|
|
26
25
|
winning_marks?(marks, mark)
|
27
26
|
end
|
28
27
|
end
|
29
|
-
|
28
|
+
|
30
29
|
def potential_winning_combinations
|
31
|
-
board.rows + board.columns + board.diagonals
|
30
|
+
@board.rows + @board.columns + @board.diagonals
|
32
31
|
end
|
33
|
-
|
32
|
+
|
34
33
|
def winning_marks?(marks, mark)
|
35
34
|
marks == [mark] * 3
|
36
35
|
end
|
37
|
-
|
38
|
-
attr_reader :board
|
39
|
-
|
36
|
+
|
40
37
|
end
|
38
|
+
|
41
39
|
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tic-tac-toe-with-ai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyrus Vandrevala
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: A tic-tac-toe-app with an unbeatable AI.
|
13
|
+
description: A simple tic-tac-toe-app with an unbeatable AI.
|
14
14
|
email: cyrus.vandrevala@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|