Advertisement
Guest User

Untitled

a guest
May 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. public class Card extends JButton {
  4.  
  5.     private int id;
  6.     private boolean matched=false;
  7.  
  8.     public int getId() {
  9.         return id;
  10.     }
  11.  
  12.     public void setId(int id) {
  13.         this.id = id;
  14.     }
  15.  
  16.     public boolean isMatched() {
  17.         return this.matched;
  18.     }
  19.  
  20.     public void setMatched(boolean matched) {
  21.         this.matched = matched;
  22.     }
  23. }
  24.  
  25.  
  26. import javax.swing.*;
  27. import java.awt.*;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import java.util.ArrayList;
  31. import java.util.Collections;
  32. import java.util.List;
  33.  
  34. public class Board extends JFrame {
  35.  
  36.     private List<Card> cards;
  37.     private Card selectedCard;
  38.     private Card c1;
  39.     private Card c2;
  40.     private Timer t;
  41.  
  42.     public Board(Object selectedGameSize){
  43.  
  44.         Timer gameTime = new Timer(0, new ActionListener() {
  45.             @Override
  46.             public void actionPerformed(ActionEvent e) {
  47.                 //konczy odmierzac czas gdy isGameWon jest true
  48.             }
  49.         });
  50.  
  51.         //watek do czasu
  52.  
  53.         int pairs = (int)selectedGameSize;
  54.         List<Card> cardsList = new ArrayList<Card>();
  55.         List<Integer> cardVals = new ArrayList<Integer>(); //podstawic tu obrazki zamiasty values
  56.  
  57.         for(int i=0; i<pairs; i++){
  58.             cardVals.add(i);
  59.             cardVals.add(i);
  60.         }
  61.         Collections.shuffle(cardVals);
  62.  
  63.         for(int val : cardVals){
  64.             Card c = new Card();
  65.             c.setId(val);
  66.             c.addActionListener(new ActionListener() {
  67.                 @Override
  68.                 public void actionPerformed(ActionEvent e) {
  69.                     selectedCard=c;
  70.                     doTurn();
  71.                 }
  72.             });
  73.             cardsList.add(c);
  74.         }
  75.  
  76.         this.cards = cardsList;
  77.  
  78.         t=new Timer(250, new ActionListener() {
  79.             @Override
  80.             public void actionPerformed(ActionEvent e) {
  81.                 checkCards();
  82.             }
  83.         });
  84.  
  85.  
  86.         t.setRepeats(false);
  87.  
  88.         Container pane = getContentPane();
  89.         pane.setLayout(new GridLayout(3, 3));
  90.         for(Card c :cards){
  91.             pane.add(c);
  92.         }
  93.         setTitle("Memory Match");
  94.  
  95.     }
  96.  
  97.  
  98.     public void doTurn(){
  99.         if(c1==null && c2==null){
  100.             c1=selectedCard;
  101.             c1.setText(String.valueOf(c1.getId()));
  102.         }
  103.  
  104.         if(c1 != null && c1 != selectedCard && c2 == null){
  105.             c2 = selectedCard;
  106.             c2.setText(String.valueOf(c2.getId()));
  107.             t.start();
  108.         }
  109.     }
  110.  
  111.     public void checkCards(){
  112.         if(c1.getId() == c2.getId()){
  113.             c1.setEnabled(false);
  114.             c2.setEnabled(false);
  115.             c1.setMatched(true);
  116.             c2.setMatched(true);
  117.             String msgwin = "You Win!\nYour Time Is: \n";
  118.             if(this.isGameWon()){
  119.                 JOptionPane.showMessageDialog(this, msgwin);
  120.                //zrobic zeby powracalo do menu i gasilo okno gry
  121.  
  122.                 MainMenu mm = new MainMenu();
  123.             }
  124.         }
  125.         else{
  126.             c1.setText("");
  127.             c2.setText("");
  128.         }
  129.         c1=null;
  130.         c2=null;
  131.     }
  132.  
  133.     public boolean isGameWon(){
  134.         return this.cards.stream().allMatch(c -> c.isMatched());
  135.     }
  136.  
  137.     private void getCardImg(){
  138.  
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement