Created
November 29, 2023 20:39
-
-
Save rafarocha/8517a295f1e251dd01f9d5fbe9410d22 to your computer and use it in GitHub Desktop.
turma1076-desafio-bingo-avaliacao-claudio.luis.java
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.Arrays; | |
import java.util.Random; | |
import java.util.Scanner; | |
import java.util.stream.IntStream; | |
public class BingoGame { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
// Etapa 1 - Boas-vindas e opções de comando | |
System.out.println("Bem-vindo ao Jogo de Bingo Multiplayer!"); | |
System.out.println("======================================="); | |
System.out.print("Digite o nome dos jogadores separados por hífen: "); | |
String[] jogadores = scanner.nextLine().split("-"); | |
// Etapa 2 - Cartelas | |
String[][] cartelas = gerarCartelas(jogadores); | |
// Etapa 3 - Sorteio | |
sortearNumeros(jogadores, cartelas); | |
// Etapa 4 - Fim do Jogo | |
exibirResultadoFinal(jogadores, cartelas); | |
} | |
private static String[][] gerarCartelas(String[] jogadores) { | |
String[][] cartelas = new String[jogadores.length][]; | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Escolha uma opção para a cartela para os jogadores :"); | |
System.out.println("1 - MANUAL"); | |
System.out.println("2 - RANDOM"); | |
int opcao = scanner.nextInt(); | |
if (opcao == 1) { | |
for (int i = 0; i < jogadores.length; i++) { | |
scanner.nextLine(); // Consumir a quebra de linha | |
System.out.print("Preencha a cartela do jogador " + jogadores[i] + " separando os números por vírgula: "); | |
cartelas[i] = scanner.nextLine().split(","); | |
} | |
} else if (opcao == 2) { | |
cartelas = gerarCartelasRandom(jogadores); | |
} else { | |
System.out.println("Opção inválida. Usando cartela RANDOM por padrão."); | |
cartelas = gerarCartelasRandom(jogadores); | |
} | |
return cartelas; | |
} | |
private static String[][] gerarCartelasRandom(String[] jogadores) { | |
String[][] cartelas = new String[jogadores.length][]; | |
Random random = new Random(); | |
int[] numeros = random.ints(1, 61).distinct().limit(5 * jogadores.length).toArray(); | |
for (int i = 0; i < jogadores.length; i++) { | |
int[] cartela = Arrays.copyOfRange(numeros, i * 5, (i + 1) * 5); | |
cartelas[i] = Arrays.stream(cartela).sorted().mapToObj(String::valueOf).toArray(String[]::new); | |
} | |
return cartelas; | |
} | |
private static void sortearNumeros(String[] jogadores, String[][] cartelas) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Escolha uma opção para sortear os numeros :"); | |
System.out.println("1 - MANUAL"); | |
System.out.println("2 - RANDOM"); | |
int opcao = scanner.nextInt(); | |
if (opcao == 1) { | |
sortearNumerosManual(jogadores, cartelas); | |
} else if (opcao == 2) { | |
sortearNumerosRandom(jogadores, cartelas); | |
} else { | |
System.out.println("Opção inválida. Usando sorteio RANDOM por padrão."); | |
sortearNumerosRandom(jogadores, cartelas); | |
} | |
} | |
private static void sortearNumerosRandom(String[] jogadores, String[][] cartelas) { | |
Scanner scanner = new Scanner(System.in); | |
int[] sorteados = new int[60]; | |
int round = 1; | |
while (round <= sorteados.length) { | |
System.out.println("\nRound " + round); | |
int numeroSorteado = sortearNumero(sorteados); | |
sorteados[round - 1] = numeroSorteado; | |
System.out.println("Número sorteado: " + numeroSorteado); | |
System.out.println("Números já sorteados: " + Arrays.toString(sorteados)); | |
for (int i = 0; i < cartelas.length; i++) { | |
marcarNumero(cartelas[i], numeroSorteado); | |
imprimirCartela(jogadores[i], cartelas[i]); | |
if (isBingo(cartelas[i])) { | |
System.out.println("BINGO! " + jogadores[i] + " venceu!"); | |
return; | |
} | |
} | |
// Exibir ranking dos top 3 | |
exibirRankingTop3(jogadores, cartelas); | |
System.out.print("Pressione Enter para continuar ou X para abortar: "); | |
String input = scanner.nextLine().trim(); | |
if (input.equalsIgnoreCase("X")) { | |
exibirResultadoFinal(jogadores, cartelas); | |
break; | |
} | |
round++; | |
} | |
} | |
private static void sortearNumerosManual(String[] jogadores, String[][] cartelas) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Escolha os numeros do sorteio manual, separando os números por vírgula: "); | |
String[] numeros = scanner.nextLine().split(","); | |
for (int i = 0; i < cartelas.length; i++) { | |
for (String numero : numeros) { | |
marcarNumero(cartelas[i], Integer.parseInt(numero)); | |
} | |
imprimirCartela(jogadores[i], cartelas[i]); | |
if (isBingo(cartelas[i])) { | |
System.out.println("BINGO! " + jogadores[i] + " venceu!"); | |
return; | |
} | |
} | |
} | |
private static int sortearNumero(int[] sorteados) { | |
Random random = new Random(); | |
int numeroSorteado; | |
do { | |
numeroSorteado = random.nextInt(60) + 1; | |
} while (numeroJaSorteado(sorteados, numeroSorteado)); | |
return numeroSorteado; | |
} | |
private static boolean numeroJaSorteado(int[] sorteados, int numeroSorteado) { | |
for (int sorteado : sorteados) { | |
if (sorteado == numeroSorteado) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private static void marcarNumero(String[] cartela, int numeroSorteado) { | |
for (int i = 0; i < cartela.length; i++) { | |
if (cartela[i] != "X" && Integer.parseInt(cartela[i]) == numeroSorteado) { | |
cartela[i] = "X"; // Marcando o número como acertado | |
break; | |
} | |
} | |
} | |
private static void imprimirCartela(String jogador, String[] cartela) { | |
System.out.println("Cartela de " + jogador + ": " + Arrays.toString(cartela)); | |
} | |
private static void exibirRankingTop3(String[] jogadores, String[][] cartelas) { | |
int[] scores = new int[jogadores.length]; | |
// Calculate scores | |
for (int i = 0; i < cartelas.length; i++) { | |
scores[i] = (int) Arrays.stream(cartelas[i]).filter(num -> num.equals("X")).count(); | |
} | |
// Create array of indices | |
Integer[] indices = IntStream.range(0, jogadores.length).boxed().toArray(Integer[]::new); | |
// Sort indices based on scores | |
Arrays.sort(indices, (i1, i2) -> Integer.compare(scores[i2], scores[i1])); | |
// Print top 3 players | |
for (int i = 0; i < 3 && i < jogadores.length; i++) { | |
System.out.println((i + 1) + ". " + jogadores[indices[i]] + " - Score: " + scores[indices[i]]); | |
} | |
} | |
private static boolean isBingo(String[] cartela) { | |
return Arrays.stream(cartela).allMatch(num -> num.equals("X")); | |
} | |
private static void exibirResultadoFinal(String[] jogadores, String[][] cartelas) { | |
System.out.println("\nFim do Jogo!"); | |
// Calculate scores | |
int[] scores = new int[jogadores.length]; | |
for (int i = 0; i < cartelas.length; i++) { | |
scores[i] = (int) Arrays.stream(cartelas[i]).filter(num -> num.equals("X")).count(); | |
} | |
// Create array of indices | |
Integer[] indices = IntStream.range(0, jogadores.length).boxed().toArray(Integer[]::new); | |
// Sort indices based on scores | |
Arrays.sort(indices, (i1, i2) -> Integer.compare(scores[i2], scores[i1])); | |
// Display final ranking | |
System.out.println("Final Ranking:"); | |
for (int i = 0; i < jogadores.length; i++) { | |
System.out.println((i + 1) + ". " + jogadores[indices[i]] + " - Score: " + scores[indices[i]]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment