Vaja 04 - Besedle
This commit is contained in:
3 files changed
+13978
No files matched your search
+1074
File diff suppressed because it is too large.
Load diff
+12653
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,251 @@
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Vaja04Besedle {
|
||||
|
||||
// Konstante barv
|
||||
static final int BELA = 0;
|
||||
static final int CRNA = 1;
|
||||
static final int RUMENA = 2;
|
||||
static final int ZELENA = 3;
|
||||
|
||||
// ANSI ukazi (za barvni izpis)
|
||||
static final String ANSI_RESET = "\u001B[0m";
|
||||
static final String ANSI_GREEN_BG = "\u001b[42m";
|
||||
static final String ANSI_YELLOW_BG = "\u001b[43m";
|
||||
static final String ANSI_WHITE_BG = "\u001b[47;1m";
|
||||
static final String ANSI_BLACK_BG = "\u001b[40m";
|
||||
static final String ANSI_WHITE = "\u001b[37m";
|
||||
static final String ANSI_BLACK = "\u001b[30m";
|
||||
|
||||
static final String abeceda = "ABCČDEFGHIJKLMNOPRSŠTUVZŽ"; // Veljavne črke
|
||||
static final int MAX_POSKUSOV = 6; // Število poskusov
|
||||
|
||||
static String[] seznamBesed; // Seznam vseh možnih besed
|
||||
static String iskanaBeseda; // Iskana beseda trenutne igre
|
||||
static int[] barveAbecede; // Barve črk pri izpisu abecede
|
||||
static String[] slovar;
|
||||
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
|
||||
// Izpiše znak v izbrani barvi
|
||||
static void izpisiZBarvo(char znak, int barva) {
|
||||
String slog;
|
||||
if (barva == ZELENA) {
|
||||
slog = ANSI_BLACK + ANSI_GREEN_BG;
|
||||
} else if (barva == RUMENA) {
|
||||
slog = ANSI_BLACK + ANSI_YELLOW_BG;
|
||||
} else if (barva == BELA) {
|
||||
slog = ANSI_BLACK + ANSI_WHITE_BG;
|
||||
} else {
|
||||
slog = ANSI_WHITE + ANSI_BLACK_BG;
|
||||
}
|
||||
System.out.print(slog + " " + znak + " " + ANSI_RESET);
|
||||
}
|
||||
|
||||
// Prebere seznam besed iz datoteke
|
||||
static void preberiBesede(String datoteka) throws FileNotFoundException {
|
||||
File file = new File(datoteka);
|
||||
Scanner scanner = new Scanner(file);
|
||||
|
||||
int n = Integer.parseInt(scanner.nextLine());
|
||||
seznamBesed = new String[n];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
seznamBesed[i] = scanner.nextLine().toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
static void preberiSlovar(String datoteka) throws FileNotFoundException {
|
||||
File file = new File(datoteka);
|
||||
Scanner scanner = new Scanner(file);
|
||||
|
||||
int n = Integer.parseInt(scanner.nextLine());
|
||||
slovar = new String[n];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
slovar[i] = scanner.nextLine().toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
// Pripravi vse za novo igro
|
||||
static void novaIgra() {
|
||||
Random rnd = new Random();
|
||||
// iskanaBeseda = seznamBesed[rnd.nextInt(seznamBesed.length)];
|
||||
iskanaBeseda = "DREVO";
|
||||
|
||||
barveAbecede = new int[abeceda.length()];
|
||||
Arrays.fill(barveAbecede, BELA);
|
||||
}
|
||||
|
||||
// Izpiše abecedo
|
||||
static void izpisiAbecedo() {
|
||||
for (int i = 0; i < abeceda.length(); i++) {
|
||||
izpisiZBarvo(abeceda.charAt(i), barveAbecede[i]);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
static boolean vSlovarju(String beseda) {
|
||||
for (int i = 0; i < slovar.length; i++) {
|
||||
if (slovar[i].equals(beseda)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ali je beseda veljavna?
|
||||
static boolean veljavnaBeseda(String beseda) {
|
||||
if (beseda.length() != 5) {
|
||||
System.out.println("Nepravilna dolžina besede!");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < beseda.length(); i++) {
|
||||
if (!veljavna(beseda.charAt(i))) {
|
||||
System.out.println("V besedi so neveljavni znaki!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!vSlovarju(beseda)) {
|
||||
System.out.println("Besede ni v slovarju!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static boolean veljavna(char c) {
|
||||
for (int i = 0; i < abeceda.length(); i++) {
|
||||
if (c == abeceda.charAt(i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int abecedaIndex(char c) {
|
||||
for (int i = 0; i < abeceda.length(); i++) {
|
||||
if (c == abeceda.charAt(i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Določi barve črk v ugibani besedi
|
||||
static int[] pobarvajBesedo(String ugibanaBeseda) {
|
||||
int[] barve = new int[5];
|
||||
for (int i = 0; i < 5; i++) {
|
||||
char c = ugibanaBeseda.charAt(i);
|
||||
if (iskanaBeseda.charAt(i) == c) {
|
||||
barve[i] = ZELENA;
|
||||
barveAbecede[abecedaIndex(c)] = ZELENA;
|
||||
} else if (iskanaBeseda.contains(Character.toString(c))) {
|
||||
barve[i] = RUMENA;
|
||||
barveAbecede[abecedaIndex(c)] = RUMENA;
|
||||
}
|
||||
}
|
||||
|
||||
return barve;
|
||||
}
|
||||
|
||||
static int[] pravilnoPobarvajBesedo(String ugibanaBeseda) {
|
||||
int[] barve = new int[5];
|
||||
int[] steviloCrk = new int[abeceda.length()];
|
||||
Arrays.fill(steviloCrk, 0);
|
||||
|
||||
for (int i = 0; i < iskanaBeseda.length(); i++) {
|
||||
steviloCrk[abecedaIndex(iskanaBeseda.charAt(i))]++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
char c = ugibanaBeseda.charAt(i);
|
||||
if (iskanaBeseda.charAt(i) == c) {
|
||||
barve[i] = ZELENA;
|
||||
barveAbecede[abecedaIndex(c)] = ZELENA;
|
||||
steviloCrk[abecedaIndex(c)]--;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
char c = ugibanaBeseda.charAt(i);
|
||||
if (steviloCrk[abecedaIndex(c)] <= 0) {
|
||||
continue;
|
||||
}
|
||||
if (iskanaBeseda.contains(Character.toString(c))) {
|
||||
barve[i] = RUMENA;
|
||||
barveAbecede[abecedaIndex(c)] = RUMENA;
|
||||
steviloCrk[abecedaIndex(c)]--;
|
||||
}
|
||||
}
|
||||
|
||||
return barve;
|
||||
}
|
||||
|
||||
// Izpiši besedo
|
||||
static void izpisiBesedo(String ugibanaBeseda, int[] barve) {
|
||||
for (int i = 0; i < ugibanaBeseda.length(); i++) {
|
||||
izpisiZBarvo(ugibanaBeseda.charAt(i), barve[i]);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
// Izvede eno igro
|
||||
static void igra() {
|
||||
novaIgra();
|
||||
|
||||
System.out.println(iskanaBeseda);
|
||||
|
||||
int poskus = 1;
|
||||
boolean uganil = false;
|
||||
while (poskus <= MAX_POSKUSOV) {
|
||||
izpisiAbecedo();
|
||||
System.out.printf("Poskus %d/%d: ", poskus, MAX_POSKUSOV);
|
||||
String ugibanaBeseda = sc.nextLine().toUpperCase();
|
||||
|
||||
// Preveri veljavnost
|
||||
if (!veljavnaBeseda(ugibanaBeseda))
|
||||
continue;
|
||||
|
||||
// Pobarvaj crke v besedi (namigi)
|
||||
// int[] barve = pobarvajBesedo(ugibanaBeseda);
|
||||
int[] barve = pravilnoPobarvajBesedo(ugibanaBeseda);
|
||||
|
||||
// Izpiši pobarvano besedo
|
||||
izpisiBesedo(ugibanaBeseda, barve);
|
||||
|
||||
if (ugibanaBeseda.equals(iskanaBeseda)) {
|
||||
uganil = true;
|
||||
break;
|
||||
}
|
||||
poskus++;
|
||||
}
|
||||
|
||||
if (uganil) {
|
||||
System.out.printf("Bravo! Besedo si uganil/a v %d poskusih.\n", poskus);
|
||||
} else {
|
||||
System.out.printf("Žal ti ni uspelo. Pravilna beseda: %s\n", iskanaBeseda);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
preberiBesede("besede.txt");
|
||||
preberiSlovar("slovar.txt");
|
||||
|
||||
while (true) {
|
||||
igra();
|
||||
System.out.print("Nova igra? (d/n): ");
|
||||
String odg = sc.nextLine();
|
||||
if (odg.toLowerCase().charAt(0) != 'd') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user