Last active
May 27, 2020 08:09
-
-
Save vierarb/df1efc2b0e38dcf8796cda439d15f334 to your computer and use it in GitHub Desktop.
[TicTacToe] The game
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
class TicTacToe | |
def initialize | |
@player1 = Player.new(1, 'X') | |
@player2 = Player.new(2, 'O') | |
@players = [@player1, @player2] | |
@winner = false | |
@board = Board.new | |
end | |
def play | |
@player1.ask_name | |
@player2.ask_name | |
0.step do |iteration| | |
selected_player = @players[iteration % 2] | |
@board.draw | |
position = ask_movement(selected_player) | |
@board.fill(position, selected_player.sign) | |
break if should_finish? | |
end | |
print_result | |
end | |
private | |
def ask_movement(selected_player) | |
position = nil | |
loop do | |
print "Please, choose your movement #{selected_player.name}" | |
intent = gets | |
if valid_intent?(intent.to_i) | |
position = intent.to_i | |
break | |
else | |
puts "Sorry, the position #{intent.chomp} is not usable. Try again" | |
end | |
end | |
print "\n" | |
position | |
end | |
def valid_intent?(intent) | |
[email protected]_spaces[intent] && intent <= @board.size | |
end | |
def do_we_have_a_winner? | |
@winner = if winner?(@player1) | |
@player1 | |
elsif winner?(@player2) | |
@player2 | |
end | |
end | |
def winner?(player) | |
Board::WINNING_COMBINATIONS.map do |combination| | |
@board.filled_spaces[combination[0]] == player.sign && | |
@board.filled_spaces[combination[1]] == player.sign && | |
@board.filled_spaces[combination[2]] == player.sign | |
end.any? | |
end | |
def should_finish? | |
do_we_have_a_winner? || @board.complete? | |
end | |
def print_result | |
if @winner | |
puts "The winner is #{@winner.name}" | |
@board.draw | |
else | |
puts "No winners in this game :(" | |
end | |
end | |
end | |
class Board | |
attr_reader :filled_spaces | |
WINNING_COMBINATIONS = [ | |
[0, 1, 2], | |
[3, 4, 5], | |
[6, 7, 8], | |
[0, 3, 6], | |
[1, 4, 7], | |
[2, 5, 8], | |
[0, 4, 8], | |
[2, 4, 6] | |
].freeze | |
def initialize | |
@filled_spaces = Array.new(9) | |
end | |
def draw | |
@filled_spaces.size.times do |time| | |
print "\n" if (time % 3).zero? | |
char = @filled_spaces[time].nil? ? ' ' : @filled_spaces[time] | |
print "| #{char} |" | |
end | |
print "\n" | |
end | |
def fill(index, sign) | |
@filled_spaces[index] = sign | |
end | |
def complete? | |
@filled_spaces.all? | |
end | |
def size | |
@size ||= 9 | |
end | |
end | |
class Player | |
attr_reader :name, :sign | |
def initialize(number, sign) | |
@number = number | |
@sign = sign | |
end | |
def ask_name | |
puts "Hi, player #{@number}. What is your name?" | |
@name = gets | |
end | |
end | |
game = TicTacToe.new | |
game.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment