Compare commits
No commits in common. "main" and "lucas" have entirely different histories.
|
@ -1,7 +1,8 @@
|
||||||
<component name="libraryTable">
|
<component name="libraryTable">
|
||||||
<library name="jlayer-1.0.3">
|
<library name="jlayer-1.0.3">
|
||||||
<CLASSES>
|
<CLASSES>
|
||||||
<root url="jar://$PROJECT_DIR$/libs/jlayer-1.0.3.jar!/" />
|
<root url="jar://$USER_HOME$/Downloads/jlayer-master/jlayer-master/target/jlayer-1.0.3.jar!/" />
|
||||||
|
<root url="jar://$USER_HOME$/Downloads/jlayer-1.0.3.jar!/" />
|
||||||
</CLASSES>
|
</CLASSES>
|
||||||
<JAVADOC />
|
<JAVADOC />
|
||||||
<SOURCES />
|
<SOURCES />
|
||||||
|
|
|
@ -1,124 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="Palette2">
|
|
||||||
<group name="Swing">
|
|
||||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="Button" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="RadioButton" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="CheckBox" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="Label" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
|
||||||
<preferred-size width="150" height="-1" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
|
||||||
<preferred-size width="150" height="-1" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
|
||||||
<preferred-size width="150" height="-1" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
|
||||||
<preferred-size width="200" height="200" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
|
||||||
<preferred-size width="200" height="200" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
|
||||||
<preferred-size width="-1" height="20" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
|
||||||
</item>
|
|
||||||
</group>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
Before Width: | Height: | Size: 2.2 MiB After Width: | Height: | Size: 2.2 MiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 959 B After Width: | Height: | Size: 959 B |
|
@ -0,0 +1,134 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="AutoImportSettings">
|
||||||
|
<option name="autoReloadType" value="SELECTIVE" />
|
||||||
|
</component>
|
||||||
|
<component name="ChangeListManager">
|
||||||
|
<list default="true" id="91d861bd-39f2-4f7d-9002-31d86c6d3b79" name="Changes" comment="" />
|
||||||
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
|
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||||
|
</component>
|
||||||
|
<component name="FileTemplateManagerImpl">
|
||||||
|
<option name="RECENT_TEMPLATES">
|
||||||
|
<list>
|
||||||
|
<option value="Record" />
|
||||||
|
<option value="Enum" />
|
||||||
|
<option value="Class" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="Git.Settings">
|
||||||
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectColorInfo">{
|
||||||
|
"customColor": "",
|
||||||
|
"associatedIndex": -1
|
||||||
|
}</component>
|
||||||
|
<component name="ProjectId" id="2o43faNdzvB3omBPjZ0MFw1dSOh" />
|
||||||
|
<component name="ProjectViewState">
|
||||||
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
|
<option name="showLibraryContents" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
|
"keyToString": {
|
||||||
|
"Downloaded.Files.Path.Enabled": "false",
|
||||||
|
"Repository.Attach.Annotations": "false",
|
||||||
|
"Repository.Attach.JavaDocs": "false",
|
||||||
|
"Repository.Attach.Sources": "false",
|
||||||
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
|
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
|
||||||
|
"git-widget-placeholder": "ole",
|
||||||
|
"last_opened_file_path": "C:/Users/OleWachtel/Desktop/Uni/WeSe2425/programmieren-projekt",
|
||||||
|
"project.structure.last.edited": "Libraries",
|
||||||
|
"project.structure.proportion": "0.0",
|
||||||
|
"project.structure.side.proportion": "0.2",
|
||||||
|
"settings.editor.selected.configurable": "preferences.lookFeel"
|
||||||
|
},
|
||||||
|
"keyToStringList": {
|
||||||
|
"GitStage.ChangesTree.GroupingKeys": [
|
||||||
|
"directory",
|
||||||
|
"module",
|
||||||
|
"repository"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}]]></component>
|
||||||
|
<component name="RunManager">
|
||||||
|
<configuration name="HalloSchiffeVersenken" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
||||||
|
<option name="MAIN_CLASS_NAME" value="HalloSchiffeVersenken" />
|
||||||
|
<module name="SchiffeVersenken" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
<recent_temporary>
|
||||||
|
<list>
|
||||||
|
<item itemvalue="Application.HalloSchiffeVersenken" />
|
||||||
|
</list>
|
||||||
|
</recent_temporary>
|
||||||
|
</component>
|
||||||
|
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||||
|
<component name="TaskManager">
|
||||||
|
<task active="true" id="Default" summary="Default task">
|
||||||
|
<changelist id="91d861bd-39f2-4f7d-9002-31d86c6d3b79" name="Changes" comment="" />
|
||||||
|
<created>1730115126753</created>
|
||||||
|
<option name="number" value="Default" />
|
||||||
|
<option name="presentableId" value="Default" />
|
||||||
|
<updated>1730115126753</updated>
|
||||||
|
</task>
|
||||||
|
<task id="LOCAL-00001" summary="add Java Project">
|
||||||
|
<option name="closed" value="true" />
|
||||||
|
<created>1730115284405</created>
|
||||||
|
<option name="number" value="00001" />
|
||||||
|
<option name="presentableId" value="LOCAL-00001" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1730115284405</updated>
|
||||||
|
</task>
|
||||||
|
<task id="LOCAL-00002" summary="add SoundHandler to Play sounds and Mute global">
|
||||||
|
<option name="closed" value="true" />
|
||||||
|
<created>1730129036486</created>
|
||||||
|
<option name="number" value="00002" />
|
||||||
|
<option name="presentableId" value="LOCAL-00002" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1730129036486</updated>
|
||||||
|
</task>
|
||||||
|
<task id="LOCAL-00003" summary="delete Sounds.java">
|
||||||
|
<option name="closed" value="true" />
|
||||||
|
<created>1730129210987</created>
|
||||||
|
<option name="number" value="00003" />
|
||||||
|
<option name="presentableId" value="LOCAL-00003" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1730129210987</updated>
|
||||||
|
</task>
|
||||||
|
<task id="LOCAL-00004" summary="add classes blank">
|
||||||
|
<option name="closed" value="true" />
|
||||||
|
<created>1730201990808</created>
|
||||||
|
<option name="number" value="00004" />
|
||||||
|
<option name="presentableId" value="LOCAL-00004" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1730201990808</updated>
|
||||||
|
</task>
|
||||||
|
<option name="localTasksCounter" value="5" />
|
||||||
|
<servers />
|
||||||
|
</component>
|
||||||
|
<component name="Vcs.Log.Tabs.Properties">
|
||||||
|
<option name="TAB_STATES">
|
||||||
|
<map>
|
||||||
|
<entry key="MAIN">
|
||||||
|
<value>
|
||||||
|
<State />
|
||||||
|
</value>
|
||||||
|
</entry>
|
||||||
|
</map>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="VcsManagerConfiguration">
|
||||||
|
<MESSAGE value="add Java Project" />
|
||||||
|
<MESSAGE value="add SoundHandler to Play sounds and Mute global" />
|
||||||
|
<MESSAGE value="delete Sounds.java" />
|
||||||
|
<MESSAGE value="add classes blank" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value="add classes blank" />
|
||||||
|
</component>
|
||||||
|
</project>
|
BIN
Sound/hit.mp3
BIN
Sound/plop.mp3
BIN
Sound/win.mp3
Before Width: | Height: | Size: 668 B |
Before Width: | Height: | Size: 396 B |
Before Width: | Height: | Size: 854 B |
Before Width: | Height: | Size: 875 B |
Before Width: | Height: | Size: 889 B |
Before Width: | Height: | Size: 84 B |
Before Width: | Height: | Size: 171 B |
Before Width: | Height: | Size: 829 B |
39
makefile
|
@ -1,39 +0,0 @@
|
||||||
MAIN_CLASS := HalloSchiffeVersenken
|
|
||||||
JAR_NAME := schiffeVersenken.jar
|
|
||||||
|
|
||||||
SRC_DIR := src
|
|
||||||
OUT_DIR := bin
|
|
||||||
LIB_DIR := libs
|
|
||||||
|
|
||||||
JC := javac
|
|
||||||
JCFLAGS := -d $(OUT_DIR)/ -cp "$(LIB_DIR)/*"
|
|
||||||
|
|
||||||
JR := java
|
|
||||||
|
|
||||||
SEPERATOR := :
|
|
||||||
ifeq ($(OS),Windows_NT)
|
|
||||||
SEPERATOR := ;
|
|
||||||
endif
|
|
||||||
|
|
||||||
.SUFFIXES: .java
|
|
||||||
.PHONY: classfiles clean
|
|
||||||
|
|
||||||
|
|
||||||
classfiles: $(SRC_DIR)/*
|
|
||||||
$(JC) $(JCFLAGS) $(SRC_DIR)/*
|
|
||||||
|
|
||||||
jar: classfiles
|
|
||||||
jar --create --file=$(OUT_DIR)/$(JAR_NAME) --main-class=$(MAIN_CLASS) -C $(OUT_DIR)/ .
|
|
||||||
|
|
||||||
test-jar: jar
|
|
||||||
# $(JR) -cp "$(OUT_DIR)/:$(LIB_DIR)/*" $(MAIN_CLASS)
|
|
||||||
$(JR) -jar $(OUT_DIR)/$(JAR_NAME)
|
|
||||||
|
|
||||||
docs:
|
|
||||||
javadoc -cp "$(LIB_DIR)/*" -d "docs" $(SRC_DIR)/*.java
|
|
||||||
|
|
||||||
test: classfiles
|
|
||||||
$(JR) -cp "$(OUT_DIR)$(SEPERATOR)$(LIB_DIR)/*" $(MAIN_CLASS)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm $(OUT_DIR)/*.class
|
|
|
@ -1,167 +0,0 @@
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Die Klasse AiPlayer ist die Basis für alle Ki Spieler und jede Spezifische Ki erweitert diese Klasse.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
* */
|
|
||||||
public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Liste aller erstellten ShootThreads
|
|
||||||
*/
|
|
||||||
List<Thread> shootThreads;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public AiPlayer() {
|
|
||||||
this.setName("AI Player");
|
|
||||||
this.shootThreads = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gibt einen zufälligen Punkt im Spielfeld zurück.
|
|
||||||
* @return Ein zufälliger Punkt
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public Point RandomPoint() {
|
|
||||||
Random random = new Random(); // Pseudo Random für zufallszahlen
|
|
||||||
int posx = random.nextInt(super.board.getSize()); // Generiert 0 - 13
|
|
||||||
int posy = random.nextInt(super.board.getSize()); //
|
|
||||||
return new Point(posx,posy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialisiert das Board.
|
|
||||||
* @param size größe des Boards
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void createBoard(int size) {
|
|
||||||
super.createBoard(size);
|
|
||||||
this.aiSetShips();
|
|
||||||
this.ready();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ki Methode zum zufälligen Setzten der Schiffe
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public void aiSetShips() {
|
|
||||||
for(int i = 0; i < super.board.getShips().size(); i++) { // Interiert durch alle Shiffe
|
|
||||||
//TODO: set horizontal
|
|
||||||
while(!super.board.getShips().get(i).setPosition(RandomPoint(), true, super.board.getShips(), super.board.getSize())) {}
|
|
||||||
} // Versucht das aktuelle Schiff zu setzten und wiederholt solange bis es funktioniert
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Ki Methode zum zufälligen Schießen auf das gegnerische Board.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public void aiShoot() {
|
|
||||||
if (!this.myTurn) return;
|
|
||||||
this.enemy.receiveShoot(RandomPoint());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Nachdem receiveShoot beim gegner den schuss verarbeitet hat,
|
|
||||||
* wird diese Methode mit der antwort aufgerufen.
|
|
||||||
* @param hitResponse the hitresponse
|
|
||||||
* @author Florian Alexy und Florian Hantzschel und Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
|
||||||
// Eltern-Klasse LocalPlayer updatet myTurn
|
|
||||||
super.receiveHit(hitResponse);
|
|
||||||
if (this.myTurn) {
|
|
||||||
// Neuer Schuss wird erstellt und gestartet.
|
|
||||||
Thread t = new Thread(this);
|
|
||||||
t.start();
|
|
||||||
this.shootThreads.add(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erhält einen schuss vom gegner und verarbeitet ihn.
|
|
||||||
* @param point the location to be shot
|
|
||||||
* @author Florian Alexy und Florian Hantzschel und Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveShoot(Point point) {
|
|
||||||
super.receiveShoot(point);
|
|
||||||
if (this.myTurn) {
|
|
||||||
Thread t = new Thread(this);
|
|
||||||
t.start();
|
|
||||||
this.shootThreads.add(t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wird aufgerufen, wenn in determineCoinToss festgestellt wurde das die Ki anfängt.
|
|
||||||
* Erster Schuss wird gestartet.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel und Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void beginTurn() {
|
|
||||||
Thread t = new Thread(this);
|
|
||||||
t.start();
|
|
||||||
this.shootThreads.add(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes past threads and tries firing a shot.
|
|
||||||
* @author Luca Conte, Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public void run() {
|
|
||||||
Iterator<Thread> i = this.shootThreads.iterator();
|
|
||||||
while(i.hasNext()) {
|
|
||||||
Thread thread = i.next();
|
|
||||||
if (!thread.isAlive()) {
|
|
||||||
try {
|
|
||||||
thread.join();
|
|
||||||
i.remove();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(250);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.aiShoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes all running threads and does some cleanup work so the garbage collector will delete the player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
super.destroy();
|
|
||||||
Iterator<Thread> i = this.shootThreads.iterator();
|
|
||||||
while(i.hasNext()) {
|
|
||||||
Thread thread = i.next();
|
|
||||||
try {
|
|
||||||
thread.interrupt();
|
|
||||||
thread.join();
|
|
||||||
i.remove();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,238 +0,0 @@
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides an Interface to communicate using a socket asynchronously
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public class AsyncSocket {
|
|
||||||
private Socket socket;
|
|
||||||
private Thread checkerThread;
|
|
||||||
private Thread connectorThread;
|
|
||||||
private AsyncSocketListener handler;
|
|
||||||
private boolean shouldStop = false;
|
|
||||||
|
|
||||||
private String sendBuffer = "";
|
|
||||||
|
|
||||||
private BufferedReader in;
|
|
||||||
private BufferedWriter out;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a socket server and turns the first incoming connection into an AsyncSocket
|
|
||||||
* @param port the port to listen on for a connection
|
|
||||||
* @param handler the handler that will be called when a message is received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public AsyncSocket(int port, AsyncSocketListener handler) {
|
|
||||||
this.setHandler(handler);
|
|
||||||
|
|
||||||
// start server in new thread
|
|
||||||
this.connectorThread = new Thread(() -> {
|
|
||||||
try {
|
|
||||||
ServerSocket serverSocket = new ServerSocket(port);
|
|
||||||
|
|
||||||
System.out.println("Waiting for client connection on port " + port);
|
|
||||||
|
|
||||||
Socket socket = serverSocket.accept();
|
|
||||||
|
|
||||||
System.out.println("Socket connected.");
|
|
||||||
|
|
||||||
serverSocket.close();
|
|
||||||
|
|
||||||
this.initSocket(socket);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
// TODO: proper error handling
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.connectorThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connects to the address provided and turns the resulting socket into an AsyncSocket
|
|
||||||
* @param address the socket address to connect to
|
|
||||||
* @param handler the handler that will be called when a message is received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public AsyncSocket(InetSocketAddress address, AsyncSocketListener handler) {
|
|
||||||
this.setHandler(handler);
|
|
||||||
|
|
||||||
// start client in new thread
|
|
||||||
this.connectorThread = new Thread(() -> {
|
|
||||||
System.out.println("Connecting to " + address.toString());
|
|
||||||
|
|
||||||
Socket socket = new Socket();
|
|
||||||
try {
|
|
||||||
socket.connect(address, 10);
|
|
||||||
System.out.println("Socket connected.");
|
|
||||||
|
|
||||||
this.initSocket(socket);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException("Connection timed out");
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
this.connectorThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param socket the socket to be wrapped in an AsyncSocket
|
|
||||||
* @param handler the handler that will be called when a message is received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public AsyncSocket(Socket socket, AsyncSocketListener handler) throws IOException {
|
|
||||||
this.setHandler(handler);
|
|
||||||
this.initSocket(socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* creates input and ouput writer / readers as well as a checker thread to repeatedly check
|
|
||||||
* for incoming messages
|
|
||||||
* @param socket the socket to be wrapped
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
private void initSocket(Socket socket) throws IOException {
|
|
||||||
System.out.println("Initialising sockets");
|
|
||||||
this.socket = socket;
|
|
||||||
|
|
||||||
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
|
||||||
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
|
|
||||||
|
|
||||||
this.shouldStop = false;
|
|
||||||
this.checkerThread = new Thread(() -> {
|
|
||||||
while (!this.shouldStop) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(100);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.connectorThread.isAlive()) {
|
|
||||||
try {
|
|
||||||
this.connectorThread.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!this.in.ready()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (this.handler == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String message = this.in.readLine();
|
|
||||||
if (message.length() <= 0) continue;
|
|
||||||
|
|
||||||
|
|
||||||
message = message.strip();
|
|
||||||
System.out.println("RECEIVED - " + message);
|
|
||||||
this.handler.receive(message);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
System.out.println("starting checker thread");
|
|
||||||
this.checkerThread.start();
|
|
||||||
this.flushBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the message handler for the async socket
|
|
||||||
* @param handler the `AsyncSocketListener` to be set as the new handler
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setHandler(AsyncSocketListener handler) {
|
|
||||||
this.handler = handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends a message through the socket
|
|
||||||
* @param socketPackage the socket package to be sent
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void send(SocketPackage socketPackage) {
|
|
||||||
this.sendLine(socketPackage.toString());
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* sends a message through the socket
|
|
||||||
* @param packageName the name of the package to be sent
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void send(String packageName) {
|
|
||||||
this.send(packageName, "");
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* sends a message through the socket
|
|
||||||
* @param packageName the name of the package to be sent
|
|
||||||
* @param packageContent the content of the package to be sent.
|
|
||||||
* `packageName` and `packageContent` are joined with a space " "
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void send(String packageName, String packageContent) {
|
|
||||||
if (packageContent.length() > 0) {
|
|
||||||
packageContent = " " + packageContent;
|
|
||||||
}
|
|
||||||
this.sendLine(packageName + packageContent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends a string of text into the socket, concatenated with CRLF
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void sendLine(String message) {
|
|
||||||
sendBuffer = sendBuffer + message + "\r\n";
|
|
||||||
this.flushBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* flushes the buffers to send all pending messages
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
private synchronized void flushBuffer() {
|
|
||||||
if (!this.sendBuffer.isEmpty() && this.out != null) {
|
|
||||||
try {
|
|
||||||
this.out.write(sendBuffer);
|
|
||||||
System.out.println("SENT - " + sendBuffer);
|
|
||||||
sendBuffer = "";
|
|
||||||
this.out.flush();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
// TODO: handle writing error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* closes the socket connection and removes the checker thread
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void close() {
|
|
||||||
this.shouldStop = true;
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.socket.close();
|
|
||||||
if (this.checkerThread != null) {
|
|
||||||
this.checkerThread.interrupt();
|
|
||||||
this.checkerThread.join();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
/**
|
|
||||||
* defines a message listener for AsyncSockets
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public interface AsyncSocketListener {
|
|
||||||
public void receive(String message);
|
|
||||||
}
|
|
160
src/Board.java
|
@ -1,162 +1,2 @@
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Klasse ist das Board von eimem spieler und enthält alle logischen operationen.
|
|
||||||
* Sprich ist das Backend Board.
|
|
||||||
*
|
|
||||||
* @author Peer Ole Wachtel, Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public class Board {
|
public class Board {
|
||||||
|
|
||||||
/**
|
|
||||||
* Alle bisher empfangenen HitResponsen
|
|
||||||
*/
|
|
||||||
private List<HitResponse> hits;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Alle Schiffe des Semesters
|
|
||||||
*/
|
|
||||||
private List<Ship> ships;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Die größe des Spielfeldes
|
|
||||||
*/
|
|
||||||
private final int size;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt ein neues Board.
|
|
||||||
* setzt die übergebene Spielfeldgröße.
|
|
||||||
* Erstellt die Liste aller Schiffe des Semesters
|
|
||||||
* @param size Die größe des Spielfeldes
|
|
||||||
* @author Peer Ole Wachtel, Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public Board(int size) {
|
|
||||||
this.size = size;
|
|
||||||
this.ships = new ArrayList<>();
|
|
||||||
this.hits = new ArrayList<>();
|
|
||||||
this.createShip(size - 13);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Nimmt einen punkt entgegen und Gibt einen HitResponse zurück.
|
|
||||||
* @param point auf den geschossen wurde
|
|
||||||
* @return
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public synchronized HitResponse hit (Point point){
|
|
||||||
HitResponse response = new HitResponse(HitResponseType.MISS,point);
|
|
||||||
for (int i = 0; i < this.ships.size(); i++) {
|
|
||||||
HitResponseType type = this.ships.get(i).shootOnShip(point);
|
|
||||||
if ( type == HitResponseType.SUNK) {
|
|
||||||
for (int ii = 0; ii < this.ships.size(); ii++) {
|
|
||||||
if (!this.ships.get(ii).isSunk()) {
|
|
||||||
response.setType(type);
|
|
||||||
this.addHits(response);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
response.setType(HitResponseType.VICTORY);
|
|
||||||
this.addHits(response);
|
|
||||||
return response;
|
|
||||||
} else if (type == HitResponseType.HIT) {
|
|
||||||
response.setType(type);
|
|
||||||
this.addHits(response);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.addHits(response);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* finds adjacened hit responses and sets their type to SUNK if they are currently HIT
|
|
||||||
* this makes it so that all the points of the ship are marked as SUNK, not just the final hit
|
|
||||||
* @param p the Point from which to propate the SUNK type
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
private void propagateSunk(Point p) {
|
|
||||||
HitResponse hit = this.getHitResponseOnPoint(p);
|
|
||||||
|
|
||||||
if (hit == null || hit.getType() != HitResponseType.HIT) return;
|
|
||||||
|
|
||||||
hit.setType(HitResponseType.SUNK);
|
|
||||||
|
|
||||||
propagateSunk(new Point(p.getX() + 1, p.getY()));
|
|
||||||
propagateSunk(new Point(p.getX() - 1, p.getY()));
|
|
||||||
propagateSunk(new Point(p.getX(), p.getY() + 1));
|
|
||||||
propagateSunk(new Point(p.getX(), p.getY() - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* creates all the ships on a board given a certain semester
|
|
||||||
* @param semester the semester to be played in
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
private void createShip(int semester){
|
|
||||||
List<ShipData> shipData = Ship.semeterList.get(semester-1);
|
|
||||||
for (int i = 0; i < shipData.size(); i++) {
|
|
||||||
this.ships.add(new Ship(shipData.get(i).size(), shipData.get(i).name()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns a list of all the Ships on the board
|
|
||||||
* @return a list of all the Ships on the board
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public List<Ship> getShips() {
|
|
||||||
return ships;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* adds a HitResponse to the list of Hits on the board
|
|
||||||
* If a hit response already exists on the same position, the hit response will not be added
|
|
||||||
* If the hit response is of type `HitResponseType.SUNK` it will propagate this hit response type
|
|
||||||
* to all adjacened hit responses with type HIT using `propagateSunk`.
|
|
||||||
* @param hitResponse the HitResponse to be added
|
|
||||||
* @return true when the hit response was added, otherwise false
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized boolean addHits(HitResponse hitResponse) {
|
|
||||||
if (this.getHitResponseOnPoint(hitResponse.getPoint()) == null){
|
|
||||||
this.hits.add(hitResponse);
|
|
||||||
|
|
||||||
//Propagate sunk for display purposes
|
|
||||||
if (hitResponse.getType() == HitResponseType.SUNK) {
|
|
||||||
hitResponse.setType(HitResponseType.HIT);
|
|
||||||
propagateSunk(hitResponse.getPoint());
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param point the position to get the hit response from
|
|
||||||
* @return the hit response at the position `point`
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public synchronized HitResponse getHitResponseOnPoint(Point point) {
|
|
||||||
for (int i = 0; i < this.hits.size(); i++){
|
|
||||||
if (this.hits.get(i).getPoint().equals(point)){
|
|
||||||
return this.hits.get(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the size of the board
|
|
||||||
* @return the size of the board
|
|
||||||
* @author Florian Alexy, Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public int getSize() {
|
|
||||||
return this.size;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,254 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.MouseAdapter;
|
|
||||||
import java.awt.event.MouseEvent;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dient dem Aufbau der Matrix (Spielfeld) und füllt diese mit Funktion
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class BoardDisplay extends JPanel {
|
|
||||||
private JButton[][] fields;
|
|
||||||
private int gridSize;
|
|
||||||
private Ship currentShip;
|
|
||||||
private Player player;
|
|
||||||
private boolean horizontal = false;
|
|
||||||
private List<ShipButton> shipButtonList;
|
|
||||||
private boolean enemyBoard;
|
|
||||||
private Point mousePosition;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fügt Buttons zu Liste hinzu und aktualisiert Feld durch Aufruf von paintFields
|
|
||||||
* @param button Jeweiliger Button der hinzugefügt werden soll
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void addShipButton(ShipButton button) {
|
|
||||||
shipButtonList.add(button);
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gibt currentShip zurück
|
|
||||||
* @return currentShip Objekt der Klasse Schiff
|
|
||||||
* @author Lucas Bronson Luca Conte
|
|
||||||
*/
|
|
||||||
public Ship getCurrentShip() {
|
|
||||||
return currentShip;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor des Board Displays
|
|
||||||
* @param gridSize Die Größe des Spielfelds
|
|
||||||
* @param player Der Spieler
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public BoardDisplay(int gridSize, Player player, boolean enemyBoard) {
|
|
||||||
super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Spalte
|
|
||||||
this.fields = new JButton[gridSize][gridSize];
|
|
||||||
this.shipButtonList = new ArrayList<>();
|
|
||||||
this.player = player;
|
|
||||||
this.gridSize = gridSize;
|
|
||||||
this.enemyBoard = enemyBoard;
|
|
||||||
|
|
||||||
// Erstellung vom Spielfeld
|
|
||||||
for (int i = 0; i <= gridSize; i++) {
|
|
||||||
for (int j = 0; j <= gridSize; j++) {
|
|
||||||
final int x = j - 1; // Temporäre Variable für überspringen von Rahmenzeile-/spalte
|
|
||||||
final int y = i - 1; // Temporäre Variable für überspringen von Rahmenzeile-/spalte
|
|
||||||
if (i == 0 && j == 0) {
|
|
||||||
add(new JLabel(" "));
|
|
||||||
} else if (i == 0) {
|
|
||||||
JLabel colLabel = new JLabel(String.valueOf(j));
|
|
||||||
colLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
colLabel.setFont(new Font("Roboto", Font.BOLD, 14));
|
|
||||||
add(colLabel);
|
|
||||||
} else if (j == 0) {
|
|
||||||
JLabel rowLabel = new JLabel(String.valueOf((char) ('A' + i - 1)));
|
|
||||||
rowLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
rowLabel.setFont(new Font("Roboto", Font.BOLD, 14));
|
|
||||||
add(rowLabel);
|
|
||||||
} else {
|
|
||||||
// Spielfeld (interaktive Zellen)
|
|
||||||
JButton field = new JButton("");
|
|
||||||
field.setBackground(Color.BLUE);
|
|
||||||
field.setOpaque(true);
|
|
||||||
field.setBorderPainted(true);
|
|
||||||
fields[x][y] = field;
|
|
||||||
add(field);
|
|
||||||
|
|
||||||
// Um Mausinteraktionen zu ermöglichen (Rechts/- Linksklick, Hover)
|
|
||||||
field.addMouseListener(new MouseAdapter() {
|
|
||||||
|
|
||||||
// Um beim "Hovern" Position zu setzten und weiterzugeben.
|
|
||||||
@Override
|
|
||||||
public void mouseEntered(MouseEvent e) {
|
|
||||||
mousePosition = new Point(x, y);
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Um nach "wegbewegen" der Maus wieder zu entfärben
|
|
||||||
@Override
|
|
||||||
public void mouseExited(MouseEvent e) {
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Um Schiffe zu rotieren/platzieren
|
|
||||||
@Override
|
|
||||||
public void mouseClicked(MouseEvent e) {
|
|
||||||
if (SwingUtilities.isRightMouseButton(e)) {
|
|
||||||
togglePlacementDirection();
|
|
||||||
} else if (SwingUtilities.isLeftMouseButton(e)) {
|
|
||||||
Point o = new Point(x, y);
|
|
||||||
handleFieldClick(o);
|
|
||||||
SoundHandler.playSound("plop");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aktuelles Schiff auswählen
|
|
||||||
* @param ship Schiff zum Auswählen
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void selectCurrentShip(Ship ship) {
|
|
||||||
this.currentShip = ship;
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Zurücksetzen von aktuellem Schiff und allen Schiffen des Spielers.
|
|
||||||
* Danach blau färben des Spielfeldes
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void resetAllShips() {
|
|
||||||
this.currentShip = null;
|
|
||||||
for (Ship ship : player.getBoard().getShips()) {
|
|
||||||
ship.resetPosition();
|
|
||||||
}
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wechselt die Platzierungsrichtung zwischen horizontal und vertikal.
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void togglePlacementDirection() {
|
|
||||||
horizontal = !horizontal;
|
|
||||||
String direction = horizontal ? "horizontal" : "vertikal";
|
|
||||||
System.out.println("Platzierungsrichtung geändert zu: " + direction);
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handhabt das Platzieren eines Schiffs auf dem Spielfeld.
|
|
||||||
* @param o Die Koordinaten des Klicks.
|
|
||||||
* @author Lucas Bronson, Luca Conte
|
|
||||||
*/
|
|
||||||
private void handleFieldClick(Point o) {
|
|
||||||
System.out.println("CLICK " + o);
|
|
||||||
if (!this.enemyBoard && !this.player.isGameRunning() && !this.player.isReady()) {
|
|
||||||
this.currentShip.setPosition(o, horizontal, player.getBoard().getShips(),this.gridSize);
|
|
||||||
}
|
|
||||||
if (this.enemyBoard && this.player.isGameRunning() && this.player.enemy.myTurn) {
|
|
||||||
this.player.enemy.shoot(o);
|
|
||||||
}
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Färbt das Spielfeld blau, überprüft, ob das aktuelle Schiff null ist.
|
|
||||||
* Färbt eine Preview in das Spielfeld ein, ob Schiff setzen möglich ist
|
|
||||||
* Schiffe die gesetzt wurden, werden grau gefärbt
|
|
||||||
* Aufrufen von refreshButtonState um zu zeigen, ob Button drückbar ist
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void paintFields() {
|
|
||||||
List<Point> test=new ArrayList<>();
|
|
||||||
if(currentShip != null) {
|
|
||||||
test = currentShip.getVirtualOccupiedPoints(mousePosition, horizontal);
|
|
||||||
}
|
|
||||||
if (player == null || player.getBoard() == null) return;
|
|
||||||
for(int i = 0; i < gridSize; i++) {
|
|
||||||
for(int j = 0; j < gridSize; j++) {
|
|
||||||
if(fields[i][j] == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (this.enemyBoard) {
|
|
||||||
// enemy board only accessible if game is running AND it's "enemy's" so local player's turn
|
|
||||||
if (!this.player.isGameRunning() || !this.player.enemy.myTurn) {
|
|
||||||
fields[i][j].setEnabled(false);
|
|
||||||
} else {
|
|
||||||
fields[i][j].setEnabled(true);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (this.player.isGameRunning() || this.player.isReady()) {
|
|
||||||
fields[i][j].setEnabled(false);
|
|
||||||
} else {
|
|
||||||
fields[i][j].setEnabled(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (this.enemyBoard) {
|
|
||||||
fields[i][j].setBackground(Color.WHITE);
|
|
||||||
} else {
|
|
||||||
fields[i][j].setBackground(Color.BLUE);
|
|
||||||
}
|
|
||||||
if (!this.player.isReady()) {
|
|
||||||
for(Point p : test) {
|
|
||||||
if(i==p.getX() && j==p.getY()) {
|
|
||||||
if (currentShip.checkValidPlacement(mousePosition,horizontal,player.getBoard().getShips(),gridSize)) {
|
|
||||||
fields[i][j].setBackground(Color.GREEN);
|
|
||||||
} else {
|
|
||||||
fields[i][j].setBackground(Color.RED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(Ship ship: player.getBoard().getShips()) {
|
|
||||||
if(ship.isShipOnPos(new Point(i,j))) {
|
|
||||||
fields[i][j].setBackground(Color.WHITE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
HitResponse hit = this.player.getBoard().getHitResponseOnPoint(new Point(i, j));
|
|
||||||
if (hit != null) {
|
|
||||||
switch (hit.getType()) {
|
|
||||||
case HIT:
|
|
||||||
fields[i][j].setBackground(Color.ORANGE);
|
|
||||||
//SoundHandler.playSound("hit");
|
|
||||||
break;
|
|
||||||
case SUNK:
|
|
||||||
//SoundHandler.playSound("destroyed");
|
|
||||||
case VICTORY:
|
|
||||||
fields[i][j].setBackground(Color.RED);
|
|
||||||
break;
|
|
||||||
case MISS:
|
|
||||||
if (this.enemyBoard) {
|
|
||||||
//SoundHandler.playSound("miss");
|
|
||||||
fields[i][j].setBackground(Color.BLUE);
|
|
||||||
} else {
|
|
||||||
fields[i][j].setBackground(Color.CYAN);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for( ShipButton shipButton: shipButtonList) {
|
|
||||||
shipButton.refreshButtonState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ruft paintFields auf, um Felder zu aktualisieren
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void refresh() {
|
|
||||||
paintFields();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,237 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Das GameBoard dient als Panel, in dem das tatsächliche Spiel stattfindet.
|
|
||||||
* Der Benutzer kann hier seine Schiffe platzieren, das Spiel starten etc.
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class GameBoard extends JPanel {
|
|
||||||
|
|
||||||
private BoardDisplay ownBoardPanel;
|
|
||||||
private BoardDisplay opponentBoardPanel;
|
|
||||||
|
|
||||||
private Player p1;
|
|
||||||
private Player p2;
|
|
||||||
|
|
||||||
// Grafiken
|
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
|
||||||
ImageIcon gameBoardEmtpy = new ImageIcon("graphics/gameboardempty.png");
|
|
||||||
ImageIcon gameBoardX = new ImageIcon("graphics/gameboardx.png");
|
|
||||||
|
|
||||||
// kontextText Text-Strings
|
|
||||||
String kT1 = "Bitte Schiffe setzten - Rechtsclick zum drehen";
|
|
||||||
String kT2 = "Warte auf Gegner";
|
|
||||||
String kT3 = "Du fängst an";
|
|
||||||
String kT4 = "Dein Gegner fängt an";
|
|
||||||
String kT5 = "Du bist am Zug";
|
|
||||||
String kT6 = "Dein Gegner ist am Zug";
|
|
||||||
String kT7 = "Du hast das Spiel gewonnen";
|
|
||||||
String kT8 = "Du hast das Spiel verloren";
|
|
||||||
String kT9 = "Bitte erst alle Schiffe setzten";
|
|
||||||
|
|
||||||
// Labels
|
|
||||||
JLabel frameTitle = new JLabel("GameBoard");
|
|
||||||
JLabel kontextText = new JLabel(kT1);
|
|
||||||
|
|
||||||
// Buttons
|
|
||||||
JButton giveUpButton = new JButton("Aufgeben");
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor von GameBoard.
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @param semesterCounter Ausgewähltes Semester
|
|
||||||
* @param p1 Erstes Spielerobjekt
|
|
||||||
* @param p2 Zweites Spielerobjekt
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
GameBoard(MainFrame frame, int semesterCounter,Player p1, Player p2) {
|
|
||||||
this.p1 = p1;
|
|
||||||
this.p2 = p2;
|
|
||||||
buildPanel(frame, semesterCounter);
|
|
||||||
List<Ship> shipsP1 =p1.getBoard().getShips();
|
|
||||||
List<Ship> shipsP2 =p2.getBoard().getShips();
|
|
||||||
|
|
||||||
giveUpButton.addActionListener((e) -> {
|
|
||||||
frame.showPanel("MainMenu");
|
|
||||||
p1.withdraw();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Timer für pulsierenden SchwarzGrau-Effekt
|
|
||||||
Timer timer = new Timer(10, new ActionListener() {
|
|
||||||
// Start-Grauwert (0 = Schwarz, 255 = Weiß)
|
|
||||||
private int value = 50;
|
|
||||||
private boolean increasing = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
kontextText.setForeground(new Color(value, value, value));
|
|
||||||
|
|
||||||
if (increasing) {
|
|
||||||
value++;
|
|
||||||
if (value >= 90) {
|
|
||||||
increasing = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
value--;
|
|
||||||
if (value <= 0) {
|
|
||||||
increasing = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Baut das grundlegende Spielboard
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @param semesterCounter Ausgewähltes Semester
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void buildPanel(MainFrame frame, int semesterCounter) {
|
|
||||||
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
|
|
||||||
setLayout(new BorderLayout());
|
|
||||||
|
|
||||||
// Spielfelder erstellen (eigenes und gegnerisches)
|
|
||||||
// Spielfelder werden in BoardDisplay erstellt
|
|
||||||
int gridSize = GameController.semesterToBoardSize(semesterCounter); // Größe des Spielfelds
|
|
||||||
this.ownBoardPanel = new BoardDisplay(gridSize,p1, false);
|
|
||||||
this.opponentBoardPanel = new BoardDisplay(gridSize, p2, true);
|
|
||||||
|
|
||||||
// Panel für das Kontext-Text-Feld
|
|
||||||
JPanel headerPanel = new JPanel();
|
|
||||||
headerPanel.setLayout(new BorderLayout());
|
|
||||||
headerPanel.add(kontextText, BorderLayout.WEST);
|
|
||||||
kontextText.setFont(new Font("Roboto", Font.BOLD, 30));
|
|
||||||
headerPanel.add(giveUpButton, BorderLayout.EAST);
|
|
||||||
|
|
||||||
JPanel leftButtonsPanel = new JPanel();
|
|
||||||
leftButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
|
|
||||||
|
|
||||||
JPanel rightButtonsPanel = new JPanel();
|
|
||||||
rightButtonsPanel.setLayout(new GridLayout(7, 1));
|
|
||||||
|
|
||||||
//Buttons in eine Gruppe packen damit diese beim drücken eines anderen Buttons wieder entwählt werden
|
|
||||||
ButtonGroup leftButtonGroup= new ButtonGroup();
|
|
||||||
ButtonGroup rightButtonGroup= new ButtonGroup();
|
|
||||||
|
|
||||||
// Panel für die Buttons des linken Spielers (ganz links)
|
|
||||||
for(Ship ship : p1.getBoard().getShips()) {
|
|
||||||
ShipButton shipButton= new ShipButton(ship,ownBoardPanel);
|
|
||||||
leftButtonsPanel.add(shipButton);
|
|
||||||
leftButtonGroup.add(shipButton);
|
|
||||||
ownBoardPanel.addShipButton(shipButton);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Ship ship : p2.getBoard().getShips()) {
|
|
||||||
ShipButton shipButton= new ShipButton(ship,opponentBoardPanel);
|
|
||||||
rightButtonsPanel.add(shipButton);
|
|
||||||
rightButtonGroup.add(shipButton);
|
|
||||||
opponentBoardPanel.addShipButton(shipButton);
|
|
||||||
shipButton.setEnabled(false);
|
|
||||||
}
|
|
||||||
JToggleButton readyButton = new JToggleButton("Bereit");
|
|
||||||
readyButton.setBackground(Color.GREEN);
|
|
||||||
rightButtonsPanel.add(readyButton);
|
|
||||||
|
|
||||||
JToggleButton resetButton = new JToggleButton("Reset");
|
|
||||||
resetButton.setBackground(Color.RED);
|
|
||||||
leftButtonsPanel.add(resetButton);
|
|
||||||
|
|
||||||
resetButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
ownBoardPanel.resetAllShips();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um Bereit-Meldung and Backend zu geben, kontextText zu setzten und ready/reset Button zu deaktivieren
|
|
||||||
readyButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
kontextText.setText(kT2);
|
|
||||||
p1.ready();
|
|
||||||
if(p1.isReady()) {
|
|
||||||
remove(readyButton);
|
|
||||||
remove(resetButton);
|
|
||||||
remove(rightButtonsPanel);
|
|
||||||
remove(leftButtonsPanel);
|
|
||||||
readyButton.setEnabled(false);
|
|
||||||
resetButton.setEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Panel für beide Spielfelder (nebeneinander in der Mitte)
|
|
||||||
JPanel centerPanel = new JPanel();
|
|
||||||
centerPanel.setLayout(new GridLayout(1, 2, 20, 0)); // 2 Spielfelder nebeneinander, mit Abstand von 20 Pixeln
|
|
||||||
centerPanel.add(ownBoardPanel);
|
|
||||||
centerPanel.add(opponentBoardPanel);
|
|
||||||
|
|
||||||
// Spieler-Namen über den Spielfeldern hinzufügen
|
|
||||||
JPanel playerNamesPanel = new JPanel();
|
|
||||||
playerNamesPanel.setLayout(new GridLayout(1, 2)); // Zwei Labels nebeneinander
|
|
||||||
JLabel player1NameLabel = new JLabel(p1.getName(), SwingConstants.CENTER);
|
|
||||||
JLabel player2NameLabel = new JLabel(p2.getName(), SwingConstants.CENTER);
|
|
||||||
System.out.println("Name in Gameboard: " + player1NameLabel.getText());
|
|
||||||
|
|
||||||
// Schrift und Formatierung der Labels
|
|
||||||
player1NameLabel.setFont(new Font("Roboto", Font.BOLD, 18));
|
|
||||||
player2NameLabel.setFont(new Font("Roboto", Font.BOLD, 18));
|
|
||||||
|
|
||||||
// Spieler-Labels zum Panel hinzufügen
|
|
||||||
playerNamesPanel.add(player1NameLabel);
|
|
||||||
playerNamesPanel.add(player2NameLabel);
|
|
||||||
|
|
||||||
// Spieler-Namen-Panel oberhalb der Spielfelder hinzufügen
|
|
||||||
JPanel namesAndBoardsPanel = new JPanel(new BorderLayout());
|
|
||||||
namesAndBoardsPanel.add(playerNamesPanel, BorderLayout.NORTH);
|
|
||||||
namesAndBoardsPanel.add(centerPanel, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
// Panels dem Hauptlayout hinzufügen
|
|
||||||
add(leftButtonsPanel, BorderLayout.WEST);
|
|
||||||
add(rightButtonsPanel, BorderLayout.EAST);
|
|
||||||
add(headerPanel, BorderLayout.NORTH);
|
|
||||||
add(namesAndBoardsPanel, BorderLayout.CENTER);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aktualisiert Zustand(kontextText) je nach Zug
|
|
||||||
* @author Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void refresh() {
|
|
||||||
if (this.p1.myTurn) {
|
|
||||||
if (!kontextText.getText().equals(kT5)) {
|
|
||||||
this.kontextText.setText(kT5);
|
|
||||||
SoundHandler.playSound("yourturn");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.kontextText.setText(kT6);
|
|
||||||
}
|
|
||||||
this.ownBoardPanel.refresh();
|
|
||||||
this.opponentBoardPanel.refresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter für Player1
|
|
||||||
* @return Player 1
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public Player getP1() {
|
|
||||||
return p1;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Getter für Player2
|
|
||||||
* @return Player 2
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public Player getP2() {
|
|
||||||
return p2;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,242 +0,0 @@
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The central Backend Component
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public class GameController {
|
|
||||||
|
|
||||||
private static MainFrame mainFrame;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the current MainFrame
|
|
||||||
* @return the current MainFrame
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static MainFrame getMainFrame() {
|
|
||||||
return GameController.mainFrame;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* sets the current MainFrame
|
|
||||||
* @param mainFrame the current MainFrame
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void setMainFrame(MainFrame mainFrame) {
|
|
||||||
GameController.mainFrame = mainFrame;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* converts a semester to be played in into its respective board size
|
|
||||||
* @param semester the semester to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static int semesterToBoardSize(int semester) {
|
|
||||||
return semester + 13;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* converts a board size into its respective semester
|
|
||||||
* @param size the board size to get the semester of
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static int boardSizeToSemester(int size) {
|
|
||||||
return size - 13;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the list of supported versions of the online protocol
|
|
||||||
* https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
*/
|
|
||||||
public static HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = new HashMap<>(Map.of(
|
|
||||||
"1.1.0", OnlinePlayer_1_1_0.class
|
|
||||||
));
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts a game with one local player and one online player.
|
|
||||||
* @param localPlayerClass the class of the local player to be instantiated. must extend `LocalPlayer`
|
|
||||||
* @param localPlayerName the name of the local player. Only necessarry when localPlayerClass is `HumanPlayer`
|
|
||||||
* @param address the Address of the opposing player. If the hostname of the address is set to be 0.0.0.0, it
|
|
||||||
* is interpreted that this instance should instead listen on the defined port for incoming connections.
|
|
||||||
* @param size the board size that it is intended to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, String localPlayerName, InetSocketAddress address, int size) throws IOException {
|
|
||||||
AsyncSocket clientSocket;
|
|
||||||
|
|
||||||
boolean localPlayerIsServer = address.getHostName() == null || address.getHostName().isEmpty() || address.getHostName().equals("0.0.0.0");
|
|
||||||
|
|
||||||
if (localPlayerIsServer) {
|
|
||||||
// SERVER MODE
|
|
||||||
clientSocket = new AsyncSocket(address.getPort(), null);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// CLIENT MODE
|
|
||||||
clientSocket = new AsyncSocket(address, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
clientSocket.send("VERSION", "Gruppe03 " + String.join(" ", supportedVersions.keySet()));
|
|
||||||
clientSocket.setHandler((message) -> {
|
|
||||||
|
|
||||||
SocketPackage socketPackage = new SocketPackage(message);
|
|
||||||
if (socketPackage.getName().equals("VERSION")) {
|
|
||||||
|
|
||||||
List<String> opponentSupportedVersions = socketPackage.splitData();
|
|
||||||
|
|
||||||
String usedVersion = findMostRecentVersion(new ArrayList<String>(supportedVersions.keySet()), opponentSupportedVersions);
|
|
||||||
|
|
||||||
if (usedVersion == null) {
|
|
||||||
throw new RuntimeException("No common versions found");
|
|
||||||
}
|
|
||||||
|
|
||||||
// instantiate players
|
|
||||||
Class<? extends OnlinePlayer> onlinePlayerClass = supportedVersions.get(usedVersion);
|
|
||||||
|
|
||||||
LocalPlayer localPlayer;
|
|
||||||
OnlinePlayer onlinePlayer;
|
|
||||||
try {
|
|
||||||
localPlayer = localPlayerClass.getDeclaredConstructor().newInstance();
|
|
||||||
onlinePlayer = onlinePlayerClass.getDeclaredConstructor(Integer.class, AsyncSocket.class).newInstance((Integer)size, clientSocket);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException("Unable to instantiate players");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
localPlayer.isServer = localPlayerIsServer;
|
|
||||||
onlinePlayer.isServer = !localPlayerIsServer;
|
|
||||||
|
|
||||||
localPlayer.setName(localPlayerName);
|
|
||||||
|
|
||||||
localPlayer.setEnemy(onlinePlayer);
|
|
||||||
onlinePlayer.setEnemy(localPlayer);
|
|
||||||
|
|
||||||
onlinePlayer.sendIAM();
|
|
||||||
|
|
||||||
// Start game only after IAM Package was exchanged
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException("Unexpected Package received before game initialisation");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* finds the largest common version in two lists of version strings
|
|
||||||
* @return null if no common versions are found otherwise returns the most recent version present in both lists
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static String findMostRecentVersion(List<String> versions1, List<String> versions2) {
|
|
||||||
if (versions1 == null || versions2 == null) return null;
|
|
||||||
String largestCommonVersion = null;
|
|
||||||
for (String v1 : versions1) {
|
|
||||||
if (!checkVersionString(v1)) continue;
|
|
||||||
|
|
||||||
for (String v2 : versions2) {
|
|
||||||
if (!checkVersionString(v2)) continue;
|
|
||||||
|
|
||||||
// strings within the lists must be equal
|
|
||||||
// if no largest common version has been found or currently checked version is larger than
|
|
||||||
// previous largest common version, new largest version has been found
|
|
||||||
if (compareVersions(v1, v2) == 0 && (largestCommonVersion == null || compareVersions(v1, largestCommonVersion) >= 1)) {
|
|
||||||
largestCommonVersion = v1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return largestCommonVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* compares two version strings
|
|
||||||
* @param version1 the first version to be compared
|
|
||||||
* @param version2 the second version to be compared
|
|
||||||
* @return
|
|
||||||
* 0 if versions are equal
|
|
||||||
* 1 if version1 is more recent than version2
|
|
||||||
* -1 otherwise
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static int compareVersions(String version1, String version2) {
|
|
||||||
if (!checkVersionString(version1) || !checkVersionString(version2)) {
|
|
||||||
throw new IllegalArgumentException("Version is not valid version string");
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] version1Split = version1.split("\\.");
|
|
||||||
String[] version2Split = version2.split("\\.");
|
|
||||||
|
|
||||||
for (int i = 0; i < version1Split.length; i++) {
|
|
||||||
int v1 = Integer.parseInt(version1Split[i]);
|
|
||||||
int v2 = Integer.parseInt(version2Split[i]);
|
|
||||||
|
|
||||||
if (v1 == v2) continue;
|
|
||||||
if (v1 > v2) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* checks if a provided string matches the format of a version number
|
|
||||||
* @param versionString the version String to be checked
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static boolean checkVersionString(String versionString) {
|
|
||||||
return versionString != null && versionString.matches("\\d+\\.\\d+\\.\\d+");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts a game with two local players
|
|
||||||
* @param localPlayerClass the class of the local player to be instantiated. must extend `LocalPlayer`
|
|
||||||
* @param localPlayerName the name of the local player. Only necessarry when localPlayerClass is `HumanPlayer`
|
|
||||||
* @param enemyClass the class of the enemy player. Must extend `AiPlayer`. For Humans VS Human games use an online game instead.
|
|
||||||
* @param size the board size that it is intended to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, String localPlayerName, Class<? extends AiPlayer> enemyClass, int size) {
|
|
||||||
|
|
||||||
LocalPlayer localPlayer;
|
|
||||||
AiPlayer aiPlayer;
|
|
||||||
try {
|
|
||||||
localPlayer = localPlayerClass.getDeclaredConstructor().newInstance();
|
|
||||||
aiPlayer = enemyClass.getDeclaredConstructor().newInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException("Unable to instantiate players");
|
|
||||||
}
|
|
||||||
|
|
||||||
localPlayer.isServer = true;
|
|
||||||
aiPlayer.isServer = false;
|
|
||||||
|
|
||||||
|
|
||||||
localPlayer.setEnemy(aiPlayer);
|
|
||||||
aiPlayer.setEnemy(localPlayer);
|
|
||||||
|
|
||||||
localPlayer.createBoard(size);
|
|
||||||
aiPlayer.createBoard(size);
|
|
||||||
|
|
||||||
localPlayer.setName(localPlayerName);
|
|
||||||
|
|
||||||
startGameWithInstancedPlayers(localPlayer, aiPlayer, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* switches to the ingame screen
|
|
||||||
* @param p1 the first player. This player is always shown on the left side of the screen and is expected to be the Human player, if there is one.
|
|
||||||
* @param p2 the second player
|
|
||||||
* @param boardSize the board size to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void startGameWithInstancedPlayers(LocalPlayer p1, Player p2, int boardSize) {
|
|
||||||
mainFrame.showPanelSLG("GameBoard", boardSizeToSemester(boardSize), p1, p2);
|
|
||||||
|
|
||||||
// TODO: frontend configuration
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,17 +1,23 @@
|
||||||
/**
|
|
||||||
* Hauptklasse über die der MainFrame gestartet wird
|
|
||||||
* @author Lucas Bronson, Peer Ole Wachtel, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class HalloSchiffeVersenken {
|
public class HalloSchiffeVersenken {
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt und setzt den Mainframe
|
|
||||||
* @param args Argumente an Main durch Konsole etc.
|
|
||||||
* @throws InterruptedException
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
MainFrame mf = new MainFrame();
|
MainMenuModel model = new MainMenuModel();
|
||||||
mf.setVisible(true);
|
MainMenuView view = new MainMenuView();
|
||||||
|
MainMenuController controller = new MainMenuController(model, view);
|
||||||
|
|
||||||
|
System.out.println("HelloSchiffeVersenekn");
|
||||||
|
|
||||||
|
/**
|
||||||
|
System.out.println("sound");
|
||||||
|
SoundHandler.playSound("hit");
|
||||||
|
Thread.sleep(3000);
|
||||||
|
System.out.println("sound");
|
||||||
|
SoundHandler.playSound("hit");
|
||||||
|
SoundHandler.setSoundOn(false);
|
||||||
|
System.out.println("sound off");
|
||||||
|
SoundHandler.playSound("hit");
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,90 +1,6 @@
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
/**
|
|
||||||
* Eine Antwort auf einen Schuss
|
|
||||||
* enthält einen Punkt, auf den geschossen wurde, sowie einen typen,
|
|
||||||
* der angibt, ob es sich bei dem Schuss um einen Treffer handelt
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public class HitResponse {
|
public class HitResponse {
|
||||||
/**
|
|
||||||
* Speichert den typ der HitResponse.
|
|
||||||
*/
|
|
||||||
private HitResponseType type;
|
private HitResponseType type;
|
||||||
/**
|
|
||||||
* Speicher den Punkt wofür die HitResponse gilt.
|
|
||||||
*/
|
|
||||||
private Point point;
|
private Point point;
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt eine neue HitResponse und setzt den Punkt und typ.
|
|
||||||
* @param type der HitResponse.
|
|
||||||
* @param point für den die HitResponse gilt.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
public HitResponse(HitResponseType type, Point point) {
|
|
||||||
this.type = type;
|
|
||||||
this.point = point;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*Erstellt eine neue HitResponse und setzt den Punkt und typ.
|
|
||||||
* @param typeIndex der HitResponse.
|
|
||||||
* @param point für den die HitResponse gilt.
|
|
||||||
* @throws IllegalArgumentException wenn der übergebene int nicht auf ein typ referenziert werden kann.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
public HitResponse (int typeIndex, Point point) {
|
|
||||||
if (typeIndex >= 0 && typeIndex < HitResponseType.values().length) {
|
|
||||||
this.type = HitResponseType.values()[typeIndex];
|
|
||||||
this.point = point;
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the type of the HitResponse
|
|
||||||
* @return the type of the HitResponse
|
|
||||||
* @author Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public HitResponseType getHitResponse() {
|
|
||||||
return this.type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the point of the HitResponse
|
|
||||||
* @return the point of the HitResponse
|
|
||||||
* @author Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public Point getPoint() {
|
|
||||||
return this.point;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setter für den type
|
|
||||||
* @param type auf den die HitResponse gesetzt werden soll.
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setType(HitResponseType type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gibt den passenden string nach Netzwerkstandard für eine HitResponse zurück.
|
|
||||||
* @return den passenden string nach Netzwerkstandard für eine HitResponse.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.getPoint().toString() + " " + this.type.ordinal();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter für den typ der HitResponse.
|
|
||||||
* @return den typ der HitRespnse.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
public HitResponseType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
/**
|
|
||||||
* Stellt die verschiedenen möglichkeiten einer HitResponse als typ dar.
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public enum HitResponseType {
|
public enum HitResponseType {
|
||||||
MISS, HIT, SUNK, VICTORY
|
MISS, HIT, SUNK, VICTORY
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
|
|
||||||
/**
|
|
||||||
* Repräsentiert einen menschlichen Spieler
|
|
||||||
* @author Luca Conte, Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public class HumanPlayer extends LocalPlayer {
|
|
||||||
/**
|
|
||||||
* shoots a shot onto the provided point on the enemy board
|
|
||||||
* if it is not the players turn, this method does nothing.
|
|
||||||
* @param point the location to be shot
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void shoot(Point point) {
|
|
||||||
if (!this.myTurn) return;
|
|
||||||
enemy.receiveShoot(point);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,132 +0,0 @@
|
||||||
import java.awt.*;
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Das JoinGame Panel dient zum setzten des Ports/IP-Adresse.
|
|
||||||
* Anschließend kann das Verbinden Panel gezeigt werden.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public class JoinGame extends JPanel {
|
|
||||||
|
|
||||||
// Funktionshilfen
|
|
||||||
String standardPort = "51525";
|
|
||||||
|
|
||||||
// Grafiken
|
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
|
||||||
|
|
||||||
// Labels
|
|
||||||
JLabel spielBeitretenLabel;
|
|
||||||
JLabel ipLabel = new JLabel("IP-Adresse");
|
|
||||||
JLabel portLabel = new JLabel("Port");
|
|
||||||
|
|
||||||
// Textfelder
|
|
||||||
JTextField ipTextField = new JTextField(20);
|
|
||||||
JTextField portTextField = new JTextField(20);
|
|
||||||
|
|
||||||
// Buttons
|
|
||||||
JButton losButton = new JButton("Los!");
|
|
||||||
JButton backButton = new JButton(backButtonIcon);
|
|
||||||
|
|
||||||
// Font
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt mittels Funktionsaufrufen das Panel.
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @param g int-Anzeige, ob es sich um "spiel erstellen" oder "spiel beitreten" handelt.
|
|
||||||
* @param playerType int-Anzeige, ob es sich um einen HumanPlayer,AIEasy... handelt.
|
|
||||||
* @param playerName Name des Spielers
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public JoinGame(MainFrame frame,int g,int playerType,String playerName, int semesterCounter) {
|
|
||||||
setLayout(null);
|
|
||||||
buildPanel(frame,g,playerType,playerName, semesterCounter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt das Panel.
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @param g int-Anzeige, ob es sich um "spiel erstellen" oder "spiel beitreten" handelt.
|
|
||||||
* @param playerType int-Anzeige, ob es sich um einen HumanPlayer,AIEasy... handelt.
|
|
||||||
* @param playerName Name des Spielers
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void buildPanel(MainFrame frame,int g,int playerType,String playerName, int semesterCounter) {
|
|
||||||
System.out.println("semesterzahl in JoinGame" + semesterCounter);
|
|
||||||
if(g==1){
|
|
||||||
spielBeitretenLabel= new JLabel("Spiel beitreten");
|
|
||||||
}else{
|
|
||||||
spielBeitretenLabel= new JLabel("Spiel erstellen");
|
|
||||||
}
|
|
||||||
|
|
||||||
spielBeitretenLabel.setBounds(20,20,700, 100);
|
|
||||||
losButton.setBounds(320, 225, 100, 50);
|
|
||||||
backButton.setBounds(1380, 20, 80, 80);
|
|
||||||
|
|
||||||
portLabel.setBounds(50, 200, 200, 30);
|
|
||||||
|
|
||||||
if(g==1) { // Wenn man Spiel erstellen will, werden IP-Felder nicht angezeigt.
|
|
||||||
ipLabel.setBounds(50, 125, 200, 30);
|
|
||||||
ipTextField.setBounds(50, 150, 250, 50);
|
|
||||||
}
|
|
||||||
|
|
||||||
portTextField.setBounds(50, 225, 250, 50);
|
|
||||||
portTextField.setText(standardPort);
|
|
||||||
|
|
||||||
spielBeitretenLabel.setFont(robotoFont.deriveFont(50f));
|
|
||||||
|
|
||||||
add(spielBeitretenLabel);
|
|
||||||
add(ipLabel);
|
|
||||||
add(portLabel);
|
|
||||||
add(losButton);
|
|
||||||
add(ipTextField);
|
|
||||||
add(portTextField);
|
|
||||||
add(backButton);
|
|
||||||
|
|
||||||
// ActionListener für Buttons.
|
|
||||||
// Um in das MultiplayerGame zurückzuwechseln.
|
|
||||||
backButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
frame.showPanel("MultiplayerGame");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um das Verbinden Panel anzuzeigen und Daten an Backend weiterzureichen.
|
|
||||||
losButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
|
|
||||||
String ipAddress = ipTextField.getText();
|
|
||||||
if (ipAddress.isEmpty()) {
|
|
||||||
ipAddress = "0.0.0.0";
|
|
||||||
}
|
|
||||||
System.out.println(portTextField.getText());
|
|
||||||
String portText = portTextField.getText();
|
|
||||||
|
|
||||||
int port = Integer.parseInt(portText);
|
|
||||||
InetSocketAddress address = new InetSocketAddress(ipAddress, port);
|
|
||||||
|
|
||||||
frame.showPanel("Verbinden");
|
|
||||||
|
|
||||||
try {
|
|
||||||
if(playerType == 0) {
|
|
||||||
GameController.startOnlineGame(HumanPlayer.class, playerName, address,GameController.semesterToBoardSize(semesterCounter));
|
|
||||||
} else if(playerType == 1) {
|
|
||||||
GameController.startOnlineGame(SpecificAiPlayerEasy.class, playerName, address,GameController.semesterToBoardSize(semesterCounter));
|
|
||||||
} else if (playerType == 2) {
|
|
||||||
GameController.startOnlineGame(SpecificAiPlayerMedium.class, playerName, address,GameController.semesterToBoardSize(semesterCounter));
|
|
||||||
} else if (playerType == 3) {
|
|
||||||
GameController.startOnlineGame(SpecificAiPlayerHard.class, playerName, address,GameController.semesterToBoardSize(semesterCounter));
|
|
||||||
}
|
|
||||||
} catch (IOException ex) {
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,104 +0,0 @@
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* repräsentiert einen Player, der Lokal an dem Gerät spielt,
|
|
||||||
* auf dem dieses Programm läuft
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel, Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public class LocalPlayer extends Player {
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* erstellt einen LocalPlayer und setzt myCoin random
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public LocalPlayer(){
|
|
||||||
super();
|
|
||||||
Random random = new Random();
|
|
||||||
this.myCoin = random.nextBoolean();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a shot onto a point from the enemy
|
|
||||||
* @param point the location to be shot
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveShoot(Point point) {
|
|
||||||
if (!this.enemy.myTurn) {
|
|
||||||
System.out.println("enemy tried to fire when not their turn!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.enemy.myTurn = false;
|
|
||||||
|
|
||||||
HitResponse hitResponse = board.getHitResponseOnPoint(point);
|
|
||||||
if (!(hitResponse == null)){
|
|
||||||
enemy.receiveHit(hitResponse);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
hitResponse = this.board.hit(point);
|
|
||||||
enemy.receiveHit(hitResponse);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (hitResponse.getType()) {
|
|
||||||
case HIT, SUNK -> this.myTurn = false;
|
|
||||||
case MISS -> this.myTurn = true;
|
|
||||||
case VICTORY -> this.lose();
|
|
||||||
}
|
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a hit response from the enemy as a response to a receiveShoot call
|
|
||||||
* @param hitResponse the hitresponse
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
|
||||||
enemy.board.addHits(hitResponse);
|
|
||||||
switch (hitResponse.getType()) {
|
|
||||||
case HIT, SUNK -> this.myTurn = true;
|
|
||||||
case MISS -> this.myTurn = false;
|
|
||||||
case VICTORY -> this.win();
|
|
||||||
}
|
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives the enemies coin toss result
|
|
||||||
* this method does nothing if the player has already received the enemies coin
|
|
||||||
* it will also call `determineCoinToss`
|
|
||||||
* @param coin the coin of the enemy player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
|
||||||
if (!this.hasReceivedCoin) {
|
|
||||||
this.hasReceivedCoin = true;
|
|
||||||
this.determineCoinToss();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends shot to enemy player.
|
|
||||||
* should ONLY be called on HumanPlayer
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void shoot(Point point){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* marks the player as ready, if all ships have been placed
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void ready() {
|
|
||||||
for (Ship ship : this.board.getShips()) {
|
|
||||||
if (!ship.isPlaced()) return;
|
|
||||||
}
|
|
||||||
super.ready();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Klasse für Erstellung von looseScreen Objekten
|
|
||||||
* Dient zur Anzeige das ein Spiel verloren wurde
|
|
||||||
* @author Joshua Kuklok, Luca Bronson
|
|
||||||
*/
|
|
||||||
public class LoseScreen extends JPanel {
|
|
||||||
JLabel loseLabel = new JLabel("Du hast Verloren");
|
|
||||||
JButton okButton = new JButton("Zurück zum Hauptmenü");
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor der LoseScreen Klasse
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public LoseScreen(MainFrame frame) {
|
|
||||||
setLayout(null);
|
|
||||||
buildPanel(frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Panel bauen/Objekte hinzufügen
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void buildPanel(MainFrame frame) {
|
|
||||||
add(loseLabel);
|
|
||||||
okButton.setBounds(625,525,200,50);
|
|
||||||
loseLabel.setBounds(550,450,500,50);
|
|
||||||
loseLabel.setFont(robotoFont);
|
|
||||||
SoundHandler.playSound("loose");
|
|
||||||
|
|
||||||
// Zurückkehren zum Hauptmenü, wenn okButton gedrückt wird
|
|
||||||
okButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
frame.showPanel("MainMenu");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
add(loseLabel);
|
|
||||||
add(okButton);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,181 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Der MainFrame dient als Hub und Übergreifendes Fenster auf dem alle weiteren Panel angezeigt werden.
|
|
||||||
* Dadurch werden keine weiteren Fenster geöffnet.
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class MainFrame extends JFrame {
|
|
||||||
|
|
||||||
private CardLayout cardLayout;
|
|
||||||
private JPanel mainPanel;
|
|
||||||
|
|
||||||
// ---------- //
|
|
||||||
// Diverse Hilfsvariablen (für Parameterübergabe etc.)
|
|
||||||
// Von startMultiplayerGame an JoinGame
|
|
||||||
int localMult;
|
|
||||||
|
|
||||||
// Von startLocalGame an startLocalGameLoadingScreen
|
|
||||||
// Von startLocalGameLoadingScreen an GameBoard
|
|
||||||
int semesterCounter;
|
|
||||||
// ---------- //
|
|
||||||
|
|
||||||
private GameBoard gameBoard;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor von MainFrame.
|
|
||||||
* Ermöglicht es Panel anzuzeigen.
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public MainFrame() {
|
|
||||||
|
|
||||||
GameController.setMainFrame(this);
|
|
||||||
|
|
||||||
setTitle("Studium Versenken");
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
setSize(1500, 1000);
|
|
||||||
setLocationRelativeTo(null);
|
|
||||||
|
|
||||||
// CardLayout und Hauptpanel erstellen
|
|
||||||
cardLayout = new CardLayout();
|
|
||||||
mainPanel = new JPanel(cardLayout);
|
|
||||||
|
|
||||||
// Panels erstellen
|
|
||||||
MainMenuView mainMenuView = new MainMenuView(this);
|
|
||||||
startLocalGame localGame = new startLocalGame(this);
|
|
||||||
startMultiplayerGame multiplayerGame = new startMultiplayerGame(this);
|
|
||||||
Verbinden verbinden = new Verbinden(this);
|
|
||||||
|
|
||||||
// Panels hinzufügen
|
|
||||||
mainPanel.add(mainMenuView, "MainMenu");
|
|
||||||
mainPanel.add(localGame, "LocalGame");
|
|
||||||
mainPanel.add(multiplayerGame, "MultiplayerGame");
|
|
||||||
mainPanel.add(verbinden, "Verbinden");
|
|
||||||
// mainPanel.add(winLooseScreen, "WinLooseScreen");
|
|
||||||
|
|
||||||
// Hauptpanel in JFrame hinzufügen
|
|
||||||
add(mainPanel);
|
|
||||||
|
|
||||||
// Hauptmenü anzeigen
|
|
||||||
cardLayout.show(mainPanel, "MainMenu");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Methode, um die Ansicht zu wechseln
|
|
||||||
* @param panelName Name des anzuzeigenden Panels
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void showPanel(String panelName) {
|
|
||||||
cardLayout.show(mainPanel, panelName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spezifische ShowPanel-Funktion der startMultiplayerGame Klasse
|
|
||||||
* @param panelName Name des anzuzeigenden Panels
|
|
||||||
* @param num Hilfsvariable um abzugleichen, ob "Spiel erstellen" oder "Spiel beitreten" ausgewählt wurde
|
|
||||||
* @param playerType Spielertyp(HumanPlayer, AIEasy etc.)
|
|
||||||
* @param playerName Name des Spielers
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void showPanelSMG(String panelName, int num, int playerType,String playerName, int semesterCounter) {
|
|
||||||
this.localMult = num;
|
|
||||||
|
|
||||||
JoinGame joinGame = new JoinGame(this, localMult, playerType, playerName, semesterCounter);
|
|
||||||
mainPanel.add(joinGame, panelName);
|
|
||||||
mainPanel.revalidate();
|
|
||||||
mainPanel.repaint();
|
|
||||||
cardLayout.show(mainPanel, panelName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spezifische ShowPanel der startLocalGameLoadingScreen Klasse (DURCH BACKEND AUFGERUFEN)
|
|
||||||
* @param panelName Name des anzuzeigenden Panels
|
|
||||||
* @param semesterCounter Ausgewähltes Semester
|
|
||||||
* @param p1 Erstes Spielerobjekt
|
|
||||||
* @param p2 Zweites Spielerobjekt
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void showPanelSLG(String panelName,int semesterCounter, Player p1, Player p2) {
|
|
||||||
this.semesterCounter = semesterCounter;
|
|
||||||
|
|
||||||
this.gameBoard = new GameBoard(this, semesterCounter, p1, p2);
|
|
||||||
mainPanel.add(gameBoard, panelName);
|
|
||||||
mainPanel.revalidate();
|
|
||||||
mainPanel.repaint();
|
|
||||||
cardLayout.show(mainPanel, panelName); // Panel anzeigen
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spezifische ShowPanel der startLocalGame Klasse
|
|
||||||
* @param panelName Name des anzuzeigenden Panels
|
|
||||||
* @param semesterCounter Ausgewähltes Semester
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void showPanelSLGLS(String panelName,int semesterCounter) {
|
|
||||||
this.semesterCounter = semesterCounter;
|
|
||||||
|
|
||||||
startLocalGameLoadingScreen LocalGameLoadingScreen = new startLocalGameLoadingScreen(this, semesterCounter);
|
|
||||||
mainPanel.add(LocalGameLoadingScreen, panelName);
|
|
||||||
mainPanel.revalidate();
|
|
||||||
mainPanel.repaint();
|
|
||||||
cardLayout.show(mainPanel, panelName); // Panel anzeigen
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spezifische ShowPanel für WinScreen Klasse
|
|
||||||
* @param panelName Name des anzuzeigenden Panels
|
|
||||||
* @param player Player von dem die funktion aufgerufen worden ist
|
|
||||||
* @author Lucas Bronson, Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public void showPanelWin(String panelName, Player player){
|
|
||||||
if(gameBoard == null || player != gameBoard.getP1()){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.gameBoard.getP1().destroy();
|
|
||||||
this.gameBoard.getP2().destroy();
|
|
||||||
this.gameBoard.removeAll();
|
|
||||||
this.gameBoard = null;
|
|
||||||
|
|
||||||
WinScreen winScreen = new WinScreen(this);
|
|
||||||
mainPanel.add(winScreen, panelName);
|
|
||||||
mainPanel.revalidate();
|
|
||||||
mainPanel.repaint();
|
|
||||||
cardLayout.show(mainPanel, panelName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spezifische ShowPanel für LooseScreen Klasse
|
|
||||||
* @param panelName Name des anzuzeigenden Panels
|
|
||||||
* @param player Player von dem die funktion aufgerufen worden ist
|
|
||||||
* @author Lucas Bronson, Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public void showPanelLose(String panelName, Player player){
|
|
||||||
if(gameBoard == null || player != gameBoard.getP1()){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.gameBoard.getP1().destroy();
|
|
||||||
this.gameBoard.getP2().destroy();
|
|
||||||
this.gameBoard.removeAll();
|
|
||||||
this.gameBoard = null;
|
|
||||||
|
|
||||||
LoseScreen looseScreen = new LoseScreen(this);
|
|
||||||
mainPanel.add(looseScreen,panelName);
|
|
||||||
mainPanel.revalidate();
|
|
||||||
mainPanel.repaint();
|
|
||||||
cardLayout.show(mainPanel, panelName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aktualisiert das Spielfeld
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void refreshGameBoard() {
|
|
||||||
if(this.gameBoard == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.gameBoard.refresh();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class MainMenuController implements ActionListener {
|
||||||
|
private MainMenuView view;
|
||||||
|
private MainMenuModel model;
|
||||||
|
|
||||||
|
public MainMenuController(MainMenuModel model, MainMenuView view) {
|
||||||
|
this.view = view;
|
||||||
|
this.model = model;
|
||||||
|
this.view.getLocalButton().addActionListener(this);
|
||||||
|
this.view.getMultiButton().addActionListener(this);
|
||||||
|
this.view.getSoundButton().addActionListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (e.getSource() == view.getMultiButton()) {
|
||||||
|
view.closeWindOw();
|
||||||
|
MultiMenuModel model = new MultiMenuModel();
|
||||||
|
MultiMenuView view = new MultiMenuView();
|
||||||
|
MultiMenuController controller2 = new MultiMenuController(model, view);
|
||||||
|
} else if (e.getSource() == view.getMultiButton()) {
|
||||||
|
model.setGameMode("Multiplayer");
|
||||||
|
JOptionPane.showMessageDialog(view.getFrame(), "Multiplayer game selected.");
|
||||||
|
}else if (e.getSource() == view.getSoundButton()) {
|
||||||
|
view.toggleMute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
public class MainMenuModel {
|
||||||
|
private String gameMode;
|
||||||
|
|
||||||
|
public void setGameMode(String mode) {
|
||||||
|
this.gameMode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGameMode() {
|
||||||
|
return gameMode;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,95 +1,73 @@
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import javax.swing.JFrame;
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
/**
|
public class MainMenuView {
|
||||||
* Klasse zum Erstellen von MainMenu Objekten
|
ImageIcon SoundIcon = new ImageIcon("Grafik/sound button.png");
|
||||||
* Dient als Hauptmenü für die Anwendung
|
private JFrame frame = new JFrame();
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public class MainMenuView extends JPanel {
|
|
||||||
|
|
||||||
private JLabel titelLabel = new JLabel("Studium versenken");
|
private JLabel titelLabel = new JLabel("Studium versenken");
|
||||||
private JButton lokalButton = new JButton("Lokal");
|
private JButton lokalButton = new JButton("Lokal");
|
||||||
private JButton multiButton = new JButton("Multiplayer");
|
private JButton multiButton= new JButton("Multiplayer");
|
||||||
private JButton soundButton;
|
private JButton soundButton = new JButton(SoundIcon);
|
||||||
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
||||||
private ImageIcon soundIcon = new ImageIcon("graphics/sound button.png");
|
public MainMenuView() {
|
||||||
private ImageIcon muteIcon = new ImageIcon("graphics/sound button muted.png");
|
buildFrame();
|
||||||
|
buildComponents();
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor der MainMenuView Klasse
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public MainMenuView(MainFrame frame) {
|
|
||||||
setLayout(null);
|
|
||||||
buildPanel(frame);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Methode zum Füllen des Panels, Objekte werden dem frame hinzugefügt
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
private void buildPanel(MainFrame frame) {
|
|
||||||
lokalButton.setBounds(200, 200, 500, 500);
|
|
||||||
multiButton.setBounds(800, 200, 500, 500);
|
|
||||||
titelLabel.setBounds(550, 10, 700, 100);
|
|
||||||
soundButton = new JButton(soundIcon);
|
|
||||||
soundButton.setBounds(20, 20, 128, 128);
|
|
||||||
|
|
||||||
|
public void buildComponents(){
|
||||||
|
lokalButton.setBounds(200,200,500,500);
|
||||||
|
multiButton.setBounds(800,200,500,500);
|
||||||
|
titelLabel.setBounds(550,10,700,100);
|
||||||
|
soundButton.setBounds(20,20,128,128);
|
||||||
titelLabel.setFont(robotoFont);
|
titelLabel.setFont(robotoFont);
|
||||||
lokalButton.setFont(robotoFont.deriveFont(50f));
|
lokalButton.setFont(robotoFont.deriveFont(50f));
|
||||||
multiButton.setFont(robotoFont.deriveFont(50f));
|
multiButton.setFont(robotoFont.deriveFont(50f));
|
||||||
|
|
||||||
add(titelLabel);
|
|
||||||
add(lokalButton);
|
|
||||||
add(multiButton);
|
|
||||||
add(soundButton);
|
|
||||||
|
|
||||||
// ActionListener um vom Hauptmenü zum LocalGame Menü zu wechseln
|
|
||||||
lokalButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
System.out.println("Lokales Spiel ausgewählt.");
|
|
||||||
frame.showPanel("LocalGame"); // Panel wechseln
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// ActionListener um vom Hauptmenü zum MultiPlayerGame Menü zu wechseln
|
|
||||||
multiButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
System.out.println("Multiplayer-Spiel ausgewählt.");
|
|
||||||
frame.showPanel("MultiplayerGame"); // Panel wechseln
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//Aufruf von toggleMute, falls der soundButton geklickt wird
|
|
||||||
soundButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
System.out.println("Soundbutton geklickt.");
|
|
||||||
toggleMute();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void buildFrame() {
|
||||||
* Setzt Sound auf Stumm/Laut und ändert Grafik, sodass
|
frame.setLayout(null);
|
||||||
* der aktuelle Stand der Grafik entspricht
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
frame.setSize(1500,1000);
|
||||||
*/
|
frame.setVisible(true);
|
||||||
private void toggleMute() {
|
frame.getContentPane().setBackground( Color.decode("#98F5FF"));
|
||||||
if (soundButton.getIcon() == soundIcon) {
|
JLabel backgroundLabel = new JLabel(new ImageIcon("Grafik/mainmenubackground.png"));
|
||||||
soundButton.setIcon(muteIcon);
|
frame.setContentPane(backgroundLabel);
|
||||||
SoundHandler.setSoundOn(false);
|
frame.add(titelLabel);
|
||||||
} else {
|
frame.add(lokalButton);
|
||||||
soundButton.setIcon(soundIcon);
|
frame.add(multiButton);
|
||||||
SoundHandler.setSoundOn(true);
|
frame.add(soundButton);
|
||||||
|
frame.setLocationRelativeTo(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toggleMute(){
|
||||||
|
ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png");
|
||||||
|
if(soundButton.getIcon()==SoundIcon) {
|
||||||
|
soundButton.setIcon(MuteIcon);
|
||||||
|
}else{
|
||||||
|
soundButton.setIcon(SoundIcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void closeWindOw(){
|
||||||
|
frame.dispose();
|
||||||
|
}
|
||||||
|
public JFrame getFrame() {
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JButton getLocalButton() {
|
||||||
|
return lokalButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JButton getMultiButton() {
|
||||||
|
return multiButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JButton getSoundButton() {
|
||||||
|
return soundButton;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class MultiMenuController implements ActionListener {
|
||||||
|
private MultiMenuView view;
|
||||||
|
private MultiMenuModel model;
|
||||||
|
|
||||||
|
public MultiMenuController(MultiMenuModel model, MultiMenuView view) {
|
||||||
|
this.view = view;
|
||||||
|
this.model = model;
|
||||||
|
this.view.getSoundButton().addActionListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (e.getSource() == view.getSoundButton()) {
|
||||||
|
view.toggleMute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
public class MultiMenuModel {
|
||||||
|
private String gameMode;
|
||||||
|
|
||||||
|
public void setGameMode(String mode) {
|
||||||
|
this.gameMode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGameMode() {
|
||||||
|
return gameMode;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class MultiMenuView {
|
||||||
|
ImageIcon SoundIcon = new ImageIcon("Grafik/sound button.png");
|
||||||
|
private JFrame frame = new JFrame();
|
||||||
|
private JButton soundButton = new JButton(SoundIcon);
|
||||||
|
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
||||||
|
public MultiMenuView() {
|
||||||
|
buildFrame();
|
||||||
|
buildComponents();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void buildComponents(){
|
||||||
|
soundButton.setBounds(20,20,128,128);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void buildFrame() {
|
||||||
|
frame.setLayout(null);
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setSize(1500,1000);
|
||||||
|
frame.setVisible(true);
|
||||||
|
frame.getContentPane().setBackground( Color.decode("#98F5FF"));
|
||||||
|
JLabel backgroundLabel = new JLabel(new ImageIcon("Grafik/mainmenubackground.png"));
|
||||||
|
frame.setContentPane(backgroundLabel);
|
||||||
|
frame.add(soundButton);
|
||||||
|
frame.setLocationRelativeTo(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toggleMute(){
|
||||||
|
ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png");
|
||||||
|
if(soundButton.getIcon()==SoundIcon) {
|
||||||
|
soundButton.setIcon(MuteIcon);
|
||||||
|
}else{
|
||||||
|
soundButton.setIcon(SoundIcon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void closeWindOw(){
|
||||||
|
frame.dispose();
|
||||||
|
}
|
||||||
|
public JFrame getFrame() {
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JButton getSoundButton() {
|
||||||
|
return soundButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,57 +1,5 @@
|
||||||
/**
|
import java.net.Socket;
|
||||||
* Abstrakte Repräsentation eines Spielers, der an einem anderen gerät spielt
|
|
||||||
* und durch eine Online Verbindung mit dieser Instanz kommuniziert
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public abstract class OnlinePlayer extends Player implements AsyncSocketListener {
|
|
||||||
protected AsyncSocket socket;
|
|
||||||
protected int wantedBoardSize;
|
|
||||||
|
|
||||||
protected boolean hasReceivedCoinPackage;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param size the size of the board the enemy player wants to play with
|
|
||||||
* the actual board size will be determined once the semester of the online partner is known
|
|
||||||
* @param socket an AsyncSocket to communicate with the enemy through
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public OnlinePlayer(Integer size, AsyncSocket socket) {
|
|
||||||
this.socket = socket;
|
|
||||||
this.wantedBoardSize = size;
|
|
||||||
this.myCoin = null;
|
|
||||||
socket.setHandler(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends the IAM Package for introduction
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public abstract void sendIAM();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a message from the AsyncSocket
|
|
||||||
* satisfies AsyncSocketListener interface
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public abstract void receive(String message);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives the coin toss result from the enemy
|
|
||||||
* @param coin the result of the coin toss
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public abstract void receiveCoin(boolean coin);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* closes the socket and does player cleanup work
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
super.destroy();
|
|
||||||
this.socket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public abstract class OnlinePlayer extends Player{
|
||||||
|
private Socket socket;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,188 +0,0 @@
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* eine Implementierung des Kommunikationsprotokoll nach Version 1.1.0 des Netzwerkstandards
|
|
||||||
* https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt einen Online Player
|
|
||||||
* @param size die maximale Größe des Spielfelds
|
|
||||||
* @param socket der Socket zur Kommunikation mit dem Online Partner
|
|
||||||
*/
|
|
||||||
public OnlinePlayer_1_1_0(Integer size, AsyncSocket socket) {
|
|
||||||
super(size, socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a message from the AsyncSocket
|
|
||||||
* implemented according to version 1.1.0 of https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* @param message the message from the socket
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte, Florian Hantzschel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void receive(String message) {
|
|
||||||
SocketPackage p = new SocketPackage(message);
|
|
||||||
|
|
||||||
List<String> data = p.splitData();
|
|
||||||
switch (p.getName()) {
|
|
||||||
case "IAM":
|
|
||||||
if (data.size() < 2) break;
|
|
||||||
if (this.board != null) break;
|
|
||||||
|
|
||||||
int semester = Integer.parseInt(data.get(0));
|
|
||||||
String username = p.getData().substring(data.get(0).length() + 1);
|
|
||||||
|
|
||||||
int usedBoardSize = Math.min(GameController.semesterToBoardSize(semester), this.wantedBoardSize);
|
|
||||||
|
|
||||||
this.setName(username);
|
|
||||||
this.createBoard(usedBoardSize);
|
|
||||||
this.enemy.createBoard(usedBoardSize);
|
|
||||||
|
|
||||||
GameController.startGameWithInstancedPlayers((LocalPlayer)this.enemy, (Player)this, usedBoardSize);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
// TODO: IAMU
|
|
||||||
|
|
||||||
case "COIN":
|
|
||||||
if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
|
|
||||||
this.myCoin = p.getData().equals("1");
|
|
||||||
this.hasReceivedCoinPackage = true;
|
|
||||||
this.ready();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
|
||||||
case "SHOOT":
|
|
||||||
if (Point.isValidSyntax(p.getData())){
|
|
||||||
Point point = new Point(p.getData());
|
|
||||||
this.enemy.receiveShoot(point);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "HIT":
|
|
||||||
if (data.size()==2){
|
|
||||||
Point point = new Point(data.get(0));
|
|
||||||
int typeIndex = Integer.parseInt(data.get(1));
|
|
||||||
if (Point.isValidSyntax(data.get(0)) && typeIndex >= 0 && typeIndex < HitResponseType.values().length){
|
|
||||||
|
|
||||||
HitResponse hitResponse = new HitResponse(typeIndex, point);
|
|
||||||
|
|
||||||
this.enemy.receiveHit(hitResponse);
|
|
||||||
|
|
||||||
switch (hitResponse.getType()) {
|
|
||||||
case HIT, SUNK:
|
|
||||||
this.myTurn = false;
|
|
||||||
break;
|
|
||||||
case MISS:
|
|
||||||
this.myTurn = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VICTORY:
|
|
||||||
// GameController.getMainFrame().showPanelWin("", this.enemy);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "CHAT":
|
|
||||||
//TODO CHAT
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "WITHDRAW":
|
|
||||||
this.withdraw();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
//nichts passier da Paket ungültig
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends introduction package IAM to online partner.
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void sendIAM() {
|
|
||||||
if (this.enemy == null) throw new RuntimeException("enemy has not yet been defined");
|
|
||||||
socket.send(new SocketPackage("IAM", GameController.boardSizeToSemester(this.wantedBoardSize) + " " + this.enemy.name));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a shot from the enemy and sends it to the online partner
|
|
||||||
* if it is not the enemies turn, this method does nothing.
|
|
||||||
* @param point the point to be shot
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveShoot(Point point){
|
|
||||||
if (!this.enemy.myTurn) return;
|
|
||||||
super.socket.send(new SocketPackage("SHOOT",point.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a hitresponse from the enemy player and sends it to the online partner
|
|
||||||
* @param hitResponse the hitresponse to be sent
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
|
||||||
switch (hitResponse.getType()) {
|
|
||||||
case HIT, SUNK:
|
|
||||||
this.myTurn = true;
|
|
||||||
break;
|
|
||||||
case MISS:
|
|
||||||
this.myTurn = false;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VICTORY:
|
|
||||||
// GameController.getMainFrame().showPanelLose("", this.enemy);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives the coin toss result from the enemy player and sends it to the online partner
|
|
||||||
* if this player has already received the enemies coin, this method does nothing.
|
|
||||||
* @param coin the result of the coin toss
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
|
||||||
if (!this.hasReceivedCoin) {
|
|
||||||
super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
|
|
||||||
this.hasReceivedCoin = true;
|
|
||||||
this.determineCoinToss();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* für Online Player nicht zu implementieren
|
|
||||||
* nur für HumanPlayer relevant
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void shoot(Point point) {
|
|
||||||
// SHOULD NEVER BE CALLED ON ONLINE PLAYER. ONLY ON HUMAN PLAYER
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* empfängt Withdraw vom Gegner und leitet es an den Online Partner weiter
|
|
||||||
* beendet das Spiel
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void receiveWithdraw() {
|
|
||||||
this.socket.send(new SocketPackage("WITHDRAW"));
|
|
||||||
super.receiveWithdraw();
|
|
||||||
}
|
|
||||||
}
|
|
211
src/Player.java
|
@ -1,211 +1,26 @@
|
||||||
/**
|
import java.awt.*;
|
||||||
* abstrakte repräsentation eines Spielers
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte, Lucas Bronson
|
|
||||||
*/
|
|
||||||
public abstract class Player {
|
public abstract class Player {
|
||||||
protected boolean myTurn;
|
private boolean myTurn;
|
||||||
protected boolean isServer;
|
private boolean isServer;
|
||||||
protected boolean waitingForResponse;
|
private boolean waitingForResponse;
|
||||||
protected Player enemy;
|
private Player enemy;
|
||||||
protected String name;
|
private String name;
|
||||||
protected Board board;
|
private Board board;
|
||||||
protected Boolean myCoin;
|
|
||||||
protected boolean gameRunning;
|
|
||||||
|
|
||||||
protected boolean sentCoin;
|
public void receiveShoot(Point point) {
|
||||||
|
|
||||||
protected boolean hasReceivedCoin;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public Player() {
|
|
||||||
this.setName("Player");
|
|
||||||
this.hasReceivedCoin = false;
|
|
||||||
this.sentCoin = false;
|
|
||||||
this.myTurn = false;
|
|
||||||
this.gameRunning = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void receiveHit(HitResponse hitResponse) {
|
||||||
* initialises this players board
|
|
||||||
* @param size the size of the board to be created
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void createBoard(int size) {
|
|
||||||
this.board = new Board(size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void click(Point point) {
|
||||||
* receives a shot onto a point from the enemy
|
|
||||||
* @param point the location to be shot
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public abstract void receiveShoot(Point point);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a hit response from the enemy as a response to a receiveShoot call
|
|
||||||
* @param hitResponse the hitresponse
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public abstract void receiveHit(HitResponse hitResponse);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends shot to enemy player.
|
|
||||||
* should ONLY be called on HumanPlayer
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public abstract void shoot(Point point);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* only relevant for AI Players.
|
|
||||||
* starts the first turn
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void beginTurn() {
|
|
||||||
System.out.println("issa my turn-a");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void beginTrun() {
|
||||||
* sets the enemy Player
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setEnemy(Player enemy) {
|
|
||||||
this.enemy = enemy;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the name of this player
|
|
||||||
* @param name the name of this player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the name of this player
|
|
||||||
* @return the name of this player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the board of this player
|
|
||||||
* @return the board of this player
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public Board getBoard() {
|
|
||||||
return this.board;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* marks the player as ready by sending their coin to the enemy player
|
|
||||||
* calls determineCoinToss if the enemy coin has already been received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void ready() {
|
|
||||||
this.enemy.receiveCoin(this.myCoin);
|
|
||||||
this.sentCoin = true;
|
|
||||||
if (hasReceivedCoin) {
|
|
||||||
this.determineCoinToss();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* determines the result of the coin toss
|
|
||||||
* this method does nothing if either player is not ready yet or has not yet sent their coin
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
protected void determineCoinToss() {
|
|
||||||
if (!this.sentCoin || this.myCoin == null || !this.hasReceivedCoin || this.enemy.myCoin == null) return;
|
|
||||||
boolean result = this.enemy.myCoin ^ this.myCoin; // XOR
|
|
||||||
this.myTurn = result == this.isServer;
|
|
||||||
if (this.myTurn) {
|
|
||||||
this.beginTurn();
|
|
||||||
}
|
|
||||||
this.gameRunning = true;
|
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives the coin toss from the enemy player
|
|
||||||
* @param coin the coin of the enemy player
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public abstract void receiveCoin(boolean coin);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether the game this player is in has started, meaning both players are ready and have sent their coins
|
|
||||||
* @return the game's running state
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean isGameRunning() {
|
|
||||||
return this.gameRunning;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether this player is ready and has sent their coin to the enemy player
|
|
||||||
* @return the player's ready state
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public boolean isReady() {
|
|
||||||
return this.sentCoin;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* removes connections to the enemy and its board as well as setting myTurn and gameRunning to false
|
|
||||||
* this stops the AI Players from making more moves and allows the garbage collector to remove the boards
|
|
||||||
* and players
|
|
||||||
*
|
|
||||||
* This method should be called at the end of a game
|
|
||||||
*
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void destroy() {
|
|
||||||
this.myTurn = false;
|
|
||||||
this.gameRunning = false;
|
|
||||||
this.board = null;
|
|
||||||
this.enemy = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the player wins
|
|
||||||
* ends the game
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void win() {
|
|
||||||
GameController.getMainFrame().showPanelWin("", this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the player loses
|
|
||||||
* ends the game
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void lose() {
|
|
||||||
GameController.getMainFrame().showPanelLose("", this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the player loses by withdrawal
|
|
||||||
* ends the game
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void withdraw() {
|
|
||||||
this.enemy.receiveWithdraw();
|
|
||||||
this.lose();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the enemy player withdraws
|
|
||||||
* ends the game
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void receiveWithdraw(){
|
|
||||||
this.win();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
131
src/Point.java
|
@ -1,131 +0,0 @@
|
||||||
/**
|
|
||||||
* repräsentation der Koordinaten eines Feldes auf dem Spielfeld
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public class Point {
|
|
||||||
private int x;
|
|
||||||
private int y;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a point using X and Y coordinates starting at 0
|
|
||||||
* @param x the x coordinate of the point starting at 0
|
|
||||||
* @param y the y coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public Point (int x, int y) {
|
|
||||||
this.setX(x);
|
|
||||||
this.setY(y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a Point using a coordinate provided in the format of a letter followed by a number
|
|
||||||
* this coordinate is checked using `isValidSyntax`
|
|
||||||
* If the coordinate is not in a valid syntax, an `IllegalArgumentException` will be thrown, stating as such
|
|
||||||
* The number part of the coordinate starts at 1 instead of 0 so for example, the
|
|
||||||
* string A1 will result in the X and Y coordinates of (0, 0)
|
|
||||||
* @param str the coordinate in alphanumeric format
|
|
||||||
* @throws IllegalArgumentException if the coordinate is invalid according to `isValidSyntax`
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public Point (String str) {
|
|
||||||
if (Point.isValidSyntax(str)) {
|
|
||||||
this.setX(str.charAt(0));
|
|
||||||
this.setY(Integer.parseInt(str.substring(1)) - 1);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("String ist keine gültige Koordinate");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns this point as a string in its alphanumeric format
|
|
||||||
* @return this point as a string in its alphanumeric format
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return (char) ('A' + this.x) + String.valueOf(this.y + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the X coordinate of the point starting at 0
|
|
||||||
* @return the X coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public int getX() {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the Y coordinate of the point starting at 0
|
|
||||||
* @return the Y coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public int getY() {
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the X coordinate of the point starting at 0
|
|
||||||
* @param x the X coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setX(int x) {
|
|
||||||
this.x = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the Y coordinate of the point starting at 0
|
|
||||||
* @param y the Y coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setY(int y) {
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the X coordinate of the from its character value in alphanumeric form
|
|
||||||
* @param c the character to be transformed into
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setX(char c) {
|
|
||||||
this.x = c - 'A';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* checks whether a string is a valid alphanumeric point coordinate
|
|
||||||
* @param str the string to be tested
|
|
||||||
* @return whether the string is valid according to the regular expression `^[A-Z]\\d+$`
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public static boolean isValidSyntax(String str) {
|
|
||||||
return str.matches("^[A-Z]\\d+$");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether two points are equal
|
|
||||||
* two points with equivalent coordinates are considered equal
|
|
||||||
* @param o the other object/Point to compare this one to
|
|
||||||
* @return whether the objects are equal
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || this.getClass() != o.getClass()) return false;
|
|
||||||
|
|
||||||
Point p = (Point)o;
|
|
||||||
return p.getX() == this.getX() && p.getY() == this.getY();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* determines whether two points are neighbours
|
|
||||||
* points are considered neighbours if their positions are equal or within a difference of 1 on both X and Y axis
|
|
||||||
* @param other the point to check for neighbourship
|
|
||||||
* @return whether the points are neighbours
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean neighbours(Point other) {
|
|
||||||
if (other == null) return false;
|
|
||||||
return (int)Math.abs(this.getX() - other.getX()) <= 1 && (int)Math.abs(this.getY() - other.getY()) <= 1;
|
|
||||||
}
|
|
||||||
}
|
|
284
src/Ship.java
|
@ -1,284 +0,0 @@
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
record ShipData (int size, String name){}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* repräsentation eines Schiffes auf dem Spielfeld
|
|
||||||
* @author Peer Ole Wachtel, Lucas Bronson, Florian Hanzschel
|
|
||||||
*/
|
|
||||||
public class Ship {
|
|
||||||
static List<List<ShipData>> semeterList =
|
|
||||||
Arrays.asList(
|
|
||||||
Arrays.asList(
|
|
||||||
new ShipData(2, "PRG 1"),
|
|
||||||
new ShipData(2, "GDI"),
|
|
||||||
new ShipData(2, "MAT 1"),
|
|
||||||
new ShipData(2, "THI"),
|
|
||||||
new ShipData(4, "STP"),
|
|
||||||
new ShipData(6, "ENG")
|
|
||||||
),
|
|
||||||
Arrays.asList(
|
|
||||||
new ShipData(2, "PRG 2"),
|
|
||||||
new ShipData(2, "DBS 1"),
|
|
||||||
new ShipData(2, "MAT 2"),
|
|
||||||
new ShipData(2, "STA"),
|
|
||||||
new ShipData(2, "AUD")
|
|
||||||
),
|
|
||||||
Arrays.asList(
|
|
||||||
new ShipData(2, "PRG 3"),
|
|
||||||
new ShipData(2, "DBS 2"),
|
|
||||||
new ShipData(2, "MAT 3"),
|
|
||||||
new ShipData(2, "BSN 1"),
|
|
||||||
new ShipData(4, "PRP"),
|
|
||||||
new ShipData(6, "BWL")
|
|
||||||
),
|
|
||||||
Arrays.asList(
|
|
||||||
new ShipData(2, "WEB"),
|
|
||||||
new ShipData(2, "SE 1"),
|
|
||||||
new ShipData(2, "CG 1"),
|
|
||||||
new ShipData(2, "BSN 2"),
|
|
||||||
new ShipData(4, "SEM"),
|
|
||||||
new ShipData(6, "E BWL")
|
|
||||||
),
|
|
||||||
Arrays.asList(
|
|
||||||
new ShipData(2, "WPF 1"),
|
|
||||||
new ShipData(2, "SE 2"),
|
|
||||||
new ShipData(2, "CG 2"),
|
|
||||||
new ShipData(2, "PXP 1"),
|
|
||||||
new ShipData(6, "EF 1")
|
|
||||||
),
|
|
||||||
Arrays.asList(
|
|
||||||
new ShipData(2, "WPF 2"),
|
|
||||||
new ShipData(1, "PXP 2"),
|
|
||||||
new ShipData(8, "BAA")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
private int size;
|
|
||||||
private boolean horizontal;
|
|
||||||
private Point position;
|
|
||||||
private String name;
|
|
||||||
private int hitsOnMe;
|
|
||||||
private boolean sunk;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a Ship with a given size and name
|
|
||||||
* @param size the size of the ship
|
|
||||||
* @param name the name of the ship
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public Ship (int size, String name) {
|
|
||||||
this.size = size;
|
|
||||||
this.name = name;
|
|
||||||
this.horizontal = false; //true = von Punkt aus nach rechts; false = von Punkt aus nach unten
|
|
||||||
this.position = null;
|
|
||||||
this.hitsOnMe = 0;
|
|
||||||
this.sunk = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* resets the position of this ship
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void resetPosition() {
|
|
||||||
this.position = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the position of this ship, provided it is valid
|
|
||||||
* @param pos the position to move the ship to
|
|
||||||
* @param horizontal whether the ship is horizontal or not
|
|
||||||
* @param shipsList the list of other ships on this board. It will be checked whether the ship is touching any of them
|
|
||||||
* @param boardSize the size of the board the ship is to be placed on
|
|
||||||
* @return true if the position was set successfully. false if the ship is out of the bounds of the board or touches a different ship
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean setPosition(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
|
||||||
if (!this.checkValidPlacement(pos, horizontal, shipsList, boardSize)) return false;
|
|
||||||
|
|
||||||
// kein ueberlappen also setze das Schiff
|
|
||||||
this.position = pos;
|
|
||||||
this.horizontal = horizontal;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* checks whether a position is valid for ship placement
|
|
||||||
* @param pos the position to check the ship placement at
|
|
||||||
* @param horizontal whether the ship is to be placed horizontally or not
|
|
||||||
* @param shipsList the list of other ships on this board. It will be checked whether the ship is touching any of them
|
|
||||||
* @param boardSize the size of the board the ship is to be placed on
|
|
||||||
* @return true if the position is valid. false if the ship is out of the bounds of the board or touches a different ship
|
|
||||||
* @author Florian Hantzschel, Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean checkValidPlacement(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
|
||||||
// ueberpruefe boundaries
|
|
||||||
if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// bestimme die Endposition anhand der Ausrichtung
|
|
||||||
int endX = pos.getX();
|
|
||||||
int endY = pos.getY();
|
|
||||||
|
|
||||||
if (horizontal) { // rechts links
|
|
||||||
endX = pos.getX() + this.size - 1;
|
|
||||||
if (endX >= boardSize) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else { // oben unten
|
|
||||||
endY = pos.getY() + this.size - 1;
|
|
||||||
if (endY >= boardSize) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Liste an Punkten die das Schiff einnehmen wuerde
|
|
||||||
List<Point> shipPoints = this.getVirtualOccupiedPoints(pos, horizontal);
|
|
||||||
|
|
||||||
// ueberlappen mit anderen Schiffen pruefen
|
|
||||||
for (Ship otherShip : shipsList) {
|
|
||||||
// eigenes Schiff ueberspringen
|
|
||||||
if (otherShip == this) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// ueberspringe falls noch nicht gesetzt
|
|
||||||
if (otherShip.position == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Punkte die das andere Schiff besetzt
|
|
||||||
List<Point> otherShipPoints = otherShip.getOccupiedPoints();
|
|
||||||
// ueberlappen checken
|
|
||||||
for (Point p : shipPoints) {
|
|
||||||
for (Point otherPoint : otherShipPoints) {
|
|
||||||
if (otherPoint.neighbours(p)) return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Points on the ship if it were to be placed at position `pos` in orientation defined by `horizontal`
|
|
||||||
* @param pos the position where the ship should be placed
|
|
||||||
* @param horizontal whether the ship should be placed horizontally
|
|
||||||
* @return a list of points the ship would occupy, were it placed at position `pos` in orientation `horizontal`
|
|
||||||
* @author Florian Hantzschel, Luca Conte
|
|
||||||
*/
|
|
||||||
public List<Point> getVirtualOccupiedPoints(Point pos, boolean horizontal) {
|
|
||||||
List<Point> points = new ArrayList<>();
|
|
||||||
if (pos == null) {
|
|
||||||
return points;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < this.size; i++) {
|
|
||||||
int x = horizontal ? pos.getX() + i : pos.getX();
|
|
||||||
int y = horizontal ? pos.getY() : pos.getY() + i;
|
|
||||||
points.add(new Point(x, y));
|
|
||||||
}
|
|
||||||
return points;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Points the ship occupies
|
|
||||||
* @return a list of points the ship occupies
|
|
||||||
* @author Florian Hantzschel, Luca Conte
|
|
||||||
*/
|
|
||||||
public List<Point> getOccupiedPoints() {
|
|
||||||
return this.getVirtualOccupiedPoints(this.position, this.horizontal);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the position of this ship
|
|
||||||
* @return the position of this ship
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public Point getPosition() {
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* checks whether the ship occupies a certain point
|
|
||||||
* @param pos the point to be checkd
|
|
||||||
* @return whether the point provided is one of the points occupied by the ship
|
|
||||||
* @author Peer Ole Wachtel, Lucas Bronson
|
|
||||||
*/
|
|
||||||
public boolean isShipOnPos(Point pos){
|
|
||||||
if(this.position == null){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ((this.horizontal && pos.getY() == this.position.getY() && pos.getX() >= this.position.getX() && pos.getX() < this.position.getX() + size) ||
|
|
||||||
(!(this.horizontal) && pos.getX() == this.position.getX() && pos.getY() >= this.position.getY() && pos.getY() < this.position.getY() + size)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* "shoots" this ship.
|
|
||||||
* @return a hit response, depending on whether the ship was hit or not. If the amount of times
|
|
||||||
* the ship was hit is greater or equal to the size of the ship, the ship is considered sunk.
|
|
||||||
* @param pos the point where the ship is shot
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public HitResponseType shootOnShip(Point pos) {
|
|
||||||
if (this.isShipOnPos(pos)) {
|
|
||||||
hitsOnMe++;
|
|
||||||
if (hitsOnMe >= size) {
|
|
||||||
this.sunk = true;
|
|
||||||
return HitResponseType.SUNK;
|
|
||||||
} else {
|
|
||||||
return HitResponseType.HIT;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return HitResponseType.MISS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether the ship has been sunk or not
|
|
||||||
* @return whether the ship has been sunk or not
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public boolean isSunk() {
|
|
||||||
return sunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the orientation of the ship
|
|
||||||
* @param horizontal whether the ship is to be placed in a horizontal orientation
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public void setHorizontal(boolean horizontal) {
|
|
||||||
this.horizontal = horizontal;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the size of the ship
|
|
||||||
* @return the size of the ship
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public int getSize() {
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the name of the ship
|
|
||||||
* @return the name of the ship
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether the ship has been placed or not
|
|
||||||
* @return whether the ship has been placed or not
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public boolean isPlaced(){
|
|
||||||
return this.position != null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,44 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Das ShipButton Panel dient dem Erstellen eines lokalen Spiels.
|
|
||||||
* Hier kann der Benutzer Spieler inklusive Namen und das Semester, in dem sich der Benutzer befindet, einstellen.
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class ShipButton extends JButton {
|
|
||||||
private Ship ship;
|
|
||||||
private BoardDisplay boardDisplay;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt Buttons für die beiden Spieler (Module/Schiffe)
|
|
||||||
* @param ship Schiff von welchem der Name genommen wird
|
|
||||||
* @param boardDisplay Klasse für Interaktion
|
|
||||||
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public ShipButton(Ship ship, BoardDisplay boardDisplay) {
|
|
||||||
super(ship.getName());
|
|
||||||
this.ship = ship;
|
|
||||||
this.boardDisplay = boardDisplay;
|
|
||||||
this.addActionListener((e) -> {
|
|
||||||
boardDisplay.selectCurrentShip(this.ship);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setzt Farbe der Modulbuttons.
|
|
||||||
* Verschiedene Farben für:
|
|
||||||
* Modul ausgewählt, platziert nicht platziert.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void refreshButtonState() {
|
|
||||||
if (ship.isPlaced()) {
|
|
||||||
setBackground(Color.LIGHT_GRAY);
|
|
||||||
} else {
|
|
||||||
setBackground(Color.WHITE);
|
|
||||||
}
|
|
||||||
if (boardDisplay.getCurrentShip() == ship) {
|
|
||||||
setBackground(Color.CYAN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,111 +0,0 @@
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
/**
|
|
||||||
* beschreibt ein Package das durch einen AsyncSocket gesendet werden kann nach Netzwerkstandard
|
|
||||||
* https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public class SocketPackage {
|
|
||||||
|
|
||||||
private String name = "";
|
|
||||||
private String data = "";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a socket package by prividing a package name and data
|
|
||||||
* @param name the name of the package
|
|
||||||
* @param data the data of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public SocketPackage(String name, String data) {
|
|
||||||
this.setName(name);
|
|
||||||
this.setData(data);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* initialises an empty socket package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public SocketPackage() {
|
|
||||||
this("","");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a socket package from a message
|
|
||||||
* the message is parsed according to https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* @param message the message to be parsed
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public SocketPackage(String message) {
|
|
||||||
if (message.length() <= 0) {
|
|
||||||
throw new IllegalArgumentException("Socket message cannot be empty.");
|
|
||||||
}
|
|
||||||
String[] components = message.split(" ");
|
|
||||||
this.setName(components[0]);
|
|
||||||
if (components.length > 1) {
|
|
||||||
this.setData(message.substring(components[0].length() + 1));
|
|
||||||
} else {
|
|
||||||
this.setData("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the package name
|
|
||||||
* the name is always stored in upper case
|
|
||||||
* @param name the new name of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setName(String name) {
|
|
||||||
if (name == null) name = "";
|
|
||||||
this.name = name.toUpperCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the package data
|
|
||||||
* @param name the new data of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setData(String data) {
|
|
||||||
if (data == null) data = "";
|
|
||||||
this.data = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the name of the package
|
|
||||||
* @return the name of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the data of the package
|
|
||||||
* @return the data of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public String getData() {
|
|
||||||
return this.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* parses the package into a string according to https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* the package name and data are joined using a space " " `0x20`
|
|
||||||
* @return the package in string format
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public String toString() {
|
|
||||||
if (this.data == null || this.data.length() == 0) {
|
|
||||||
return this.name;
|
|
||||||
} else {
|
|
||||||
return this.name + " " + this.data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the data string as a list, split at every space " " `0x20`
|
|
||||||
* @return the data string as a list, split at every space " " `0x20`
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public List<String> splitData() {
|
|
||||||
return Arrays.asList(this.data.split(" "));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,88 +1,35 @@
|
||||||
import javazoom.jl.decoder.JavaLayerException;
|
import javazoom.jl.decoder.JavaLayerException;
|
||||||
import javazoom.jl.player.Player;
|
import javazoom.jl.player.Player;
|
||||||
|
|
||||||
import java.awt.List;
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
|
||||||
* Der SoundHandler dient zum Anlegen und Abspielen von Sounds
|
|
||||||
* @author Lucas Bronson, Luca Conte, Ole Wachtel, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class SoundHandler {
|
public class SoundHandler {
|
||||||
|
|
||||||
private static boolean soundOn = true;
|
private static boolean soundOn = true;
|
||||||
|
|
||||||
private static ArrayList<Thread> runningThreads = new ArrayList<Thread>();
|
|
||||||
|
|
||||||
// Wenn fehler beim erstellen von .jar mit sound hier gucken
|
|
||||||
private static HashMap<String, String> sounds = new HashMap<String, String>(Map.of(
|
private static HashMap<String, String> sounds = new HashMap<String, String>(Map.of(
|
||||||
"miss", "./Sound/water-drip.mp3",
|
"hit", "~/../Sound/water-drip.mp3"
|
||||||
"hit", "./Sound/hit.mp3",
|
|
||||||
"destroyed", "./Sound/hit.mp3",
|
|
||||||
"plop", "./Sound/plop.mp3",
|
|
||||||
"loose", "./Sound/loosescreenlaugh.mp3",
|
|
||||||
"win", "./Sound/win.mp3",
|
|
||||||
"yourturn", "./Sound/yourturn.mp3"
|
|
||||||
));
|
));
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt neuen Thread, um ausgewählten Sound abzuspielen
|
|
||||||
* @param soundName Name der Audiodatei, welche abgespielt werden soll
|
|
||||||
* @author Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public static void playSound(String soundName) {
|
public static void playSound(String soundName) {
|
||||||
if (soundOn) {
|
if (soundOn) {
|
||||||
Thread thread = new Thread(new Runnable() {
|
try {
|
||||||
@Override
|
Player player = new Player(new FileInputStream(sounds.get(soundName)));
|
||||||
public void run() {
|
player.play();
|
||||||
try {
|
} catch (JavaLayerException | FileNotFoundException e) {
|
||||||
Player player = new Player(new FileInputStream(sounds.get(soundName)));
|
System.out.println("dslkfsfnsldfnlsnfsdnölscls");
|
||||||
player.play();
|
e.printStackTrace();
|
||||||
} catch (JavaLayerException | FileNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
thread.start();
|
|
||||||
runningThreads.add(thread);
|
|
||||||
}
|
|
||||||
Iterator<Thread> i = runningThreads.iterator();
|
|
||||||
while (i.hasNext()) {
|
|
||||||
Thread oldThread = i.next();
|
|
||||||
if (!oldThread.isAlive()) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
oldThread.join();
|
|
||||||
i.remove();
|
|
||||||
System.out.println("cleared thread");
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* fügt einen neuen Sound zum SoundHanlder hinzu
|
|
||||||
* @param soundName der intern zu verwendende Name des Sounds
|
|
||||||
* @param path der Dateipfad zur Sound Datei
|
|
||||||
* @author Ole Wachtel
|
|
||||||
*/
|
|
||||||
static void add(String soundName, String path){
|
static void add(String soundName, String path){
|
||||||
sounds.put(soundName, path);
|
sounds.put(soundName, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* schaltet den Ton an oder aus
|
|
||||||
* @param sound ob der sound an ist
|
|
||||||
* @author Ole Wachtel
|
|
||||||
*/
|
|
||||||
static void setSoundOn(boolean sound){
|
static void setSoundOn(boolean sound){
|
||||||
soundOn= sound;
|
soundOn= sound;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
/**
|
|
||||||
* Diese Klasse implementiert den Einfachsten Ki Spieler.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
* */
|
|
||||||
public class SpecificAiPlayerEasy extends AiPlayer{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bein einfachen Ki Spieler wird nur der AiPlayer initialisiert und der Name gesetzt,
|
|
||||||
* da in der Eltern-Klasse AiPlayer eine default implementierung für alle Methoden existieren.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public SpecificAiPlayerEasy() {
|
|
||||||
super();
|
|
||||||
this.setName("AI Player Easy");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,273 +0,0 @@
|
||||||
import java.util.ArrayList;
|
|
||||||
// import java.util.Random; wird nicht mehr verwendet
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Klasse implementiert den Harten Ki Spieler.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
* */
|
|
||||||
public class SpecificAiPlayerHard extends AiPlayer{
|
|
||||||
|
|
||||||
private int gridSize;
|
|
||||||
private boolean[][] shotsFired;
|
|
||||||
private final ArrayList<Point> hitQueue;
|
|
||||||
//private final Random random; wird nicht mehr verwendet
|
|
||||||
private int nextChessRow;
|
|
||||||
private int nextChessCol;
|
|
||||||
|
|
||||||
// Enum für die Orientierung
|
|
||||||
private enum Orientierung {
|
|
||||||
UNBEKANNT,
|
|
||||||
HORIZONTAL,
|
|
||||||
VERTIKAL
|
|
||||||
}
|
|
||||||
private Orientierung orientierung;
|
|
||||||
// Speichert den ersten Treffer zur Bestimmung der Orientierung
|
|
||||||
private Point firstHit;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Eltern-Klasse wird initialisiert und alle lokalen variablen,
|
|
||||||
* die gesetzt werden können, werden initialisiert.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public SpecificAiPlayerHard() {
|
|
||||||
super();
|
|
||||||
this.setName("AI Player Hard");
|
|
||||||
/*this.gridSize = super.board.getSize();
|
|
||||||
this.shotsFired = new boolean[gridSize][gridSize];*/
|
|
||||||
this.gridSize = 0;
|
|
||||||
this.hitQueue = new ArrayList<>();
|
|
||||||
//this.random = new Random(); wird nicht mehr verwendet
|
|
||||||
this.nextChessRow = 0;
|
|
||||||
this.nextChessCol = 0;
|
|
||||||
this.orientierung = Orientierung.UNBEKANNT;
|
|
||||||
this.firstHit = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prüft, ob auf den punkt schonmal geschossen wurde.
|
|
||||||
* @param p zu prüfender Punkt
|
|
||||||
* @return boolean
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public boolean alreadyShot(Point p) {
|
|
||||||
return shotsFired[p.getX()][p.getY()];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bestimmt den nächsten punkt, der beschossen werden soll.
|
|
||||||
* @return Position
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public Point getNextMove() {
|
|
||||||
if(gridSize == 0) {
|
|
||||||
this.gridSize = super.board.getSize();
|
|
||||||
this.shotsFired = new boolean[gridSize][gridSize];
|
|
||||||
}
|
|
||||||
// Wenn wir noch Treffer in der Queue haben, diese priorisieren
|
|
||||||
while (!hitQueue.isEmpty()) {
|
|
||||||
Point target = hitQueue.remove(0);
|
|
||||||
if (!alreadyShot(target)) {
|
|
||||||
shotsFired[target.getX()][target.getY()] = true;
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ansonsten weiterhin "Schachbrettmuster"
|
|
||||||
int row = nextChessRow;
|
|
||||||
int col = nextChessCol;
|
|
||||||
while (alreadyShot(new Point(row, col))) {
|
|
||||||
advanceChessPattern();
|
|
||||||
row = nextChessRow;
|
|
||||||
col = nextChessCol;
|
|
||||||
}
|
|
||||||
|
|
||||||
shotsFired[row][col] = true;
|
|
||||||
advanceChessPattern();
|
|
||||||
return new Point(row, col);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Nachdem receiveShoot beim gegner den schuss verarbeitet hat,
|
|
||||||
* wird diese Methode mit der antwort aufgerufen.
|
|
||||||
* @param hitResponse the hitresponse
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
|
||||||
super.receiveHit(hitResponse);
|
|
||||||
|
|
||||||
// Wenn es ein Treffer ist, Adjacent-Punkte hinzufügen
|
|
||||||
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
|
||||||
Point hitPoint = hitResponse.getPoint();
|
|
||||||
|
|
||||||
// Wenn wir noch keinen ersten Treffer haben, speicher ihn
|
|
||||||
if (firstHit == null) {
|
|
||||||
firstHit = hitPoint;
|
|
||||||
// Orientierung noch unbekannt: alle möglichen Richtungen hinzufügen
|
|
||||||
addAdjacentPoints(hitPoint);
|
|
||||||
} else {
|
|
||||||
// Wenn Orientierung noch nicht bestimmt, jetzt prüfen
|
|
||||||
if (this.orientierung == Orientierung.UNBEKANNT) {
|
|
||||||
// Prüfen, ob der zweite Treffer horizontal oder vertikal liegt
|
|
||||||
if (firstHit.getY() == hitPoint.getY()) {
|
|
||||||
this.orientierung = Orientierung.VERTIKAL;
|
|
||||||
} else if (firstHit.getX() == hitPoint.getX()) {
|
|
||||||
this.orientierung = Orientierung.HORIZONTAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sobald die Orientierung erkannt wurde, entferne alle „unpassenden“ Punkte
|
|
||||||
if (this.orientierung != Orientierung.UNBEKANNT) {
|
|
||||||
cleanUpHitQueue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Für den aktuellen Treffer nur in passender Orientierung erweitern
|
|
||||||
addPointsByOrientation(hitPoint);
|
|
||||||
}
|
|
||||||
} else if (hitResponse.getHitResponse() == HitResponseType.SUNK) {
|
|
||||||
this.orientierung = Orientierung.UNBEKANNT;
|
|
||||||
firstHit = null;
|
|
||||||
hitQueue.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Entfernt aus der hitQueue alle Punkte, die nicht der erkannten Orientierung entsprechen.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private void cleanUpHitQueue() {
|
|
||||||
if (firstHit == null || this.orientierung == Orientierung.UNBEKANNT) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ArrayList<Point> toRemove = new ArrayList<>();
|
|
||||||
for (Point p : hitQueue) {
|
|
||||||
if (this.orientierung == Orientierung.HORIZONTAL) {
|
|
||||||
// HORIZONTAL => gleiche Zeile wie firstHit
|
|
||||||
if (p.getX() != firstHit.getX()) {
|
|
||||||
toRemove.add(p);
|
|
||||||
}
|
|
||||||
} else if (this.orientierung == Orientierung.VERTIKAL) {
|
|
||||||
// VERTICAL => gleiche Spalte wie firstHit
|
|
||||||
if (p.getY() != firstHit.getY()) {
|
|
||||||
toRemove.add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hitQueue.removeAll(toRemove);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fügt benachbarte Felder in der erkannten Orientierung hinzu.
|
|
||||||
* Ist die Orientierung HORIZONTAL, so werden nur links/rechts hinzugefügt.
|
|
||||||
* Ist sie VERTICAL, so werden nur oben/unten hinzugefügt.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private void addPointsByOrientation(Point point) {
|
|
||||||
if (this.orientierung == Orientierung.UNBEKANNT) {
|
|
||||||
// Fallback: füge alle benachbarten Punkte hinzu
|
|
||||||
addAdjacentPoints(point);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int x = point.getX();
|
|
||||||
int y = point.getY();
|
|
||||||
if (this.orientierung == Orientierung.HORIZONTAL) {
|
|
||||||
// Gleiche Zeile => links und rechts vom Point
|
|
||||||
Point left = new Point(x, y - 1);
|
|
||||||
Point right = new Point(x, y + 1);
|
|
||||||
|
|
||||||
if (isValidPoint(left) && !alreadyShot(left) && !hitQueue.contains(left)) {
|
|
||||||
hitQueue.add(left);
|
|
||||||
}
|
|
||||||
if (isValidPoint(right) && !alreadyShot(right) && !hitQueue.contains(right)) {
|
|
||||||
hitQueue.add(right);
|
|
||||||
}
|
|
||||||
} else if (this.orientierung == Orientierung.VERTIKAL) {
|
|
||||||
// Gleiche Spalte => oben und unten
|
|
||||||
Point up = new Point(x - 1, y);
|
|
||||||
Point down = new Point(x + 1, y);
|
|
||||||
|
|
||||||
if (isValidPoint(up) && !alreadyShot(up) && !hitQueue.contains(up)) {
|
|
||||||
hitQueue.add(up);
|
|
||||||
}
|
|
||||||
if (isValidPoint(down) && !alreadyShot(down) && !hitQueue.contains(down)) {
|
|
||||||
hitQueue.add(down);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Methode erweitert die hitsQueue um die umliegenden Punkte die Schiffe seien könnten.
|
|
||||||
* @param point
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private void addAdjacentPoints(Point point) {
|
|
||||||
int x = point.getX();
|
|
||||||
int y = point.getY();
|
|
||||||
|
|
||||||
// Possible adjacent positions (up, down, left, right)
|
|
||||||
Point[] adjacentPoints = {
|
|
||||||
new Point(x, y - 1),
|
|
||||||
new Point(x, y + 1),
|
|
||||||
new Point(x - 1, y),
|
|
||||||
new Point(x + 1, y)
|
|
||||||
};
|
|
||||||
|
|
||||||
for (Point p : adjacentPoints) {
|
|
||||||
if (isValidPoint(p) && !alreadyShot(p) && !hitQueue.contains(p)) {
|
|
||||||
hitQueue.add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Die Methode gibt zurück, ob eine Position auf dem Board ist. (Boolean)
|
|
||||||
* @param point Punkt der geprüft werden soll
|
|
||||||
* @return Ist auf dem Board oder nicht.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private boolean isValidPoint(Point point) {
|
|
||||||
return point.getX() >= 0 && point.getX() < gridSize &&
|
|
||||||
point.getY() >= 0 && point.getY() < gridSize;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Ki Methode um zu schießen.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void aiShoot() {
|
|
||||||
this.enemy.receiveShoot(getNextMove());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Die Zeilen und spalten variable wird hier angepasst, sodass beim nächsten schuss im Muster geschossen wird.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private void advanceChessPattern() {
|
|
||||||
nextChessCol += 2;
|
|
||||||
if (nextChessCol >= gridSize) {
|
|
||||||
nextChessRow += 1;
|
|
||||||
nextChessCol = (nextChessRow % 2 == 0) ? 0 : 1; // Alternate starting points for chess pattern
|
|
||||||
}
|
|
||||||
if (nextChessRow >= gridSize) {
|
|
||||||
nextChessRow = 0;
|
|
||||||
nextChessCol = 0; // Reset if pattern wraps around
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adds adjacent cells to the hit queue
|
|
||||||
/* public void processHit(int row, int col) {
|
|
||||||
if (row > 0 && !alreadyShot(row - 1, col)) {
|
|
||||||
hitQueue.add(new int[]{row - 1, col});
|
|
||||||
}
|
|
||||||
if (row < gridSize - 1 && !alreadyShot(row + 1, col)) {
|
|
||||||
hitQueue.add(new int[]{row + 1, col});
|
|
||||||
}
|
|
||||||
if (col > 0 && !alreadyShot(row, col - 1)) {
|
|
||||||
hitQueue.add(new int[]{row, col - 1});
|
|
||||||
}
|
|
||||||
if (col < gridSize - 1 && !alreadyShot(row, col + 1)) {
|
|
||||||
hitQueue.add(new int[]{row, col + 1});
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
|
@ -1,119 +0,0 @@
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Klasse implementiert den Medium Ki Spieler.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
* */
|
|
||||||
public class SpecificAiPlayerMedium extends AiPlayer{
|
|
||||||
/**
|
|
||||||
* Liste an Punkten die beschossen werden sollen. (Mögliche weitere Segmente vom schiff)
|
|
||||||
*/
|
|
||||||
private List<Point> hitsQueue = new ArrayList<>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Eltern-Klasse wird initialisiert und der Name wird gesetzt.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public SpecificAiPlayerMedium() {
|
|
||||||
super();
|
|
||||||
this.setName("AI Player Medium");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ki Methode um zu schießen.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void aiShoot() {
|
|
||||||
Point nextShot = ComputeNextShot();
|
|
||||||
// Shoot at the enemy and receive the hit response
|
|
||||||
enemy.receiveShoot(nextShot);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Nachdem receiveShoot beim gegner den schuss verarbeitet hat,
|
|
||||||
* wird diese Methode mit der antwort aufgerufen.
|
|
||||||
* @param hitResponse the hitresponse
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
|
||||||
super.receiveHit(hitResponse);
|
|
||||||
// If it's a hit or sunk, add adjacent cells to the hitsQueue
|
|
||||||
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
|
||||||
addAdjacentPoints(hitResponse.getPoint());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Die Methode bestimmt welche Position als nächstes beschossen werden soll.
|
|
||||||
* @return der Punkt auf den als nächtest geschossen werden soll
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
public Point ComputeNextShot() {
|
|
||||||
Point nextShot;
|
|
||||||
|
|
||||||
// Prioritize shots from the hitsQueue
|
|
||||||
while (!hitsQueue.isEmpty()) {
|
|
||||||
nextShot = hitsQueue.remove(0);
|
|
||||||
if (!alreadyShot(nextShot)) {
|
|
||||||
return nextShot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If hitsQueue is empty, pick a random point that hasn't been shot
|
|
||||||
do {
|
|
||||||
nextShot = RandomPoint();
|
|
||||||
} while (alreadyShot(nextShot));
|
|
||||||
|
|
||||||
return nextShot;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Methode erweitert die hitsQueue um die umliegenden Punkte die Schiffe seien könnten.
|
|
||||||
* @param point der Punkt dessen nachbarn zur hitQueue hinzugefügt werden sollen
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private void addAdjacentPoints(Point point) {
|
|
||||||
int x = point.getX();
|
|
||||||
int y = point.getY();
|
|
||||||
|
|
||||||
// Possible adjacent positions (up, down, left, right)
|
|
||||||
Point[] adjacentPoints = {
|
|
||||||
new Point(x, y - 1),
|
|
||||||
new Point(x, y + 1),
|
|
||||||
new Point(x - 1, y),
|
|
||||||
new Point(x + 1, y)
|
|
||||||
};
|
|
||||||
|
|
||||||
for (Point p : adjacentPoints) {
|
|
||||||
if (isValidPoint(p) && !alreadyShot(p) && !hitsQueue.contains(p)) {
|
|
||||||
hitsQueue.add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Methode gibt zurück, ob eine Position schon beschossen wurde. (Boolean)
|
|
||||||
* @param p Punkt der geprüft werden soll
|
|
||||||
* @return wurde schon beschossen oder nicht.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private boolean alreadyShot(Point p) {
|
|
||||||
|
|
||||||
return this.enemy.board.getHitResponseOnPoint(p) != null;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Die Methode gibt zurück, ob eine Position auf dem Board ist. (Boolean)
|
|
||||||
* @param point Punkt der geprüft werden soll
|
|
||||||
* @return Ist auf dem Board oder nicht.
|
|
||||||
* @author Florian Alexy und Florian Hantzschel
|
|
||||||
*/
|
|
||||||
private boolean isValidPoint(Point point) {
|
|
||||||
return point.getX() >= 0 && point.getX() < board.getSize() &&
|
|
||||||
point.getY() >= 0 && point.getY() < board.getSize();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,37 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Das Verbinden Panel dient als "Überblende", während im Backend das Spiel erstellt/ eine Verbindung hergestellt wird.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public class Verbinden extends JPanel{
|
|
||||||
|
|
||||||
//ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
|
||||||
|
|
||||||
JLabel verbindenLabel = new JLabel("Verbinde . . .",SwingConstants.CENTER);
|
|
||||||
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor der Verbinden Klasse.
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public Verbinden(MainFrame frame) {
|
|
||||||
setLayout(null);
|
|
||||||
buildPanel(frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Baut Panel auf.
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
private void buildPanel(MainFrame frame) {
|
|
||||||
setLayout(new BorderLayout());
|
|
||||||
verbindenLabel.setFont(robotoFont.deriveFont(50f));
|
|
||||||
add(verbindenLabel, BorderLayout.CENTER);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Klasse für Erstellung von winScreen Objekten
|
|
||||||
* Dient zur Anzeige des Sieges nachdem ein Spiel
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public class WinScreen extends JPanel {
|
|
||||||
JLabel winLabel = new JLabel("Du hast Gewonnen!");
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
|
||||||
JButton okButton = new JButton("Zurück zum Hauptmenü");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor der WinScreen Klasse
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public WinScreen(MainFrame frame) {
|
|
||||||
setLayout(null);
|
|
||||||
buildPanel(frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Panel bauen/Objekte hinzufuegen
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public void buildPanel(MainFrame frame) {
|
|
||||||
Timer timer = new Timer(5, new ActionListener() {
|
|
||||||
private float hue = 0; // Farbton-Wert für HSB-Farbmodell
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
// Farbe basierend auf dem Farbton-Wert berechnen
|
|
||||||
Color pulsierendeFarbe = Color.getHSBColor(hue, 0.8f, 0.8f); // Sättigung und Helligkeit fix
|
|
||||||
winLabel.setForeground(pulsierendeFarbe);
|
|
||||||
|
|
||||||
// Farbton leicht verändern (Zyklus zwischen 0 und 1)
|
|
||||||
hue += 0.01f;
|
|
||||||
if (hue > 1) {
|
|
||||||
hue = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
okButton.setBounds(650,525,200,50);
|
|
||||||
winLabel.setBounds(500,450,500,50);
|
|
||||||
timer.start(); // Timer starten
|
|
||||||
winLabel.setFont(robotoFont);
|
|
||||||
winLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
SoundHandler.playSound("win");
|
|
||||||
|
|
||||||
// Zurückkehren zum Hauptmenü, wenn okButton gedrückt wird
|
|
||||||
okButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
frame.showPanel("MainMenu");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
add(winLabel);
|
|
||||||
add(okButton);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,321 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Das startLocalGame Panel dient dem Erstellen eines lokalen Spiels.
|
|
||||||
* Hier kann der Benutzer Spieler inklusive Namen und das Semester, in dem sich der Benutzer befindet, einstellen.
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok, Luca Conte
|
|
||||||
*/
|
|
||||||
public class startLocalGame extends JPanel {
|
|
||||||
|
|
||||||
// Funktionshilfen
|
|
||||||
int semesterCounter = 1; // Semester Counter Label
|
|
||||||
String leftPlayerNickname = "Spieler 1";
|
|
||||||
String rightPlayerNickname = "Einfach";
|
|
||||||
|
|
||||||
// Grafiken
|
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
|
||||||
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
|
||||||
ImageIcon aiPlayerEasyIcon = new ImageIcon("graphics/botPlayerEasy.png");
|
|
||||||
ImageIcon aiPlayerNormalIcon = new ImageIcon("graphics/botPlayerNormal.png");
|
|
||||||
ImageIcon aiPlayerHardIcon = new ImageIcon("graphics/botPlayerHard.png");
|
|
||||||
|
|
||||||
// Labels
|
|
||||||
JLabel frameTitle = new JLabel("Lokales Spiel");
|
|
||||||
JLabel semesterLabel = new JLabel("Semester");
|
|
||||||
JLabel leftPlayerName = new JLabel("Name");
|
|
||||||
JLabel rightPlayerName = new JLabel("KI-Level");
|
|
||||||
JLabel leftPlayerIcon = new JLabel(humanPlayerIcon);
|
|
||||||
JLabel rightPlayerIcon = new JLabel(aiPlayerEasyIcon);
|
|
||||||
JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));
|
|
||||||
|
|
||||||
// Buttons
|
|
||||||
JButton backButton = new JButton(backButtonIcon);
|
|
||||||
JButton leftPlayerLeftButton = new JButton("<-");
|
|
||||||
JButton leftPlayerRightButton = new JButton("->");
|
|
||||||
JButton semesterUpButton = new JButton("^");
|
|
||||||
JButton semesterDownButton = new JButton("v");
|
|
||||||
JButton rightPlayerLeftButton = new JButton("<-");
|
|
||||||
JButton rightPlayerRightButton = new JButton("->");
|
|
||||||
JButton startButton = new JButton("Start!");
|
|
||||||
|
|
||||||
// Textfelder
|
|
||||||
JTextField leftPlayerTextField = new JTextField(20);
|
|
||||||
JTextField rightPlayerTextField = new JTextField(20);
|
|
||||||
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
|
||||||
/**
|
|
||||||
* Konstruktor der startLocalGame.
|
|
||||||
* Fügt Buttons, Textfelder und Label hinzu.
|
|
||||||
* Fügt ebenfalls ActionListeners hinzu, damit Buttons etc. ihre gewünschte Funktion haben
|
|
||||||
*
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Lucas Bronson, Joshua Kuklok
|
|
||||||
*/
|
|
||||||
startLocalGame(MainFrame frame) {
|
|
||||||
// Layout des Panels
|
|
||||||
setLayout(null);
|
|
||||||
|
|
||||||
// Setze Komponentenpositionen
|
|
||||||
frameTitle.setBounds(20, 20, 500, 60);
|
|
||||||
frameTitle.setFont(robotoFont.deriveFont(50f));
|
|
||||||
add(frameTitle);
|
|
||||||
|
|
||||||
semesterLabel.setBounds(700, 300, 200, 30);
|
|
||||||
add(semesterLabel);
|
|
||||||
|
|
||||||
leftPlayerName.setBounds(50, 625, 200, 30);
|
|
||||||
add(leftPlayerName);
|
|
||||||
|
|
||||||
rightPlayerName.setBounds(1200, 625, 200, 30);
|
|
||||||
add(rightPlayerName);
|
|
||||||
|
|
||||||
leftPlayerIcon.setBounds(75, 400, 200, 128);
|
|
||||||
add(leftPlayerIcon);
|
|
||||||
|
|
||||||
rightPlayerIcon.setBounds(1225, 400, 200, 128);
|
|
||||||
add(rightPlayerIcon);
|
|
||||||
|
|
||||||
semesterCounterLabel.setBounds(705, 475, 50, 50);
|
|
||||||
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
add(semesterCounterLabel);
|
|
||||||
|
|
||||||
backButton.setBounds(1380, 20, 80, 80);
|
|
||||||
add(backButton);
|
|
||||||
|
|
||||||
leftPlayerLeftButton.setBounds(50, 450, 50, 50);
|
|
||||||
add(leftPlayerLeftButton);
|
|
||||||
|
|
||||||
leftPlayerRightButton.setBounds(250, 450, 50, 50);
|
|
||||||
add(leftPlayerRightButton);
|
|
||||||
|
|
||||||
semesterUpButton.setBounds(705, 400, 50, 50);
|
|
||||||
add(semesterUpButton);
|
|
||||||
|
|
||||||
semesterDownButton.setBounds(705, 550, 50, 50);
|
|
||||||
add(semesterDownButton);
|
|
||||||
|
|
||||||
rightPlayerLeftButton.setBounds(1200, 450, 50, 50);
|
|
||||||
add(rightPlayerLeftButton);
|
|
||||||
|
|
||||||
rightPlayerRightButton.setBounds(1400, 450, 50, 50);
|
|
||||||
add(rightPlayerRightButton);
|
|
||||||
|
|
||||||
startButton.setBounds(700, 750, 100, 50);
|
|
||||||
add(startButton);
|
|
||||||
|
|
||||||
leftPlayerTextField.setBounds(50, 650, 250, 50);
|
|
||||||
leftPlayerTextField.setText(leftPlayerNickname);
|
|
||||||
add(leftPlayerTextField);
|
|
||||||
|
|
||||||
rightPlayerTextField.setBounds(1200, 650, 250, 50);
|
|
||||||
rightPlayerTextField.setText(rightPlayerNickname);
|
|
||||||
add(rightPlayerTextField);
|
|
||||||
|
|
||||||
// ActionListener für Buttons.
|
|
||||||
// Um das Semester zu erhöhen.
|
|
||||||
semesterUpButton.addActionListener(e -> {
|
|
||||||
if (semesterCounter < 6) {
|
|
||||||
semesterCounter++;
|
|
||||||
semesterCounterLabel.setText(String.valueOf(semesterCounter));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um das Semester zu senken.
|
|
||||||
semesterDownButton.addActionListener(e -> {
|
|
||||||
if (semesterCounter > 1) {
|
|
||||||
semesterCounter--;
|
|
||||||
semesterCounterLabel.setText(String.valueOf(semesterCounter));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um linken Player nach links zu "rotieren".
|
|
||||||
leftPlayerLeftButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
toggleLeftPlayerIconLeft();
|
|
||||||
updateTextFields();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um linken Player nach rechts zu "rotieren".
|
|
||||||
leftPlayerRightButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
toggleLeftPlayerIconRight();
|
|
||||||
updateTextFields();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um rechten Player nach links zu "rotieren".
|
|
||||||
rightPlayerLeftButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
toggleRightPlayerIconLeft();
|
|
||||||
updateTextFields();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um den rechten Player nach rechts zu "rotieren".
|
|
||||||
rightPlayerRightButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
toggleRightPlayerIconRight();
|
|
||||||
updateTextFields();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um Namen des linken Spielers zu speichern.
|
|
||||||
leftPlayerTextField.addActionListener(e -> {
|
|
||||||
leftPlayerNickname = leftPlayerTextField.getText();
|
|
||||||
System.out.println("Linker Spielername geändert zu: " + leftPlayerNickname); // Debugging-Ausgabe
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um Namen des linken Spielers zu speichern, auch wenn nicht Enter gedrückt wird.
|
|
||||||
leftPlayerTextField.addFocusListener(new java.awt.event.FocusAdapter() {
|
|
||||||
@Override
|
|
||||||
public void focusLost(java.awt.event.FocusEvent evt) {
|
|
||||||
leftPlayerNickname = leftPlayerTextField.getText();
|
|
||||||
System.out.println("Linker Spielername geändert zu: " + leftPlayerNickname); // Debugging-Ausgabe
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um zum MainMenu zu wechseln.
|
|
||||||
backButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
frame.showPanel("MainMenu");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um zum startLocalGameLoadingScreen zu wechseln und Daten an Backend weiterzureichen.
|
|
||||||
startButton.addActionListener(new ActionListener() {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
HashMap<ImageIcon, Class<? extends LocalPlayer>> playerClassMap = new HashMap<>();
|
|
||||||
playerClassMap.put(humanPlayerIcon, HumanPlayer.class);
|
|
||||||
playerClassMap.put(aiPlayerEasyIcon, SpecificAiPlayerEasy.class);
|
|
||||||
playerClassMap.put(aiPlayerNormalIcon, SpecificAiPlayerMedium.class);
|
|
||||||
playerClassMap.put(aiPlayerHardIcon, SpecificAiPlayerHard.class);
|
|
||||||
|
|
||||||
|
|
||||||
frame.showPanelSLGLS("startLocalGameLoadingScreen", semesterCounter);
|
|
||||||
|
|
||||||
Class<? extends LocalPlayer> leftPlayerClass = playerClassMap.get(leftPlayerIcon.getIcon());
|
|
||||||
Class<? extends AiPlayer> rightPlayerClass = (Class<? extends AiPlayer>) playerClassMap.get(rightPlayerIcon.getIcon());
|
|
||||||
GameController.startLocalGame(
|
|
||||||
leftPlayerClass, leftPlayerNickname,
|
|
||||||
rightPlayerClass,
|
|
||||||
GameController.semesterToBoardSize(semesterCounter)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
// Hilfsfunktionen
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setzt das jeweils "nächste" Icon, wenn der leftPlayerLeftButton gedrückt wird.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void toggleLeftPlayerIconLeft() {
|
|
||||||
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
leftPlayerIcon.setIcon(aiPlayerHardIcon);
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
|
||||||
leftPlayerIcon.setIcon(humanPlayerIcon);
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
leftPlayerIcon.setIcon(aiPlayerEasyIcon);
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
leftPlayerIcon.setIcon(aiPlayerNormalIcon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setzt das jeweils "nächste" Icon, wenn der leftPlayerRightButton gedrückt wird.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void toggleLeftPlayerIconRight() {
|
|
||||||
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
leftPlayerIcon.setIcon(aiPlayerEasyIcon);
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
|
||||||
leftPlayerIcon.setIcon(aiPlayerNormalIcon);
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
leftPlayerIcon.setIcon(aiPlayerHardIcon);
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
leftPlayerIcon.setIcon(humanPlayerIcon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setzt das jeweils "nächste" Icon, wenn der RightPlayerLeftButton gedrückt wird.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void toggleRightPlayerIconLeft() {
|
|
||||||
if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
|
||||||
rightPlayerIcon.setIcon(aiPlayerHardIcon);
|
|
||||||
|
|
||||||
} else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon){
|
|
||||||
rightPlayerIcon.setIcon(aiPlayerEasyIcon);
|
|
||||||
|
|
||||||
} else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
rightPlayerIcon.setIcon(aiPlayerNormalIcon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setzt das jeweils "nächste" Icon, wenn der RightPlayerRightButton gedrückt wird.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void toggleRightPlayerIconRight() {
|
|
||||||
if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
|
||||||
rightPlayerIcon.setIcon(aiPlayerNormalIcon);
|
|
||||||
|
|
||||||
} else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon){
|
|
||||||
rightPlayerIcon.setIcon(aiPlayerHardIcon);
|
|
||||||
|
|
||||||
} else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
rightPlayerIcon.setIcon(aiPlayerEasyIcon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aktualisiert die Textfelder basierend auf den Icons
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void updateTextFields() {
|
|
||||||
// Für Linken Spieler
|
|
||||||
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
leftPlayerTextField.setText(leftPlayerNickname);
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
|
||||||
leftPlayerTextField.setText("Einfach");
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
leftPlayerTextField.setText("Mittel");
|
|
||||||
|
|
||||||
} else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
leftPlayerTextField.setText("Schwer");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Für Rechten Spieler
|
|
||||||
if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
|
||||||
rightPlayerTextField.setText("Einfach");
|
|
||||||
|
|
||||||
} else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
rightPlayerTextField.setText("Mittel");
|
|
||||||
|
|
||||||
} else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
rightPlayerTextField.setText("Schwer");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Das startLocalGameLoadingScreen Panel dient als "Überblende", während im Backend das Spiel erstellt wird.
|
|
||||||
* Hier wird lediglich Text angezeigt
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class startLocalGameLoadingScreen extends JPanel{
|
|
||||||
/**
|
|
||||||
* Konstruktor der startLocalGameLoadingScreen.
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @param semesterCounter Ein Zähler, der das gewählte Semester speichert (hier unbenutzt)
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
startLocalGameLoadingScreen(MainFrame frame, int semesterCounter) {
|
|
||||||
|
|
||||||
// Layout des Panels
|
|
||||||
setLayout(new BorderLayout());
|
|
||||||
|
|
||||||
// Label mit dem Text erstellen
|
|
||||||
JLabel loadingLabel = new JLabel("Spiel wird gestartet, bitte warten...");
|
|
||||||
loadingLabel.setHorizontalAlignment(SwingConstants.CENTER); // Horizontal zentrieren
|
|
||||||
loadingLabel.setVerticalAlignment(SwingConstants.CENTER); // Vertikal zentrieren
|
|
||||||
|
|
||||||
// Schriftgröße anpassen
|
|
||||||
loadingLabel.setFont(new Font("Roboto", Font.BOLD, 45));
|
|
||||||
|
|
||||||
// Label zum Panel hinzufügen
|
|
||||||
add(loadingLabel, BorderLayout.CENTER);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,244 +0,0 @@
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Das startMultiplayerGame Panel dient dem Erstellen eines Online Spiels.
|
|
||||||
* Hier kann der Benutzer Spieler inklusive Namen und das Semester, in dem sich der Benutzer befindet, einstellen.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
public class startMultiplayerGame extends JPanel {
|
|
||||||
|
|
||||||
// Funktionshilfen
|
|
||||||
int semesterCounter = 1;
|
|
||||||
String PlayerNickname = "Spieler 1";
|
|
||||||
|
|
||||||
// Grafiken
|
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
|
||||||
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
|
||||||
ImageIcon aiPlayerEasyIcon = new ImageIcon("graphics/botPlayerEasy.png");
|
|
||||||
ImageIcon aiPlayerNormalIcon = new ImageIcon("graphics/botPlayerNormal.png");
|
|
||||||
ImageIcon aiPlayerHardIcon = new ImageIcon("graphics/botPlayerHard.png");
|
|
||||||
|
|
||||||
// Labels
|
|
||||||
JLabel frameTitle = new JLabel("Multiplayer Spiel");
|
|
||||||
JLabel semesterLabel = new JLabel("Semester");
|
|
||||||
JLabel PlayerName = new JLabel("Name");
|
|
||||||
JLabel PlayerIcon = new JLabel(humanPlayerIcon);
|
|
||||||
JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));
|
|
||||||
|
|
||||||
// Buttons
|
|
||||||
JButton backButton = new JButton(backButtonIcon);
|
|
||||||
JButton PlayerLeftButton = new JButton("<-");
|
|
||||||
JButton PlayerRightButton = new JButton("->");
|
|
||||||
JButton semesterUpButton = new JButton("^");
|
|
||||||
JButton semesterDownButton = new JButton("v");
|
|
||||||
JButton createGameButton = new JButton("Spiel erstellen");
|
|
||||||
JButton joinGameButton = new JButton("Spiel beitreten");
|
|
||||||
|
|
||||||
// Textfelder
|
|
||||||
JTextField PlayerTextField = new JTextField(20);
|
|
||||||
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor der startLocalGame.
|
|
||||||
* Fügt Buttons, Textfelder und Label hinzu.
|
|
||||||
* Fügt ebenfalls ActionListeners hinzu, damit Buttons etc. ihre gewünschte Funktion haben
|
|
||||||
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
|
||||||
* @author Joshua Kuklok, Lucas Bronson
|
|
||||||
*/
|
|
||||||
startMultiplayerGame(MainFrame frame) {
|
|
||||||
|
|
||||||
// Layout des Panels
|
|
||||||
setLayout(null);
|
|
||||||
|
|
||||||
// Setze Komponentenpositionen
|
|
||||||
frameTitle.setBounds(20, 20, 500, 60);
|
|
||||||
frameTitle.setFont(robotoFont.deriveFont(50f));
|
|
||||||
add(frameTitle);
|
|
||||||
|
|
||||||
semesterLabel.setBounds(700, 300, 200, 30);
|
|
||||||
add(semesterLabel);
|
|
||||||
|
|
||||||
PlayerName.setBounds(50, 625, 200, 30);
|
|
||||||
add(PlayerName);
|
|
||||||
|
|
||||||
PlayerIcon.setBounds(75, 400, 200, 128);
|
|
||||||
add(PlayerIcon);
|
|
||||||
|
|
||||||
semesterCounterLabel.setBounds(705, 475, 50, 50); // zwischen den Up/Down-Buttons
|
|
||||||
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
add(semesterCounterLabel);
|
|
||||||
|
|
||||||
backButton.setBounds(1380, 20, 80, 80);
|
|
||||||
add(backButton);
|
|
||||||
|
|
||||||
PlayerLeftButton.setBounds(50, 450, 50, 50);
|
|
||||||
add(PlayerLeftButton);
|
|
||||||
|
|
||||||
PlayerRightButton.setBounds(250, 450, 50, 50);
|
|
||||||
add(PlayerRightButton);
|
|
||||||
|
|
||||||
semesterUpButton.setBounds(705, 400, 50, 50);
|
|
||||||
add(semesterUpButton);
|
|
||||||
|
|
||||||
semesterDownButton.setBounds(705, 550, 50, 50);
|
|
||||||
add(semesterDownButton);
|
|
||||||
|
|
||||||
joinGameButton.setBounds(1100, 350, 200, 50);
|
|
||||||
add(joinGameButton);
|
|
||||||
|
|
||||||
createGameButton.setBounds(1100, 550, 200, 50);
|
|
||||||
add(createGameButton);
|
|
||||||
|
|
||||||
PlayerTextField.setBounds(50, 650, 250, 50);
|
|
||||||
PlayerTextField.setText(PlayerNickname);
|
|
||||||
add(PlayerTextField);
|
|
||||||
|
|
||||||
// ActionListener für Buttons
|
|
||||||
// Um das Semester zu erhöhen.
|
|
||||||
semesterUpButton.addActionListener(e -> {
|
|
||||||
if (semesterCounter < 6) {
|
|
||||||
semesterCounter++;
|
|
||||||
semesterCounterLabel.setText(String.valueOf(semesterCounter));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um das Semester zu senken.
|
|
||||||
semesterDownButton.addActionListener(e -> {
|
|
||||||
if (semesterCounter > 1) {
|
|
||||||
semesterCounter--;
|
|
||||||
semesterCounterLabel.setText(String.valueOf(semesterCounter));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um Player nach links zu "rotieren".
|
|
||||||
PlayerLeftButton.addActionListener(e -> {
|
|
||||||
togglePlayerIconLeft();
|
|
||||||
updateTextFields();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um Player nach rechts zu "rotieren".
|
|
||||||
PlayerRightButton.addActionListener(e -> {
|
|
||||||
togglePlayerIconRight();
|
|
||||||
updateTextFields();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um Namen des linken Spielers zu speichern.
|
|
||||||
PlayerTextField.addActionListener(e -> {
|
|
||||||
PlayerNickname = PlayerTextField.getText();
|
|
||||||
System.out.println("Linker Spielername geändert zu: " + PlayerNickname); // Debugging-Ausgabe
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um Namen des linken Spielers zu speichern, auch wenn nicht Enter gedrückt wird.
|
|
||||||
PlayerTextField.addFocusListener(new java.awt.event.FocusAdapter() {
|
|
||||||
@Override
|
|
||||||
public void focusLost(java.awt.event.FocusEvent evt) {
|
|
||||||
PlayerNickname = PlayerTextField.getText();
|
|
||||||
System.out.println("Linker Spielername geändert zu: " + PlayerNickname); // Debugging-Ausgabe
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um zum MainMenu zu wechseln.
|
|
||||||
backButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
frame.showPanel("MainMenu");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um zu JoinGame mit richtigen Parametern zu wechseln.
|
|
||||||
joinGameButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",1,0, PlayerNickname, semesterCounter);
|
|
||||||
} else if ( PlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",1,1, PlayerNickname, semesterCounter);
|
|
||||||
} else if ( PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",1,2, PlayerNickname, semesterCounter);
|
|
||||||
} else if ( PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",1,3, PlayerNickname, semesterCounter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Um zu JoinGame mit richtigen Parametern zu wechseln.
|
|
||||||
createGameButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
//Parameter -> panelName, Spiel erstellen oder beitreten (int), Spielertyp(int 0-3), Spielername
|
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",0,0, PlayerNickname, semesterCounter);
|
|
||||||
} else if ( PlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",0,1, PlayerNickname, semesterCounter);
|
|
||||||
} else if ( PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",0,2, PlayerNickname, semesterCounter);
|
|
||||||
} else if ( PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
frame.showPanelSMG("JoinGame",0,3, PlayerNickname, semesterCounter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hilfsfunktionen
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setzt das jeweils "nächste" Icon, wenn der PlayerLeftButton gedrückt wird.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void togglePlayerIconLeft() {
|
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
PlayerIcon.setIcon(aiPlayerHardIcon);
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerEasyIcon){
|
|
||||||
PlayerIcon.setIcon(humanPlayerIcon);
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
PlayerIcon.setIcon(aiPlayerEasyIcon);
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
PlayerIcon.setIcon(aiPlayerNormalIcon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setzt das jeweils "nächste" Icon, wenn der PlayerRightButton gedrückt wird.
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void togglePlayerIconRight() {
|
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
PlayerIcon.setIcon(aiPlayerEasyIcon);
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerEasyIcon){
|
|
||||||
PlayerIcon.setIcon(aiPlayerNormalIcon);
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
PlayerIcon.setIcon(aiPlayerHardIcon);
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
PlayerIcon.setIcon(humanPlayerIcon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aktualisiert die Textfelder basierend auf den Icons
|
|
||||||
* @author Joshua Kuklok
|
|
||||||
*/
|
|
||||||
private void updateTextFields() {
|
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
PlayerTextField.setText(PlayerNickname);
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerEasyIcon){
|
|
||||||
PlayerTextField.setText("Einfach");
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
|
||||||
PlayerTextField.setText("Mittel");
|
|
||||||
|
|
||||||
} else if (PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
|
||||||
PlayerTextField.setText("Schwer");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|