Tic-tac-toe: Difference between revisions
Content deleted Content added
→{{header|Racket}}: be more specific about the generality of game.rkt |
→{{header|Ruby}}: simplify creation of players and looking up of current player |
||
Line 5,260: | Line 5,260: | ||
@free_positions = Set.new(1..9) |
@free_positions = Set.new(1..9) |
||
⚫ | |||
@current_player_id = 0 |
@current_player_id = 0 |
||
⚫ | |||
⚫ | |||
⚫ | |||
@players[other_player_id].marker = "O" |
|||
⚫ | |||
end |
end |
||
attr_reader :board, :free_positions, :current_player_id |
attr_reader :board, :free_positions, :current_player_id |
||
Line 5,270: | Line 5,268: | ||
def play |
def play |
||
loop do |
loop do |
||
place_player_marker(current_player) |
|||
player = @players[@current_player_id] |
|||
if player_has_won?(current_player) |
|||
⚫ | |||
⚫ | |||
if player_has_won?(player) |
|||
⚫ | |||
print_board |
print_board |
||
return |
return |
||
Line 5,312: | Line 5,308: | ||
def switch_players! |
def switch_players! |
||
@current_player_id = other_player_id |
@current_player_id = other_player_id |
||
⚫ | |||
def current_player |
|||
⚫ | |||
end |
end |
||
Line 5,334: | Line 5,334: | ||
class Player |
class Player |
||
def initialize(game) |
def initialize(game, marker) |
||
@game = game |
@game = game |
||
@marker = |
@marker = marker |
||
end |
end |
||
attr_reader :marker |
|||
end |
end |
||