Tictactoe-alu4116 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ require "Tictactoe-alu4116/player"
2
+ require "Tictactoe-alu4116/squares_container"
3
+
4
+ module Tictactoe
5
+ class SmartPlayer < Player
6
+ def move( board )
7
+ moves = board.moves
8
+
9
+ # If I have a win, take it. If he is threatening to win, stop it.
10
+ board.each_row do |row|
11
+ if row.blanks == 1 and (row.xs == 2 or row.os == 2)
12
+ (0..2).each do |e|
13
+ return row.to_board_name(e) if row[e] == " "
14
+ end
15
+ end
16
+ end
17
+
18
+ # Take the center if open.
19
+ return "b2" if moves.include? "b2"
20
+
21
+ # Defend opposite corners.
22
+ if board[0] != @mark and board[0] != " " and board[8] == " "
23
+ return "c3"
24
+ elsif board[8] != @mark and board[8] != " " and board[0] == " "
25
+ return "a1"
26
+ elsif board[2] != @mark and board[2] != " " and board[6] == " "
27
+ return "c1"
28
+ elsif board[6] != @mark and board[6] != " " and board[2] == " "
29
+ return "a3"
30
+ end
31
+
32
+ # Defend against the special case XOX on a diagonal.
33
+ if board.xs == 2 and board.os == 1 and board[4] == "O" and
34
+ (board[0] == "X" and board[8] == "X") or
35
+ (board[2] == "X" and board[6] == "X")
36
+ return %w{a2 b1 b3 c2}[rand(4)]
37
+ end
38
+
39
+ # Or make a random move.
40
+ moves[rand(moves.size)]
41
+ end
42
+
43
+ def start()
44
+ "SmartPlayer = #{@mark}"
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ require "Tictactoe-alu4116/player"
2
+ require "Tictactoe-alu4116/squares_container"
3
+
4
+ module Tictactoe
5
+ class SmartPlayer < Player
6
+ def move( board )
7
+ moves = board.moves
8
+
9
+ # If I have a win, take it. If he is threatening to win, stop it.
10
+ board.each_row do |row|
11
+ if row.blanks == 1 and (row.xs == 2 or row.os == 2)
12
+ (0..2).each do |e|
13
+ return row.to_board_name(e) if row[e] == " "
14
+ end
15
+ end
16
+ end
17
+
18
+ # Take the center if open.
19
+ return "b2" if moves.include? "b2"
20
+
21
+ # Defend opposite corners.
22
+ if board[0] != @mark and board[0] != " " and board[8] == " "
23
+ return "c3"
24
+ elsif board[8] != @mark and board[8] != " " and board[0] == " "
25
+ return "a1"
26
+ elsif board[2] != @mark and board[2] != " " and board[6] == " "
27
+ return "c1"
28
+ elsif board[6] != @mark and board[6] != " " and board[2] == " "
29
+ return "a3"
30
+ end
31
+
32
+ # Defend against the special case XOX on a diagonal.
33
+ if board.xs == 2 and board.os == 1 and board[4] == "O" and
34
+ (board[0] == "X" and board[8] == "X") or
35
+ (board[2] == "X" and board[6] == "X")
36
+ return %w{a2 b1 b3 c2}[rand(4)]
37
+ end
38
+
39
+ # Or make a random move.
40
+ moves[rand(moves.size)]
41
+ end
42
+
43
+ def start()
44
+ "SmartPlayer = #{@mark}"
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ module Tictactoe
2
+ module SquaresContainer
3
+ def []( index ) @squares[index] end
4
+
5
+ def blanks() @squares.find_all { |s| s == " " }.size end
6
+ def os() @squares.find_all { |s| s == "O" }.size end
7
+ def xs() @squares.find_all { |s| s == "X" }.size end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Tictactoe
2
+ module Alu4116
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,134 @@
1
+ require 'Tictactoe-alu4116'
2
+
3
+ describe Tictactoe do
4
+
5
+ #-------------------------------------------------------------------------------
6
+ it "Movimientos válidos del tablero" do
7
+ @board = Tictactoe::Board.new([" "] * 9)
8
+ @board.moves.include?("a1").should == true
9
+ @board.moves.include?("b1").should == true
10
+ @board.moves.include?("c1").should == true
11
+ @board.moves.include?("a2").should == true
12
+ @board.moves.include?("b2").should == true
13
+ @board.moves.include?("c2").should == true
14
+ @board.moves.include?("a3").should == true
15
+ @board.moves.include?("b3").should == true
16
+ @board.moves.include?("c3").should == true
17
+ end
18
+
19
+ #-------------------------------------------------------------------------------
20
+ it "Movimiento tablero inválido" do
21
+ @board = Tictactoe::Board.new([" "] * 9)
22
+ @board.moves.include?("d1").should == false
23
+ @board.moves.include?("a4").should == false
24
+ @board.moves.include?("c5").should == false
25
+ end
26
+
27
+
28
+ #-------------------------------------------------------------------------------
29
+ it "Almacenamiento correcto de una jugada de un determinado jugador" do
30
+ @board = Tictactoe::Board.new([" "] * 9)
31
+ @x_player = Tictactoe::Player.new("X")
32
+ @board["a1"] = @x_player.mark
33
+ @board["a1"].should == "X"
34
+ end
35
+
36
+
37
+
38
+
39
+ #-------------------------------------------------------------------------------
40
+ it "Partida ganada jugador X en fila" do
41
+ @board = Tictactoe::Board.new([" "] * 9)
42
+ @board["a1"] = "X"
43
+ @board["a2"] = "X"
44
+ @board["a3"] = "X"
45
+ @board.won?.should == "X"
46
+ end
47
+
48
+ #-------------------------------------------------------------------------------
49
+ it "Partida ganada por jugador 0 en diagonal" do
50
+ @board = Tictactoe::Board.new([" "] * 9)
51
+ @board["a1"] = "O"
52
+ @board["b2"] = "O"
53
+ @board["c3"] = "O"
54
+ @board.won?.should == "O"
55
+ end
56
+
57
+
58
+ #-------------------------------------------------------------------------------
59
+ it "Partida ganada por jugador 0 en columna" do
60
+ @board = Tictactoe::Board.new([" "] * 9)
61
+ @board["a1"] = "O"
62
+ @board["b1"] = "O"
63
+ @board["c1"] = "O"
64
+ @board.won?.should == "O"
65
+ end
66
+
67
+ #-------------------------------------------------------------------------------
68
+ it "Empate de partida" do
69
+ @board = Tictactoe::Board.new([" "] * 9)
70
+ @board["a1"] = "O"
71
+ @board["a2"] = "O"
72
+ @board["a3"] = "X"
73
+ @board["b1"] = "X"
74
+ @board["b2"] = "O"
75
+ @board["b3"] = "O"
76
+ @board["c1"] = "O"
77
+ @board["c2"] = "X"
78
+ @board["c3"] = "O"
79
+ @board.won?.should == "O"
80
+ end
81
+
82
+
83
+ #-------------------------------------------------------------------------------
84
+ it "Jugador Humano X gana partida" do
85
+ @board = Tictactoe::Board.new([" "] * 9)
86
+ @x_player = Tictactoe::HumanPlayer.new("X")
87
+ @board["a1"] = "X"
88
+ @board["a2"] = "X"
89
+ @board["a3"] = "X"
90
+ @x_player.finish_pruebas(@board).should == "Congratulations, you win.\n\n"
91
+ end
92
+
93
+
94
+
95
+ #-------------------------------------------------------------------------------
96
+ it "Jugador Humano O pierde partida" do
97
+ @board = Tictactoe::Board.new([" "] * 9)
98
+ @x_player = Tictactoe::HumanPlayer.new("O")
99
+ @board["a1"] = "X"
100
+ @board["b1"] = "O"
101
+ @board["a2"] = "X"
102
+ @board["b2"] = "O"
103
+ @board["a3"] = "X"
104
+ @x_player.finish_pruebas(@board).should == "You lost tic-tac-toe?!\n\n"
105
+ end
106
+
107
+
108
+
109
+ #-------------------------------------------------------------------------------
110
+ it "Comportamiento SmartPlayer correcto para movimiento ganador" do
111
+ @board = Tictactoe::Board.new([" "] * 9)
112
+ @x_player = Tictactoe::SmartPlayer.new("O")
113
+ @board["a1"] = "O"
114
+ @board["a3"] = "X"
115
+ @board["c1"] = "O"
116
+ @board["b2"] = "X"
117
+ @board["a2"] = "O"
118
+ @board["b3"] = "X"
119
+ @x_player.move(@board).should == "b1"
120
+ end
121
+
122
+
123
+ #-------------------------------------------------------------------------------
124
+ it "Defensa de esquina correcta SmartPlayer" do
125
+ @board = Tictactoe::Board.new([" "] * 9)
126
+ @x_player = Tictactoe::SmartPlayer.new("O")
127
+ @board["a3"] = "X"
128
+ @board["b2"] = "O"
129
+ @x_player.move(@board).should == "c1"
130
+ end
131
+
132
+
133
+
134
+ end
@@ -0,0 +1,134 @@
1
+ require 'Tictactoe-alu4116'
2
+
3
+ describe TicTacToe do
4
+
5
+ #-------------------------------------------------------------------------------
6
+ it "Movimientos válidos del tablero" do
7
+ @board = TicTacToe::Board.new([" "] * 9)
8
+ @board.moves.include?("a1").should == true
9
+ @board.moves.include?("b1").should == true
10
+ @board.moves.include?("c1").should == true
11
+ @board.moves.include?("a2").should == true
12
+ @board.moves.include?("b2").should == true
13
+ @board.moves.include?("c2").should == true
14
+ @board.moves.include?("a3").should == true
15
+ @board.moves.include?("b3").should == true
16
+ @board.moves.include?("c3").should == true
17
+ end
18
+
19
+ #-------------------------------------------------------------------------------
20
+ it "Movimiento tablero inválido" do
21
+ @board = TicTacToe::Board.new([" "] * 9)
22
+ @board.moves.include?("d1").should == false
23
+ @board.moves.include?("a4").should == false
24
+ @board.moves.include?("c5").should == false
25
+ end
26
+
27
+
28
+ #-------------------------------------------------------------------------------
29
+ it "Almacenamiento correcto de una jugada de un determinado jugador" do
30
+ @board = TicTacToe::Board.new([" "] * 9)
31
+ @x_player = TicTacToe::Player.new("X")
32
+ @board["a1"] = @x_player.mark
33
+ @board["a1"].should == "X"
34
+ end
35
+
36
+
37
+
38
+
39
+ #-------------------------------------------------------------------------------
40
+ it "Partida ganada jugador X en fila" do
41
+ @board = TicTacToe::Board.new([" "] * 9)
42
+ @board["a1"] = "X"
43
+ @board["a2"] = "X"
44
+ @board["a3"] = "X"
45
+ @board.won?.should == "X"
46
+ end
47
+
48
+ #-------------------------------------------------------------------------------
49
+ it "Partida ganada por jugador 0 en diagonal" do
50
+ @board = TicTacToe::Board.new([" "] * 9)
51
+ @board["a1"] = "O"
52
+ @board["b2"] = "O"
53
+ @board["c3"] = "O"
54
+ @board.won?.should == "O"
55
+ end
56
+
57
+
58
+ #-------------------------------------------------------------------------------
59
+ it "Partida ganada por jugador 0 en columna" do
60
+ @board = TicTacToe::Board.new([" "] * 9)
61
+ @board["a1"] = "O"
62
+ @board["b1"] = "O"
63
+ @board["c1"] = "O"
64
+ @board.won?.should == "O"
65
+ end
66
+
67
+ #-------------------------------------------------------------------------------
68
+ it "Empate de partida" do
69
+ @board = TicTacToe::Board.new([" "] * 9)
70
+ @board["a1"] = "O"
71
+ @board["a2"] = "O"
72
+ @board["a3"] = "X"
73
+ @board["b1"] = "X"
74
+ @board["b2"] = "O"
75
+ @board["b3"] = "O"
76
+ @board["c1"] = "O"
77
+ @board["c2"] = "X"
78
+ @board["c3"] = "O"
79
+ @board.won?.should == "O"
80
+ end
81
+
82
+
83
+ #-------------------------------------------------------------------------------
84
+ it "Jugador Humano X gana partida" do
85
+ @board = TicTacToe::Board.new([" "] * 9)
86
+ @x_player = TicTacToe::HumanPlayer.new("X")
87
+ @board["a1"] = "X"
88
+ @board["a2"] = "X"
89
+ @board["a3"] = "X"
90
+ @x_player.finish_pruebas(@board).should == "Congratulations, you win.\n\n"
91
+ end
92
+
93
+
94
+
95
+ #-------------------------------------------------------------------------------
96
+ it "Jugador Humano O pierde partida" do
97
+ @board = TicTacToe::Board.new([" "] * 9)
98
+ @x_player = TicTacToe::HumanPlayer.new("O")
99
+ @board["a1"] = "X"
100
+ @board["b1"] = "O"
101
+ @board["a2"] = "X"
102
+ @board["b2"] = "O"
103
+ @board["a3"] = "X"
104
+ @x_player.finish_pruebas(@board).should == "You lost tic-tac-toe?!\n\n"
105
+ end
106
+
107
+
108
+
109
+ #-------------------------------------------------------------------------------
110
+ it "Comportamiento SmartPlayer correcto para movimiento ganador" do
111
+ @board = TicTacToe::Board.new([" "] * 9)
112
+ @x_player = TicTacToe::SmartPlayer.new("O")
113
+ @board["a1"] = "O"
114
+ @board["a3"] = "X"
115
+ @board["c1"] = "O"
116
+ @board["b2"] = "X"
117
+ @board["a2"] = "O"
118
+ @board["b3"] = "X"
119
+ @x_player.move(@board).should == "b1"
120
+ end
121
+
122
+
123
+ #-------------------------------------------------------------------------------
124
+ it "Defensa de esquina correcta SmartPlayer" do
125
+ @board = TicTacToe::Board.new([" "] * 9)
126
+ @x_player = TicTacToe::SmartPlayer.new("O")
127
+ @board["a3"] = "X"
128
+ @board["b2"] = "O"
129
+ @x_player.move(@board).should == "c1"
130
+ end
131
+
132
+
133
+
134
+ end
@@ -0,0 +1,135 @@
1
+ require 'Tictactoe-alu4116'
2
+ require "test/unit"
3
+
4
+
5
+ #Test que comprueba si se calcula el radio de una circunferencia correctamente.
6
+ class TestTictactoe < Test::Unit::TestCase
7
+
8
+ #ejecutar antes de cada test
9
+ def setup
10
+ @board = Tictactoe::Board.new([" "] * 9)
11
+ end
12
+
13
+ #-------------------------------------------------------------------------------
14
+ def test_MovimientosValidos
15
+ assert(@board.moves.include?("a1"))
16
+ assert(@board.moves.include?("a2"))
17
+ assert(@board.moves.include?("a3"))
18
+ assert(@board.moves.include?("b1"))
19
+ assert(@board.moves.include?("b2"))
20
+ assert(@board.moves.include?("b3"))
21
+ assert(@board.moves.include?("c1"))
22
+ assert(@board.moves.include?("c2"))
23
+ assert(@board.moves.include?("c3"))
24
+ end
25
+
26
+ #-------------------------------------------------------------------------------
27
+ def test_MovimientosInvalidos
28
+ assert(!@board.moves.include?("a5"))
29
+ assert(!@board.moves.include?("d2"))
30
+ assert(!@board.moves.include?("e3"))
31
+ assert(!@board.moves.include?("e8"))
32
+ end
33
+
34
+
35
+
36
+ #-------------------------------------------------------------------------------
37
+ def test_JugadaCorrectaJugador
38
+ @x_player = Tictactoe::Player.new("X")
39
+ @board["a1"] = @x_player.mark
40
+ assert_equal(@board["a1"], "X")
41
+ end
42
+
43
+
44
+
45
+ #-------------------------------------------------------------------------------
46
+ def test_PartidaGanadaFila
47
+ @board["a1"] = "X"
48
+ @board["a2"] = "X"
49
+ @board["a3"] = "X"
50
+ assert_equal(@board.won?, "X")
51
+ end
52
+
53
+ #-------------------------------------------------------------------------------
54
+ def test_PartidaGanadaDiagonal
55
+ @board["a1"] = "O"
56
+ @board["b2"] = "O"
57
+ @board["c3"] = "O"
58
+ assert_equal(@board.won?, "O")
59
+ end
60
+
61
+
62
+ #-------------------------------------------------------------------------------
63
+ def test_PartidaGanadaColumna
64
+ @board["a1"] = "O"
65
+ @board["b1"] = "O"
66
+ @board["c1"] = "O"
67
+ assert_equal(@board.won?, "O")
68
+ end
69
+
70
+
71
+
72
+ #-------------------------------------------------------------------------------
73
+ def test_empate
74
+ @board["a1"] = "O"
75
+ @board["a2"] = "O"
76
+ @board["a3"] = "X"
77
+ @board["b1"] = "X"
78
+ @board["b2"] = "O"
79
+ @board["b3"] = "O"
80
+ @board["c1"] = "O"
81
+ @board["c2"] = "X"
82
+ @board["c3"] = "O"
83
+ assert_equal(@board.won?, "O")
84
+ end
85
+
86
+
87
+ #-------------------------------------------------------------------------------
88
+ def test_HumanoXGanaPartida
89
+ @x_player = Tictactoe::HumanPlayer.new("X")
90
+ @board["a1"] = "X"
91
+ @board["a2"] = "X"
92
+ @board["a3"] = "X"
93
+ assert_equal(@x_player.finish_pruebas(@board), "Congratulations, you win.\n\n")
94
+ end
95
+
96
+
97
+
98
+ #-------------------------------------------------------------------------------
99
+ def test_HumanoPierdePartida
100
+ @x_player = Tictactoe::HumanPlayer.new("O")
101
+ @board["a1"] = "X"
102
+ @board["b1"] = "O"
103
+ @board["a2"] = "X"
104
+ @board["b2"] = "O"
105
+ @board["a3"] = "X"
106
+ assert_equal(@x_player.finish_pruebas(@board), "You lost tic-tac-toe?!\n\n")
107
+ end
108
+
109
+
110
+
111
+ #-------------------------------------------------------------------------------
112
+ def test_SmartPlayertCorrectaJugadaGanar
113
+ @x_player = Tictactoe::SmartPlayer.new("O")
114
+ @board["a1"] = "O"
115
+ @board["a3"] = "X"
116
+ @board["c1"] = "O"
117
+ @board["b2"] = "X"
118
+ @board["a2"] = "O"
119
+ @board["b3"] = "X"
120
+ assert_equal(@x_player.move(@board), "b1")
121
+ end
122
+
123
+
124
+ #-------------------------------------------------------------------------------
125
+ def test_SmartPlayertDefensaEsquina
126
+ @x_player = Tictactoe::SmartPlayer.new("O")
127
+ @board["a3"] = "X"
128
+ @board["b2"] = "O"
129
+ assert_equal(@x_player.move(@board) , "c1")
130
+ end
131
+
132
+
133
+
134
+
135
+ end