Using Java In the childrens game Duck Duck Goose a group of
Using Java:
In the children’s game, Duck, Duck, Goose, a group of children sit in a circle (ask user to input how many player and how many round to play). One of them is elected “it” and that person walks around the outside of the circle. The person who is “it” pats each child on the head, saying “Duck” each time, until randomly reaching a child that the “it” person identifies as “Goose.” At this point there is a mad scramble, as the “Goose” and the “it” person race around the circle. Whoever returns to the Goose’s former place first gets to remain in the circle. The loser of this race is the “it” person for the next round of play. The game continues like this until the children get bored or an adult tells them it’s snack time. Write software that simulates a game of Duck, Duck, Goose.
Using Linklist or Array
Solution
import java.util.*;
import javax.swing.JOptionPane;
public class GoosePickerGame
{
public static Random numGen =new Random();
public static int RandNum(int count){
int rand = Math.abs((0)+numGen.nextInt(count));
return rand;
}
public static void main(String[] args)
{
String cycles = JOptionPane.showInputDialog(\"Please input the number of rounds\");
int cyclesUse = Integer.parseInt(cycles);
String players = JOptionPane.showInputDialog(\"Please input the number of players\");
int playersUse = Integer.parseInt(players);
ArrayList<String> playerList = new ArrayList<String>(playersUse);
int countUp = 0;
while(countUp < playersUse)
{
countUp++;
playerList.add(\"Player\" + countUp);
}
System.out.println(\"Player List is : \"+playerList);
int it = RandNum(playersUse-1);
String playerIT = playerList.get(it);
// Removing the player who is IT from the array
playerList.remove(it);
for(int iCount=1;iCount<=cyclesUse;iCount++){
System.out.println(\"Round \"+iCount+ \" started\");
System.out.println(\"IT for round \"+iCount+\" is : \"+playerIT);
System.out.println(\"Round \"+iCount+\" player list is :\"+playerList);
int gooseIndex = RandNum(playersUse-1);
System.out.println(\"Goose is : \"+playerList.get(gooseIndex));
String playerGoose = playerList.get(gooseIndex);
ArrayList<String> runnerList = new ArrayList<String>(2);
runnerList.add(playerIT);
runnerList.add(playerGoose);
int winner = RandNum(runnerList.size()-1);
System.out.println(\"Winner is : \"+ runnerList.get(winner));
if(winner == 1){
playerIT = runnerList.get(0);
}else{
playerIT = runnerList.get(1);
}
// Replacing the winner to main list
playerList.remove(gooseIndex);
playerList.add(gooseIndex, runnerList.get(winner));
System.out.println(\"Player list after Round \"+iCount+\" is : \"+playerList);
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Output for above game
Input Rounds : 3
Input Players : 5
Output is
-----------------------------------------------------------
Player List is : [Player1, Player2, Player3, Player4, Player5]
Round 1 started
IT for round 1 is : Player4
Round 1 player list is :[Player1, Player2, Player3, Player5]
Goose is : Player1
Winner is : Player4
Player list after Round 1 is : [Player4, Player2, Player3, Player5]
Round 2 started
IT for round 2 is : Player1
Round 2 player list is :[Player4, Player2, Player3, Player5]
Goose is : Player4
Winner is : Player1
Player list after Round 2 is : [Player1, Player2, Player3, Player5]
Round 3 started
IT for round 3 is : Player4
Round 3 player list is :[Player1, Player2, Player3, Player5]
Goose is : Player1
Winner is : Player4
Player list after Round 3 is : [Player4, Player2, Player3, Player5]
---------------------------------------------------------------------------------------------------------------------------


