Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Replace game class with board class

- ask board not game what the #best_move_for? a player is
- ask board not game what the #winning_move? is for a player
- stop assigning the task of marking the board to Game.new
- instead, have state attr_accessor manage board state
  • Loading branch information...
medwards1771 committed Apr 19, 2018
1 parent 8e4b5de commit fa0f8a2a31cb9f0cda31b2bb45f0973e0171ae27
@@ -1,28 +1,26 @@
class Game
attr_accessor :board
class Board
attr_accessor :state

NO_MOVE = -1
POSITIONS_ON_BOARD = (0..8)

def initialize(board, position=nil, player=nil)
@board = board.dup
@board[position] = player unless position == nil
def initialize(state = "---------")
@state = state
end

def best_move_for(player)
move = NO_MOVE
POSITIONS_ON_BOARD.each do |position|
if position_unoccupied?(position)
move = position if winning_move?(position, player) || move == NO_MOVE
move = position if winning_move?(player) || move == NO_MOVE
end
end

return move
end

def winning_move?(square, player)
game = Game.new(board, square, player)
game.winner == player
def winning_move?(player)
winner == player
end

def winner
@@ -70,6 +68,6 @@ def position_occupied?(position)
end

def mark_at(position)
board[position]
state[position]
end
end
@@ -1,62 +1,61 @@
require 'test/unit'
require_relative './tic_tac_toe'

class GameTest < Test::Unit::TestCase

def test_default_move
game = Game.new("XOX" +
"OX-" +
"OXO")
assert_equal(5, game.best_move_for('X'))

game = Game.new("XOX" +
"OXO" +
"OX-")
assert_equal(8, game.best_move_for('O'))

game = Game.new("---" +
"---" +
"---")
assert_equal(0, game.best_move_for('X'))

game = Game.new("XXX" +
"XXX" +
"XXX")
assert_equal(-1, game.best_move_for('X'))
end

def test_find_winning_move
game = Game.new("XO-" +
"XX-" +
"OOX")
assert_equal(5, game.best_move_for('X'))
end

def test_win_horizontal_conditions
game = Game.new("---" +
"XXX" +
"---")
assert_equal('X', game.winner())
end

def test_win_vertical_conditions
game = Game.new("--O" +
"--O" +
"--O")
assert_equal('O', game.winner())
end

def test_win_left_diagonal_conditions
game = Game.new("X--" +
"-X-" +
"--X")
assert_equal('X', game.winner())
end

def test_win_right_diagonal_conditions
game = Game.new("--O" +
"-O-" +
"O--")
assert_equal('O', game.winner())
end
end
require 'test/unit'
require_relative '../lib/board'

class BoardTest < Test::Unit::TestCase
def test_default_move
board = Board.new("XOX" +
"OX-" +
"OXO")
assert_equal(5, board.best_move_for('X'))

board = Board.new("XOX" +
"OXO" +
"OX-")
assert_equal(8, board.best_move_for('O'))

board = Board.new("---" +
"---" +
"---")
assert_equal(0, board.best_move_for('X'))

board = Board.new("XXX" +
"XXX" +
"XXX")
assert_equal(-1, board.best_move_for('X'))
end

def test_find_winning_move
board = Board.new("XO-" +
"XX-" +
"OOX")
assert_equal(5, board.best_move_for('X'))
end

def test_win_horizontal_conditions
board = Board.new("---" +
"XXX" +
"---")
assert_equal('X', board.winner())
end

def test_win_vertical_conditions
board = Board.new("--O" +
"--O" +
"--O")
assert_equal('O', board.winner())
end

def test_win_left_diagonal_conditions
board = Board.new("X--" +
"-X-" +
"--X")
assert_equal('X', board.winner())
end

def test_win_right_diagonal_conditions
board = Board.new("--O" +
"-O-" +
"O--")
assert_equal('O', board.winner())
end
end

1 comment on commit fa0f8a2

@edgenard

This comment has been minimized.

Copy link
Contributor

commented on fa0f8a2 Apr 24, 2018

This is a big change. What led you to the board knowing who wins or loses vs the Game?

Please sign in to comment.
You can’t perform that action at this time.