Blackjack made at the beginning ↓ ↓ I made blackjack with Ruby (I tried using minitest)
--Added a rule that you win if you earn 1 million yen and lose if you have 0 yen. -** play.rb (game launch class) ** --Since I wrote the dealer turn in two places in the same sentence, I made a dealer turn method to make the code easier to read. --In order to increase the feeling of excitement when drawing a card, a wait method was created so that the dealer must press Enter before drawing. --You can now bet the specified amount. -** card.rb (card class) ** -Added katakana notation such as "spades" and "hearts" to make the marks easier to understand.
-** deck.rb (deck class) ** --Since we created a play class, the player is no longer reset every time, so we created a reset method so that the deck can be reset. -** player.rb (player class) ** --Similar to the deck class, the player is no longer initialized every time, so the reset method used for testing is now used during the game. --Added a function to the score calculation method so that "A" becomes "1" when bursting. --Added the function to calculate the bet amount. -** judge.rb (judge class) ** --Added a bet calculation function when winning or losing is decided. -Added the function to judge whether the game is clear or the game is over based on your money.
-** test.rb (operation check test) ** --Added test of bet calculation function. -** main.rb (It was the main file that runs blackjack, but I deleted it because I made a play class) **
--Win with more than 1 million money in your possession. Defeat when it reaches 0 yen --"A" is converted to "1" to a convenient number. Rather, convert the total score. --Hit or stand only ――The one with the higher total score wins --You can get 3 times the bet amount by winning blackjack. Usually double.
play.rb
require './card'
require './deck'
require './judge'
require './money'
require './player'
class Play
def initialize
@player = Player.new
@dealer = Player.new(name: 'dealer')
play
end
def message(title, choice1="hit", choice2="stand") # win or lose or draw or error
case title
when :win
puts 'You win'
when :lose
puts 'You lose'
when :draw
puts 'It's a draw'
when :error
puts 'It is invalid. Type it again'
when :choice
puts "Please select\s\sPush「 1 」→ #{choice1} 、Push「 2 」→ #{choice2}"
end
end
def dealer_turn
#Processing at stand (dealer's turn)
puts 'It's the dealer's turn'
wait
#Dealer handling when player is blackjack
if @player.blackjack?
@dealer.draw
@dealer.show
if [email protected]?
message :win
@player.money.stock = @player.bet_judge(@@bet, :blackjack)
@player.money.show
return
else
message :draw
@player.money.stock = @player.bet_judge(@@bet, :draw)
@player.money.show
return
end
end
#The dealer keeps hitting until he wins the player
while true
@dealer.draw
@dealer.show
wait
if @dealer.burst?
puts 'burst!'
message :win
@player.money.stock = @player.bet_judge(@@bet, :win)
@player.money.show
break
elsif @dealer.total_score == 21 && @player.total_score == 21
message :draw
@player.money.stock = @player.bet_judge(@@bet, :draw)
@player.money.show
break
elsif @player.total_score < @dealer.total_score
message :lose
@player.money.stock = @player.bet_judge(@@bet, :lose)
@player.money.show
break
end
end
end
def bet_turn
@player.money.show
puts "How much do you want to bet?"
while true
bet = gets.to_i
if bet < 1
message :error
redo
elsif bet > @player.money.stock.to_i
puts "Your stake is higher than you have. Type it again."
redo
else
puts <<~TEXT
#{bet}I made a yen bet.
#{'-' * 41}
TEXT
return bet
end
end
end
def play
while true #Loop processing for replay
#Hand reset
@player.reset
@dealer.reset
#First turn processing
puts "Welcome to Blackjack"
puts "Rule: You win if you have more than 1 million yen. You lose when you reach 0 yen."
puts "Distribute cards"
@player.draw
@player.draw
@dealer.draw
@dealer.show
@@bet = bet_turn
@player.bet_calc(@@bet)
@player.money.show
#Processing to confirm that the player is not blackjack
if @player.blackjack?
puts "black Jack!"
@player.show
else
@player.show
message :choice
end
#Hit, stand processing (player's turn)
while true
#In the case of blackjack, the process of forcibly standing
if @player.blackjack?
command = 2
else
command = gets.to_i #Choose hit or stand
end
#Processing in case of hit
if command == 1
while true
@player.draw
if @player.burst? #Check if there is a burst
@player.show
puts "burst! !!"
message :lose
@player.money.stock = @player.bet_judge(@@bet, :lose)
@player.money.show
break
end
#Confirmation of total score after draw
@player.show
#In the case of the maximum value (21), the stand is forcibly processed.
if @player.total_score == 21
puts 'The total score has reached the maximum'
command = 2
break
end
#If it is not the maximum value, hit again or process
puts "Would you like to pull it again?"
message :choice
while true
command = gets.to_i #Again, hit or stand selection
if command == 1
break
elsif command == 2
break
else
message :error
redo
end
end
#Hit or stand handling
if command == 1
redo
elsif command == 2
break
end
end
#Forced termination processing at the time of burst
if @player.burst?
break
end
elsif command == 2
dealer_turn
command = nil
break
else
# h,Processing when input other than s
message :error
redo
end
if command == 2
dealer_turn
break
end
end
#Judgment if the game is over
if @player.gameover?
puts "The amount of money you have is now 0 yen."
puts "The game is over."
break
elsif @player.win?
puts "The amount of money I have has exceeded 1 million yen."
puts "You win! !! !! !! !! !! Congrats! !! !! !! !! !! !! !! !! !! !! !!"
break
end
#Replay processing
puts "Would you like to play again?"
message :choice, "play", "Don't play"
while true
command = gets.to_i #Choose to replay
if command == 1
command = nil
break
elsif command == 2
break
else
message :error
redo
end
end
#Replay processing
command == nil ? redo : break
end
end
def gameover
end
def wait(words='Press enter')
puts words
c = gets
end
end
Play.new
card.rb
class Card
attr_reader :mark, :number
def initialize(mark, number)
@mark = mark.freeze
@number = number.freeze
end
def convert
if @number == 'J' || @number == 'Q' || @number == 'K' || @number == 'A'
10
else
@number.to_i
end
end
end
deck.rb
class Deck
attr_accessor :cards
@@draw_count = 0
def initialize
@cards = []
create
@cards = @cards.shuffle
end
def create
nums = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']
mark = ["❤ Heart\s\s\s\s", "♦ ︎ Diamond\s\s\s\s", "♤ Spades\s\s", "♧ Clover"]
mark.each do |m|
nums.each do |n|
@cards << Card.new(m, n)
end
end
end
def draw
cards[@@draw_count]
end
def draw_count
@@draw_count += 1
end
#Card reference method for testing
def show
52.times do |n|
puts "[ #{cards[n].mark} #{cards[n].number} ]"
end
end
def reset
@cards = @cards.shuffle
@@draw_count = 0
end
end
player.rb
class Player
include Judge
attr_accessor :hands, :total_score, :name, :money
@@deck = Deck.new
def initialize(name: "player")
@hands = []
@total_score = 0
@name = name
@money = Money.new
end
def draw
self.hands << @@deck.draw
self.total_score = self.total_score_calc
@@deck.draw_count
end
def show
hands = []
self.hands.each do |hand|
hands << "#{hand.mark} #{hand.number}"
end
puts "#{self.name}Is in your hand"
puts hands
puts <<~TEXT
#{puts "[total#{@total_score} ]"}
-----------------------------------------
TEXT
end
def a_count
a = 0
self.hands.each do |card|
if card.number == 'A'
a += 1
end
end
a
end
def reset
self.hands = []
self.total_score = 0
@@deck.reset
end
def total_score_calc
@total_score = 0 #If you calculate multiple times, reset the total score each time so that it will not be added
self.hands.each do |hand| # @If there is an "A" in the number and the total score is 22 or more, the number of "A" will be calculated from the total score.-9 Create a method to count the number of "A".
@total_score += hand.convert
end
if self.burst? && self.in_a?
@total_score -= (self.a_count * 9) #From the total score by the number of "A"-9 Create a method to count the number of "A".
end
@total_score
end
def bet_calc(bet)
self.money.stock = self.money.stock.to_i - bet
end
#Test method
def test
puts "Test --------"
end
def reset_score
self.total_score = 0
end
def in_blackjack
self.hands << Card.new("♦︎", "J")
self.hands << Card.new("♦︎", "A")
end
end
# player = Player.new
# enemy = Player.new
# player.draw
# player.draw
# enemy.draw
# enemy.draw
# player.hands
# player.total_score_calc
# enemy.total_score_calc
# p player.blackjack?
# p enemy.blackjack?
judge.rb
module Judge
def bet_judge(bet, judge)
stock = self.money.stock
case judge
when :win
stock += bet * 2
when :lose
stock += 0
when :blackjack
stock += bet * 3
when :draw
stock += bet
end
end
def blackjack?
if self.hands[1].number == "A" || self.hands[0].number == "A"
if self.hands[0].number == "J" || self.hands[0].number == "Q"|| self.hands[0].number == "K" || \
self.hands[1].number == "J" || self.hands[1].number == "Q"|| self.hands[1].number == "K"
self.total_score = "black Jack"
true
else
false
end
else
false
end
end
def burst?
self.total_score > 21 ? true : false
end
def in_a?
self.hands.map do |card|
if card.number == 'A'
return true
end
end
false
end
def gameover?
self.money.stock <= 0 ? true : false
end
def win?
self.money.stock > 1000000 ? true : false
end
end
test.rb(minitest)
test.rb
require 'minitest/autorun'
require './card'
require './deck'
require './judge'
require './money'
require './player'
class BlackJackTest < Minitest::Test
#The test card mark is unified with "♦ ︎"
@@test_J = Card.new('♦︎', 'J')
@@test_Q = Card.new('♦︎', 'Q')
@@test_K = Card.new('♦︎', 'K')
@@test_A = Card.new('♦︎', 'A')
@@test_5 = Card.new('♦︎', 5)
@@test_10 = Card.new('♦︎', 10)
@@test_player = Player.new
def test_blackjack?
blackjack_player = Player.new
blackjack_player.hands << @@test_A
blackjack_player.hands << @@test_J
assert blackjack_player.blackjack?
blackjack_player.reset
blackjack_player.hands << @@test_J
blackjack_player.hands << @@test_A
assert blackjack_player.blackjack?
blackjack_player.reset
blackjack_player.hands << @@test_Q
blackjack_player.hands << @@test_A
assert blackjack_player.blackjack?
blackjack_player.reset
blackjack_player.hands << @@test_A
blackjack_player.hands << @@test_Q
assert blackjack_player.blackjack?
blackjack_player.reset
blackjack_player.hands << @@test_A
blackjack_player.hands << @@test_K
assert blackjack_player.blackjack?
blackjack_player.reset
blackjack_player.hands << @@test_K
blackjack_player.hands << @@test_A
assert blackjack_player.blackjack?
#false pattern
# blackjack_player.reset
# blackjack_player.hands << @@test_A
# blackjack_player.hands << @@test_10
# assert blackjack_player.blackjack?
# blackjack_player.reset
# blackjack_player.hands << @@test_5
# blackjack_player.hands << @@test_5
# assert blackjack_player.blackjack?
# blackjack_player.reset
# blackjack_player.hands << @@test_J
# blackjack_player.hands << @@test_Q
# assert blackjack_player.blackjack?
end
def test_burst?
burst_player = Player.new
burst_player.hands << @@test_J
burst_player.hands << @@test_J
burst_player.hands << @@test_5
burst_player.total_score_calc
burst_player.total_score
assert burst_player.burst?
# false
# burst_player.reset
# burst_player.hands << @@test_10
# burst_player.total_score
# assert burst_player.burst?
end
def test_total_score_calc
total_score_calc_player = Player.new
total_score_calc_player.hands << @@test_10
total_score_calc_player.hands << @@test_5
total_score_calc_player.total_score_calc
assert_equal 15, total_score_calc_player.total_score
total_score_calc_player.reset
total_score_calc_player.hands << @@test_A
total_score_calc_player.hands << @@test_10
total_score_calc_player.hands << @@test_5
total_score_calc_player.total_score_calc
assert_equal 16, total_score_calc_player.total_score
total_score_calc_player.reset
total_score_calc_player.hands << @@test_A
total_score_calc_player.hands << @@test_J
assert_equal 20, total_score_calc_player.total_score_calc
total_score_calc_player.hands << @@test_5
assert_equal 16, total_score_calc_player.total_score_calc
total_score_calc_player.hands << @@test_A
assert_equal 17, total_score_calc_player.total_score_calc
# false
#assert_equal 10, total_score_calc_player.total_score
end
def test_convert
assert_equal 10, @@test_J.convert
assert_equal 10, @@test_Q.convert
assert_equal 10, @@test_K.convert
assert_equal 10, @@test_A.convert
assert_equal 5, @@test_5.convert
end
def test_draw_count
deck = Deck.new
assert_equal 1, @@test_player.draw
assert_equal 2, @@test_player.draw
assert_equal 3, @@test_player.draw
end
def test_a_count
a_player = Player.new
a_player.hands << @@test_A
a_player.hands << @@test_A
a_player.hands << @@test_A
assert_equal 3, a_player.a_count
end
def test_bet_calc
#assert_equal 19000, @@test_player.bet_calc(1000)
end
def test_bet_judge
p @@test_player.money.stock -= 250 #Bed
assert_equal 20250, @@test_player.bet_judge(250, :win)
assert_equal 19750, @@test_player.bet_judge(250, :lose)
assert_equal 20500, @@test_player.bet_judge(250, :blackjack)
assert_equal 20000, @@test_player.bet_judge(250, :draw)
end
end
Recommended Posts