This commit is contained in:
brausjonas
2022-11-23 23:24:34 +01:00
commit 196e88aa00
22 changed files with 552 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
+8
View File
@@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="VierGewinnt:jar">
<output-path>$PROJECT_DIR$/out/artifacts/VierGewinnt_jar</output-path>
<root id="archive" name="VierGewinnt.jar">
<element id="module-output" name="VierGewinnt" />
</root>
</artifact>
</component>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JpaBuddyIdeaProjectConfig">
<option name="renamerInitialized" value="true" />
</component>
</project>
+9
View File
@@ -0,0 +1,9 @@
<?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>
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/VierGewinnt.iml" filepath="$PROJECT_DIR$/VierGewinnt.iml" />
</modules>
</component>
</project>
+124
View 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.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>
+11
View File
@@ -0,0 +1,11 @@
<?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$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main
Binary file not shown.
+108
View File
@@ -0,0 +1,108 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Canvas extends JPanel
{
private int sizeX;
private int sizeY;
private Graphics2D g2d;
private Control control;
private Color[][] grid;
private GUI gui;
private Line winLine;
public Canvas(Control control, Color[][] grid, GUI gui)
{
this.grid = grid;
this.control = control;
this.gui = gui;
winLine = control.getWinLine();
addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
if (e.getButton() == 1)
{
int x = e.getX() / sizeX;
control.placeCoin(x, true);
}
}
});
}
@Override
public void paintComponent(Graphics g)
{
sizeX = gui.getWidth() / Control.fieldsCountX;
sizeY = gui.getHeight() / Control.fieldsCountY;
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(2));
int yHover = -100;
int xHover = -100;
int finalSize = Math.min(sizeX, sizeY);
if (getMousePosition() != null)
{
try
{
xHover = (int) (getMousePosition().getX()) / sizeX;
yHover = control.placeCoin(xHover, false);
} catch (Exception e)
{
}
}
for (int x = 0; x < Control.fieldsCountX; x++)
{
for (int y = 0; y < Control.fieldsCountY; y++)
{
if (grid[x][y] != null)
{
if (grid[x][y] == Color.RED)
{
g2d.setColor(java.awt.Color.RED);
} else if (grid[x][y] == Color.GREEN)
{
g2d.setColor(java.awt.Color.GREEN);
}
g2d.fillOval(x * sizeX + 3, y * sizeY + 3, finalSize - 6, finalSize - 6);
} else
{
g2d.setColor(java.awt.Color.GRAY);
g2d.drawOval(x * sizeX + 3, y * sizeY + 3, finalSize - 6, finalSize - 6);
}
}
}
if (winLine.x1 != winLine.x2 || winLine.y1 != winLine.y2)
{
g2d.setColor(java.awt.Color.GRAY);
g2d.setStroke(new BasicStroke(5));
g2d.drawLine(winLine.x1 * sizeX + sizeX / 2, winLine.y1 * sizeY + sizeY / 2, winLine.x2 * sizeX + sizeX / 2, winLine.y2 * sizeY + sizeY / 2);
}
float transparency = yHover < 0 ? 0 : 0.5f;
g2d.setColor(control.getCurrentPlayer() == Color.RED ? new java.awt.Color(1, 0, 0, transparency) : new java.awt.Color(0, 1, 0, transparency));
g2d.fillOval(xHover * sizeX - 2, yHover * sizeY - 2, finalSize + 5, finalSize + 5);
repaint();
}
}
+3
View File
@@ -0,0 +1,3 @@
public enum Color {
RED, GREEN
}
+193
View File
@@ -0,0 +1,193 @@
public class Control
{
public static int fieldsCountX = 15, fieldsCountY = 10;
private Color[][] grid;
private Color currentPlayer = Color.RED;
private GUI gui;
private Line winLine = new Line();
public Control()
{
gui = new GUI(this);
boolean success = false;
while(!success)
{
String[] size = new String[2];
try
{
size = gui.showSettings().split(",");
}
catch(NullPointerException e)
{
System.exit(0);
}
try
{
fieldsCountX = Integer.parseInt(size[0]);
fieldsCountY = Integer.parseInt(size[1]);
success = true;
} catch (NumberFormatException e)
{
}
}
grid = new Color[fieldsCountX][fieldsCountY];
gui.createGameWindow(grid);
}
public int placeCoin(int x, boolean realPlace)
{
if (grid[x][0] == null)
{
for (int i = 0; i < fieldsCountY; i++)
{
if (grid[x][i] != null)
{
if (realPlace)
{
grid[x][i - 1] = currentPlayer;
if (checkWin())
{
gui.showWinner(currentPlayer);
resetGame();
}
currentPlayer = currentPlayer == Color.RED ? Color.GREEN : Color.RED;
}
return i - 1;
} else if (i == fieldsCountY - 1)
{
if (realPlace)
{
grid[x][i] = currentPlayer;
if (checkWin())
{
gui.showWinner(currentPlayer);
resetGame();
}
currentPlayer = currentPlayer == Color.RED ? Color.GREEN : Color.RED;
}
return i;
}
}
}
return -1;
}
private void resetGame()
{
winLine.reset();
for(int x = 0; x < fieldsCountX; x++)
{
for(int y = 0; y < fieldsCountY; y++)
{
grid[x][y] = null;
}
}
}
private boolean checkWin()
{
for (int x = 0; x < fieldsCountX; x++) {
for (int y = 0; y < fieldsCountY; y++) {
if (grid[x][y] == currentPlayer) {
int countHorizontal = 0;
int countVertical = 0;
int countDiagonal1 = 0;
int countDiagonal2 = 0;
for (int i = 1; i <= 3; i++) {
if(x + i < fieldsCountX) {
if(grid[x + i][y] == currentPlayer)
{
countHorizontal++;
if(countHorizontal == 3)
{
winLine.x1 = x;
winLine.x2 = x + i;
winLine.y1 = winLine.y2 = y;
return true;
}
}
if(y + i < fieldsCountY) {
if(grid[x + i][y + i] == currentPlayer)
{
countDiagonal1++;
if(countDiagonal1 == 3)
{
winLine.x1 = x;
winLine.x2 = x + i;
winLine.y1 = y;
winLine.y2 = y + i;
return true;
}
}
}
}
if(y + i < fieldsCountY) {
if(grid[x][y + i] == currentPlayer)
{
countVertical++;
if(countVertical == 3)
{
winLine.x1 = winLine.x2 = x;
winLine.y1 = y;
winLine.y2 = y + i;
return true;
}
}
if(x - i >= 0) {
if(grid[x - i][y + i] == currentPlayer)
{
countDiagonal2++;
if(countDiagonal2 == 3)
{
winLine.x1 = x;
winLine.x2 = x - i;
winLine.y1 = y;
winLine.y2 = y + i;
return true;
}
}
}
}
}
}
}
}
for(int i = 0; i < fieldsCountX; i++)
{
if(grid[i][0] == null)
{
return false;
}
}
gui.showWinner(null);
resetGame();
return false;
}
public Line getWinLine()
{
return winLine;
}
public Color getCurrentPlayer()
{
return currentPlayer;
}
}
+54
View File
@@ -0,0 +1,54 @@
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame {
private Canvas canvas;
private Control control;
public GUI(Control control){
this.control = control;
}
public void createGameWindow(Color[][] grid)
{
getContentPane().setPreferredSize(new Dimension(Control.fieldsCountX * 50, Control.fieldsCountY * 50));
pack();
setLocationRelativeTo(null);
setTitle("Vier gewinnt");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
canvas = new Canvas(this.control, grid, this);
add(canvas);
setVisible(true);
}
public void showWinner(Color color)
{
String opt = color == Color.RED ? "Rot" : "Gruen";
opt = color == null ? "Niemand" : opt;
JOptionPane.showMessageDialog(new JFrame(), opt + " hat gewonnen");
}
public String showSettings()
{
return JOptionPane.showInputDialog(new JFrame(), "Wir groß soll das Spiel sein? \n breite,hoehe");
}
public int getWidth()
{
return getContentPane().getWidth();
}
public int getHeight()
{
return getContentPane().getHeight();
}
}
+9
View File
@@ -0,0 +1,9 @@
public class Line
{
public int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
public void reset()
{
x1 = x2 = y1 = y2 = 0;
}
}
+3
View File
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main
+5
View File
@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
new Control();
}
}