Einfaches Vier Gewinnt mit GUI

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Einfaches Vier Gewinnt mit GUI

    Hallo,

    ich musste als Scheinaufgabe ein Spiel inkl. GUI programmieren. Vielleicht kann das dem ein oder anderem helfen, wenn er mal dieselbe Aufgabe zu lösen hat. Ist allerdings schlecht dokumentiert. Bestimmt kann man es verbessern oder erweitern.

    Quellcode

    1. import java.awt.*;
    2. import javax.swing.*;
    3. import java.net.*;
    4. import java.awt.event.*;
    5. public class Test extends JFrame
    6. {
    7. private Spiel game = new Spiel(this);
    8. private static final long serialVersionUID = 1L;
    9. private JButton[] button = new JButton[7];
    10. private JLabel[] field = new JLabel[42];
    11. private JLabel display;
    12. private JPanel centerPanel;
    13. JMenuItem neu;
    14. private Icon g;
    15. private Icon r;
    16. URL resc = ImageIcon.class.getResource( "/images/unten.png" );
    17. URL rescGreen = ImageIcon.class.getResource( "/images/apple-green.png" );
    18. URL rescRed = ImageIcon.class.getResource( "/images/apple-red.png" );
    19. public Test() throws UnsupportedLookAndFeelException,
    20. ClassNotFoundException, IllegalAccessException, InstantiationException
    21. {
    22. super("Vier Gewinnt");
    23. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    24. JMenuBar menu = new JMenuBar();
    25. JMenu option = new JMenu("Spiel");
    26. neu = new JMenuItem("Neues Spiel starten");
    27. option.add(neu); menu.add(option); //setJMenuBar(menu);
    28. UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
    29. Icon icon = new ImageIcon( resc );
    30. g = new ImageIcon(rescGreen);
    31. r = new ImageIcon(rescRed);
    32. display = new JLabel("Rot beginnt.");
    33. JPanel menuPanel = new JPanel();
    34. menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    35. menuPanel.add(menu);
    36. centerPanel = new JPanel();
    37. centerPanel.setLayout(new GridLayout(7,7));
    38. centerPanel.setBackground(Color.BLACK);
    39. JPanel labelPanel = new JPanel();
    40. labelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    41. labelPanel.add(display);
    42. add(menuPanel, BorderLayout.NORTH);
    43. add(centerPanel, BorderLayout.CENTER);
    44. add(labelPanel, BorderLayout.SOUTH);
    45. for(int i = 0; i<7; i++)
    46. {
    47. button[i] = new JButton(icon);
    48. button[i].addActionListener(this.game);
    49. centerPanel.add(button[i]);
    50. }
    51. for(int i = 0; i<42; i++)
    52. {
    53. field[i] = new JLabel();
    54. field[i].setBorder(BorderFactory
    55. .createLineBorder(Color.WHITE));
    56. centerPanel.add(field[i]);
    57. }
    58. ActionListener al = new ActionListener()
    59. {
    60. public void actionPerformed(ActionEvent ae)
    61. {
    62. Object object = ae.getSource();
    63. if(object == neu)
    64. {
    65. for(int i = 0; i<7; i++)
    66. {
    67. // falls jemand waehrend des Spiels neu starten will
    68. button[i].removeActionListener(game);
    69. game.clean();
    70. button[i].addActionListener(game);
    71. }
    72. for(int i = 0; i<42; i++)
    73. {
    74. field[i].setIcon(null);
    75. }
    76. }
    77. }
    78. };
    79. neu.addActionListener(al);
    80. setSize(347, 409);
    81. }
    82. public JButton getButton(int i)
    83. {
    84. return button[i];
    85. }
    86. public void setIcon(int i, int color)
    87. {
    88. if(color == 0)
    89. field[i].setIcon(g);
    90. if(color==1)
    91. field[i].setIcon(r);
    92. }
    93. public void setDisplay(String s)
    94. {
    95. display.setText(s);
    96. }
    97. public void gameOver()
    98. {
    99. for(int i = 0; i<7; i++)
    100. button[i].removeActionListener(game);
    101. }
    102. public static void main(String[] args)
    103. {
    104. try{Test fläche = new Test();
    105. fläche.setVisible(true);}
    106. catch(Exception e){}
    107. System.out.println("FOO");
    108. }
    109. }
    Alles anzeigen


    Quellcode

    1. import java.awt.event.*;
    2. import javax.swing.JOptionPane;
    3. public class Spiel implements ActionListener
    4. {
    5. private int[][] board;
    6. private boolean red = true;
    7. //private boolean green = false;
    8. private Test gui;
    9. // Zeilenindizes werden von 5 bis auf 0 dekrementiert
    10. private int[] row = {5, 5, 5 ,5 ,5, 5, 5};
    11. // nummerische Werte der letzten Felder im Swing-Container
    12. private int[] index = {35, 36, 37, 38, 39, 40, 41};
    13. public Spiel(Test gui)
    14. {
    15. this.gui = gui;
    16. board = new int[6][7];
    17. }
    18. public void setField(int i, int j, int y)
    19. {
    20. board[i][j] = y;
    21. }
    22. public void actionPerformed(ActionEvent e)
    23. {
    24. Object button = e.getSource();
    25. for(int i = 0; i < 7; i++)
    26. {
    27. try
    28. {
    29. if(button == gui.getButton(i))
    30. {
    31. if(red)
    32. {
    33. gui.setIcon(index[i], 1);
    34. setField(row[i], i, 1);
    35. red = false; //green = true;
    36. index[i] -= 7;
    37. pruefeHorizontal(i, 1);
    38. pruefeVertikal(row[i], 1);
    39. pruefeDiagonal(1);
    40. row[i]--;
    41. gui.setDisplay("Gruen ist am Zug.");
    42. break;
    43. }
    44. else // (green)
    45. {
    46. gui.setIcon(index[i], 0);
    47. setField(row[i], i, 2);
    48. red = true; //green = false;
    49. index[i] -=7;
    50. pruefeHorizontal(i, 2);
    51. pruefeVertikal(row[i], 2);
    52. pruefeDiagonal(2);
    53. row[i]--;
    54. gui.setDisplay("Rot ist am Zug.");
    55. break;
    56. }
    57. }
    58. }
    59. catch(ArrayIndexOutOfBoundsException ex)
    60. {
    61. gui.setDisplay("Spalte ist voll!");
    62. //ex.printStackTrace();
    63. break;
    64. }
    65. }
    66. }
    67. /* Ueberprueft, ob es horizontale Vierer-Reihen
    68. * von der Farbe f in der Spalte c gibt.
    69. * */
    70. public void pruefeHorizontal(int c, int f)
    71. {
    72. int win = 0;
    73. for(int j = 5; j >= 0; j--)
    74. {
    75. if(j<5)
    76. {
    77. if(board[j][code=c]==f && board[j+1][code=c]==f)
    78. win++;
    79. if(win==3)
    80. {
    81. gewonnen(f); clean();
    82. gui.gameOver();
    83. break;
    84. }
    85. }
    86. }
    87. }
    88. /* Ueberprueft, ob es vertikale Vierer-Reihen
    89. * von der Farbe f in der Zeile r gibt.
    90. * */
    91. public void pruefeVertikal(int r, int f)
    92. {
    93. int win = 0;
    94. for(int j = 0; j <= 6; j++)
    95. {
    96. if(j<6)
    97. {
    98. if(board[r][j]==f && board[r][j+1]==f)
    99. win++;
    100. if(win==3)
    101. {
    102. gewonnen(f); clean();
    103. gui.gameOver();
    104. break;
    105. }
    106. }
    107. }
    108. }
    109. /*Ueberprueft alle moglichen Vierer-Diagonalen
    110. * */
    111. public void pruefeDiagonal(int f)
    112. {
    113. int win = 0;
    114. /* uebepruefe alle -potenziellen- Vierer-Diagonalen
    115. von links unten nach rechts oben
    116. */
    117. for(int i = 0; i < 4; i++)
    118. {
    119. for(int j = 3; j < 6; j++)
    120. {
    121. win = 0;
    122. int tmp = j;
    123. for(int y = i; y < 6; y++)
    124. {
    125. if(tmp>0)
    126. {
    127. if(board[tmp][y]==f && board[tmp-1][y+1]==f)
    128. win++;
    129. if(win==3)
    130. {
    131. gewonnen(f); clean();
    132. gui.gameOver();
    133. break;
    134. }
    135. }
    136. tmp--;
    137. }
    138. }
    139. }
    140. int win2 = 0;
    141. /* uebepruefe alle -potenziellen- Vierer-Diagonalen
    142. von rechts unten nach links oben
    143. */
    144. for(int i = 6; i > 3; i--)
    145. {
    146. for(int j = 3; j < 6; j++)
    147. {
    148. win2 = 0;
    149. int tmp = j;
    150. for(int y = i; y > 0; y--)
    151. {
    152. if(tmp>0)
    153. {
    154. if(board[tmp][y]==f && board[tmp-1][y-1]==f)
    155. {
    156. win2++;
    157. }
    158. if(win2==3)
    159. {
    160. gewonnen(f); clean();
    161. gui.gameOver();
    162. break;
    163. }
    164. }
    165. tmp--;
    166. }
    167. }
    168. }
    169. }
    170. // Arrays saeubern und wiederherstellen für neues Spiel
    171. public void clean()
    172. {
    173. int nI = 35;
    174. for(int i = 0; i < 7; i++)
    175. {
    176. row[i] = 5;
    177. index[i] = nI; nI++;
    178. for(int j = 0; j < 6; j++)
    179. {
    180. board[j][i] = 0;
    181. }
    182. }
    183. red = true;
    184. gui.setDisplay("Rot beginnt");
    185. }
    186. public void gewonnen(int farbe)
    187. {
    188. if(farbe==1)
    189. JOptionPane.showMessageDialog(null, "Rot hat gewonnen!" );
    190. if(farbe==2)
    191. JOptionPane.showMessageDialog(null, "Gruen hat gewonnen!" );
    192. }
    193. }
    Alles anzeigen


    Im Anhang befindet sich das Spiel als ausführbares jar-Archiv in ein ZIP-Packet verpackt. Im Jar-Archiv befinden sich auch die png's.

    ps.: es können nur zwei menschliche Spieler gegeneinander spielen.
    Dateien
    • Game.zip

      (27,98 kB, 2.638 mal heruntergeladen, zuletzt: )

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von muhammad ()