Permalink
Browse files

make move assignment into one liner and make winner method conditiona…

…ls more alike
  • Loading branch information...
edgenard committed Apr 2, 2018
1 parent 0c72024 commit 37bb1a1eb2c408251f6e047378fc3e930da3d3d5
Showing with 10 additions and 11 deletions.
  1. +10 −11 tic-tac-toe/tic_tac_toe.rb
@@ -13,8 +13,7 @@ def best_move_for(player)
move = NO_MOVE
SQUARES_ON_BOARD.each do |square|
if square_unoccupied?(square)
move = square if winning_move?(square, player)
move = square if move == NO_MOVE
move = square if winning_move?(square, player) || move == NO_MOVE
end
end

@@ -27,17 +26,17 @@ def winning_move?(square, player)
end

def winner
if board[0] != '-' && board[0] == board[1] &&
board[1] == board[2]
return board[0]
square = 0
if board[square] != '-' && board[square] == board[square + 1] && board[square + 1] == board[square + 2]
return board[square]
end
if board[3] != '-' && board[3] == board[4] &&
board[4] == board[5]
return board[3]
square = 3
if board[square] != '-' && board[square] == board[square + 1] && board[square + 1] == board[square + 2]
return board[square]
end
if board[6] != '-' && board[6] == board[7] &&
board[7] == board[8]
return board[6]
square = 6
if board[square] != '-' && board[square] == board[square + 1] && board[square + 1] == board[square + 2]
return board[square]

This comment has been minimized.

@medwards1771

medwards1771 Apr 3, 2018

Collaborator

The naming changes -- abstracting the index into a variable named square -- makes a huge difference here. How did you decide on this particular change?

end
return '-'
end

0 comments on commit 37bb1a1

Please sign in to comment.