Created
March 21, 2017 02:28
-
-
Save itzsaga/c70c20beb600a7c1118ad573d8656b45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Players | |
| class Computer < Player | |
| def move(board) | |
| if board.cells.count{|square| square != " " } == 0 | |
| "1" | |
| elsif board.cells.count{|square| square != " " } == 1 | |
| board.cells.find_index("X") == 4 ? "1" : "5" | |
| else | |
| possible_moves = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] | |
| valid_moves = [] | |
| possible_moves.each do |move| | |
| valid_moves << move if board.cells[move.to_i-1] == " " | |
| end | |
| valid_moves.sample | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment