2024年10月23日 作者:admin | 分类:news | 浏览:30 | 评论:0
Brief introduction
This article describes the BlackJack game written in JavaScript and provides a link on GitHub that you can use to download and demo the code. In a later section, I'll also show how to create a Blackjack game in Python that resembles the same JavaScript version.
Rules of the game
Here are some basic rules of the game:
- Each player starts with a standard deck, i.e. 52 cards (4 suits: clubs, diamonds, three-horse soldiers, and hearts).
- Players choose whether they want to bet or stay through their own strategy.
- If at the end of a round, the total value exceeds 21, it is considered a "bust" and loses all chips.
- Once a certain number of chips are reached, or when there are other options, it is also possible to decide not to participate in them.
- When a round does not win any chips, it will continue until one person wins.
Implement the BlackJack game in JavaScript
First, we need to set up some variables to keep track of the state in the game. These include the player's wallet, current game progress, and a new set of cards each time is introduced. Here's the bare minimum variable we need:
```javascript
letplayerMoney=100;
lettotalRoundCount=0; Count the number of rounds you have
constnewDeckOfCards=createNewDeck();
```
Next, we define some functions to handle the different phases of the operation:
createNewDeck()
This is the function that initializes the initial playing cards, which randomly shuffles a complete new deck and returns it to the list.
```javascript
functioncreateNewDeck(){
constsuits=['hearts','diamonds','spades','clubs'];
constranks=['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King'];
letdeck=[];
for(letsuitofsuits){
for(letrankofranks){
if(rank==="Ace"){
deck.push([rank,suit]);
}else{
deck.push(`${rank}${suit}`);
}
}
}
returnshuffle(deck);
}
```
dealCard()
This function is used to assign new cards to players.
```javascript
functiondealCard(){
constrandomIndex=Math.floor(Math.random()newDeckOfCards.length);
varcard=newDeckOfCards[randomIndex];
console.log("Youhavebeendealta:"+card[0]+"of"+card[1]);
newDeckOfCards.splice(randomIndex,1);
}
Call the function
dealsCard(); The dealCard is called here, not the dealCard
```
The above is a simple and restrictive applet that can be further expanded and optimized according to needs. You can also try modifying the code to make it more in line with your personal preferences and test different results.
Perform the same function in Python
Now, let's see how this type of Blackjack game can be created in Python. This will involve simulating a deck of cards, as well as creating two important objects, the Player and the Game. Let's break it down in detail so we can better understand how to accomplish this task.
Create a card object
Let's assume that our cards are represented like this:
```python
classCard:
def__init__(self,value,suit):
self.value=value
self.suit=suit
defget_value(self):
values={'Two':2,
'Three':3,
'Four':4,
'Five':5,
'Six':6,
'Seven':7,
'Eight':8,
'Nine':9,
'Ten':10,
'Jack':11,
'Queen':12,
'King':13,
'Ace':14
}
returnvalues[self.value]
```
The '__init__()' method here is responsible for constructing a new card with the specified value and suit. The get\_value() method then returns its actual value. In addition, the program contains many common nouns like "Jack", "Queen", etc.
Define the Player Object
Next, let's look at how to create an object that represents the player:
```python
fromcollectionsimportdeque
classPlayer:
def__init__(self,name):
self.name=name
defadd_card_to_hand(self,card):
self.hand.append(card)
defcalculate_sc