From d8afc9f5c2aa97a96ca326c0178a7fc9577afaaf Mon Sep 17 00:00:00 2001 From: Przemyslaw Date: Mon, 1 Apr 2024 09:02:22 +0200 Subject: Init --- src/Actions.java | 128 +++++++ src/ConfigFile.java | 96 ++++++ src/KopiecKreta.java | 922 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/MyResource.java | 132 ++++++++ src/StartScreen.java | 30 ++ src/wavResource.java | 71 ++++ 6 files changed, 1379 insertions(+) create mode 100644 src/Actions.java create mode 100644 src/ConfigFile.java create mode 100644 src/KopiecKreta.java create mode 100644 src/MyResource.java create mode 100644 src/StartScreen.java create mode 100644 src/wavResource.java (limited to 'src') diff --git a/src/Actions.java b/src/Actions.java new file mode 100644 index 0000000..57540ab --- /dev/null +++ b/src/Actions.java @@ -0,0 +1,128 @@ + +package src; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +class Actions implements KeyListener { + String cheat=""; + char pressKey =' '; + + public void Actions(){} + @Override + public void keyTyped(KeyEvent e) { + } + + @Override + public void keyReleased(KeyEvent event) { + } + + @Override + public void keyPressed(KeyEvent event) { + + if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('a')) { + isThere('a'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('w')) { + isThere('w'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('e')) { + isThere('e'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('r')) { + isThere('r'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('t')) { + isThere('t'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('y')) { + isThere('y'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('u')) { + isThere('u'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('i')) { + isThere('i'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('o')) { + isThere('o'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('p')) { + isThere('p'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('s')) { + isThere('s'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('d')) { + isThere('d'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('f')) { + isThere('f'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('g')) { + isThere('g'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('h')) { + isThere('h'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('j')) { + isThere('j'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('k')) { + isThere('k'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('l')) { + isThere('l'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('z')) { + isThere('z'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('c')) { + isThere('c'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('b')) { + isThere('b'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('n')) { + isThere('n'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('m')) { + isThere('m'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('1')) { + hungryCheat('1'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('2')) { + hungryCheat('2'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('3')) { + hungryCheat('3'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('4')) { + hungryCheat('4'); + } + else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar(' ')) { + resetCheat(); + } + + } + public char getKey () { + return pressKey; + } + public String getCheat(){ + return cheat; + } + + private void isThere(char c){ + // Nie wiem czemu od razu nie przekazalem duzej litery... + pressKey=Character.toUpperCase(c); + resetCheat(); + } + private void hungryCheat(char c){ + String s =Character.toString(c); + cheat += s; + } + public void resetCheat(){ + cheat=""; + } +} diff --git a/src/ConfigFile.java b/src/ConfigFile.java new file mode 100644 index 0000000..b3c380d --- /dev/null +++ b/src/ConfigFile.java @@ -0,0 +1,96 @@ + +package src; + +/** + * + * @author przem + */ + +import java.io.FileWriter; +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; // Import this class to handle errors +import java.util.Scanner; +import java.util.*; + +public class ConfigFile { + public String userDir="", userHome="", osName="", osVersion="", jvmVersion=""; + String name = "/kopieckreta.txt"; + public ConfigFile() throws IOException{ + for (Map.Entry e : System.getProperties().entrySet()) { + //System.out.println(String.format("%s = %s", e.getKey(), e.getValue())); + if (e.getKey().equals("user.dir")) { + userDir=String.format("%s", e.getValue()); + } + if (e.getKey().equals("user.home")) { + userHome=String.format("%s", e.getValue()); + + } + if (e.getKey().equals("os.name")) { + osName=String.format("%s", e.getValue()); + + } + if (e.getKey().equals("os.version")) { + osVersion=String.format("%s", e.getValue()); + + } + if (e.getKey().equals("java.vm.version")) { + jvmVersion=String.format("%s", e.getValue()); + + } + } + + + File file=new File (userHome+name); + if (file.createNewFile()){ + System.out.println("Plik zostal utworzony"); + try { + FileWriter myWriter = new FileWriter(userHome+name); + myWriter.write("1"); + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } + else { + System.out.println("Plik istnieje"); + } + } + public void save(boolean b){ + String s=""; + try { + FileWriter myWriter = new FileWriter(userHome+name); + if (b) s="1"; + else s="0"; + myWriter.write(s); + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } + public boolean load() { + String data = ""; + boolean r =false; + try { + File myObj = new File(userHome+name); + Scanner myReader = new Scanner(myObj); + while (myReader.hasNextLine()) { + data = myReader.nextLine(); + System.out.println(data); + } + myReader.close(); + } catch (FileNotFoundException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + + if (data.equals("1")) r=true; + + return r; + } + +} diff --git a/src/KopiecKreta.java b/src/KopiecKreta.java new file mode 100644 index 0000000..45c2b7d --- /dev/null +++ b/src/KopiecKreta.java @@ -0,0 +1,922 @@ + +/* + Kopiec Kreta - gra w odgadywanie slow + ver. 0.4.0-beta + autor: Przemyslaw R. Pietraszczyk + data: 2020-09-30 + licencja: GPL v3 + + slownik pochodzi z: sjp.pl + +*/ + +package src; + +import java.awt.*; // Toolkit +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JButton; +//import javax.swing.JComponent; +import javax.swing.JMenuBar; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.Box; +import javax.swing.JTextArea; +import javax.swing.JDialog; +import javax.swing.JTextField; +import javax.swing.JCheckBox; +import javax.swing.BorderFactory; +import javax.swing.WindowConstants; +import java.awt.event.*; +import java.awt.event.ActionListener; +//import java.awt.event.KeyListener; +import java.awt.event.ActionEvent; +import java.awt.event.WindowEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.ItemEvent; +//import java.awt.event.KeyEvent; +import java.util.*; //Timer +//import java.awt.Robot; +import java.awt.Toolkit; + +import java.beans.*; //property change stuff +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.InputStreamReader; +import java.io.IOException; + +public class KopiecKreta extends JTextArea implements ActionListener { + + static JMenuBar menuBar; + static JMenu menu, info, menu2; + static JMenuItem menuItem, menuExit, menuInfo, menuPref; + static JFrame frame; + static boolean go=false; + static Timer timer = new Timer(); + static String content; + final static int NRINGS=11; + final static int NCHAR=23; //32; + static MyResource res = new MyResource(); + static wavResource wav; + static Ring []truering = new Ring[NRINGS]; + static Ring []falsering = new Ring[NRINGS]; + static String [] mound = new String[20]; // tekst do wyswietlenia przez paint + static JTextArea textArea; + static JPanel startScreen; + static int activeRing; // indeks ringu na ktorym jest prowadzona aktualnie gra + static KopiecKreta kk; + static Actions actions; + final static String CONSTCH="1234"; + final static Character[] charsInStock= {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'y', 'z'}; + static ArrayList cheatSheet; + static int pointsForTheRing; + static int pointsFOrTheMund; + static int totalScore, level; + static boolean cheatActivate=false; + static SoundDialog sounddialog; + //static ResetDialog resetdialog; + //static GetNameDialog getNameDialog; + //Boolean runSoundDialog=Boolean.valueOf(false); + static ConfigFile configfile; + //static char pressKey=' '; + + public KopiecKreta() { + //GridBagLayout gridbag = (GridBagLayout)getLayout(); + //GridBagConstraints c = new GridBagConstraints(); + + menuBar = new JMenuBar(); + menu = new JMenu("Plik"); + menuBar.add(menu); + menu2 = new JMenu("Opcje"); + menuBar.add(menu2); + // przenosi INFO na prawa strone + menuBar.add(Box.createHorizontalGlue()); + info = new JMenu("Info"); + // kierunek wyswietlania menu + info.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); + menuBar.add(info); + menuItem = new JMenuItem("Nowa Gra", null); + menuItem.setActionCommand("MenuItem1"); + menuItem.addActionListener(this); + menuItem.setToolTipText("Rozpoczyna nowa gre."); + menu.add(menuItem); + //menuItem = new JMenuItem("Sala slawy", null); + //menuItem.setActionCommand("MenuItem11"); + //menuItem.addActionListener(this); + //menuItem.setToolTipText("Wyswietla najlepsze wyniki"); + //menu.add(menuItem); + menu.addSeparator(); + menuExit = new JMenuItem("Zakoncz", null); + menuExit.setActionCommand("MenuItem2"); + menuExit.addActionListener(this); + menuExit.setToolTipText("Wyjscie z programu"); + menu.add(menuExit); + menuPref = new JMenuItem("Preferencje dzwieku",null); + menuPref.setActionCommand("MenuItem4"); + menuPref.addActionListener(this); + menuPref.setToolTipText("Preferencje dotyczace dzwieku"); + menu2.add(menuPref); + /* + menuPref = new JMenuItem("Resetowanie",null); + menuPref.setActionCommand("MenuItem5"); + menuPref.addActionListener(this); + menuPref.setToolTipText("Resetowanie najlepszych wynikow"); + menu2.add(menuPref); + */ + menuInfo = new JMenuItem("Pomoc", null); + menuInfo.setActionCommand("MenuItem33"); + menuInfo.addActionListener(this); + menuInfo.setToolTipText("Samouczek"); + info.add(menuInfo); + menuInfo = new JMenuItem("O Programie", null); + menuInfo.setActionCommand("MenuItem3"); + menuInfo.addActionListener(this); + menuInfo.setToolTipText("Informacje o programie"); + info.add(menuInfo); + + //addKeyListener(this); + //actions = new Actions(); + //addKeyListener(actions); + //setFocusable(true); + + + } + + + public void actionPerformed(final ActionEvent e) { + + // Nowa gra + if(e.getActionCommand().equals("MenuItem1")) { + //getNameDialog.initUI(configfile, frame, getNameDialog.runGetNameDialog); + //getNameDialog.runGetNameDialog=true; + //if (getNameDialog.go){ + + activeRing=10; + totalScore=0; + pointsForTheRing=0; + pointsFOrTheMund=0; + level=1; + //frame.remove(startScreen); + //frame.add(textArea); + startScreen.setVisible(false); + textArea.setVisible(true); + create_secret_mound(); + create_real_mound(); // generuje prawdziwy kopiec + cheatSheet = new ArrayList(); + setCharCheatSheet (); + go=true; + setDisplayMound(); + // } + } + // Zakonczenie + else if(e.getActionCommand().equals("MenuItem2")) { + System.exit(0); + } + // O Programie + else if(e.getActionCommand().equals("MenuItem3")) { + String s1=" Port mojej gry pierwotnie napisanej dla Androida 2.3\n\n", + s2=" Program napisany na podstawie fragmentow kodow,\n", + s3=" wyszukanych w wyszukiwarce Google,\n", + s4=" plus wlasna inwencja\n\n", + s5=" Autor zarzeka sie ze nigdy nie posiadal, ani nie posiada\n", + s6=" zadnej ksiazki na temat Javy.\n", + s7="Oraz nie przeczytal ani jednej ksiazki na temat tego jezyka!\n\n", + s8=" Przemyslaw R. Pietraszczyk - jesien 2020\n", + s9=" Licencja: GPL v3\n\n", + s10=" Stworzono w oparciu o OpenJDK 11 oraz ANT\n\n", + s11=" o/s: "+configfile.osName+" ver. "+configfile.osVersion+"\n", + s12=" java ver: "+configfile.jvmVersion; + + JOptionPane.showMessageDialog(frame, + s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11+s12, + "Kopiec Kreta 0.230924-0", + JOptionPane.PLAIN_MESSAGE); + + } + else if(e.getActionCommand().equals("MenuItem33")) { + String s1="Celem gry jest odgadniecie wszyskich zakrytych pol.\n", + s2="Rozgrywke rozpoczyna sie od dolnego pierscienia.\n", + s3="Po odgadnieciu wszyskich liter w danym pierscieniu,\n", + s4="rozgrywka przenosi sie na kolejny, wyzszy ring.\n", + s5="Po odgadnieciu wszystkich liter, gracz pytany jest\n", + s6="o chec gry na opcjonalnym nastepnym poziomie.\n\n", + s7="UWAGA ! Caps Lock - bez znaczenia !\n", + s8="\n"; + JOptionPane.showMessageDialog(frame, + s1+s2+s3+s4+s5+s6+s7+s8, + "Pomoc", + JOptionPane.PLAIN_MESSAGE); + } + else if(e.getActionCommand().equals("MenuItem4")) { + sounddialog.initUI(configfile, frame, sounddialog.runSoundDialog); + sounddialog.runSoundDialog=true; + } + /* + else if(e.getActionCommand().equals("MenuItem5")) { + resetdialog.initUI(configfile); + } + */ + // sala slawy + /* + else if(e.getActionCommand().equals("MenuItem11")) { + + } + */ + //System.out.println("ACTION PERFORMED " + e); + } + + private static void createAndShowGUI() { + + frame=new JFrame("Kopiec Kreta"); + //Robot robot; + // menuBar z konstruktora + final int frameWidth = 250; + final int frameHeight = 460; + final int GNOMEPANEL=80; // nieco nizej niz panel + final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + //frame.setBounds((int) screenSize.getWidth() - frameWidth, + // (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight); + frame.setBounds((int) screenSize.getWidth() - frameWidth, + GNOMEPANEL, frameWidth, frameHeight); + frame.setResizable(false); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + sounddialog = new SoundDialog(); + //resetdialog = new ResetDialog(); + //getNameDialog = new GetNameDialog(); + textArea = new KopiecKreta(); + startScreen = new StartScreen(); + //textArea.setFont(new Font("Serif", Font.ITALIC, 18)); + textArea.setFont(new Font("Serif", Font.BOLD, 18)); + textArea.setLineWrap(true); //nowe + textArea.setWrapStyleWord(true); // nowe + textArea.setEditable(false); + textArea.setBounds(5,5,240,430); + frame.add(textArea); + textArea.setVisible(false); + frame.add(menuBar); + frame.add(startScreen); + startScreen.setFont(new Font("Serif", Font.BOLD, 18)); + startScreen.setBounds(5,5,240,430); + + frame.setJMenuBar(menuBar); + frame.setLayout(null); + frame.setSize(250,500); // 400 x 400 + frame.setVisible(true); + actions = new Actions(); + frame.addKeyListener(actions); + frame.setFocusable(true); + textArea.setForeground(Color.BLACK); + textArea.setBackground(Color.GREEN); + // zapobiega utracie KeyEvent po wyjsciu z SoundDialog i ResetDialog + frame.addFocusListener(new FocusListener(){ + public void focusGained(FocusEvent e){ + //System.out.println("Focus GAINED:"+e); + } + public void focusLost(FocusEvent e){ + //System.out.println("Focus LOST:"+e); + + // FIX FOR GNOME/XWIN FOCUS BUG + e.getComponent().requestFocus(); + } + }); + try { + configfile = new ConfigFile(); + }catch (IOException e){} + configfile.load(); + + /* + try { + // obiekt robot posiada metode do pobierania koloru pixela + robot = new Robot(); + robot.mouseMove(screenSize.width-100, 200); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + }catch (AWTException e) {} + frame.setAlwaysOnTop(true); + */ + + timer.schedule(new KeyTicks(), 0, 1000); // lepiej 1000 + + } + // dodaje punkty za pierscien i kazuje pozostale litery sciagi + static public void addScores(){ + int n; + pointsForTheRing=0; + if (!cheatActivate){ + for (int i=0;i< cheatSheet.size();i++){ + pointsForTheRing++; + } + pointsFOrTheMund+=pointsForTheRing; + } + //System.out.println("Naliczylem "+pointsForTheRing+" punktow za ring"); + } + // zapelnia tablice ze znakami do podpowiedzi + static private void setCharCheatSheet (){ + + for (int i=0; i indexExist = new ArrayList(); + int returnIndex; + + + pressKey=actions.getKey(); + + if (go) { + wav.stop(); + indexExist=checkInstance(pressKey); + for (int exist: indexExist){ + falsering[activeRing].setChar(pressKey,exist); + wav.PlayFlag=true; + if (lastKey != pressKey) { + if (sounddialog.soundAccept()){ + //System.out.println("Dzwiek akceptuje"); + wav.play("/smooth-ok.wav"); + } + else System.out.println("Dzwiek NIE akceptuje"); + } + } + if (lastKey != pressKey && indexExist.isEmpty() && !firstRun) { + //if (lastKey != pressKey && indexExist.isEmpty()) { + + wav.PlayFlag=true; + if (sounddialog.soundAccept()){ + //System.out.println("Dzwiek akceptuje"); + wav.play("/bass-bad.wav"); + } + else System.out.println("Dzwiek NIE akceptuje"); + } + lastKey=pressKey; + + returnIndex = searchCharChaetSheet(pressKey); + if (returnIndex != -1){ + refreshCheatSheet(returnIndex); + } + setDisplayMound(); + pressKey=' '; + indexExist.clear(); + + if (actions.getCheat().equals(CONSTCH)){ + //pointsForTheRing=0; + actions.resetCheat(); + cheatActivate=true; + unhideWords(); + } + if (checkFullRing()) { + totalScore+=pointsForTheRing; + setCharCheatSheet (); + setDisplayMound(); + + } + + firstRun=false; + } + } + static private boolean checkFullRing() { + if (truering[activeRing].getString().equals(falsering[activeRing].getString())){ + //System.out.println("Czy zaliczony pierscien ? activeRing:"+activeRing); + if (activeRing >1){ + addScores(); + cheatActivate=false; + // nowa sciaga + cheatSheet = new ArrayList(); + activeRing--; + return true; + } + else { + go=false; + + setDisplayMound(); + if(activeRing==1){ + + String s1="Ukonczyles poziom "+level+" !\n", + s2="Na tym poziomie uzyskales "+pointsFOrTheMund+" punktow.\n", + s3="Poki co ugrales "+totalScore+" punktow !\n\n", + s4="Kontynujesz gre ?"; + + // moze boolean ? + int dialogButton = JOptionPane.YES_NO_OPTION; + int answer = JOptionPane.showConfirmDialog (frame, s1+s2+s3+s4,"Komunikat", dialogButton); + + if(answer == JOptionPane.YES_OPTION) { + activeRing=10; + go=true; + level++; + create_secret_mound(); + create_real_mound(); // generuje prawdziwy kopiec + setDisplayMound(); + pointsFOrTheMund=0; + } + if(answer == JOptionPane.NO_OPTION) { + System.exit(0); + + } + } + } + } + return false; + } + // sprawdza czy rzadana litera znajduje sie w pierscieniu + static private ArrayList checkInstance(char key){ + ArrayList indexExist = new ArrayList(); + + for (int i=0; i directory3 = new ArrayList(); + static ArrayList directory4 = new ArrayList(); + static ArrayList directory5 = new ArrayList(); + static ArrayList directory6 = new ArrayList(); + static ArrayList directory7 = new ArrayList(); + static ArrayList directory8 = new ArrayList(); + static ArrayList directory9 = new ArrayList(); + static ArrayList directory10 = new ArrayList(); + static ArrayList directory11 = new ArrayList(); + static ArrayList directory12 = new ArrayList(); + static ArrayList directory13 = new ArrayList(); + static ArrayList directory14 = new ArrayList(); + //static int mem=0; + + public void read() throws Exception{ + String list; + + try { + InputStream is = this.getClass().getResourceAsStream(absName); + BufferedReader br=new BufferedReader(new InputStreamReader(is, "utf-8")); + for (int i=0; i