Write a MATLAB program that can play the pig dice game with multiple human and/o
ID: 3755302 • Letter: W
Question
Write a MATLAB program that can play the pig dice game with multiple human and/or computer players using 1 dice in a pig dice game party. • the program should let all the human players decide to roll or to pass the pig for themselves • the program should automatically play pig for all the computer players using a winning strategy ME 105 Midterm 1: Pig Dice Game Party Write a MATLAB program that can play the Pig dice game with multiple human and/or computer players using 1 dice in a Pig dice game party. The program should let all the human players decide to roll or to pass the pig for The program should automatically play Pig for all the computer players using a winning strategy. . The Pig dice game is very simple to learn, but complex to play optimally. Each roll is a bet to risk the accumulated points to gain more points. Here are some well-known probability tactics that can improve the odds to win 1. Roll when the accumulated points are less than 20-The odds te roll a 1 vs the other 5 values are 1:5. The average points that we can gain from each successful roll (rolling a 2, 3, 4, 5, or 6) is 4 points. When the accumulated points are less, we put less points at stake to potentially gain 4 more points on average. The idea is to risk less to gain more. When we accumulate 20 points or more, it becomes risk more to gain less. Stop rolling after reaching 100 points- If the game score is 90 points, stop rolling when the score in the current turn accumulates to 10 points. we can win the game already. Roll to prevent others from winning-If one of the opponents has a game score of 96 or higher, this opponent is highly likely to win the game if this opponent get to roll in the next turn. If my game score is 76 and I accumulated 20 points in the current turn, it makes sense to risk the 20 points and keep rolling to try to win the game before the opponent. 2. Don't take unnecessary risk if 3. Whether you are stuck in a waiting room, needing entertainment at a restaurant a simple Pig is considered a jeopardy game, where you risk everything to see if you can win more. All you , or looking for game while on vacation, Pig is the perfect way to pass the time with family and friends. need is one or two six-sided dice and a way to keep score. Pig is best with 2 players, but it can work with more people. Here is a recap of the Pig dice game rules:Explanation / Answer
Answer: See the code below:
--------------------------------------
#This program shows the pig dice game.
#number of human players
num_human_players = 0;
#number of computer players
num_computer_players = 0;
#enter number of human players
num_human_players = input("Enter number of human players:");
#enter number of computer players
num_computer_players = input("Enter number of computer players:");
#number of total players
num_total_players = num_human_players+num_computer_players;
human_players = 1:num_human_players;
computer_players = linspace(num_human_players+1,num_total_players,num_computer_players);
#Welcome message and purpose of game
disp("Welcome to the Pig Dice Game Party!!!");
disp("-------------------------------------");
disp("How to play:");
disp("------------");
disp("1. In this game, players roll the dice in turns.");
disp("2. On a turn, a player rolls the dice repeatedly.");
disp("3. Numbers rolled are added to the turn score of that player.");
disp("4. However, if the players rolls a 1, player's turn is over , and the score for that turn is 0.");
disp("5. Before rolling a 1, player can choose to pass the pig (to end its turn) anytime");
disp("if the player doesn't want to take a chance of rolling a 1 and loosing all");
disp("of the points accumulated from this turn.");
disp("6. Winner is the first player to reach 100 points.");
turn_number = 0;
disp("Lets start playing the game!!!")
turn_number=turn_number+1;
printf("Turn:%d ",turn_number);
current_player = 0;
if(num_human_players != 0)
current_player_type = 'H'; #H of human player and C for computer player
#randomly select a human player to play
current_player = randi(num_human_players);
else
current_player_type = 'C';
#randomly select a computer player to play
current_player = randi([num_human_players+1,num_total_players],1);
end
printf("Player %d, it is your turn to play. ");
human_players_scores = zeros(1,num_human_players); #scores of human players
computer_players_scores = zeros(1,num_computer_players); #scores of computer players
pass_turn = 'n';
dice1=0; #value of dice1
dice2=0; #value of dice2
if (current_player_type == 'H')
pass_turn = input("Do you want to roll or pass the turn(y/n)?");
elseif (current_player_type == 'C')
if ((any(human_players_scores(:) >= 96) and (computer_players_scores(current_player-num_human_players) >= 76))
printf("Rolling dice for Player %d... ",current_player);
while(computer_players_scores(current_player-num_human_players) < 100)
dice1 = randi([1 6],1);
dice2 = randi([1 6],1);
computer_players_scores(current_player-num_human_players)=computer_players_scores(current_player-num_human_players)+dice1+dice2;
end
pass_turn = 'y';
elseif (computer_players_scores(current_player-num_human_players) <= 80)
accumulated_points = 0;
while(accumulated_points != 20)
dice1 = randi([1 6],1);
dice2 = randi([1 6],1);
accumulated_points = dice1+dice2;
end
computer_players_scores(current_player-num_human_players)=computer_players_scores(current_player-num_human_players)+accumulated_points;
pass_turn='y';
else
while(computer_players_scores(current_player-num_human_players) < 100)
dice1 = randi([1 6],1);
dice2 = randi([1 6],1);
computer_players_scores(current_player-num_human_players)=computer_players_scores(current_player-num_human_players)+dice1+dice2;
end
end
end
--------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.