game-tictactoe-esegredo 0.0.1 → 0.0.2
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.
- data/Rakefile +12 -0
- data/bin/tictactoe_game_players.rb +26 -0
- data/lib/game-tictactoe-esegredo.rb +12 -4
- data/lib/tictactoe/humanplayer.rb +0 -12
- data/lib/tictactoe/minimaxplayer.rb +20 -18
- data/lib/tictactoe/player.rb +1 -3
- data/lib/tictactoe/version.rb +1 -1
- data/spec/dumbplayer_spec.rb +0 -4
- data/spec/humanplayer_spec.rb +0 -3
- data/spec/minimaxplayer_spec.rb +21 -0
- data/spec/smartplayer_spec.rb +0 -4
- data/test/tc_dumbplayer.rb +0 -4
- data/test/tc_humanplayer.rb +0 -4
- data/test/tc_smartplayer.rb +0 -4
- metadata +12 -8
data/Rakefile
CHANGED
@@ -11,6 +11,11 @@ task :smart do
|
|
11
11
|
sh "ruby -Ilib bin/tictactoe_game.rb"
|
12
12
|
end
|
13
13
|
|
14
|
+
desc "Run TicTacToe game vs minimax player"
|
15
|
+
task :minimax do
|
16
|
+
sh "ruby -Ilib bin/tictactoe_game.rb -m"
|
17
|
+
end
|
18
|
+
|
14
19
|
desc "Run TicTacToe game vs dumb player"
|
15
20
|
task :dumb do
|
16
21
|
sh "ruby -Ilib bin/tictactoe_game.rb -d"
|
@@ -35,3 +40,10 @@ desc "Run DumbPlayer tests"
|
|
35
40
|
task :dp_test do
|
36
41
|
sh "ruby -Ilib test/tc_dumbplayer.rb"
|
37
42
|
end
|
43
|
+
|
44
|
+
desc "Run TicTacToe by specifying two different players"
|
45
|
+
task :player, :x_player, :o_player do |player, args|
|
46
|
+
x_player = args[:x_player] || 'human'
|
47
|
+
o_player = args[:o_player] || 'minimax'
|
48
|
+
sh "ruby -Ilib bin/tictactoe_game_players.rb #{x_player} #{o_player}"
|
49
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "game-tictactoe-esegredo"
|
2
|
+
|
3
|
+
if __FILE__ == $0
|
4
|
+
if (ARGV.size == 2)
|
5
|
+
player1 = case ARGV[0].downcase
|
6
|
+
when "smart" then Tictactoe::SmartPlayer
|
7
|
+
when "minimax" then Tictactoe::MinimaxPlayer
|
8
|
+
when "dumb" then Tictactoe::DumbPlayer
|
9
|
+
when "human" then Tictactoe::HumanPlayer
|
10
|
+
else Tictactoe::HumanPlayer
|
11
|
+
end
|
12
|
+
|
13
|
+
player2 = case ARGV[1].downcase
|
14
|
+
when "smart" then Tictactoe::SmartPlayer
|
15
|
+
when "minimax" then Tictactoe::MinimaxPlayer
|
16
|
+
when "dumb" then Tictactoe::DumbPlayer
|
17
|
+
when "human" then Tictactoe::HumanPlayer
|
18
|
+
else Tictactoe::HumanPlayer
|
19
|
+
end
|
20
|
+
|
21
|
+
game = Tictactoe::Game.new(player1, player2, false)
|
22
|
+
game.play
|
23
|
+
else
|
24
|
+
puts "Usage: ruby -Ilib bin/tictactoe_game_players.rb player_x player_o"
|
25
|
+
end
|
26
|
+
end
|
@@ -27,10 +27,18 @@ module Tictactoe
|
|
27
27
|
break if @board.won?
|
28
28
|
|
29
29
|
@board[@o_player.move(@board)] = @o_player.mark
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
end
|
31
|
+
|
32
|
+
puts @board
|
33
|
+
|
34
|
+
if @board.won? == @x_player.mark
|
35
|
+
print "Player (#{@x_player.mark}) has won!\n\n"
|
36
|
+
elsif @board.won? == " "
|
37
|
+
print "Tie game.\n\n"
|
38
|
+
else
|
39
|
+
print "Player (#{@o_player.mark}) has won!\n\n"
|
40
|
+
end
|
34
41
|
end
|
42
|
+
|
35
43
|
end
|
36
44
|
end
|
@@ -14,17 +14,5 @@ module Tictactoe
|
|
14
14
|
end
|
15
15
|
move.chomp
|
16
16
|
end
|
17
|
-
|
18
|
-
def finish( final_board )
|
19
|
-
print final_board
|
20
|
-
|
21
|
-
if final_board.won? == @mark
|
22
|
-
print "Congratulations, you win.\n\n"
|
23
|
-
elsif final_board.won? == " "
|
24
|
-
print "Tie game.\n\n"
|
25
|
-
else
|
26
|
-
print "You lost tic-tac-toe?!\n\n"
|
27
|
-
end
|
28
|
-
end
|
29
17
|
end
|
30
18
|
end
|
@@ -9,47 +9,49 @@ module Tictactoe
|
|
9
9
|
# For each available movement
|
10
10
|
bestValue = -2
|
11
11
|
bestMove = ""
|
12
|
-
moves.each do |
|
12
|
+
moves.each do |mov|
|
13
13
|
newsquares = board.squares.dup
|
14
14
|
newboard = Board.new(newsquares)
|
15
|
-
|
16
|
-
|
15
|
+
newboard[mov] = self.mark
|
16
|
+
value = search_best_move(newboard, opponent_mark, 1)
|
17
|
+
if (value > bestValue)
|
17
18
|
bestValue = value
|
18
|
-
bestMove =
|
19
|
+
bestMove = mov
|
19
20
|
end
|
20
21
|
end
|
21
22
|
bestMove
|
22
23
|
end
|
23
24
|
|
24
|
-
def search_best_move(board,
|
25
|
-
|
26
|
-
board[move] = mark
|
27
|
-
moves = board.moves
|
28
|
-
|
29
|
-
# Checks whether there is a winner or not
|
25
|
+
def search_best_move(board, mark, level)
|
26
|
+
# Checks if there is a winner
|
30
27
|
return 1 if (board.won? == self.mark)
|
31
28
|
return 0 if (board.won? == " ")
|
32
29
|
return -1 if (board.won? == opponent_mark)
|
30
|
+
|
31
|
+
# Gets the available movements
|
32
|
+
moves = board.moves
|
33
33
|
|
34
|
-
#
|
34
|
+
# Min level
|
35
35
|
if ((level % 2 == 1) && (!board.won?))
|
36
|
-
bestValue =
|
36
|
+
bestValue = 2
|
37
37
|
moves.each do |mov|
|
38
38
|
newsquares = board.squares.dup
|
39
39
|
newboard = Board.new(newsquares)
|
40
|
-
|
41
|
-
|
40
|
+
newboard[mov] = mark
|
41
|
+
value = search_best_move(newboard, self.mark, level + 1)
|
42
|
+
if (value < bestValue)
|
42
43
|
bestValue = value
|
43
44
|
end
|
44
45
|
end
|
45
|
-
#
|
46
|
+
# Max level
|
46
47
|
elsif ((level % 2 == 0) && (!board.won?))
|
47
|
-
bestValue = 2
|
48
|
+
bestValue = -2
|
48
49
|
moves.each do |mov|
|
49
50
|
newsquares = board.squares.dup
|
50
51
|
newboard = Board.new(newsquares)
|
51
|
-
|
52
|
-
|
52
|
+
newboard[mov] = mark
|
53
|
+
value = search_best_move(newboard, opponent_mark, level + 1)
|
54
|
+
if (value > bestValue)
|
53
55
|
bestValue = value
|
54
56
|
end
|
55
57
|
end
|
data/lib/tictactoe/player.rb
CHANGED
data/lib/tictactoe/version.rb
CHANGED
data/spec/dumbplayer_spec.rb
CHANGED
data/spec/humanplayer_spec.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "game-tictactoe-esegredo"
|
2
|
+
|
3
|
+
describe Tictactoe::MinimaxPlayer do
|
4
|
+
before :each do
|
5
|
+
@mp = Tictactoe::MinimaxPlayer.new("X")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "Debe existir un metodo move" do
|
9
|
+
@mp.respond_to?("move").should == true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "El metodo move debe tratar de evitar la derrota" do
|
13
|
+
@board = Tictactoe::Board.new(["X", "X", "O", "O", "O", "X", " ", "O", " "])
|
14
|
+
@mp.move(@board).should == "c1"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "El metodo move debe tratar de ganar" do
|
18
|
+
@board = Tictactoe::Board.new([" ", "O", "X", " ", "X", " ", "O", "O", "X"])
|
19
|
+
@mp.move(@board).should == "a1"
|
20
|
+
end
|
21
|
+
end
|
data/spec/smartplayer_spec.rb
CHANGED
data/test/tc_dumbplayer.rb
CHANGED
data/test/tc_humanplayer.rb
CHANGED
data/test/tc_smartplayer.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: game-tictactoe-esegredo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &15662220 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *15662220
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &15661560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *15661560
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: guard-rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &15660620 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,12 +43,13 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *15660620
|
47
47
|
description: Gem which implements the Tic-Tac-Toe game with different kind of players
|
48
48
|
email:
|
49
49
|
- edusegre@gmail.com
|
50
50
|
executables:
|
51
51
|
- tictactoe_game.rb
|
52
|
+
- tictactoe_game_players.rb
|
52
53
|
extensions: []
|
53
54
|
extra_rdoc_files: []
|
54
55
|
files:
|
@@ -59,6 +60,7 @@ files:
|
|
59
60
|
- README.md
|
60
61
|
- Rakefile
|
61
62
|
- bin/tictactoe_game.rb
|
63
|
+
- bin/tictactoe_game_players.rb
|
62
64
|
- game-tictactoe-esegredo.gemspec
|
63
65
|
- lib/game-tictactoe-esegredo.rb
|
64
66
|
- lib/tictactoe/board.rb
|
@@ -70,6 +72,7 @@ files:
|
|
70
72
|
- lib/tictactoe/version.rb
|
71
73
|
- spec/dumbplayer_spec.rb
|
72
74
|
- spec/humanplayer_spec.rb
|
75
|
+
- spec/minimaxplayer_spec.rb
|
73
76
|
- spec/smartplayer_spec.rb
|
74
77
|
- test/tc_dumbplayer.rb
|
75
78
|
- test/tc_humanplayer.rb
|
@@ -101,6 +104,7 @@ summary: Gem which implements the Tic-Tac-Toe game with different kind of player
|
|
101
104
|
test_files:
|
102
105
|
- spec/dumbplayer_spec.rb
|
103
106
|
- spec/humanplayer_spec.rb
|
107
|
+
- spec/minimaxplayer_spec.rb
|
104
108
|
- spec/smartplayer_spec.rb
|
105
109
|
- test/tc_dumbplayer.rb
|
106
110
|
- test/tc_humanplayer.rb
|