I need some help to finish my app that helps to make optimal decision in card game.
Short game description:
Single player game - 1 player one deck only for himself
24 card deck, card values from 1 to 8, 3 colors of each (Red,Yellow, Blue).
At start of the game deck gets randomly shuffled then player pulls 5 cards (19 left in deck) to his hand and from this point he's allowed to:
Put away one card (this card is not in game anylonger) and take one card from top of shuffled deck for its place.
or
Can put away 3 cards that form a one of possible point granting combinations, then takes 3 cards to his hand.
Game ends when cards in deck ends and player have no more possible scoring combinations.
Goal of the game is to obtain as much points as possible.
List of variants: (called later combinations)
Three of a kind (8 possible in total):
111 - 20 points
222 - 30 points
333 - 40 points
444 - 50 points
555 - 60 points
666 - 70 points
777 - 80 points
888 - 90 points
Straight (card can have any color, 144 possible in total):
123 - 10 points
234 - 20 points
345 - 30 points
456 - 40 points
567 - 50 points
678 - 60 points
Straight flush (cards have to be in the same color, RRR/YYY/BBB, 18 possible in total):
123 - 50 points
234 - 60 points
345 - 70 points
456 - 80 points
567 - 90 points
678 - 100 points
I created the whole game logic in JS but suffer to make algoritm that will help me:
which card to put away from hand? - to get highest chance for combination
or:
which card to put away from hand?- to lose least amount of possible combinations
I don't really know which approach i should take. Or should i combine them somehow and let the formula decide? Currently I'm reading about risk reward ratio maybe thats the way to go?
I have to remember that with each card less i have less and less possible combinations (starting with 170), but sometimes its worth in later stage of the game to sacrifice cheaper combination for higher point score.
My past ways of thinking:
Calculate the probability of each possible remaining combination when putting away each card from hand.(i.e. 5 times) and decide which one is the worst.
Calculate the probability of each possible remaining combination for every 2 card subset of 5 card set in hand (so 10 times looking for 1 card to fill the combination) and decide which card from hand is the worst.
Calculate the probability of each possible remaining combination left in deck (not counting cards in hand there). 3+ moves in advance
And somehow add those three together to get optimal/close to optimal solution?
Should i add potential loss to my calculations and how to form them? If i take weak combination and later in the game i can not obtain higher ones since they are mutually exclusive.
Related
I am trying to bring logic and programming to a currently manually driven process.
We take the weights of 16 different crushing hammers, organize them into sets of 4 based on how close each set's weight is to the others. We are looking to have less than 1 pound difference between all 4 sets. The weights are known but I cannot logically program a method to do this without pen and paper.
Example below.
Set A
Set B
Set C
Set D
39.1
40.1
42.0
41.5
40.05
41.0
40.05
38.90
41.2
42.1
41.3
43.1
38.5
43.60
42.1
41.5
Totals
158.85
166.80
165.45
165
As you can see in the first example, Sets C and D are close enough. Sets A and B are too far apart and need readjusting, but due to the severe difference, I would most likely have to rearrange all 4 sets to get with 1 pound difference for all 4 sets. Now mind you, this is all done on paper, and I am looking for a way to plug in all numbers and have it spit out the best configuration given the hammer weights, no more paper.
Set A
Set B
Set C
Set D
42.1
39.1
42.0
41.5
40.05
41.0
40.05
38.90
43.60
43.1
41.3
42.1
38.5
41.20
40.1
41.5
Totals
164.25
164.40
163.45
164
I could do this all day, splitting hairs to get as close as possible. The closer we get the weights, the less vibration we experience and our equipments last a lot longer. Anyone have any thoughts to accomplishing this?
I'm not a good math guy but I think in order to arrange ALL 4 SETS within 1lb tolerance, all 16 hammers should have some what consistent weight that is a bit apart from targeted weight.
The most simple way I can think of is to sort all of them in order and assign index 0~3 to each group until all 16 are assigned.
I am writing a fairly basic (for now) number crunching battle simulator in javascript. Its purpose is to test formula for a forthcoming game, so we can balance players stats.
Currently, the sim reduces a players health based on the following formula
player1.hp -= (Math.floor(Math.random() * 5) + 1) * player2[Math.floor(Math.random() * player2.length)].str;
HP starts on 100, and str is 2.
player2[Math.floor(Math.random() * player2.length)].str
The above code picks 1 of 3 "parts" of the player (head, body, legs) as each piece has its own stats.
So for any particular turn, starting with player 1, their head's HP is reduced by an amount based on the first formula (random part of player 2 picked, it's str stat is multiplied by a random number between 1 and 5)
Then player 2's parts are attacked, with a random part of player 1 being picked for the str stat.
(Yes, player 2 attacks first always currently, then player 1 - this may be the issue?)
When I run a single "battle", the results are fairly mixed. But when I run 100 or 1000 battles, player 2 typically comes out on top, with an average 60% win rate.
JS file here (download and run via Node)
Player who starts always has advantage because he is 1st to be able to do the finishing blow if you think of a situation where each player has 100 hp and 1 hit = 10 damage then player who started will do the 10th hit and finish the battle while player 2 will have done only 9 hits. Randomizing damage still gives advantage as 1st player has 1 hit advantage
This is a well-known problem in military combat simulation models which are turn-based. There are a couple of solutions that are commonly used. One is to randomize who gets to go first. The second is to calculate the deltas for both players before applying those deltas. The second approach has the interesting (and realistic) result that in some cases both players can be killed.
I'm implementing a Snake game in javascript for fun, I have successfully implemented the snake, its movements and the snake-growth thing as it eats an apple.
To calculate the apple position I'm currently following these steps:
create a new apple object
create random coordinates (X and Y, between game-container boundaries) for the apple
check if the coordinates of the apple are equal to one of the snake-blocks coordinates
if step #3 is TRUE, recalculate the position of the apple, else draw the apple in the game-container
Unfortunately I found out that this algorithm is very weak.. let's say I have a 10 x 10 game container, the red square is the apple, the green square is my snake head (initial game state)
as the game progresses the snake eats more and more apples, increasing its length and leaving less and less empty cells to place an apple
Now suppose that the snake reaches a length equals to 99 while eating an apple. This means that there's only one square left to place the next apple. My algorithm (this is the worst case obviously) could take forever in order to randomize the correct value, as it discards any randomized position that it's already taken by the snake's head or tail, not caring at all to randomize the new position in a range of "empty cells" only but instead randomizing on the whole 10 x 10 game canvas.
How should I proceed to solve my busillis? Can you give me any advice on a good algorithm that I can use?
Thank you
As said in comments, the easiest solution I can think is to make a list of free coordinates and then just choose randomly from them.
And you can calculate free coordinates only if needed(when you need to add an apple).
I am trying to measure the percentage of the amount of enemies in a javascript game I am developing.
Math.round(10000000 / V.S * 100) / 100
This code works fine when V.S is over 1 million, however returns observed values such as 3400% when V.S is around the thousands, but this should be about 0.01%.
V.S is the amount of enemies
10 million is the amount of enemies needed to make you lose the game
I want to show the percentage between the amount currently and the amount needed to lose the game.
How can I make it show that?
V.S is the amount of enemies
10 million is the amount of enemies needed to make you lose the game
Your current code yields how many times V.S goes into 10 million
I want to show the percentage between the amount currently and the
amount needed to lose the game.
If you want to know what percentage of the 10 million has shown up:
var pctShown = V.S/100000; //or Math.round(V.S/100000) if you want only whole pcts
(note: reduction of dividing by 10,000,000 then * 100 to get a pct representation)
If you want the percent remaining, simply find the inverse:
var pctRemaining = 100 - V.S/100000; //or again Math.round(100 - V.S/100000)
I've tried to look through several sources online and they all deal with 5 card and 7 card hands. Also, I'm not really looking for the code, I'll try to do that on my own (although perhaps if you're willing, I'm looking to implement this in either Python or JavaScript). I just want someone to explain to me the steps involved in finding such a hand (using pseudocode).
Basically, what I'm asking is: How can I find the highest poker hand from a 9 card hand between 4 players?
Should I just assign all the highest poker hand numbers in ranking and then parsing through each player's hand and see if their hand contains that number? That seems a little tedious and I'm not sure that's the right way to do it.
Also, I noticed other hand evaluators are set for optimization and some count the number of bits per card played which confused me.
EDIT: Here's the game I'm working on:
It's a game with a 6x6 grid in which four players pick up cards where wherever their player piece lands on, they pick up that card. Cards are from a standard 52 card deck but the cards are face up and only 36 randomly selected cards from the deck are used.
Eventually, toward the end of the game, the player could at most contain 9 cards in their hands.
The way to win the game is to contain the highest poker hand amongst the four players around you.
So, essentially, if you have a royal flush and everyone else has a pair or a straight, they lose.
In another game where the highest hand is a straight and the rest have a three of a kind or a two pair, then the person with the straight wins.
So, highest poker hand is the hand highest relative to other player's hand. It is the hand that has the highest ranking cards among 4 players.
poker hand is just regular hand that may or may not be the highest in rank.
There are only 126 possible 5-cards combinations for a 9-cards hand. So you can just iterate over them to find the highest. itertools.combinations(9_cards, 5) can generate all of them.
To find the highest, a trivial implement could be define a function which gives a hand a score. Then use this function as key: max(all_5_cards_hands, key=hand_score)
You can use a tuple to represent the score. Leading by hand ranking and followed by card rankings.
An example:
STRAIGHT_FLUSH = 9
...
TWO_PAIR = 2
ONE_PAIR = 1
HIGH_CARD = 0
hand_score('A7532') # I omit suits here
# => (HIGH_CARD, 14,7,5,3,2)
hand_score('KK333')
# => (FULL_HOUSE, 3, 13)
hand_score('33444')
# => (FULL_HOUSE, 4, 3)
hand_score('AKQJ0')
# => (STRAIGHT, 14)
# Tuples can be easily compared:
(HIGH_CARD, 14,7,5,3,2) < (STRAIGHT, 14)
# => True