0% found this document useful (0 votes)
22 views7 pages

Mi Board

Uploaded by

Holt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Mi Board

Uploaded by

Holt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

package CardGameBien;

import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;

public class MiBoard extends JPanel {

private MyButton[][] botones = new MyButton[4][4];


private JLabel numberCupless;
private Listener l=new Listener();
private int cardsFliped;
private MyButton flipedCard=null;
private ArrayList<Integer> cardaIds = new ArrayList<Integer>();
private JButton botonRestart;
Timer timer;
private int contador;
private int cardsMached=0;

private MyButton segundaCarta;

private Iterator it;

private JButton botonWin;


private Random rand= new Random();

public MiBoard(){
this.inicialize();

//Asignamos los action Listeners


for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
botones[i][j].addActionListener(l);
}
}
botonWin.addMouseListener(l);
botonWin.addActionListener(l);
botonRestart.addActionListener(l);
timer=new Timer(1000,l);
timer.addActionListener(l);
timer.setInitialDelay(0);
timer.start();

public void inicialize(){

this.setLayout(null); //ponemos el layout


contador=0;

//<editor-fold desc="InicializacionBotones">
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4 ; j++) {
botones[i][j]=new MyButton();
if(i==0){
if(j==0) {
botones[i][j].setBounds(0, 0, 175, 175);

}
else if(j==1){
botones[i][j].setBounds(175, 0, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 0, 175, 175);
}
else{
botones[i][j].setBounds(525, 0, 175, 175);
}
}
else if(i==1){
if(j==0) {
botones[i][j].setBounds(0, 175, 175, 175);
}
else if(j==1){
botones[i][j].setBounds(175, 175, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 175, 175, 175);
}
else{
botones[i][j].setBounds(525, 175, 175, 175);
}
}
else if(i==2){
if(j==0) {
botones[i][j].setBounds(0, 350, 175, 175);
}
else if(j==1){
botones[i][j].setBounds(175, 350, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 350, 175, 175);
}
else{
botones[i][j].setBounds(525, 350, 175, 175);
}
}
else{
if(j==0) {
botones[i][j].setBounds(0, 525, 175, 175);
}
else if(j==1){
botones[i][j].setBounds(175, 525, 175, 175);
}
else if(j==2){
botones[i][j].setBounds(350, 525, 175, 175);
}
else{
botones[i][j].setBounds(525, 525, 175, 175);
}
}
this.add(botones[i][j]);
botones[i][j].setEnabled(true);
}
}
//</editor-fold>

for (int i = 1; i < 9; i++) {


cardaIds.add(i);
cardaIds.add(i);
System.out.println(cardaIds);
}

Collections.shuffle(cardaIds);
System.out.println(cardaIds);

it=cardaIds.iterator();

while(it.hasNext()){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
botones[i][j].setCard(new Card((int)it.next()));
System.out.println(botones[i][j].getCard().getId());
}
}
}

//INicializamos la etiqueta
numberCupless=new JLabel("Number of mached cards: 0");
numberCupless.setBounds(720,50,200,40);
this.add(numberCupless);

//Inicializamos el boton de restart

botonRestart=new JButton("Restart");
botonRestart.setBounds(770,150,100,20);
this.add(botonRestart);

botonWin=new JButton("Win");
botonWin.setBounds(770,220,100,20);
botonWin.setVisible(false);
this.add(botonWin);

private class Listener implements ActionListener, MouseListener{

private MyButton boton;

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer){
contador++;

// System.out.println(contador);

if(contador==6){
botonWin.setVisible(true);
}
}

if(e.getSource()==botonRestart){
JOptionPane.showMessageDialog(null,"Restarting");
this.restart();

if(e.getSource()==botonWin){
JOptionPane.showMessageDialog(null,"You have won");
System.exit(1);
}

for (int i = 0; i < 4; i++) {


for (int j = 0; j < 4; j++) {
if(e.getSource()==botones[i][j]) {
boton = botones[i][j];
}
}
}

if(e.getSource()==boton){

cardsFliped++;

if(cardsFliped==2){

((MyButton) e.getSource()).setEnabled(false);
((MyButton) e.getSource()).setText("" + ((MyButton)
e.getSource()).getCard().getId());

boton.validate();

flipedCard.getCard().setSegund(true);
segundaCarta=boton;
if(((MyButton)
e.getSource()).getCard().equals(flipedCard.getCard())){

boton.getCard().mached=true;
flipedCard.getCard().mached=true;
mached(flipedCard,boton);

cardsFliped=0;
}
else{
if(boton.getCard().isSegund()) {
if(!(flipedCard.getCard().mached)) {

try {
Thread.sleep(1000);
} catch (InterruptedException ex) {

flipedCard.setText("");
flipedCard.setEnabled(true);

segundaCarta.setText("");
segundaCarta.setEnabled(true);

cardsFliped = 0;
}
boton.getCard().setSegund(false);
}

//notMached(flipedCard,boton);

boton.validate();
}

if(cardsFliped==1){

((MyButton) e.getSource()).setEnabled(false);
((MyButton) e.getSource()).setText("" + ((MyButton)
e.getSource()).getCard().getId());

flipedCard=((MyButton) e.getSource());

if(cardsFliped>2){
cardsFliped=2;
}
}

public void mached(MyButton flipped, MyButton elDeAhora){


cardsMached++;
numberCupless.setText("Number of mached cards: " + cardsMached);

if(cardsMached==8){
JOptionPane.showMessageDialog(null,"You have won");
System.exit(1);
}
}

@Override
public void mouseClicked(MouseEvent e) {

@Override
public void mousePressed(MouseEvent e) {

@Override
public void mouseReleased(MouseEvent e) {

@Override
public void mouseEntered(MouseEvent e) {

if(e.getSource()==botonWin) {

int x = rand.nextInt(700, 850);


int y = rand.nextInt(720);
botonWin.setLocation(x, y);
JOptionPane.showMessageDialog(null, "Don't cheat");
}
}

@Override
public void mouseExited(MouseEvent e) {

public void restart(){


for (int i = 0; i < 4; i++){
for (int j = 0; j < 4 ; j++) {
botones[i][j].setEnabled(true);
}
}
Collections.shuffle(cardaIds);
System.out.println(cardaIds);

it=cardaIds.iterator();

while(it.hasNext()){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
botones[i][j].setCard(new Card((int)it.next()));
botones[i][j].setText("");
System.out.println(botones[i][j].getCard().getId());
}
}
}

numberCupless.setText("Number of mached cards: 0");

botonWin.setBounds(770,220,100,20);

botonWin.setVisible(false);

contador=0;

segundaCarta=null;

cardsFliped=0;

cardsMached=0;
}

You might also like