#๐ hog
114 messages ยท Page 1 of 1 (latest)
@lethal cradle
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
I don't understand it either without context but if you break it down into small pieces, that would be a good starting point.
Scores after swap - Player 0: 1, Player 1: 106
Wrong result from play: input (<function always_roll.<locals>.strategy at 0x1035c8ae0>, <function always_roll.<locals>.strategy at 0x1035c91c0>)
returned (1, 106) not (92, 106)
I still have no clue. Why would it return 92,106?
maybe im missing something
Can you explain how all of this works? It might help
its a hog project (game)
and im supposed to work on hog.py which u can see in the pastebin link i sent. and im running python3 hog_grader.py in terminal. That's the file my professor made, which im not supposed to touch. I did all the functions correct im just not sure why test 4 is failing
OK I understood the hog_grader.py
Can you explain how to run hog.py?
And as it runs, can you explain what its doing and returning what out of each function?
Idk if you're also tasked to write documentation out of your code, but if you have one, share that and I'll probably just read that instead
u can find the whole thing on github, its mostly the same but with some slight tweaks
https://github.com/czahie/CS61A/blob/master/Projects/hog/README.md
no not really
ive been sturggling on it for 3 hour
So back to this. Player 0, returned 1 instead of 92. Which function handles that?
strategy0?
yes i believe it is the take turn function
they either call roll dice or free bacon
I'm looking at how this test plays out
Scores - Player 0: 1, Player 1: 91
Scores after swap - Player 0: 91, Player 1: 1
Scores - Player 0: 106, Player 1: 1
Scores after swap - Player 0: 1, Player 1: 106
Wrong result from play: input (<function always_roll.<locals>.strategy at 0x1035c8ae0>, <function always_roll.<locals>.strategy at 0x1035c91c0>)
returned (1, 106) not (92, 106)
In this, it shows Player 0 and Player 1 being 1,91 first and then swapped
After the swap its 91,1
Then Player 0 plays its grew to 106, but Player 1 doesn't get to play?
oh wait I see it
yes it swap
Where does is_swap function get used?
nvm I see it
The swap doesn't make sense
its like, it will always swap because 1 is always there
is my swap function wrong?
I think so
return (score0 < score1 and score1 % score0 == 0) or (score1 < score0 and score0 % score1 == 0)
Why do you have to check if either score is higher or lower than the other?
We just need to know if one score is a multiple over the other
Example 1: Player 0 has 20 points and Player 1 has 5; it is Player 1's turn. She scores 5 more, bringing her total to 10. The players swap scores: Player 0 now has 10 points and Player 1 has 20. It is now Player 0's turn. Example 2: Player 0 has 90 points and Player 1 has 50; it is Player 0's turn. She scores 10 more, bringing her total to 100. The players swap scores, and Player 1 wins the game 100 to 50. ```
because double mean thet swap
This rule is different from the one on Github
The one on Github is any positive multiple factor over the other
Not an exact double
Which one do you follow?
oh sorry ``` Introduction
In this project, you will develop a simulator and multiple strategies for the dice game Hog. You will need to use control and higher-order functions together, from Sections 1.1 through 1.6 of the Composing Programs online text.
In Hog, two players alternate turns trying to reach 100 points first. On each turn, the current player chooses some number of dice to roll, up to 10. His/her turn score is the sum of the dice outcomes, unless any of the dice come up a 1, in which case the score for his/her turn is only 1 point (the Pig out rule).
To spice up the game, we will play with some special rules:
Free bacon: If a player chooses to roll zero dice, he/she scores one more than the largest digit in her opponent's score.
For example, if Player 1 has 42 points, Player 0 gains 1 + max(4, 2) = 5 points by rolling zero dice. If Player 1 has 48 points, Player 0 gains 1 + max(4, 8) = 9 points.
Hog wild: If the sum of both players' total scores is a multiple of seven (e.g., 14, 21, 35), then the current player rolls four-sided dice instead of the usual six-sided dice.
Swine swap: If at the end of a turn one of the player's total score is exactly double the other's, then the players swap total scores.
Example 1: Player 0 has 20 points and Player 1 has 5; it is Player 1's turn. She scores 5 more, bringing her total to 10. The players swap scores: Player 0 now has 10 points and Player 1 has 20. It is now Player 0's turn. Example 2: Player 0 has 90 points and Player 1 has 50; it is Player 0's turn. She scores 10 more, bringing her total to 100. The players swap scores, and Player 1 wins the game 100 to 50.
This project includes six files, but all of your changes will be made to the first one (hog.py), and it is the only one you should need to read and understand.
To get started, download all of the project code as a zip archive: Download the project file```
do u want the link to the project file?
This is not in the one in Github:
Hog wild: If the sum of both players' total scores is a multiple of seven (e.g., 14, 21, 35), then the current player rolls four-sided dice instead of the usual six-sided dice.
I don't see any of your function changing the dice to four-sided
oh, its there
Man, my eyes today
Yeah the Swine Swap rule makes a huge difference
>>> score1 = 100
>>> score0 = 50
>>> score1 % score0
0
>>> score1 = 90
>>> score1 % score0
40
>>> score0 % score1
50
>>> score1 = 150
>>> score1 % score0
0
That last one, 150 % 50 gets you 0. Because 150 is a factor of 50. So the logic is incorrect.
you should use floor division of either side and check
then swap if they are the same
Does that make sense @lethal cradle ?
return (score0 < score1 and score1 // score0 == score1 / score0) or (score1 < score0 and score0 // score1 == score0 / score1)
to check ?
not quite
i dont get wym
So you don't check which score is higher than the other
You just check if one score's floor divided by 2 is equal to the other score
or even just divided by 2 is fine
Because that's the rule. Check if the score is exactly double of the other
ah return (score0 // 2 == score1) or (score1 // 2 == score0) ??
yes!
ok i changed my is_swap function to
return False
return (score0 // 2 == score1) or (score1 // 2 == score0)```
is this correct?
Test it and find out
awesome
Explain swap_strategy function. I don't get it. Why would it return 4?
def swap_strategy(score, opponent_score):
"""This strategy rolls 0 dice when it would result in a beneficial swap and
rolls BASELINE_NUM_ROLLS if it would result in a harmful swap."""
score_after_bacon = free_bacon(opponent_score) + score
if score < opponent_score and is_swap(score_after_bacon, opponent_score):
return 0 # Beneficial swap
elif free_bacon(opponent_score) >= BACON_MARGIN:
return 0 # Free Bacon is beneficial
return BASELINE_NUM_ROLLS

it decides how many dice to roll based on urs and ur opponent score to help u score more points and avoid losing points on a swap
How do you decide? According to the test grader, it expected 4 dices to roll, not 0 dice.
If the current player score is 8 and the opponent player score is 9, why not just roll 10 six-sided dice?
u cant roll 10 because it against the rules
Is there anything in the project instructions to help define this?
You can roll 10
up to 10
Still though, why 4?
Do you know?
ok so free bacon helps u gain points based on the opponent score, so if ur opponent has 9 it gives u 10 because 9+1
if u roll 0 dice and do free bacon u can get more points instead of rolling like 1
if u roll 10 dice u have higher chance of rolling 1 which means 0 points but the risk of rolling 1 is higher so its a really big gamble
i think he said rolling 4 dice is better because its a balance between risk and gaining point instead of rolling max because the risk is really high
the swap strategy is for finding a balance i think
"""This strategy rolls 0 dice when it would result in a beneficial swap and
rolls BASELINE_NUM_ROLLS if it would result in a harmful swap."""
right
The strategy part is calling a bunch of functions so thats difficult to follow. Think you can handle it or?
i will try it
def swap_strategy(score, opponent_score):
"""This strategy rolls 0 dice when it would result in a beneficial swap and
rolls BASELINE_NUM_ROLLS if it would result in a harmful swap. It also rolls
0 dice if that gives at least BACON_MARGIN points and rolls
BASELINE_NUM_ROLLS otherwise.
>>> swap_strategy(23, 60) # 23 + (1 + max(6, 0)) = 30: Beneficial swap
0
>>> swap_strategy(27, 18) # 27 + (1 + max(1, 8)) = 36: Harmful swap
5
>>> swap_strategy(50, 80) # (1 + max(8, 0)) = 9: Lots of free bacon
0
>>> swap_strategy(12, 12) # Baseline
5
"""
# Calculate the score if rolling 0 dice (Free Bacon)
free_bacon_score = free_bacon(opponent_score)
potential_score = score + free_bacon_score
if is_swap(potential_score, opponent_score):
return 0 # Roll 0 dice for a beneficial swa
if free_bacon_score >= BACON_MARGIN:
return 0 # Roll 0 dice for free bacon
baseline_roll_score = roll_dice(BASELINE_NUM_ROLLS, select_dice(score, opponent_score))
new_score_if_baseline = score + baseline_roll_score
# If rolling 0 dice is a harmful swap, roll the baseline number of dice
if is_swap(potential_score, opponent_score):
return BASELINE_NUM_ROLLS # Harmful swap because rolling 0
# If rolling the baseline is a harmful swap, roll more dice
if is_swap(new_score_if_baseline, opponent_score):
return BASELINE_NUM_ROLLS # Avoid harmful swap by rolling the baseline
# Set default to rolling the base number of dice
return BASELINE_NUM_ROLLS
Hey @lethal cradle!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
oops
i will dp pastebin
i got all right except for
Failed example:
swap_strategy(27, 18) # 27 + (1 + max(1, 8)) = 36: Harmful swap
Expected:
5
Got:
0
A doctest example failed for swap_strategy.
:(
haha i tried to fix it and
now the other one broken
Wrong result from swap_strategy: input (12, 34)
returned 4 not 0
im hysterical
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.