PigNotes

py

School

Colorado Technical University *

*We aren’t endorsed by this school

Course

011

Subject

Industrial Engineering

Date

Jan 9, 2024

Type

py

Pages

4

Uploaded by MagistratePheasantPerson1064

Report
#Part 1 One Automated Turn of Pig and 2 Hold at 20 Outcomes import random def rolld6(): return random.randint(1, 6) def holdAt20Turn(): turnTotal = 0 while turnTotal < 20: points = rolld6() print("Roll:", points) if points == 1: turnTotal = 0 break else: turnTotal += points print("Turn total:", turnTotal) return turnTotal def holdAt20Sim(trials): results = {} for _ in range(trials): turnTotal = holdAt20Turn() if turnTotal in results: results[turnTotal] += 1 else: results[turnTotal] = 1 for score in results: results[score] = results[score] / trials return results print("How many Hold-at-20 turn simulations?") num_trials = int(input()) results = holdAt20Sim(num_trials) print("Score\tEstimated Probability") for score in sorted(results): print(f"{score}\t{results[score]:.6f}") #Part 3 Hold@Xoutcomes def roll_game(): total = 0 roll = 0 while total < 100 and roll !=1: roll = random.randint(1,6) total += roll return total >= 100 def main(): count = int(input('How many Hold at X turn simulations')) wins = 0 for _ in range(count): if roll_game(): wins += 1 print('Probability of winning in a single turn:', wins / count) main() #Part 4 Hold at 20 or Goal Turn def HoldAt20orGoalTurn (limit,score): turnTotal = 0 while turnTotal < limit and turnTotal + score < 100: roll = random.randint(1,6)
print("Roll:",roll) if roll == 1: turnTotal = 0 return 0, score else: turnTotal += roll return turnTotal, score+turnTotal #Part 5 Hold at 20 or Goal Game def main(): total_score = 0 while total_score < 100: score = 0 while score < 20: roll = random.randint(1,6) print("Roll:",roll) if roll == 1: score = 0 break score += roll print('Turn total:', score) total_score += score print('New score:', total_score) main() #Part 6 Average Pig Turns def AveragePigTurns(Numberofgames): turntotal = 0 Averageturns = 0 score = 0 dievalue = 0 for i in range(Numberofgames): while((score+turntotal)<100 and turntotal<20 and dievalue!=1): dievalue = random.randint(1,6) Averageturns +=1 score += dievalue turntotal += 1 score = 0 turntotal = 0 dievalue = 0 return Averageturns / Numberofgames def main(): games = int(input("\nEnter a single positive integer indicating \nthe number of games simulated!!!\nGames? : \t")) AverageTurns = AveragePigTurns(games) print("Average Turns : "+str(AverageTurns)) main() #Part 7 Two Player Pig scores = [0, 0] for i in range(2): turn_total = 0 print("Player 1 score:", scores[0]) print("Player 2 score:", scores[1]) if i % 2 == 0:
print("It is player 1's turn") else: print("It is player 2's turn") while turn_total < 20 and scores[i % 2] + turn_total < 100: a = random.randint(1, 6) print("Roll:", a) if a == 1: turn_total = 0 else: turn_total += a scores[i % 2] += turn_total print("Turn Total:", turn_total) print("New score:", scores[i % 2]) #Part 8 Pig Game def win_condition(player_score, computer_score): if player_score >= 100 or computer_score >= 100: return True return False def computer_play(computer_score): turn_total = 0 while turn_total < 20 and turn_total + computer_score < 100: roll = random.randint(1, 6) print(f'Roll: {roll}') if roll == 1: print("Pig !!!") return 0 turn_total += roll return turn_total def human_play(player_score): turn_total = 0 while turn_total + player_score < 100: print(f'Turn total: {turn_total} Roll/Hold?') round_input = input() if round_input == "": roll = random.randint(1,6) print(f'Roll: {roll}') if roll == 1: print("Pig !!!") return 0 turn_total += roll else: break print(f'Turn Total: {turn_total}') return turn_total def main(): player_id = random.randint(1, 2) if player_id == 1: computer_id = 2 else: computer_id = 1 print(f'You will be Player {player_id}') print("Enter nothing to roll\nEnter anything to hold") player_score = 0
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
computer_score = 0 turn = 1 while not win_condition(player_score, computer_score): print(f'Player {player_id} score: {player_score}') print(f'Player {computer_id} score: {computer_score}') print(f'It is Player {turn}\'s turn') if turn == player_id: turn_score = human_play(player_score) player_score += turn_score print(f'New Score: {computer_score}') turn = computer_id else: turn_score = computer_play(computer_score) computer_score += turn_score print(f'New Score: {computer_score}') turn = player_id if player_score >= 100: print(f'Player {player_id} has won the game') else: print(f'Player {computer_id} has won the game') if __name__== "__main__": main()