Jump to content

Tic-tac-toe: Difference between revisions

48 bytes removed ,  10 years ago
→‎{{header|Ruby}}: simplify creation of players and looking up of current player
(→‎{{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:
@free_positions = Set.new(1..9)
@players = [player_1_class.new(self), player_2_class.new(self)]
@current_player_id = 0
@players = [player_1_class.new(self, "X"), player_2_class.new(self, "O")]
@players[@current_player_id].marker = "X"
puts "#{@players[@current_player_id]current_player} goes first."
@players[other_player_id].marker = "O"
puts "#{@players[@current_player_id]} goes first."
end
attr_reader :board, :free_positions, :current_player_id
Line 5,270 ⟶ 5,268:
def play
loop do
place_player_marker(current_player)
player = @players[@current_player_id]
place_player_markerif player_has_won?(playercurrent_player)
puts "#{playercurrent_player} wins!"
if player_has_won?(player)
puts "#{player} wins!"
print_board
return
Line 5,312 ⟶ 5,308:
def switch_players!
@current_player_id = other_player_id
end
def current_player
@players[@current_player_id].marker = "X"
end
Line 5,334:
class Player
def initialize(game, marker)
@game = game
@marker = nilmarker
end
attr_accessorattr_reader :marker
end
21

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.