Hey Michael, do you think you can help me with some other specs I’m unable to pass? I have 4 that I can’t seem to figure out.
for ComputerPlayer class, I get these error messages and my code like like this
class ComputerPlayer
attr_accessor :mark, :get_move, :name, :board
def initialize(name)
@name = name
end
def display(board)
@board = board
end
def get_move
moves = []
board.each_with_index do |row, idx|
row.each_with_index do |colum, idx2|
moves << [idx, idx2] if board[idx][idx2].nil?
end
end
moves.each do |move|
return move if winning_move?(move)
end
moves.sample
end
def winning_move?(move)
board[move[0]][move[1]] = mark
if board.winner == mark
board[move[0]][move[1]] = nil
return true
else
board[move[0]][move[1]] = nil
return false
end
end
end
I tried rewriting the method few times and read the spec file multiple times but I don’t seem to be able to get the code to run. The main issue seems to be the ‘each_with_index’ method, which I don’t understand why since it is a valid enumeralbe method.
The next error I have is for the HumanPlayer method. I made a very simple display method but it doesn’t seem to like it. The error message and my code look like this:
class HumanPlayer
attr_accessor :mark, :get_move, :name, :board
def initialize(name)
@name = name
end
def display(board)
print board
end
def get_move
puts "Where would you like to place your mark? (row, col)"
pos = gets.split(",").map! { |int| int.to_i }
end
end
my last error message is for the Game class. I think it might just be because I didn’t name my players player1 & player2 but I’m not sure. The error message and my code look like this:
require_relative ‘board’
require_relative ‘human_player’
require_relative ‘computer_player’
class Game
attr_accessor :current_player, :board
def initialize(human_player, computer_player)
@human_player = human_player
@computer_players = computer_player
human_player.mark = :O
computer_player.mark = :X
@board = Board.new
@current_player = @human_player
end
def play_turn
board.place_mark(current_player.get_move, current_player.mark)
current_player.display(board)
switch_players!
end
def switch_players!
if @current_player == @human_player
@current_player = @computer_player
else
@current_player = @human_player
end
end
def play
current_player.display(board)
until board.over?
play_turn
end
if board.winner == :O
@human_player.display(board)
puts "#{@huuman_player.name} wins!"
elsif board.winner == :X
puts "#{@computer_player.name} wins!"
else
puts "TIED"
end
end
end
Also I have some other questions about this exercise as well:
- Are @grid and board same things?
- In Game class, how can the play_turn method know where the current_player should get_move (computer_player or human_player) even though it’s not labeled?
- How do I use pry to test out the game? I’ve been doing
[1] pry(main)> load ’ lib/game.rb '
=> true
[2] pry(main)> Game .new( " Josh " , " Computer " )
but I get this message:
NoMethodError: undefined method mark=' for "Josh":String from lib/game.rb:11:in
initialize’
[3] pry(main)>
I know this is a lot but I’ve been working on the same exercise for a long time and it was very difficult to grasp all the materials. I’d also appreciate it if you have any recommendation on where I could find study materials online to help me better understand related materials. Thank you!