taka_tic_tac_toe 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Takayuki Goto
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ taka_tic_tac-toe
2
+ ================
3
+
4
+ tic tac toe gem
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = taka_tic_tac_toe
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to taka_tic_tac_toe
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Takayuki Goto. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,8 @@
1
+ module TicTacToe
2
+ class EasyAi < Computer
3
+
4
+ def make_move(board)
5
+ board.empty_slots.first
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,45 @@
1
+ module TicTacToe
2
+ class ImpossibleAi < Computer
3
+
4
+ Negative_Infinity = -1.0/0
5
+
6
+ def make_move(board)
7
+ #possible_moves = {}
8
+ best_score = Negative_Infinity
9
+ best_index = 0
10
+ board.empty_slots.each do |slots|
11
+ board.set_move(@mark, slots)
12
+ score = -negamax_score(board, -1)
13
+ #possible_moves[slots] = -negamax_score(board, -1)
14
+ board.undo_move(slots)
15
+ if score > best_score
16
+ best_score = score
17
+ best_index = slots
18
+ end
19
+ end
20
+ #puts possible_moves
21
+ return best_index
22
+ end
23
+
24
+ def negamax_score(board, color)
25
+ score = Negative_Infinity
26
+
27
+ mark = color == 1 ? @mark : @opponent_mark
28
+
29
+ return check_game_state(board, (mark)) if board.has_winner || board.is_tie
30
+
31
+ board.empty_slots.each do |slots|
32
+ board.set_move(mark, slots)
33
+ score = [score, -negamax_score(board, -color)].max
34
+ board.undo_move(slots)
35
+ end
36
+ return score
37
+ end
38
+
39
+ def check_game_state(board, mark)
40
+ return 0 if board.is_tie
41
+ return 1 if board.winner == mark
42
+ -1
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,70 @@
1
+ module TicTacToe
2
+ class Board
3
+ attr_reader :slots
4
+
5
+ WinComb = [[0,1,2],[3,4,5],[6,7,8],
6
+ [0,3,6],[1,4,7],[2,5,8],
7
+ [0,4,8],[2,4,6]]
8
+ def initialize
9
+ @slots = %w[1 2 3 4 5 6 7 8 9]
10
+ end
11
+
12
+ def get(index)
13
+ @slots[index-1]
14
+ end
15
+
16
+ def full?
17
+ (@slots.count {|x| x == 'X'} + @slots.count {|x| x == 'O'}) == 9
18
+ end
19
+
20
+ def is_empty?
21
+ @slots.uniq.length == 9
22
+ end
23
+
24
+ def empty_slots
25
+ @slots.select {|x| x.to_i != 0}
26
+ end
27
+
28
+ def is_filled(index)
29
+ @slots[index-1].to_i == 0
30
+ end
31
+
32
+ def set_move(mark, *index)
33
+ index.each do |index|
34
+ @slots[(index.to_i)-1] = mark
35
+ end
36
+ end
37
+
38
+ def is_tie
39
+ full? && !has_winner
40
+ end
41
+
42
+ def reset
43
+ @slots = %w[1 2 3 4 5 6 7 8 9]
44
+ end
45
+
46
+ def print_slots
47
+ @slots.each_slice(3) {|line| p line}
48
+ end
49
+
50
+ def has_winner
51
+ has_winner = false
52
+ WinComb.each {|num, num2, num3| has_winner = true if unique?(num, num2, num3)}
53
+ has_winner
54
+ end
55
+
56
+ def unique?(one, two, three)
57
+ [@slots[one], @slots[two], @slots[three]].uniq.length == 1
58
+ end
59
+
60
+ def winner
61
+ winner = ""
62
+ WinComb.each {|num, num2, num3| winner = @slots[num] if unique?(num, num2, num3)}
63
+ winner
64
+ end
65
+
66
+ def undo_move(index)
67
+ @slots[(index.to_i)-1] = index.to_s
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,21 @@
1
+ module TicTacToe
2
+ class Computer
3
+ attr_accessor :mark
4
+ attr_reader :difficulty
5
+
6
+ def initialize(mark, opponent_mark)
7
+ @mark = mark
8
+ @opponent_mark = opponent_mark
9
+ end
10
+
11
+ def difficulty= difficulty
12
+ @difficulty = TicTacToe.const_get(difficulty).new(@mark, @opponent_mark)
13
+ end
14
+
15
+ def make_move(board)
16
+ @move = @difficulty.make_move(board)
17
+ end
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,80 @@
1
+ module TicTacToe
2
+ class Game
3
+
4
+ attr_accessor :human
5
+ attr_writer :output, :input
6
+
7
+
8
+ def initialize
9
+ @human = Human.new('X')
10
+ @board = Board.new
11
+ @computer = Computer.new('O', @human.mark)
12
+ @output = STDOUT
13
+ @input = STDIN
14
+ end
15
+
16
+ def ask_difficulty
17
+ @output.puts "Enter computer difficulty: (e)asy, (m)edium, (i)mpossible"
18
+ difficulty = @input.gets.chomp.upcase
19
+ @computer.difficulty = "ImpossibleAi" if difficulty == "I"
20
+ @computer.difficulty = "EasyAi" if difficulty == "E"
21
+ end
22
+
23
+ def play
24
+ @output.puts "Welcome to Tic Tac Toe!"
25
+ print_board
26
+ end
27
+
28
+ def ask_move
29
+ @output.puts "Enter your move: "
30
+ end
31
+
32
+ def make_moves(move)
33
+ choose_move(get_human_mark, move)
34
+ choose_move(get_computer_mark, computer_move) if !is_over?
35
+ print_board
36
+ end
37
+
38
+ def reset_board
39
+ @board.reset
40
+ end
41
+
42
+ def ask_mark
43
+ @output.puts "Enter a letter for your mark"
44
+ @human.ask_for_mark
45
+ end
46
+
47
+ def continue?
48
+ @output.puts "Play again? y/n"
49
+ @input.gets.chomp.upcase == 'Y'
50
+ end
51
+
52
+ def print_board
53
+ @output.puts @board.print_slots
54
+ end
55
+
56
+ def get_human_mark
57
+ @human.mark
58
+ end
59
+
60
+ def computer_move
61
+ @computer.make_move(@board)
62
+ end
63
+
64
+ def get_computer_mark
65
+ @computer.mark
66
+ end
67
+
68
+ def is_over?
69
+ @board.is_tie || @board.has_winner
70
+ end
71
+
72
+ def choose_move(mark, index)
73
+ @board.set_move(mark, index)
74
+ end
75
+
76
+ def finished
77
+ @output.puts "End of game, good bye!"
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,13 @@
1
+ module TicTacToe
2
+ class Human
3
+ attr_accessor :mark
4
+
5
+ def initialize(mark)
6
+ @mark = mark
7
+ end
8
+
9
+ def ask_for_mark
10
+ @mark = gets.chomp.upcase
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'taka_tic_tac_toe/game'
2
+ require 'taka_tic_tac_toe/board'
3
+ require 'taka_tic_tac_toe/human'
4
+ require 'taka_tic_tac_toe/computer'
5
+ require 'taka_tic_tac_toe/decorators/ai_decorator.rb'
6
+ require 'taka_tic_tac_toe/ai_difficulty/impossible_ai'
7
+ require 'taka_tic_tac_toe/ai_difficulty/easy_ai'
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taka_tic_tac_toe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Takayuki Goto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.5
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.5
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.4
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ description: tic tac toe application
63
+ email: tak.yuki@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.md
69
+ - README.rdoc
70
+ files:
71
+ - lib/taka_tic_tac_toe.rb
72
+ - lib/taka_tic_tac_toe/ai_difficulty/easy_ai.rb
73
+ - lib/taka_tic_tac_toe/ai_difficulty/impossible_ai.rb
74
+ - lib/taka_tic_tac_toe/board.rb
75
+ - lib/taka_tic_tac_toe/computer.rb
76
+ - lib/taka_tic_tac_toe/game.rb
77
+ - lib/taka_tic_tac_toe/human.rb
78
+ - LICENSE.txt
79
+ - README.md
80
+ - README.rdoc
81
+ homepage: http://github.com/TakaGoto/taka_tic_tac_toe
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: -1302414180920013736
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.24
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: tic tac toe application
109
+ test_files: []