Created
November 29, 2023 23:32
-
-
Save rafarocha/4468a891ec07d6a3e8243a83d89b626a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class BingoDaSorte { | |
public static void main(String[] args) { | |
Scanner entrada = new Scanner(System.in); | |
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); | |
System.out.println("--------BINGO DA SORTE----------"); | |
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); | |
String[] nomesDosParticipantes = {"Claudio Luis", "Clarice", "Oliver", "Patrícia", "Nicolau", "Carlos Alberto Angelo", "Carlos Cesar Perreira", "Dirce Mitiko Taira", "Otair Caetano", "Rejane Meneses"}; | |
System.out.println("Não perca a sua chance!!! Escolha o número de participantes? "); | |
System.out.print("> "); | |
int numeroParticipantes = entrada.nextInt(); | |
entrada.nextLine(); | |
if (numeroParticipantes > nomesDosParticipantes.length) { | |
System.out.println("Número de participantes escolhido é maior que o número de participantes disponíveis. "); | |
entrada.close(); | |
return; | |
} | |
// Conjunto para armazenar os números disponíveis | |
Set<Integer> numerosDisponiveis = new HashSet<>(); | |
for (int i = 1; i <= 60; i++) { | |
numerosDisponiveis.add(i); | |
} | |
// Lista para armazenar os números sorteados em cada rodada | |
List<Integer[]> numerosSorteadosPorRodada = new ArrayList<>(); | |
// Mapa para armazenar as cartelas dos participantes e os acertos | |
Map<String, Integer[]> cartelasDosParticipantes = new HashMap<>(); | |
Map<String, int[]> acertosDosParticipantes = new HashMap<>(); | |
// Inicializa as cartelas dos participantes aleatoriamente uma única vez | |
for (int i = 0; i < numeroParticipantes; i++) { | |
String participante = nomesDosParticipantes[i]; | |
Integer[] cartela = gerarCartelaAleatoria(numerosDisponiveis); | |
cartelasDosParticipantes.put(participante, cartela); | |
// Inicializa o array de acertos com zeros | |
int[] acertos = new int[cartela.length]; | |
acertosDosParticipantes.put(participante, acertos); | |
} | |
boolean bingo = false; | |
int rodada = 1; | |
while (!bingo) { | |
// Aguarda pressionar ENTER para uma nova rodada | |
System.out.println("Pressione ENTER para a rodada " + rodada + "..."); | |
entrada.nextLine(); | |
// Array para armazenar os números sorteados | |
Integer[] numerosSorteados = sortearNumeros(numerosDisponiveis); | |
// Remove os números sorteados do conjunto total de números disponíveis | |
numerosDisponiveis.removeAll(Arrays.asList(numerosSorteados)); | |
// Adiciona os números sorteados à lista de cada rodada | |
numerosSorteadosPorRodada.add(numerosSorteados); | |
// Exibe as dezenas sorteadas em cada rodada | |
System.out.println("Números sorteados na rodada " + rodada + ": " + Arrays.toString(numerosSorteados)); | |
for (Map.Entry<String, Integer[]> entry : cartelasDosParticipantes.entrySet()) { | |
String participante = entry.getKey(); | |
Integer[] cartela = entry.getValue(); | |
int[] acertos = acertosDosParticipantes.get(participante); | |
// Verifica quais números foram acertados | |
for (int j = 0; j < cartela.length; j++) { | |
if (Arrays.asList(numerosSorteados).contains(cartela[j])) { | |
acertos[j] = 1; // Substitui zero por um quando acerta | |
} | |
} | |
System.out.println(participante + ": Cartela - " + Arrays.toString(cartela) + " | Acertos: " + Arrays.toString(acertos)); | |
// Verifica se o participante acertou todos os números | |
if (Arrays.stream(acertos).sum() == acertos.length) { | |
bingo = true; | |
System.out.println("BINGO!!!! O jogador " + participante + " acertou as três dezenas " + Arrays.toString(cartela)); | |
break; | |
} | |
} | |
rodada++; | |
} | |
entrada.close(); | |
} | |
private static Integer[] sortearNumeros(Set<Integer> numerosDisponiveis) { | |
Random random = new Random(); | |
Integer[] numerosSorteados = new Integer[3]; | |
List<Integer> disponiveis = new ArrayList<>(numerosDisponiveis); | |
Collections.shuffle(disponiveis); | |
for (int i = 0; i < 3; i++) { | |
numerosSorteados[i] = disponiveis.get(i); | |
} | |
Arrays.sort(numerosSorteados); // Ordena os números sorteados | |
return numerosSorteados; | |
} | |
private static Integer[] gerarCartelaAleatoria(Set<Integer> numerosDisponiveis) { | |
Random random = new Random(); | |
Integer[] cartela = new Integer[3]; | |
List<Integer> disponiveis = new ArrayList<>(numerosDisponiveis); | |
Collections.shuffle(disponiveis); | |
for (int i = 0; i < 3; i++) { | |
cartela[i] = disponiveis.get(i); | |
} | |
return cartela; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment