Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Luke's RPS Challenge #2116

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Conversation

lukestorey95
Copy link

Working 1 player vs computer version
Working 2 player vs player version

end

get '/play' do
@game = $game

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure you had your reasons, but it looks to me like you could have avoided a global variable by using a session?

end

post '/rps' do
@game = $game

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have to assign this twice? Already assigned on line 23.

@turn = players[0]
@round = round
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are player_1 and player_2 methods necessary? It feels like these can just be instance variables of just stay as items in the players array.

attr_reader :players, :turn, :round

def initialize(player_1, player_2, round = Round.new)
@players = [player_1, player_2]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it have been cleaner to have two instance variables rather than an array?

round.winner.increase_score
calculate_outcome_message
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY

round.set_outcome('cuts')
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these messages

self.class == Computer
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fancy! So you can create an instance of computer and copy in all the methods and values from Player? Love it

Copy link

@eoinmakers eoinmakers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You demonstrated the learning objectives of the week - well done. I left a comment around your refactor of controller -> model, let me know what you think.

Testing of all possible combinations of player / comp moves to test the outcomes would also be a good addition in future projects like this.

player2 = Computer.new
else
player2 = Player.new(params[:player_2_name])
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned finding it difficult to draw this logic into a Model - firstly, well done on achieving the functionality of the user stories!

One thing that may make this refactor simpler is thinking about what parts of your program actually need to know about the Player. It's the Game Model that does - in order to know Player 1's move, the Computer's move, or Player 2's move.

If your Game class was responsible for taking player name params as arguments when initialised, you could use a default argument for player 2 - defaulting to nil if no player 2 name is provided.

Additionally in your Game logic, the code that handles comparing the two player moves to calculate the winner, could then determine if it's going to be using player2's move or generating a computer move if player2 is nil.

What do you think?

@game.turn.throw(params[:throw].to_sym)
@game.switch_turn
@game.act_for_computer
@game.calculate_outcome

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of method calls to keep in the route, when perhaps you could push the responsibility of calling each of these methods into the @GAMe instance, so you only need to call something like @game.generate_winner.

private

def rps_logic
win_condition = { scissors: :paper, paper: :rock, rock: :scissors }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concise handling of game winner logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants