Creating a Cee-lo game with Ruby 4th Creating a game progress process

1.First of all

Finally, I will write the process related to the progress of the game. Since each method is implemented in advance, it should be easier than starting from nothing. Let's do it.

2. Create main.rb

Create main.rb in the same folder as each file. First, let's write the flow as follows.


#Cee-lo game

#Creating a player

# ----Loop until settled
#Set stake (yourself)
#Betting setting (opposite)
#Roll the dice
#Decision of role
#Win / loss judgment
#Move stakes
#Final victory / defeat judgment
# ----Loop until settled

I will add it little by little. transfer_money.rb has been changed to player_class.rb.

require './player_class'  # transfer_money.Change from rb
require './dice_roll'
#Cee-lo game

#Creating a player
player_A = Player.new(money:1000,name:'Kaiji')
player_B = Player.new(money:1000,name:'Team leader')
puts '---Cee-lo game---'

# ----Loop until settled
loop do
#Set stake (yourself)
  puts 'Please enter the bet'
  bet_money = gets.chomp
  player_A.bet_money = bet_money.to_i
#Betting setting (opposite)
  bet_money_B = rand(1..4) * 200
  player_B.bet_money = player_B.money < bet_money_B ? player_B.money : bet_money_B

    puts <<~TEXT
name:#{player_A.name} 
Money in possession:#{player_A.money}Pelica
Bet:#{player_A.bet_money}Pelica
    -
name:#{player_B.name} 
Money in possession:#{player_B.money}Pelica
Bet:#{player_B.bet_money}Pelica

    press enter
    TEXT
    teisi = gets
#Roll the dice/Decision of role
    eye_on_the_dices1 = [rand(1..6),rand(1..6),rand(1..6)]
    eye_on_the_dices2 = [rand(1..6),rand(1..6),rand(1..6)]
    player_A.hand = roll_dice(eye_on_the_dices1)
    player_B.hand = roll_dice(eye_on_the_dices2)

    #Confirmation of appearance / determination of role
    puts <<~TEXT
name:#{player_A.name} 
Roll#{eye_on_the_dices1}
Role:#{player_A.hand}
    -
name:#{player_B.name}
Roll#{eye_on_the_dices2} 
Role:#{player_B.hand}

    press enter
    TEXT
    teisi = gets
#Win / loss judgment
   win_or_lose = player_A.check_win_lose(player_B)
#Move stakes
   move_money = player_A.transfer_money(player_B,win_or_lose)

   player_A.money += move_money
   player_B.money -= move_money
    puts <<~TEXT
name:#{player_A.name} 
Money in possession:#{player_A.money}Pelica
    -
name:#{player_B.name} 
Money in possession:#{player_B.money}Pelica
    --------------------------
    TEXT
#Final victory / defeat judgment
    if player_A.money <= 0
        puts 'No money in your possession. lost…'  
        break
    elsif player_B.money <= 0
        puts 'Won!'
        break
    end
# ----Loop until settled
end

Ichiou Now you can play. However, it still needs improvement.

3. Betting exception handling

The above code has no restrictions on stake entry. In this case, you can enter more stakes than you have, and we will accept negative numbers, decimal points, and strings. Let's improve.

#Set stake (yourself)
    while true
        puts 'Enter your bet (enter exit to exit)'
        bet_money = gets.chomp
        
        if bet_money == "exit"
            puts 'End'
            exit
        end
        if bet_money =~ /^[0-9]+$/ 
            if bet_money.to_i > player_A.money
                puts 'You cannot bet more than your money'
            elsif bet_money.to_i == 0
                puts '0 is invalid'
            else
                player_A.bet_money = bet_money.to_i
                break
            end
        else
            puts 'Please enter as a positive integer'
        end
    end

There are three changes.

--Changed so that you can exit the game by typing ʻexit --Check that there are no character strings or decimal points with= ~ / ^ [0-9] + $ /` --An error occurs if you have more than your money or if it is 0

This should work fine.

4. The game is ready!

I made a game only with Ruby. I'm a little happy. I was so happy that I made a presentation at the study session. At an event called Hiroshima cluster, you can announce anything that is IT-like in Hiroshima prefecture.

https://hmcn.connpass.com/event/175209/

Click here for materials.  https://www.slideshare.net/Kkondo2/ruby-234646600?ref=https://hmcn.connpass.com/event/175209/presentation/

5. There are many improvements!

I was able to write the code safely, but there are still some improvements. When I asked the engineers for a review, I received a lot of feedback. For the time being, I put it in an issue on github and decided to respond more and more.

I'll do it! https://github.com/kyokucho1989/ruby-game/issues

Also, feel free to use this source code. You can clone and modify it. https://github.com/kyokucho1989/ruby-game

Recommended Posts

Creating a Cee-lo game with Ruby 4th Creating a game progress process
Make a typing game with ruby
Making a Cee-lo game with Ruby Final improvement after receiving a review
Creating a browser automation tool with Ruby + Selenium
11th, Classification with Ruby
Creating a calendar using Ruby
[Rails] Creating a new project with rails new
Creating a timer app with a muddy
Ruby Learning # 22 Building a Guessing Game
Start with a browser Ruby game programming: Introduction to Nyle-canvas (DXRuby style)
With ruby ● × Game and Othello (basic review)
The road to creating a music game 2
Let's make a smart home with Ruby!
Ruby Learning # 12 Building a Mad Libs Game
I made a risky die with Ruby
Extract a part of a string with Ruby
The road to creating a music game 3
The road to creating a music game 1
A story about creating a library that operates next-generation sequencer data with Ruby ruby-htslib
Escape processing when creating a URL in Ruby
AtCoder Beginner Contest 169 A, B, C with ruby
Let's create a timed process with Java Timer! !!
(Ruby on Rails6) Creating data in a table
A simple rock-paper-scissors game with JavaFX and SceneBuilder
Creating a common repository with Spring Data JPA
I made a portfolio with Ruby On Rails