Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:43:50+00:00 2026-06-13T09:43:50+00:00

Below is the code for a simulation of a simple blackjack game. The player

  • 0

Below is the code for a simulation of a simple blackjack game. The player and dealer (house) draw two cards. The player then hits until he reaches 17. The dealer (house) then hits until he draws with the player or goes bust. Considering the player plays first the odds of him winning should be less than 50% for any strategy. However, for some reason when I repeat the simulation it seems the player wins more than 50% of the time. There must be some error in my code giving me this result.

one_suit = [2,3,4,5,6,7,8,9,10,10,10,10,11]; #the value of the cards for blackjack
$full_deck = one_suit*4; #clubs, diamonds, hearts and spades
$deck = $full_deck.dup; #start off the game with a full deck

class Player
  attr_accessor :ace_count
  attr_accessor :hand_value

  def initialize(ace_count,hand_value)
    @ace_count  = ace_count;
    @hand_value = hand_value;
  end

  def hit #instance method, vs. self.hit = class method
    choice_of_card = rand($deck.length); #choose a random card out of the deck
    drawn_card = $deck[choice_of_card]; #draw that random card from the deck
    $deck.delete_at(choice_of_card); #remove that card from the array
    if drawn_card == 11 #if you draw an ace
     self.ace_count += 1;
    end 
    self.hand_value += drawn_card;
  end


  def flip_aces
    while self.hand_value > 21 && self.ace_count > 0 #repeat until hand is below 21 or aces are all flipped
     self.ace_count -= 1 #ace gets flipped to a 1
     self.hand_value -= 10 #ace goes from 11 to 1
    end
  end

end


def oneplayergame
 $deck = $full_deck.dup; #start a new game with a full deck 
 #generate the house and the player 
 house = Player.new(0,0);
 player1 = Player.new(0,0);
 #both the house and the player draw two cards
 house.hit; house.hit; player1.hit; player1.hit;
 while player1.hand_value <= 17 #PLAYER STRATEGY: ON WHAT CARD DOES THE PLAYER STAND
  player1.hit;
  player1.flip_aces; 
 end
 while house.hand_value < player1.hand_value && player1.hand_value <=21 #HOUSE DRAWS CARDS IF UNDER PLAYER VALUE AND PLAYER IS NOT BUST
  house.hit;
  house.flip_aces; 
 end
 #outcome



 if player1.hand_value < house.hand_value && house.hand_value <= 21
  return -1;
 elsif player1.hand_value > house.hand_value && player1.hand_value <= 21
  return 1;
 elsif player1.hand_value < house.hand_value && house.hand_value > 21
  return 1;
 elsif player1.hand_value > house.hand_value && player1.hand_value > 21
  return -1;
 else return 0;
 end
end

#the simulation
wins = 0;
losses = 0;
rounds=10000;

for i in 1..rounds
 oneplayergame;
 if oneplayergame >0 
  wins +=1;
 elsif oneplayergame <0
  losses +=1
 end
end

print wins/(wins+losses).round(3)*100;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:43:51+00:00Added an answer on June 13, 2026 at 9:43 am

    This code

    for i in 1..rounds
     oneplayergame;
     if oneplayergame >0 
       wins +=1;
     elsif oneplayergame <0
       losses +=1
    end
    

    Is wrong: that is calling oneplayergame three times: a loss is only counted if a player looses the second and 3rd calls, which would skew the results. You should only call oneplayergame once, assigning the result to a local variable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below code is very simple. I have a jQuery autocomplete bound to an
Below is my code that shows a very simple scenario with multiple triggers and
Let's say I have a simple script with two functions below. As callback_func is
Below code not work, but it's work fine for jsf1.2. Now the framework is
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
below code is my databasehandler class i got it from a tutorial. Beside that
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =
Below code makes form to be submitted without html5 validation... $j(document).on(click, #client_entry_add, function(event){ ajax_submit();});
In below code when I click 'Vote' a vote results screen is displayed but
The below code give me a foreach error. What appears to be the issue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.