Skip to content
This repository has been archived by the owner on Apr 24, 2019. It is now read-only.

Commit

Permalink
make move assignment into one liner and make winner method conditiona…
Browse files Browse the repository at this point in the history
…ls more alike
  • Loading branch information
edgenard committed Apr 2, 2018
1 parent 0c72024 commit 37bb1a1
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions tic-tac-toe/tic_tac_toe.rb
Expand Up @@ -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

Expand All @@ -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.

Copy link
@medwards1771

medwards1771 Apr 3, 2018

Contributor

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
Expand Down

0 comments on commit 37bb1a1

Please sign in to comment.