ttt_gem_8thlight 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/game.rb +1 -3
  3. data/lib/ttt.rb +44 -36
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f2cc3c477ba387aba5c0d1a49590249051f958e
4
- data.tar.gz: f7b45c9722117b062a91a92e06d52f1d0533e8e6
3
+ metadata.gz: ca1b97f0a33561487272e06ca4f1a0b72780ffbb
4
+ data.tar.gz: 28499f2ae30528728c6598e16da377bc8d71f0fc
5
5
  SHA512:
6
- metadata.gz: 2203e97b566a66d277637e3e37c253c96941f9431ff4fe39e1bced9c1a2400b443698e446cb76190e5c4dd92eb612171404347d5ad5e79ee197ca450fb271e35
7
- data.tar.gz: e0dca17b9f0d13ebcb164a540a6638de9727ed0a6d66a07ca6e2be1a4bfb40464b415f9e6b971d37e42b8c61b49e68a424006da1b1843cab07a375754394fae9
6
+ metadata.gz: ac96d3e8c1065a638dc095a178a8267dd740fc7dd06f92f682c1480f81ec422e14ca1b167e55e91c63d5c3bb32888404878e538a9694c0a1a55ee10795c1123c
7
+ data.tar.gz: f78b39ef0d0a0b7491c6f45d4036c97544922d8f12704ac80e80b3a99b4c3f1f0e45c38541304f808533453fc54be5ddacf2431987018c4d2721eb87efbe8e2b
data/lib/game.rb CHANGED
@@ -32,8 +32,6 @@ class Game
32
32
  end
33
33
 
34
34
  def over?
35
- return true if @ttt.winning_player?('x') ||
36
- @ttt.winning_player?('o') ||
37
- @ttt.available_spaces == []
35
+ return true if @ttt.x_won || @ttt.o_won || @ttt.tie
38
36
  end
39
37
  end
data/lib/ttt.rb CHANGED
@@ -2,71 +2,79 @@ class TTT
2
2
  PLAYER_ONE = 'x'
3
3
  PLAYER_TWO = 'o'
4
4
 
5
- attr_accessor :board, :turn
5
+ attr_accessor :board, :current_player
6
6
 
7
- def initialize(board = Array.new(9) {"-"}, turn = PLAYER_ONE)
7
+ def initialize(board = Array.new(9) {"-"}, current_player = PLAYER_ONE)
8
8
  @board = board
9
- @turn = turn
9
+ @current_player = current_player
10
10
  end
11
11
 
12
12
  def make_move(space)
13
- @board[space] = @turn
14
- @turn = @turn == PLAYER_ONE ? PLAYER_TWO : PLAYER_ONE
13
+ @board[space] = @current_player
14
+ @current_player = @current_player == PLAYER_ONE ? PLAYER_TWO : PLAYER_ONE
15
15
  end
16
16
 
17
17
  def available_spaces
18
18
  @board.map.with_index { |space, index| index if space == '-' }.compact
19
19
  end
20
20
 
21
- def count_empty_spaces
22
- available_spaces.count
23
- end
24
-
25
- def new_move(space)
26
- new_ttt = deep_copy
27
- new_ttt.make_move(space)
28
- new_ttt
21
+ def new_board_with_move(space)
22
+ board_with_current_player = deep_copy
23
+ board_with_current_player.make_move(space)
24
+ board_with_current_player
29
25
  end
30
26
 
31
27
  def deep_copy
32
28
  copy = dup
33
29
  copy.board = board.dup
34
- copy.turn = turn.dup
30
+ copy.current_player = current_player.dup
35
31
  copy
36
32
  end
37
33
 
38
- def possible_moves
39
- available_spaces.map { |space| new_move(space) }
34
+ def possible_boards
35
+ available_spaces.map { |space| new_board_with_move(space) }
40
36
  end
41
37
 
42
38
  def possible_values
43
- possible_moves.map { |move| move.minimax }
39
+ possible_boards.map { |board| board.minimax }
40
+ end
41
+
42
+ def best_move
43
+ return available_spaces.max_by { |space| new_board_with_move(space).minimax } if @current_player == PLAYER_ONE
44
+ return available_spaces.min_by { |space| new_board_with_move(space).minimax } if @current_player == PLAYER_TWO
44
45
  end
45
46
 
46
47
  def minimax
47
- return 100 if winning_player?(PLAYER_ONE)
48
- return -100 if winning_player?(PLAYER_TWO)
49
- return 0 if !winning_player?(PLAYER_ONE) &&
50
- !winning_player?(PLAYER_TWO) &&
51
- count_empty_spaces == 0
52
- return possible_values.max + count_empty_spaces if @turn == PLAYER_ONE
53
- return possible_values.min - count_empty_spaces if @turn == PLAYER_TWO
48
+ return 100 if x_won
49
+ return -100 if o_won
50
+ return 0 if tie
51
+ return possible_values.max + available_spaces.count if @current_player == PLAYER_ONE
52
+ return possible_values.min - available_spaces.count if @current_player == PLAYER_TWO
54
53
  end
55
54
 
56
- def best_move
57
- return available_spaces.max_by { |space| new_move(space).minimax } if @turn == PLAYER_ONE
58
- return available_spaces.min_by { |space| new_move(space).minimax } if @turn == PLAYER_TWO
55
+ def x_won
56
+ winning_player?(PLAYER_ONE)
57
+ end
58
+
59
+ def o_won
60
+ winning_player?(PLAYER_TWO)
61
+ end
62
+
63
+ def tie
64
+ !winning_player?(PLAYER_ONE) &&
65
+ !winning_player?(PLAYER_TWO) &&
66
+ available_spaces.count == 0
59
67
  end
60
68
 
61
- def winning_player?(turn)
62
- return true if @board[0..2] == [turn] * 3
63
- return true if @board[3..5] == [turn] * 3
64
- return true if @board[6..8] == [turn] * 3
65
- return true if [@board[0], @board[3], @board[6]] == [turn] * 3
66
- return true if [@board[1], @board[4], @board[7]] == [turn] * 3
67
- return true if [@board[2], @board[5], @board[8]] == [turn] * 3
68
- return true if [@board[0], @board[4], @board[8]] == [turn] * 3
69
- return true if [@board[2], @board[4], @board[6]] == [turn] * 3
69
+ def winning_player?(current_player)
70
+ return true if @board[0..2] == [current_player] * 3
71
+ return true if @board[3..5] == [current_player] * 3
72
+ return true if @board[6..8] == [current_player] * 3
73
+ return true if [@board[0], @board[3], @board[6]] == [current_player] * 3
74
+ return true if [@board[1], @board[4], @board[7]] == [current_player] * 3
75
+ return true if [@board[2], @board[5], @board[8]] == [current_player] * 3
76
+ return true if [@board[0], @board[4], @board[8]] == [current_player] * 3
77
+ return true if [@board[2], @board[4], @board[6]] == [current_player] * 3
70
78
  end
71
79
  end
72
80
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ttt_gem_8thlight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meagan Waller