hilfe bei java projekt memory

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

  • hilfe bei java projekt memory

    hallo leute,

    habe in der uni ein einzelprojekt aufgebrummt bekommen, doch leider steck ich mitten in der lernphase für meine klausuren und muss auch noch mein assembler projekt fertigkriegen :( ...
    hoffe ihr könnt mir helfen:

    muss ein memory spiel programmieren mit: - einem 6x6 feld,
    - einem counter, der die spielzüge zählt ...und
    - einem timer, der die spielzeit ausgibt...
    - ...achja, und natürlich mit gui (awt oder swing)

    ich hoffe ihr könnt mir helfen, bin schon am verzweifeln und brauch unbedingt den schein...

    danke im voraus
  • hallo,

    ich bin mit sicherheit auch kein java ass, aber mir fallen spontan ne paar sachen ein:

    -grid layout verwenden (6x6 felder)
    -jedem memory eine "id" verpassen die sich wie folg zusammensetzt: als erstezahl eine int-zahl zwischen 1-6 die du zum vergleichen brauchst. heißt du hast die erste zahl immer zweimal.
    zweitezahl eine randomzahl zwischen 1-6 mit der du zu begin des spiels die position auf dem gridlayout festlegst. du musst natürlich dafür sorgen, dass nicht zwei auf dem selben liegen!


    thx, truespin
  • ...fast fertig

    hallo leute...ich nochmal...also...ich habs fast fertig gekriegt...mit fehlen noch n paar sachen, und zwar:
    - zeig er mir die karten nicht richtig an (ein grid-layout wäre vielleicht sinniger gewesen, kriegs aber nicht hin)
    - der timer und der counter, der die spielzüge zählt, fehlt noch , hoffe ihr könnt mir weiterhelfen


    Quellcode

    1. package memory;
    2. import java.awt.event.MouseEvent;
    3. import java.awt.event.MouseListener;
    4. import java.io.File;
    5. import java.util.List;
    6. import javax.imageio.ImageIO;
    7. import javax.swing.ImageIcon;
    8. import javax.swing.JLabel;
    9. import javax.swing.JPanel;
    10. public class Card extends JLabel implements MouseListener
    11. {
    12. private int typ;
    13. private ImageIcon[] image;
    14. private List<Card> cardList;
    15. private Card selectedCard;
    16. private JPanel jpanel;
    17. public Card(int typ, List<Card> cardList, JPanel jpanel)
    18. {
    19. super();
    20. this.setSize(80, 80);
    21. this.addMouseListener(this);
    22. try
    23. {
    24. this.image = new ImageIcon[2];
    25. this.image[0] = new ImageIcon(ImageIO.read(new File("img\\card"+typ+".jpg")));
    26. this.image[1] = new ImageIcon(ImageIO.read(new File("img\\cardback.png")));
    27. this.setIcon(this.image[1]);
    28. }
    29. catch(Exception e)
    30. {
    31. e.printStackTrace();
    32. }
    33. this.typ = typ;
    34. this.cardList = cardList;
    35. this.jpanel = jpanel;
    36. jpanel.add(this);
    37. }
    38. public void setSelectedCard(Card card)
    39. {
    40. this.selectedCard = card;
    41. }
    42. public void mouseClicked(MouseEvent arg0)
    43. {
    44. if(this.selectedCard == null)
    45. {
    46. this.setIcon(this.image[0]);
    47. for(Card card:this.cardList)
    48. card.setSelectedCard(this);
    49. }
    50. else if(this.selectedCard.equals(this))
    51. return;
    52. else
    53. {
    54. new Thread(new Runnable(){
    55. public void run()
    56. {
    57. setIcon(image[0]);
    58. jpanel.repaint();
    59. }
    60. }).start();
    61. try
    62. {
    63. Thread.sleep(200L);
    64. }
    65. catch(Exception e)
    66. {
    67. e.printStackTrace();
    68. }
    69. if(this.typ == this.selectedCard.getTyp())
    70. {
    71. this.jpanel.remove(this);
    72. this.jpanel.remove(this.selectedCard);
    73. for(Card card:this.cardList)
    74. card.setSelectedCard(null);
    75. this.jpanel.repaint();
    76. }
    77. else
    78. {
    79. this.setIcon(this.image[1]);
    80. this.selectedCard.setIcon(this.image[1]);
    81. for(Card card:this.cardList)
    82. card.setSelectedCard(null);
    83. }
    84. }
    85. }
    86. public int getTyp()
    87. {
    88. return(this.typ);
    89. }
    90. public void mouseEntered(MouseEvent arg0){}
    91. public void mouseExited(MouseEvent arg0){}
    92. public void mousePressed(MouseEvent arg0){}
    93. public void mouseReleased(MouseEvent arg0){}
    94. }
    Alles anzeigen


    Quellcode

    1. package memory;
    2. import java.util.ArrayList;
    3. import java.util.Collections;
    4. import java.util.List;
    5. import javax.swing.BorderFactory;
    6. import javax.swing.JPanel;
    7. public class GamePanel extends JPanel
    8. {
    9. public GamePanel()
    10. {
    11. super();
    12. this.setLocation(10, 10);
    13. this.setSize(600, 500);
    14. this.setLayout(null);
    15. this.setBorder(BorderFactory.createRaisedBevelBorder());
    16. List<Card> shuffleList = new ArrayList<Card>();
    17. shuffleList.add(new Card(0, shuffleList, this));
    18. shuffleList.add(new Card(0, shuffleList, this));
    19. shuffleList.add(new Card(1, shuffleList, this));
    20. shuffleList.add(new Card(1, shuffleList, this));
    21. shuffleList.add(new Card(2, shuffleList, this));
    22. shuffleList.add(new Card(2, shuffleList, this));
    23. shuffleList.add(new Card(3, shuffleList, this));
    24. shuffleList.add(new Card(3, shuffleList, this));
    25. shuffleList.add(new Card(4, shuffleList, this));
    26. shuffleList.add(new Card(4, shuffleList, this));
    27. shuffleList.add(new Card(5, shuffleList, this));
    28. shuffleList.add(new Card(5, shuffleList, this));
    29. shuffleList.add(new Card(6, shuffleList, this));
    30. shuffleList.add(new Card(6, shuffleList, this));
    31. shuffleList.add(new Card(7, shuffleList, this));
    32. shuffleList.add(new Card(7, shuffleList, this));
    33. shuffleList.add(new Card(8, shuffleList, this));
    34. shuffleList.add(new Card(8, shuffleList, this));
    35. shuffleList.add(new Card(9, shuffleList, this));
    36. shuffleList.add(new Card(9, shuffleList, this));
    37. shuffleList.add(new Card(10, shuffleList, this));
    38. shuffleList.add(new Card(10, shuffleList, this));
    39. shuffleList.add(new Card(11, shuffleList, this));
    40. shuffleList.add(new Card(11, shuffleList, this));
    41. shuffleList.add(new Card(12, shuffleList, this));
    42. shuffleList.add(new Card(12, shuffleList, this));
    43. shuffleList.add(new Card(13, shuffleList, this));
    44. shuffleList.add(new Card(13, shuffleList, this));
    45. shuffleList.add(new Card(14, shuffleList, this));
    46. shuffleList.add(new Card(14, shuffleList, this));
    47. shuffleList.add(new Card(15, shuffleList, this));
    48. shuffleList.add(new Card(15, shuffleList, this));
    49. shuffleList.add(new Card(16, shuffleList, this));
    50. shuffleList.add(new Card(17, shuffleList, this));
    51. shuffleList.add(new Card(17, shuffleList, this));
    52. Collections.shuffle(shuffleList);
    53. for(int x = 10, y = 10, i = 0; i < shuffleList.size(); i++)
    54. {
    55. Card card = shuffleList.get(i);
    56. card.setLocation(x, y);
    57. x+= 90;
    58. if(i == 5)
    59. {
    60. y+= 90;
    61. x = 10;
    62. if (i == 11)
    63. {
    64. y+=180;
    65. x=10;
    66. }
    67. }
    68. }
    69. }
    70. }
    Alles anzeigen


    Quellcode

    1. package memory;
    2. import javax.swing.JFrame;
    3. import javax.swing.UIManager;
    4. public class Main extends Object
    5. {
    6. private static final int LOOK_AND_FEEL_ID = 1;
    7. private static final String[] LOOK_AND_FEEL_CLASSES = {
    8. "javax.swing.plaf.metal.MetalLookAndFeel",
    9. "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel",
    10. "com.sun.java.swing.plaf.motif.MotifLookAndFeel",
    11. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel",
    12. "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"
    13. };
    14. public Main()
    15. {
    16. super();
    17. try
    18. {
    19. UIManager.setLookAndFeel(
    20. Main.LOOK_AND_FEEL_CLASSES[Main.LOOK_AND_FEEL_ID]);
    21. }catch(Exception e){}
    22. JFrame jframe = new JFrame();
    23. jframe.setTitle("Memory");
    24. jframe.setSize(740, 530);
    25. jframe.setResizable(false);
    26. jframe.setLayout(null);
    27. GamePanel gamePanel = new GamePanel();
    28. jframe.add(gamePanel);
    29. /*MenuPanel menuPanel = new MenuPanel();
    30. jframe.add(menuPanel);*/
    31. jframe.setVisible(true);
    32. }
    33. public static void main(String[] args)
    34. {
    35. new Main();
    36. }
    37. }
    Alles anzeigen



    ich bedanke mich schonmal im voraus
  • Warum das GridLayout nicht funktioniert hab ich auf die Schnelle auch nicht rausgefunden.

    Allerdings hab ich einen Workaround für die Sichtbarkeiten der Karten gefunden:

    Quellcode

    1. private Card getThis()
    2. {
    3. return this;
    4. }
    5. public void mouseClicked(MouseEvent arg0)
    6. {
    7. if (this.selectedCard == null)
    8. {
    9. this.setIcon(this.image[0]);
    10. for (Card card : this.cardList)
    11. card.setSelectedCard(this);
    12. }
    13. else if (this.selectedCard.equals(this)) return;
    14. else
    15. {
    16. setIcon(image[0]);
    17. new Thread(new Runnable()
    18. {
    19. public void run()
    20. {
    21. try
    22. {
    23. Thread.sleep(4000L);
    24. }
    25. catch (Exception e)
    26. {
    27. e.printStackTrace();
    28. }
    29. if (typ == selectedCard.getTyp())
    30. {
    31. jpanel.remove(getThis());
    32. jpanel.remove(selectedCard);
    33. for (Card card : cardList)
    34. card.setSelectedCard(null);
    35. jpanel.repaint();
    36. }
    37. else
    38. {
    39. setIcon(image[1]);
    40. selectedCard.setIcon(image[1]);
    41. for (Card card : cardList)
    42. card.setSelectedCard(null);
    43. }
    44. }
    45. }).start();
    46. }
    47. }
    Alles anzeigen


    Das das enorm hässlicher Code ist, sieht man ja (nicht zur Nachahmung empfohlen). Aber du kannst es ja gern schön machen.
    Es passt zumindest zu deinem Code. :D

    Du hast sehr viele Selbstbesteigungen in deinem Code drin. Die Card bekommt ne Liste von Cads in der sie selbst drinnen ist und weist sich sich selbst als Listener zu.
    Ach ja, du solltest vielleicht noch dafür sorgen, dass die Anwendung richtig beendet wird, wenn man das Fenster schließt. Momentan läuft sie im Hintergrund weiter. Dazu musst du beim WindowAdapter die windowClosing()-Methode überschreiben, damit die System.exit(0); aufruft.