commit 196e88aa00267390f067ac45e952f63c527c8b66 Author: brausjonas <47791011+jonasbraus@users.noreply.github.com> Date: Wed Nov 23 23:24:34 2022 +0100 Ready diff --git a/FourWins/.idea/.gitignore b/FourWins/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/FourWins/.idea/.gitignore @@ -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 diff --git a/FourWins/.idea/artifacts/VierGewinnt_jar.xml b/FourWins/.idea/artifacts/VierGewinnt_jar.xml new file mode 100644 index 0000000..973573c --- /dev/null +++ b/FourWins/.idea/artifacts/VierGewinnt_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/VierGewinnt_jar + + + + + \ No newline at end of file diff --git a/FourWins/.idea/jpa-buddy.xml b/FourWins/.idea/jpa-buddy.xml new file mode 100644 index 0000000..966d5f5 --- /dev/null +++ b/FourWins/.idea/jpa-buddy.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/FourWins/.idea/misc.xml b/FourWins/.idea/misc.xml new file mode 100644 index 0000000..51890da --- /dev/null +++ b/FourWins/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/FourWins/.idea/modules.xml b/FourWins/.idea/modules.xml new file mode 100644 index 0000000..f340827 --- /dev/null +++ b/FourWins/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/FourWins/.idea/uiDesigner.xml b/FourWins/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/FourWins/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FourWins/VierGewinnt.iml b/FourWins/VierGewinnt.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/FourWins/VierGewinnt.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/FourWins/out/production/VierGewinnt/Canvas$1.class b/FourWins/out/production/VierGewinnt/Canvas$1.class new file mode 100644 index 0000000..b606128 Binary files /dev/null and b/FourWins/out/production/VierGewinnt/Canvas$1.class differ diff --git a/FourWins/out/production/VierGewinnt/Canvas.class b/FourWins/out/production/VierGewinnt/Canvas.class new file mode 100644 index 0000000..fad2763 Binary files /dev/null and b/FourWins/out/production/VierGewinnt/Canvas.class differ diff --git a/FourWins/out/production/VierGewinnt/Color.class b/FourWins/out/production/VierGewinnt/Color.class new file mode 100644 index 0000000..26fdbbd Binary files /dev/null and b/FourWins/out/production/VierGewinnt/Color.class differ diff --git a/FourWins/out/production/VierGewinnt/Control.class b/FourWins/out/production/VierGewinnt/Control.class new file mode 100644 index 0000000..e9564ce Binary files /dev/null and b/FourWins/out/production/VierGewinnt/Control.class differ diff --git a/FourWins/out/production/VierGewinnt/GUI.class b/FourWins/out/production/VierGewinnt/GUI.class new file mode 100644 index 0000000..142edf5 Binary files /dev/null and b/FourWins/out/production/VierGewinnt/GUI.class differ diff --git a/FourWins/out/production/VierGewinnt/Line.class b/FourWins/out/production/VierGewinnt/Line.class new file mode 100644 index 0000000..d4bfdf7 Binary files /dev/null and b/FourWins/out/production/VierGewinnt/Line.class differ diff --git a/FourWins/out/production/VierGewinnt/META-INF/MANIFEST.MF b/FourWins/out/production/VierGewinnt/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/FourWins/out/production/VierGewinnt/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/FourWins/out/production/VierGewinnt/Main.class b/FourWins/out/production/VierGewinnt/Main.class new file mode 100644 index 0000000..6009b89 Binary files /dev/null and b/FourWins/out/production/VierGewinnt/Main.class differ diff --git a/FourWins/src/Canvas.java b/FourWins/src/Canvas.java new file mode 100644 index 0000000..bfb441c --- /dev/null +++ b/FourWins/src/Canvas.java @@ -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(); + } +} diff --git a/FourWins/src/Color.java b/FourWins/src/Color.java new file mode 100644 index 0000000..7657b38 --- /dev/null +++ b/FourWins/src/Color.java @@ -0,0 +1,3 @@ +public enum Color { + RED, GREEN +} diff --git a/FourWins/src/Control.java b/FourWins/src/Control.java new file mode 100644 index 0000000..811e39f --- /dev/null +++ b/FourWins/src/Control.java @@ -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; + } +} diff --git a/FourWins/src/GUI.java b/FourWins/src/GUI.java new file mode 100644 index 0000000..b99ba7b --- /dev/null +++ b/FourWins/src/GUI.java @@ -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(); + } + +} diff --git a/FourWins/src/Line.java b/FourWins/src/Line.java new file mode 100644 index 0000000..e12f85d --- /dev/null +++ b/FourWins/src/Line.java @@ -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; + } +} diff --git a/FourWins/src/META-INF/MANIFEST.MF b/FourWins/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/FourWins/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/FourWins/src/Main.java b/FourWins/src/Main.java new file mode 100644 index 0000000..e63c63a --- /dev/null +++ b/FourWins/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + new Control(); + } +}