Created
November 29, 2023 23:20
-
-
Save rafarocha/d7b01fdc3cdde26da049cd57c174677f 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 JogoDeBingo { | |
private static final int MAX_NUMEROS = 60; | |
private static final int NUMEROS_POR_SORTEIO = 5; | |
private int[] numeros; | |
private Set<Integer> numerosSorteados; | |
private boolean sorteioAutomatico; | |
private String[] jogadores; | |
private int[][] cartelasJogadores; | |
private Map<String, Integer> pontuacaoJogadores; | |
private Map<String, int[]> numerosAcertadosJogadores; | |
public JogoDeBingo(boolean sorteioAutomatico, String[] jogadores) { | |
this.sorteioAutomatico = sorteioAutomatico; | |
this.jogadores = jogadores; | |
this.cartelasJogadores = new int[jogadores.length][NUMEROS_POR_SORTEIO]; | |
this.pontuacaoJogadores = new HashMap<>(); | |
this.numerosAcertadosJogadores = new HashMap<>(); | |
inicializar(); | |
} | |
private void inicializar() { | |
numeros = new int[MAX_NUMEROS]; | |
numerosSorteados = new HashSet<>(); | |
for (int i = 0; i < MAX_NUMEROS; i++) { | |
numeros[i] = i + 1; | |
} | |
embaralharArray(numeros); | |
} | |
private void embaralharArray(int[] array) { | |
Random random = new Random(); | |
for (int i = array.length - 1; i > 0; i--) { | |
int indice = random.nextInt(i + 1); | |
int temp = array[indice]; | |
array[indice] = array[i]; | |
array[i] = temp; | |
} | |
} | |
public void iniciarJogo() { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("╔════════════════════════════╗"); | |
System.out.println("║ Bem-vindo ao Bingo Mania ║"); | |
System.out.println("╚════════════════════════════╝"); | |
System.out.println("Opções:"); | |
System.out.println("1. Modo Automático"); | |
System.out.println("2. Modo Manual"); | |
if (sorteioAutomatico) { | |
System.out.println("Sorteio automático de números..."); | |
} else { | |
System.out.println("Pressione Enter para sortear números manualmente..."); | |
} | |
System.out.println("Por favor, digite os nomes dos jogadores separados por hífen (ex.: jogador1-jogador2):"); | |
String entradaJogadores = scanner.nextLine(); | |
jogadores = entradaJogadores.split("-"); | |
System.out.println("Vamos começar o Bingo Mania!"); | |
System.out.println("Jogadores: " + String.join(", ", jogadores)); | |
System.out.println("Marque as cartelas manualmente."); | |
inicializarPontuacaoJogadores(); | |
inicializarNumerosAcertados(); | |
int rodadas = 0; | |
while (!bingo()) { | |
rodadas++; | |
if (sorteioAutomatico) { | |
realizarSorteioAutomatico(); | |
} else { | |
System.out.println("Pressione Enter para sortear números..."); | |
scanner.nextLine(); | |
realizarSorteioManual(scanner); | |
} | |
exibirEstatisticas(); | |
mostrarTopJogadores(); | |
System.out.println("Pressione qualquer tecla para continuar ou 'X' para abortar..."); | |
String entrada = scanner.nextLine(); | |
if (entrada.equalsIgnoreCase("X")) { | |
System.out.println("Abortando o jogo..."); | |
break; | |
} | |
} | |
exibirEstatisticasFinais(rodadas); | |
gerarCartelas(); | |
} | |
private void gerarCartelas() { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Deseja gerar as cartelas de forma RANDOM ou MANUAL? (Digite RANDOM ou MANUAL):"); | |
String opcao = scanner.nextLine(); | |
switch (opcao.toUpperCase()) { | |
case "RANDOM": | |
gerarCartelasRandom(); | |
break; | |
case "MANUAL": | |
gerarCartelasManual(scanner); | |
break; | |
default: | |
System.out.println("Opção inválida. Gerando cartelas RANDOM..."); | |
gerarCartelasRandom(); | |
break; | |
} | |
} | |
private void gerarCartelasRandom() { | |
for (int i = 0; i < jogadores.length; i++) { | |
Set<Integer> cartela = new HashSet<>(); | |
while (cartela.size() < NUMEROS_POR_SORTEIO) { | |
int num = numeros[i * NUMEROS_POR_SORTEIO + cartela.size()]; | |
cartela.add(num); | |
} | |
cartelasJogadores[i] = cartela.stream().mapToInt(Integer::intValue).toArray(); | |
} | |
} | |
private void gerarCartelasManual(Scanner scanner) { | |
for (int i = 0; i < jogadores.length; i++) { | |
System.out.println("Insira os números da cartela para " + jogadores[i] + " separados por vírgula:"); | |
String input = scanner.nextLine(); | |
String[] numerosStr = input.split(","); | |
int[] cartela = Arrays.stream(numerosStr).mapToInt(Integer::parseInt).toArray(); | |
cartelasJogadores[i] = cartela; | |
} | |
} | |
private void inicializarPontuacaoJogadores() { | |
for (String jogador : jogadores) { | |
pontuacaoJogadores.put(jogador, 0); | |
} | |
} | |
private void inicializarNumerosAcertados() { | |
for (String jogador : jogadores) { | |
int[] numerosAcertados = new int[NUMEROS_POR_SORTEIO]; | |
numerosAcertadosJogadores.put(jogador, numerosAcertados); | |
} | |
} | |
private void realizarSorteioAutomatico() { | |
System.out.println("Sorteio automático de números..."); | |
realizarSorteio(); | |
} | |
private void realizarSorteioManual(Scanner scanner) { | |
System.out.println("Digite os números sorteados separados por vírgula (ex.: 1,2,3,4,5):"); | |
String entrada = scanner.nextLine(); | |
String[] numerosSorteadosStr = entrada.split(","); | |
List<Integer> numerosSorteadosTemp = new ArrayList<>(); | |
for (String num : numerosSorteadosStr) { | |
int numero = Integer.parseInt(num); | |
numerosSorteadosTemp.add(numero); | |
conferirAcertos(numero); | |
} | |
numerosSorteados = new HashSet<>(numerosSorteadosTemp); | |
} | |
private void realizarSorteio() { | |
Random random = new Random(); | |
numerosSorteados.clear(); | |
while (numerosSorteados.size() < NUMEROS_POR_SORTEIO) { | |
int num = sortearProximoNumero(random); | |
numerosSorteados.add(num); | |
conferirAcertos(num); | |
} | |
} | |
private int sortearProximoNumero(Random random) { | |
int numSorteado; | |
do { | |
numSorteado = numeros[random.nextInt(MAX_NUMEROS)]; | |
} while (numerosSorteados.contains(numSorteado)); | |
return numSorteado; | |
} | |
private void removerNumerosSorteadosDoConjunto() { | |
Iterator<Integer> iterator = numerosSorteados.iterator(); | |
while (iterator.hasNext()) { | |
Integer numerosSorteados = iterator.next(); | |
for (int i = 0; i < MAX_NUMEROS; i++) { | |
if (numeros[i] == numerosSorteados) { | |
numeros[i] = -1; | |
break; | |
} | |
} | |
} | |
} | |
private void conferirAcertos(int numero) { | |
for (int i = 0; i < cartelasJogadores.length; i++) { | |
for (int j = 0; j < cartelasJogadores[i].length; j++) { | |
if (cartelasJogadores[i][j] == numero) { | |
if (i < jogadores.length && j < NUMEROS_POR_SORTEIO) { | |
numerosAcertadosJogadores.get(jogadores[i])[j] = 1; | |
pontuacaoJogadores.put(jogadores[i], pontuacaoJogadores.get(jogadores[i]) + 1); | |
if (bingoJogador(jogadores[i])) { | |
System.out.println("Bingo! Jogador " + jogadores[i] + " completou a cartela!"); | |
} | |
} else { | |
System.out.println("Erro: Índices fora do intervalo."); | |
} | |
} | |
} | |
} | |
} | |
private boolean bingoJogador(String jogador) { | |
int[] numerosAcertados = numerosAcertadosJogadores.get(jogador); | |
for (int acerto : numerosAcertados) { | |
if (acerto == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private boolean bingo() { | |
for (String jogador : jogadores) { | |
if (bingoJogador(jogador)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private void exibirEstatisticas() { | |
System.out.println("Números sorteados até agora: " + numerosSorteados); | |
System.out.println("Números restantes: " + (MAX_NUMEROS - numerosSorteados.size())); | |
} | |
private void mostrarTopJogadores() { | |
List<Map.Entry<String, Integer>> lista = new LinkedList<>(pontuacaoJogadores.entrySet()); | |
lista.sort(Map.Entry.comparingByValue(Comparator.reverseOrder())); | |
System.out.println("Top 3 jogadores:"); | |
int contador = 0; | |
for (Map.Entry<String, Integer> entrada : lista) { | |
if (contador >= 3) { | |
break; | |
} | |
System.out.println(entrada.getKey() + ": " + entrada.getValue() + " acertos"); | |
contador++; | |
} | |
} | |
private void exibirEstatisticasFinais(int rodadas) { | |
System.out.println("Fim do jogo!"); | |
System.out.println("Total de rodadas jogadas: " + rodadas); | |
String jogadorVencedor = ""; | |
int maxAcertos = -1; | |
for (String jogador : jogadores) { | |
if (pontuacaoJogadores.get(jogador) > maxAcertos) { | |
maxAcertos = pontuacaoJogadores.get(jogador); | |
jogadorVencedor = jogador; | |
} | |
} | |
List<Map.Entry<String, Integer>> lista = new LinkedList<>(pontuacaoJogadores.entrySet()); | |
lista.sort(Map.Entry.comparingByValue(Comparator.reverseOrder())); | |
System.out.println("Ranking geral:"); | |
for (Map.Entry<String, Integer> entrada : lista) { | |
System.out.println(entrada.getKey() + ": " + entrada.getValue() + " acertos"); | |
} | |
System.out.println("O vencedor é: " + jogadorVencedor); | |
} | |
public static void main(String[] args) { | |
System.out.println("╔════════════════════════════╗"); | |
System.out.println("║ Bem-vindo ao Bingo Mania ║"); | |
System.out.println("╚════════════════════════════╝"); | |
boolean sorteioAutomatico = true; // true para o modo automático | |
String[] jogadores = new String[]{"Jogador1", "Jogador2"}; | |
JogoDeBingo jogo = new JogoDeBingo(sorteioAutomatico, jogadores); | |
jogo.iniciarJogo(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment