ttt_gem_8thlight 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ttt.rb +76 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 069fb85e1030b593e24d0b95203cfa254e33a77f
4
+ data.tar.gz: d2d181cc4c9c7b3bea606dcf0045aa1c29c3d282
5
+ SHA512:
6
+ metadata.gz: 8f34f7d8a64742ca21b408bce321221572ac757ef85906d9a5857ded0a209721aba8e201476a547fc96f327a8d9305144dcedfaa04159336410dddd946302e4c
7
+ data.tar.gz: b665ecc4c95c64791165066070482ee731d578fa22e7e28139d8a715455b7aee1568104ec31d19291e3271491cd1c2cb2c51cd45bc544d3ce8127e0eba587ca1
data/lib/ttt.rb ADDED
@@ -0,0 +1,76 @@
1
+ class TTT
2
+ PLAYER_ONE = 'x'
3
+ PLAYER_TWO = 'o'
4
+
5
+ attr_accessor :board, :turn
6
+
7
+ def initialize(board = Array.new(9) {"-"}, turn = PLAYER_ONE)
8
+ @board = board
9
+ @turn = turn
10
+ end
11
+
12
+ def hi
13
+ puts "Hi"
14
+ end
15
+
16
+ def make_move(space)
17
+ @board[space] = @turn
18
+ @turn = @turn == PLAYER_ONE ? PLAYER_TWO : PLAYER_ONE
19
+ end
20
+
21
+ def available_spaces
22
+ @board.map.with_index { |space, index| index if space == '-' }.compact
23
+ end
24
+
25
+ def count_empty_spaces
26
+ available_spaces.count
27
+ end
28
+
29
+ def new_move(space)
30
+ new_ttt = deep_copy
31
+ new_ttt.make_move(space)
32
+ new_ttt
33
+ end
34
+
35
+ def deep_copy
36
+ copy = dup
37
+ copy.board = board.dup
38
+ copy.turn = turn.dup
39
+ copy
40
+ end
41
+
42
+ def possible_moves
43
+ available_spaces.map { |space| new_move(space) }
44
+ end
45
+
46
+ def possible_values
47
+ possible_moves.map { |move| move.minimax }
48
+ end
49
+
50
+ def minimax
51
+ return 100 if winning_player?(PLAYER_ONE)
52
+ return -100 if winning_player?(PLAYER_TWO)
53
+ return 0 if !winning_player?(PLAYER_ONE) &&
54
+ !winning_player?(PLAYER_TWO) &&
55
+ count_empty_spaces == 0
56
+ return possible_values.max + count_empty_spaces if @turn == PLAYER_ONE
57
+ return possible_values.min - count_empty_spaces if @turn == PLAYER_TWO
58
+ end
59
+
60
+ def best_move
61
+ return available_spaces.max_by { |space| new_move(space).minimax } if @turn == PLAYER_ONE
62
+ return available_spaces.min_by { |space| new_move(space).minimax } if @turn == PLAYER_TWO
63
+ end
64
+
65
+ def winning_player?(turn)
66
+ return true if @board[0..2] == [turn] * 3
67
+ return true if @board[3..5] == [turn] * 3
68
+ return true if @board[6..8] == [turn] * 3
69
+ return true if [@board[0], @board[3], @board[6]] == [turn] * 3
70
+ return true if [@board[1], @board[4], @board[7]] == [turn] * 3
71
+ return true if [@board[2], @board[5], @board[8]] == [turn] * 3
72
+ return true if [@board[0], @board[4], @board[8]] == [turn] * 3
73
+ return true if [@board[2], @board[4], @board[6]] == [turn] * 3
74
+ end
75
+ end
76
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttt_gem_8thlight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Meagan Waller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tic Tac Toe Gem
14
+ email: meaganewaller@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ttt.rb
20
+ homepage: http://rubygems.org/gems/ttt_gem_8thlight
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Tic Tac Toe
44
+ test_files: []