In the previous article, the whole code was finished for the time being. However, there are still many ara. That's why I got a code review at the online salon I belong to. There were quite a lot of suggestions, so I responded one by one.
Ruby indent is 2 half-width spaces
Simple comparison uses == / then no need
Better to use variable expansion
File name is difficult to understand (to transfer_money
-> player.rb
)
Roll the dice / spend money should be in the player (Because it is a player's action)
The check_win_lose
and transfer_money
methods may not be Player methods
main.rb
is made into a method in the unit of the game flow, the whole will be easier to see.strength_relationship
, etc.)roll_dice
It's not good that the return value of the method is a string Use constants or enumerators
It is a waste to set the stake of the team leader (change the stake according to the amount of money you have)
It would be nice to be able to win or lose and move money with the + operator.
Too many! !! !!
Let's do it. First of all, it is related to grammar, but this could be improved rather quickly. Indentation correction was easy with VS Code. I referred to the following article.
Change the number of indented spaces in VS Code
Variable expansion is like this. If you do it like this, you can write clearly.
dice_hand = 'Normal eyes(' + uniq_value.to_s + ')'
↓
dice_hand = "Normal eyes(#{uniq_value.to_s})"
URL of the corresponding issue https://github.com/kyokucho1989/ruby-game/issues/13
I did my best in this. First of all, it was said that it was strange for Player to judge victory or defeat and move stakes, so I created a new Game class. The Game class determines the outcome and moves the stake.
Also, I decided to do it with a class method. This is because the game class has no state. (This was also pointed out)
I also summarized the output of sentences. I created a new message.rb
so that all messages can be output from it. This will make it easier to correct the text. How nice!
A new module-hand-game.rb
was created, and the roles and win / loss results were made constant.
module Match
WIN = 'win'
LOSE = 'Lose'
DRAW = 'draw'
end
When referring to it, it looks like this. How nice!
if(my_hand_rank > opponent_hand_rank)
Match::WIN
elsif(my_hand_rank == opponent_hand_rank)
Match::DRAW
else
Match::LOSE
end
"I hope the operator can win or lose and move money." Could not be dealt with. difficult.
It was fun to be able to program like this with Ruby alone. If you do various things including Rails, you tend to be discouraged due to errors in environment construction. At first, I feel that it is simply more powerful to make something only with Ruby.
I want to make teaching materials with this one day. end. The completed code is here ↓ https://github.com/kyokucho1989/ruby-game
Recommended Posts