jdl_tictactoe 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- class JdlTictactoe::Board
1
+ class JdlTictactoe::Game::Board
2
2
 
3
3
  attr_reader :size, :squares
4
4
 
@@ -1,57 +1,59 @@
1
- class JdlTictactoe::Player
1
+ module JdlTicTacToe
2
+ class Player
2
3
 
3
- attr_accessor :species, :mark, :opponent
4
+ attr_accessor :species, :mark, :opponent
4
5
 
5
- def initialize(species, mark="X")
6
- @species = "human" if species == "H"
7
- @species = "computer" if species == "C"
8
- @mark = mark
9
- end
6
+ def initialize(species, mark="X")
7
+ @species = "human" if species == "H"
8
+ @species = "computer" if species == "C"
9
+ @mark = mark
10
+ end
10
11
 
11
- def mark_square(board, square)
12
- board.mark_square(square, @mark)
13
- end
12
+ def mark_square(board, square)
13
+ board.mark_square(square, @mark)
14
+ end
14
15
 
15
- def minimax(game, turn)
16
- if game.empty_squares.count == 9
17
- return 1
18
- elsif game.empty_squares.count == 8
19
- return 5 if game.empty_squares.include?(5)
20
- return 1 if !game.empty_squares.include?(5)
21
- else
22
- return get_minimax_square(game, turn)
16
+ def minimax(game, turn)
17
+ if game.empty_squares.count == 9
18
+ return 1
19
+ elsif game.empty_squares.count == 8
20
+ return 5 if game.empty_squares.include?(5)
21
+ return 1 if !game.empty_squares.include?(5)
22
+ else
23
+ return get_minimax_square(game, turn)
24
+ end
23
25
  end
24
- end
25
26
 
26
- def get_minimax_scores(game, turn)
27
- scores = []
28
- return [game.board.score(self)] if game.over?
27
+ def get_minimax_scores(game, turn)
28
+ scores = []
29
+ return [game.board.score(self)] if game.over?
29
30
 
30
- game.empty_squares.each do |move|
31
- game.mark_square(move, game.send(turn).mark)
32
- minimax_scores = get_minimax_scores(game, game.switch_turn(turn))
31
+ game.empty_squares.each do |move|
32
+ game.mark_square(move, game.send(turn).mark)
33
+ minimax_scores = get_minimax_scores(game, game.switch_turn(turn))
33
34
 
34
- if game.send(turn) == self
35
- scores << minimax_scores.min
36
- elsif game.send(turn) != self
37
- scores << minimax_scores.max
38
- end
35
+ if game.send(turn) == self
36
+ scores << minimax_scores.min
37
+ elsif game.send(turn) != self
38
+ scores << minimax_scores.max
39
+ end
39
40
 
40
- game.undo_move(move)
41
+ game.undo_move(move)
42
+ end
43
+ return scores
41
44
  end
42
- return scores
43
- end
44
45
 
45
- def get_minimax_square(game, turn)
46
- moves_with_score = {}
47
- scores = get_minimax_scores(game, turn)
46
+ def get_minimax_square(game, turn)
47
+ moves_with_score = {}
48
+ scores = get_minimax_scores(game, turn)
48
49
 
49
- game.empty_squares.each_with_index do |move, index|
50
- moves_with_score[move] = scores[index]
50
+ game.empty_squares.each_with_index do |move, index|
51
+ moves_with_score[move] = scores[index]
52
+ end
53
+
54
+ max = moves_with_score.values.max
55
+ return moves_with_score.select { |k,v| v == max }.keys.first
51
56
  end
52
57
 
53
- max = moves_with_score.values.max
54
- return moves_with_score.select { |k,v| v == max }.keys.first
55
58
  end
56
-
57
59
  end
data/lib/jdl_tictactoe.rb CHANGED
@@ -1,12 +1,3 @@
1
- module JdlTictactoe
2
- class Board
3
- end
4
- class Player
5
- end
6
- class Game
7
- end
8
- end
9
-
10
1
  require 'jdl_tictactoe/board'
11
2
  require 'jdl_tictactoe/player'
12
3
  require 'jdl_tictactoe/game'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jdl_tictactoe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: