From 196e88aa00267390f067ac45e952f63c527c8b66 Mon Sep 17 00:00:00 2001 From: brausjonas <47791011+jonasbraus@users.noreply.github.com> Date: Wed, 23 Nov 2022 23:24:34 +0100 Subject: [PATCH] Ready --- FourWins/.idea/.gitignore | 8 + FourWins/.idea/artifacts/VierGewinnt_jar.xml | 8 + FourWins/.idea/jpa-buddy.xml | 6 + FourWins/.idea/misc.xml | 9 + FourWins/.idea/modules.xml | 8 + FourWins/.idea/uiDesigner.xml | 124 +++++++++++ FourWins/VierGewinnt.iml | 11 + .../out/production/VierGewinnt/Canvas$1.class | Bin 0 -> 879 bytes .../out/production/VierGewinnt/Canvas.class | Bin 0 -> 3090 bytes .../out/production/VierGewinnt/Color.class | Bin 0 -> 837 bytes .../out/production/VierGewinnt/Control.class | Bin 0 -> 3241 bytes FourWins/out/production/VierGewinnt/GUI.class | Bin 0 -> 2218 bytes .../out/production/VierGewinnt/Line.class | Bin 0 -> 446 bytes .../VierGewinnt/META-INF/MANIFEST.MF | 3 + .../out/production/VierGewinnt/Main.class | Bin 0 -> 385 bytes FourWins/src/Canvas.java | 108 ++++++++++ FourWins/src/Color.java | 3 + FourWins/src/Control.java | 193 ++++++++++++++++++ FourWins/src/GUI.java | 54 +++++ FourWins/src/Line.java | 9 + FourWins/src/META-INF/MANIFEST.MF | 3 + FourWins/src/Main.java | 5 + 22 files changed, 552 insertions(+) create mode 100644 FourWins/.idea/.gitignore create mode 100644 FourWins/.idea/artifacts/VierGewinnt_jar.xml create mode 100644 FourWins/.idea/jpa-buddy.xml create mode 100644 FourWins/.idea/misc.xml create mode 100644 FourWins/.idea/modules.xml create mode 100644 FourWins/.idea/uiDesigner.xml create mode 100644 FourWins/VierGewinnt.iml create mode 100644 FourWins/out/production/VierGewinnt/Canvas$1.class create mode 100644 FourWins/out/production/VierGewinnt/Canvas.class create mode 100644 FourWins/out/production/VierGewinnt/Color.class create mode 100644 FourWins/out/production/VierGewinnt/Control.class create mode 100644 FourWins/out/production/VierGewinnt/GUI.class create mode 100644 FourWins/out/production/VierGewinnt/Line.class create mode 100644 FourWins/out/production/VierGewinnt/META-INF/MANIFEST.MF create mode 100644 FourWins/out/production/VierGewinnt/Main.class create mode 100644 FourWins/src/Canvas.java create mode 100644 FourWins/src/Color.java create mode 100644 FourWins/src/Control.java create mode 100644 FourWins/src/GUI.java create mode 100644 FourWins/src/Line.java create mode 100644 FourWins/src/META-INF/MANIFEST.MF create mode 100644 FourWins/src/Main.java 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 0000000000000000000000000000000000000000..b60612807a6cc15725e470861418965c24e382c0 GIT binary patch literal 879 zcmaJK z(D=b0;Eyssx0`$-Ho51XbM86Mb6)!U&(B`~Hu0o@JeIY%X`z6cg%dM z;e>r9Q0jVtdObMaSJ4}}@6%K1hOYE?W#nml>@+lkfZYiK6*<0)V@1;%BiC9aw2#9< ztQ_BSk5pe^vEKbChq5U@B~3L{LDKB$_ie4VbphftPltz`^Z$tznA}OEd(@LB<6^9x za1gobrKig+jyhh~WNc?UaQ!g$f`gt)4#U2QyEdkS!Sx+>;kH@Mp3`5JYEIVNQGi7#E{gdVde>YWcO9sW3Z z{!2inT-F@}yxG3?@Jyiufig9Rc9wUkv3d&8SUW|g@g3Q(Y2YQw90P$lR4A{c$t7II z3@s+5kX#>Mu8sj?!fC+j>KP2<3+8xFG`>OnK+bV{58y`yw!8wD(jgi-$IHZmKb=QY6go;2H~oNnxi5+_^K83mNhNDNwOeHVsJ#OhQb6641iU%+1V&;m$aB z5|WCoRjQ&DU$wr_B3k03J`%~K7NxBZ)K=?S{?xVnPt;ZX_PxoFEc%04nSJ&-d!K#w z-b8Hm%b`5eND&L zbv&du?4Yq<)VYd>inQ&9bj{HQ`O+mTUdOr16-%~tbVxwWrk7ISvm?bx?l zK~EQ)a;cD4h^D%ImZza?$>yc#i4#1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r$wmTAYRQB$){TR`g! zYGqqv_}j(6C66knWj1G}VcN=F z8(P>myVx^*?3V-lZXF;Lj}wF@zqkwRjA`CH&(3&?8W#z~=W&qV)dQHtA>70e-o`NA z!4bTRV=97C)xt0CcJ!$SaZ-)KRB4PU7io11hWUH;IlRQT zI}ye+sL*~VyN@7WY7cTLzNm0O{R@MX1~8!B;rpG8>$rN0UUVWv>m&FGz0tUWF~o1v zaZSfz9Y>Vn?;ssV)qfG-C#KrhZ{7B9X#C&uALR9}z-`Wua8)@V+K=Bx&5E-C&vYD% zGfvv~Cq*k)X7OEt&ib@TGZ literal 0 HcmV?d00001 diff --git a/FourWins/out/production/VierGewinnt/Color.class b/FourWins/out/production/VierGewinnt/Color.class new file mode 100644 index 0000000000000000000000000000000000000000..26fdbbdac762fba2cfb9037a0a17cd17001a2baa GIT binary patch literal 837 zcmZuuVNcUg6g_Y4)~=Kds-hbx3aIVSO%nXHgVqbI%iHNaKcf?PGUbCb&Z*kG$tiM=YZUEgS@y=kT%0~+{+u< zqctdYl*X?2%I9+^*>c)^0u@VqwaY4=qD|Y%5EU!vs943Cibd2^)KO!o%_rvSM0SHH zB)OZqQE)yQo_cOVmd}f`^aE!y=@w|SrCnF;?(fi3s`UQCeuJ9>bdx`@)a1PqBBBZn zH$cu5ZP3k+G6nmJ(j|n?6fxYQepAQpGrfj}Q1wYJwTQ2COiLJ9B3y7a=4Qqd0yhjZZBS4&$>1g%|dS07r}a&`5gPkripyOvLVGpx{W-x+>l=tEaB z=j^l3Is5GW?QiePmp4EE1Ar!+kK!I2jo>K^Mo^8X4IGO?7fm&W3=BtM;J7SK7NGGPp8i z4R{7}3g)O|r;^$BOfKymQ&8Qvs)iKwG1o~dM2;WtY0soG?qdoiiJa@&X|F$J&DySl zvAUy;Bo=n0@4O*@v>!MO|qq^s`b=GP~S?RHczFaERpK;Qj?RLy0>?zO5r1^%Pb*u*~6GZYiks4=kzH3UyyGVuWRG79Ec&divo zMV&&~T3!lZo0!Ep2EW(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>X`wDO1KX_AVcuQ@<+iDx$QQPrTwG%&6O?Xc|j0>t2 z7gYy-uKICF9m8dnz!haxD+GXvVm;boIs%f2eH!>Lb9{+K literal 0 HcmV?d00001 diff --git a/FourWins/out/production/VierGewinnt/GUI.class b/FourWins/out/production/VierGewinnt/GUI.class new file mode 100644 index 0000000000000000000000000000000000000000..142edf53322de72f2e980bddd37b4ba3d609f367 GIT binary patch literal 2218 zcma)7TXz#x6#k}7n`s^@)vLvwVTE6(;TK*=N%WqH8G}J!0va-)P`|QiNZ)g7c=eOSh9Kp8<>_D~y z`!Sp#xe&)l2S#yG$XEg~TuR_&jLY4Gkjo|}WxW!|E3!@r$;C09Ko_oxof+ZG3c1#S zIb1h!!^Ep5UW?=PINnI04_P5^ny_V^Pauzi2_@@-cqxiLExGL{9naCH4Or<0w|~Y!EbAAPfzENqQHdur&7#t?PC=JwKHWb?aAaImPO(H+iJV^v@@mA96qtqV2H)5#m)Rmqr4X%)qwwc|Bio+=2=$e2UMQ;#A4j0@i1- z7~SVM6~`AAzQk7s4$L|XB=GVTo8mrLP2PK?Q*-@$BlQZ%VC?PzJQp1Sqsz7}-bDKe}U?24!H<^c=KB zV=Dc1VVJFGm&;1p>cB=Z{AcRwZN+Mt+OJJ;z4;m)Ia!t*)@t1?b3CEK&@QTB$9Daq zf&LAVH+hM&krz@z4y?T+%_@rC->X$AvQR%f*o_r)C&5z>>R9OS8_K= z6;D3GQ-ZD)lDZ_51{R?cdx>K|jJj$*Oh!xOTM7)?6Mt4sut(|5Lc0tQZVs* de_E%1uu0!+=<^laSbyJo{lgpTpNqVl{}S6otBttL^egXY;4=uQ81=Q3hIjHmByGJi=#Bp z(mbus70msEp@KCi9<)L$Omlrbd5(2?7sVMXov=uvY#5cPvc{7qrndO)E2YLItO~e2UK9b!w@k=ERn`rz0#(-92Kku6DxEcJr~iz lYF+Qnp#xqsK%WQ$YrhfiJMjPj literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..6009b89e57213f7c70c4ee19f7a5e4d97af8d8b3 GIT binary patch literal 385 zcmZutu};G<5Pg@VF$sm17Rt<47|4Rup{PqGrT{}x5n^(J6$SNP6(#elOl9dZPd+b znJIfGvyz!!TII5wORHV|O9u6`eqoLDT52=q?Ij~&FnmlGaxIfmnj)FiR-0nns7qV$ zeE**|A)Zw$o2x7B>>{Ul?nK%MAqZkxnc7w*o46LCyLGtD7AmiCf`DJ>jt9OJ1jik& z5=LTdoozs`O*`TkHYEFU-u@ZDE@wc5J?t~%mPd^kbMNpTHt_Ga0`4CHpT~kfA`v?1 LGEX?*=%M!wPPIdT literal 0 HcmV?d00001 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(); + } +}