Initial commit
This commit is contained in:
3
Java/TicTacToe/.idea/.gitignore
generated
vendored
Normal file
3
Java/TicTacToe/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
8
Java/TicTacToe/.idea/artifacts/TicTacToe_jar.xml
generated
Normal file
8
Java/TicTacToe/.idea/artifacts/TicTacToe_jar.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<component name="ArtifactManager">
|
||||
<artifact type="jar" name="TicTacToe:jar">
|
||||
<output-path>$PROJECT_DIR$/out/artifacts/TicTacToe_jar</output-path>
|
||||
<root id="archive" name="TicTacToe.jar">
|
||||
<element id="module-output" name="TicTacToe" />
|
||||
</root>
|
||||
</artifact>
|
||||
</component>
|
||||
7
Java/TicTacToe/.idea/discord.xml
generated
Normal file
7
Java/TicTacToe/.idea/discord.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
||||
6
Java/TicTacToe/.idea/misc.xml
generated
Normal file
6
Java/TicTacToe/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
Java/TicTacToe/.idea/modules.xml
generated
Normal file
8
Java/TicTacToe/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/TicTacToe.iml" filepath="$PROJECT_DIR$/TicTacToe.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
10
Java/TicTacToe/.idea/runConfigurations.xml
generated
Normal file
10
Java/TicTacToe/.idea/runConfigurations.xml
generated
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RunConfigurationProducerService">
|
||||
<option name="ignoredProducers">
|
||||
<set>
|
||||
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
124
Java/TicTacToe/.idea/uiDesigner.xml
generated
Normal file
124
Java/TicTacToe/.idea/uiDesigner.xml
generated
Normal file
@@ -0,0 +1,124 @@
|
||||
<?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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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>
|
||||
12
Java/TicTacToe/TicTacToe.iml
Normal file
12
Java/TicTacToe/TicTacToe.iml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
BIN
Java/TicTacToe/out/artifacts/TicTacToe_jar/TicTacToe.jar
Normal file
BIN
Java/TicTacToe/out/artifacts/TicTacToe_jar/TicTacToe.jar
Normal file
Binary file not shown.
1
Java/TicTacToe/out/artifacts/TicTacToe_jar/start.bat
Normal file
1
Java/TicTacToe/out/artifacts/TicTacToe_jar/start.bat
Normal file
@@ -0,0 +1 @@
|
||||
java -jar TicTacToe.jar
|
||||
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: de.craftix.tictactoe.Main
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Java/TicTacToe/out/production/TicTacToe/o.png
Normal file
BIN
Java/TicTacToe/out/production/TicTacToe/o.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
BIN
Java/TicTacToe/out/production/TicTacToe/x.png
Normal file
BIN
Java/TicTacToe/out/production/TicTacToe/x.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 955 B |
BIN
Java/TicTacToe/res/o.png
Normal file
BIN
Java/TicTacToe/res/o.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
BIN
Java/TicTacToe/res/x.png
Normal file
BIN
Java/TicTacToe/res/x.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 955 B |
3
Java/TicTacToe/src/META-INF/MANIFEST.MF
Normal file
3
Java/TicTacToe/src/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: de.craftix.tictactoe.Main
|
||||
|
||||
36
Java/TicTacToe/src/de/craftix/tictactoe/ActionHandler.java
Normal file
36
Java/TicTacToe/src/de/craftix/tictactoe/ActionHandler.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package de.craftix.tictactoe;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ActionHandler implements ActionListener {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
if (Gui.winner == 0) {
|
||||
int btn = -1;
|
||||
for (int i = 0; i < Gui.button.length; i++) if (e.getSource() == Gui.button[i]) btn = i;
|
||||
if (btn == -1) return;
|
||||
if (Gui.state[btn] == 0 && Gui.player == 1) {
|
||||
Gui.state[btn] = 1;
|
||||
if (Function.checkWinning()) Gui.winner = Gui.player;
|
||||
Gui.player = 2;
|
||||
if (Gui.winner == 0) {
|
||||
Function.calculateNextField(Gui.useKI);
|
||||
if (Function.checkWinning(2)) Gui.winner = 2;
|
||||
}
|
||||
}else if (Gui.state[btn] == 0 && Gui.player == 2) {
|
||||
Gui.state[btn] = 2;
|
||||
if (Function.checkWinning()) Gui.winner = Gui.player;
|
||||
Gui.player = 1;
|
||||
}
|
||||
ArrayList<Integer> freeFields = new ArrayList<>();
|
||||
for (int i = 0; i < Gui.state.length; i++) if (Gui.state[i] == 0) freeFields.add(i);
|
||||
if (freeFields.size() == 0) Gui.winner = 3;
|
||||
if (Gui.winner == 1) Gui.scorePlayer1++;
|
||||
if (Gui.winner == 2) Gui.scorePlayer2++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
17
Java/TicTacToe/src/de/craftix/tictactoe/ButtonPlacement.java
Normal file
17
Java/TicTacToe/src/de/craftix/tictactoe/ButtonPlacement.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package de.craftix.tictactoe;
|
||||
|
||||
public class ButtonPlacement {
|
||||
|
||||
public static void place() {
|
||||
Gui.button[0].setBounds(175, 50, 150, 150);
|
||||
Gui.button[1].setBounds(325, 50, 150, 150);
|
||||
Gui.button[2].setBounds(475, 50, 150, 150);
|
||||
Gui.button[3].setBounds(175, 200, 150, 150);
|
||||
Gui.button[4].setBounds(325, 200, 150, 150);
|
||||
Gui.button[5].setBounds(475, 200, 150, 150);
|
||||
Gui.button[6].setBounds(175, 350, 150, 150);
|
||||
Gui.button[7].setBounds(325, 350, 150, 150);
|
||||
Gui.button[8].setBounds(475, 350, 150, 150);
|
||||
}
|
||||
|
||||
}
|
||||
44
Java/TicTacToe/src/de/craftix/tictactoe/Draw.java
Normal file
44
Java/TicTacToe/src/de/craftix/tictactoe/Draw.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package de.craftix.tictactoe;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Draw extends JLabel {
|
||||
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
|
||||
|
||||
g.setColor(new Color(146, 146, 146, 150));
|
||||
g.fillRect(0, 0, 800, 600);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawLine(325, 50, 325, 500);
|
||||
g.drawLine(475, 50, 475, 500);
|
||||
g.drawLine(175, 200, 625, 200);
|
||||
g.drawLine(175, 350, 625, 350);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
if (Gui.player == 1) g.drawString("Spieler: X", 25, 50);
|
||||
if (Gui.player == 2) g.drawString("Spieler: O", 25, 50);
|
||||
|
||||
g.drawString("Wins X: " + Gui.scorePlayer1, 25, 65);
|
||||
g.drawString("Wins O: " + Gui.scorePlayer2, 25, 80);
|
||||
|
||||
if (Gui.winner == 1) g.drawString("Gewinner: X", 25, 100);
|
||||
if (Gui.winner == 2) g.drawString("Gewinner: O", 25, 100);
|
||||
if (Gui.winner == 3) g.drawString("Unentschieden", 25, 100);
|
||||
|
||||
for (int i = 0; i < Gui.state.length; i++) {
|
||||
if (Gui.state[i] == 1) {
|
||||
g.drawImage(ImageLoader.imgX, Gui.button[i].getX(), Gui.button[i].getY(), null);
|
||||
}else if (Gui.state[i] == 2) {
|
||||
g.drawImage(ImageLoader.imgO, Gui.button[i].getX(), Gui.button[i].getY(), null);
|
||||
}
|
||||
}
|
||||
|
||||
repaint();
|
||||
}
|
||||
|
||||
}
|
||||
157
Java/TicTacToe/src/de/craftix/tictactoe/Function.java
Normal file
157
Java/TicTacToe/src/de/craftix/tictactoe/Function.java
Normal file
@@ -0,0 +1,157 @@
|
||||
package de.craftix.tictactoe;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
public class Function {
|
||||
|
||||
public static void reset() {
|
||||
Arrays.fill(Gui.state, 0);
|
||||
Gui.player = new Random().nextBoolean() ? 1 : 2;
|
||||
Gui.winner = 0;
|
||||
if (Gui.player == 2) calculateNextField(Gui.useKI);
|
||||
}
|
||||
|
||||
public static boolean checkWinning() {
|
||||
ArrayList<Point> points = new ArrayList<>();
|
||||
for (int i = 0; i < Gui.state.length; i++) if (Gui.state[i] == Gui.player) points.add(getCoordinates(i + 1));
|
||||
|
||||
if (points.contains(new Point(1, 1)) && points.contains(new Point(1, 2)) && points.contains(new Point(1, 3))) return true;
|
||||
if (points.contains(new Point(2, 1)) && points.contains(new Point(2, 2)) && points.contains(new Point(2, 3))) return true;
|
||||
if (points.contains(new Point(3, 1)) && points.contains(new Point(3, 2)) && points.contains(new Point(3, 3))) return true;
|
||||
|
||||
if (points.contains(new Point(1, 1)) && points.contains(new Point(2, 1)) && points.contains(new Point(3, 1))) return true;
|
||||
if (points.contains(new Point(1, 2)) && points.contains(new Point(2, 2)) && points.contains(new Point(3, 2))) return true;
|
||||
if (points.contains(new Point(1, 3)) && points.contains(new Point(2, 3)) && points.contains(new Point(3, 3))) return true;
|
||||
|
||||
if (points.contains(new Point(1, 1)) && points.contains(new Point(2, 2)) && points.contains(new Point(3, 3))) return true;
|
||||
return points.contains(new Point(1, 3)) && points.contains(new Point(2, 2)) && points.contains(new Point(3, 1));
|
||||
}
|
||||
|
||||
public static boolean checkWinning(int player) {
|
||||
ArrayList<Point> points = new ArrayList<>();
|
||||
for (int i = 0; i < Gui.state.length; i++) if (Gui.state[i] == player) points.add(getCoordinates(i + 1));
|
||||
|
||||
if (points.contains(new Point(1, 1)) && points.contains(new Point(1, 2)) && points.contains(new Point(1, 3))) return true;
|
||||
if (points.contains(new Point(2, 1)) && points.contains(new Point(2, 2)) && points.contains(new Point(2, 3))) return true;
|
||||
if (points.contains(new Point(3, 1)) && points.contains(new Point(3, 2)) && points.contains(new Point(3, 3))) return true;
|
||||
|
||||
if (points.contains(new Point(1, 1)) && points.contains(new Point(2, 1)) && points.contains(new Point(3, 1))) return true;
|
||||
if (points.contains(new Point(1, 2)) && points.contains(new Point(2, 2)) && points.contains(new Point(3, 2))) return true;
|
||||
if (points.contains(new Point(1, 3)) && points.contains(new Point(2, 3)) && points.contains(new Point(3, 3))) return true;
|
||||
|
||||
if (points.contains(new Point(1, 1)) && points.contains(new Point(2, 2)) && points.contains(new Point(3, 3))) return true;
|
||||
return points.contains(new Point(1, 3)) && points.contains(new Point(2, 2)) && points.contains(new Point(3, 1));
|
||||
}
|
||||
|
||||
private static Point getCoordinates(int btnIndex) {
|
||||
Point p = new Point(0, 0);
|
||||
if (btnIndex <= 3) p.y = 1;
|
||||
if (btnIndex >= 4 && btnIndex <= 6) p.y = 2;
|
||||
if (btnIndex >= 7) p.y = 3;
|
||||
if (btnIndex == 1 || btnIndex == 4 || btnIndex == 7) p.x = 1;
|
||||
if (btnIndex == 2 || btnIndex == 5 || btnIndex == 8) p.x = 2;
|
||||
if (btnIndex == 3 || btnIndex == 6 || btnIndex == 9) p.x = 3;
|
||||
return p;
|
||||
}
|
||||
|
||||
public static void calculateNextField(boolean useKI) {
|
||||
if (!useKI) return;
|
||||
ArrayList<Integer> freeFields = new ArrayList<>();
|
||||
for (int i = 0; i < Gui.state.length; i++) if (Gui.state[i] == 0) freeFields.add(i);
|
||||
if (freeFields.size() == 0) return;
|
||||
if (freeFields.size() == 1) {
|
||||
Gui.state[freeFields.get(0)] = 2;
|
||||
return;
|
||||
}
|
||||
if (Gui.diffLevel == 1) {
|
||||
int rand = new Random().nextInt(freeFields.size() - 1);
|
||||
Gui.state[freeFields.get(rand)] = 2;
|
||||
if (Function.checkWinning()) Gui.winner = Gui.player;
|
||||
Gui.player = 1;
|
||||
}
|
||||
else if (Gui.diffLevel == 2) {
|
||||
if (new Random().nextBoolean()) {
|
||||
if (calculateWinning()) return;
|
||||
if (checkPositions(1, 2, 3)) return;
|
||||
if (checkPositions(4, 5, 6)) return;
|
||||
if (checkPositions(7, 8, 9)) return;
|
||||
if (checkPositions(1, 4, 7)) return;
|
||||
if (checkPositions(2, 5, 8)) return;
|
||||
if (checkPositions(3, 6, 9)) return;
|
||||
if (checkPositions(1, 5, 9)) return;
|
||||
if (checkPositions(7, 5, 3)) return;
|
||||
int rand = new Random().nextInt(freeFields.size() - 1);
|
||||
Gui.state[freeFields.get(rand)] = 2;
|
||||
}else {
|
||||
int rand = new Random().nextInt(freeFields.size() - 1);
|
||||
Gui.state[freeFields.get(rand)] = 2;
|
||||
}
|
||||
if (Function.checkWinning()) Gui.winner = Gui.player;
|
||||
Gui.player = 1;
|
||||
}
|
||||
else if (Gui.diffLevel == 3) {
|
||||
if (calculateWinning()) return;
|
||||
if (checkPositions(1, 2, 3)) return;
|
||||
if (checkPositions(4, 5, 6)) return;
|
||||
if (checkPositions(7, 8, 9)) return;
|
||||
if (checkPositions(1, 4, 7)) return;
|
||||
if (checkPositions(2, 5, 8)) return;
|
||||
if (checkPositions(3, 6, 9)) return;
|
||||
if (checkPositions(1, 5, 9)) return;
|
||||
if (checkPositions(7, 5, 3)) return;
|
||||
int rand = new Random().nextInt(freeFields.size() - 1);
|
||||
Gui.state[freeFields.get(rand)] = 2;
|
||||
if (Function.checkWinning()) Gui.winner = Gui.player;
|
||||
Gui.player = 1;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean checkPositions(int index1, int index2, int index3) {
|
||||
int fieldsPlayer1 = 0;
|
||||
int freeIndex = -1;
|
||||
if (Gui.state[index1 - 1] == 1) fieldsPlayer1++;
|
||||
if (Gui.state[index2 - 1] == 1) fieldsPlayer1++;
|
||||
if (Gui.state[index3 - 1] == 1) fieldsPlayer1++;
|
||||
if (Gui.state[index1 - 1] == 0) freeIndex = index1 - 1;
|
||||
if (Gui.state[index2 - 1] == 0) freeIndex = index2 - 1;
|
||||
if (Gui.state[index3 - 1] == 0) freeIndex = index3 - 1;
|
||||
if (fieldsPlayer1 == 2 && freeIndex != -1) {
|
||||
Gui.state[freeIndex] = 2;
|
||||
Gui.player = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean calculateWinning() {
|
||||
if (checkPositionsOwn(1, 2, 3)) return true;
|
||||
if (checkPositionsOwn(4, 5, 6)) return true;
|
||||
if (checkPositionsOwn(7, 8, 9)) return true;
|
||||
if (checkPositionsOwn(1, 4, 7)) return true;
|
||||
if (checkPositionsOwn(2, 5, 8)) return true;
|
||||
if (checkPositionsOwn(3, 6, 9)) return true;
|
||||
if (checkPositionsOwn(1, 5, 9)) return true;
|
||||
return checkPositionsOwn(3, 5, 7);
|
||||
}
|
||||
|
||||
private static boolean checkPositionsOwn(int index1, int index2, int index3) {
|
||||
int fieldsPlayer2 = 0;
|
||||
int freeIndex = -1;
|
||||
if (Gui.state[index1 - 1] == 2) fieldsPlayer2++;
|
||||
if (Gui.state[index2 - 1] == 2) fieldsPlayer2++;
|
||||
if (Gui.state[index3 - 1] == 2) fieldsPlayer2++;
|
||||
if (Gui.state[index1 - 1] == 0) freeIndex = index1 - 1;
|
||||
if (Gui.state[index2 - 1] == 0) freeIndex = index2 - 1;
|
||||
if (Gui.state[index3 - 1] == 0) freeIndex = index3 - 1;
|
||||
if (fieldsPlayer2 == 2 && freeIndex != -1) {
|
||||
Gui.state[freeIndex] = 2;
|
||||
Gui.player = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
100
Java/TicTacToe/src/de/craftix/tictactoe/Gui.java
Normal file
100
Java/TicTacToe/src/de/craftix/tictactoe/Gui.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package de.craftix.tictactoe;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class Gui {
|
||||
|
||||
public static JButton[] button = new JButton[9];
|
||||
public static int[] state = new int[9];
|
||||
public static int player = 0;
|
||||
public static int winner = 0;
|
||||
public static boolean useKI = false;
|
||||
public static int diffLevel = 1;
|
||||
|
||||
public static int scorePlayer1 = 0;
|
||||
public static int scorePlayer2 = 0;
|
||||
|
||||
public void setup(ImageLoader imgLoader) {
|
||||
player = new Random().nextBoolean() ? 1 : 2;
|
||||
imgLoader.loadImages();
|
||||
|
||||
JFrame frame = new JFrame();
|
||||
Draw draw = new Draw();
|
||||
JButton reset = new JButton("Spiel zurücksetzten");
|
||||
JButton mode = new JButton("Modus: 2 Spieler");
|
||||
JButton difficulty = new JButton("Einfach");
|
||||
|
||||
frame.setSize(800, 600);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setResizable(false);
|
||||
frame.setTitle("TicTacToe");
|
||||
|
||||
for (int i = 0; i < button.length; i++) {
|
||||
button[i] = new JButton();
|
||||
button[i].setVisible(true);
|
||||
button[i].addActionListener(new ActionHandler());
|
||||
button[i].setFocusPainted(false);
|
||||
button[i].setContentAreaFilled(false);
|
||||
button[i].setBorder(null);
|
||||
frame.add(button[i]);
|
||||
}
|
||||
|
||||
ButtonPlacement.place();
|
||||
|
||||
reset.setBounds(630, 500, 150, 40);
|
||||
reset.setVisible(true);
|
||||
reset.setBackground(new Color(51, 102, 153));
|
||||
reset.setForeground(Color.WHITE);
|
||||
reset.setFocusPainted(false);
|
||||
reset.addActionListener(e -> Function.reset());
|
||||
frame.add(reset);
|
||||
|
||||
mode.setBounds(630, 450, 150, 40);
|
||||
mode.setVisible(true);
|
||||
mode.setBackground(new Color(51, 102, 153));
|
||||
mode.setForeground(Color.WHITE);
|
||||
mode.setFocusPainted(false);
|
||||
mode.addActionListener(e -> {
|
||||
if (!useKI) {
|
||||
mode.setText("Modus: 1 Spieler");
|
||||
difficulty.setVisible(true);
|
||||
}else {
|
||||
mode.setText("Modus: 2 Spieler");
|
||||
difficulty.setVisible(false);
|
||||
}
|
||||
useKI = !useKI;
|
||||
if (player == 2) Function.calculateNextField(useKI);
|
||||
});
|
||||
frame.add(mode);
|
||||
|
||||
difficulty.setBounds(630, 400, 150, 40);
|
||||
difficulty.setVisible(false);
|
||||
difficulty.setBackground(new Color(51, 102, 153));
|
||||
difficulty.setForeground(Color.WHITE);
|
||||
difficulty.setFocusPainted(false);
|
||||
difficulty.addActionListener(e -> {
|
||||
diffLevel++;
|
||||
if (diffLevel > 3) diffLevel = 1;
|
||||
if (diffLevel == 1) {
|
||||
difficulty.setText("Einfach");
|
||||
}
|
||||
else if (diffLevel == 2) {
|
||||
difficulty.setText("Mittel");
|
||||
}
|
||||
else if (diffLevel == 3) {
|
||||
difficulty.setText("Schwer");
|
||||
}
|
||||
});
|
||||
frame.add(difficulty);
|
||||
|
||||
draw.setBounds(0, 0, 800, 600);
|
||||
draw.setVisible(true);
|
||||
frame.add(draw);
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
23
Java/TicTacToe/src/de/craftix/tictactoe/ImageLoader.java
Normal file
23
Java/TicTacToe/src/de/craftix/tictactoe/ImageLoader.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package de.craftix.tictactoe;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ImageLoader {
|
||||
|
||||
public static BufferedImage imgX;
|
||||
public static BufferedImage imgO;
|
||||
|
||||
public void loadImages() {
|
||||
try {
|
||||
/*imgX = ImageIO.read(new File("res/x.png"));
|
||||
imgO = ImageIO.read(new File("res/o.png"));*/
|
||||
imgX = ImageIO.read(Objects.requireNonNull(BufferedImage.class.getClassLoader().getResource("x.png")));
|
||||
imgO = ImageIO.read(Objects.requireNonNull(BufferedImage.class.getClassLoader().getResource("o.png")));
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
}
|
||||
10
Java/TicTacToe/src/de/craftix/tictactoe/Main.java
Normal file
10
Java/TicTacToe/src/de/craftix/tictactoe/Main.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package de.craftix.tictactoe;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Gui gui = new Gui();
|
||||
gui.setup(new ImageLoader());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user