A gamer should take consecutively 4 balls from an urn that c
A gamer should take consecutively 4 balls from an urn that contains an infinite number of balls having 4 different colors: White, Red, Green and Yellow. This gamer can win if and only if three of the 4 balls he takes have the same color.
Design and code an algorithm that can lead to the following results:
1- enumerate all possibilities without redundancy.
2- give the total number of possibilities that can lead to win this game.
I need pseudocode for this, and code in java or c++
This question related to Algorithms Design and Analysis
Solution
import java.util.*;
public class Game{
public static void main(String args[]){
int randomInt;
int[][] array = new int[35][5];
int[][] array_pattern = new int[35][5];
int array_index=0,k=0,array_pattern_index = 0;
while(true){
boolean flag = true;
int temp_array_index=0;
int[] temp_array=new int[5];
int[] temp_count_array=new int[4];
Random random = new Random();
for(int i=0; i<4 ; i++){
randomInt = random.nextInt(4);
temp_array[temp_array_index++] = randomInt;
temp_count_array[randomInt]++;
if(temp_count_array[randomInt]==3){
temp_array[4]=1;
}
}
for(int j=0; j < array_index; j++){
if(array[j][0]==temp_count_array[0] && array[j][1]==temp_count_array[1] && array[j][2]==temp_count_array[2] && array[j][3]==temp_count_array[3]){
flag = false;
break;
}
}
if(flag){
array_pattern[array_pattern_index][0]=temp_array[0];
array_pattern[array_pattern_index][1]=temp_array[1];
array_pattern[array_pattern_index][2]=temp_array[2];
array_pattern[array_pattern_index][3]=temp_array[3];
array_pattern[array_pattern_index][4]=temp_array[4];
array[array_index][0]=temp_count_array[0];
array[array_index][1]=temp_count_array[1];
array[array_index][2]=temp_count_array[2];
array[array_index][3]=temp_count_array[3];
array_index++;
array_pattern_index++;
}
if(array_index==35){
break;
}
}
String[] colors= new String[4];
colors[0]=\" White\";
colors[1]=\" Red\";
colors[2]=\" Green\";
colors[3]=\"Yellow\";
System.out.println(\"\\t\\t**NOTE** \ \\torder of the colors is not importance here!!\ \\t\\tWhite Green Red Yellow\ \\t\\tGreen White Red Yellow ---> both are same\ \");
System.out.println(\"All the possibilities without redundancy:-\ \");
for(int i=0;i<35;i++){
for(int j=0;j<4;j++){
System.out.print(colors[array_pattern[i][j]]+\" \");
}
System.out.println();
}
int count_winning_possibilities = 0;
System.out.println(\"\ possibilities that can lead to win this game:-\ \");
for(int i=0;i<35;i++){
if(array_pattern[i][4]==0){
continue;
}
count_winning_possibilities++;
for(int j=0;j<4;j++){
System.out.print(colors[array_pattern[i][j]]+\" \");
}
System.out.println();
}
System.out.println(\"The total number of possibilities that can lead to win this game:- \"+count_winning_possibilities);
}
}

