Jogo

1 resposta
ajloureiro
Este é um jogo da memória que tenho que fazer para uma disciplina.

tenho dificuldades em tornar as cartas aleatório, se me pudessem ajudar agradecia

Obrigado!!!  :D :lol:

import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.List;

public class layout extends JFrame implements ActionListener {

	carta botao[] = new carta [10];
	JLabel score, blank, tempo;
	int s = 0;
	int index,a,b,current;
	int contador = 0;
	//Timer t;
	private boolean primeiroClick;
	private JCheckBox Som;
	private JMenuItem itemCriarJogo;
	private JMenuItem itemReiniJogo;
	private JMenuItem Highscore;
	private JMenuItem itemAjuda;
	private JMenuItem itemSairJogo;
	private JTextField nActual;
	private JTextField nTotal;
	private JTextField nTempo;
	private AudioClip sound;
	ImageIcon imagem;
	ImageIcon padrao  = new ImageIcon("cartatras.gif");

	public layout(){
		super();
		botao[0] = new carta (new ImageIcon("10.gif"), 0);
		botao[1] = new carta (new ImageIcon("As.gif"), 1);
		botao[2] = new carta (new ImageIcon("dama.gif"), 2);
		botao[3] = new carta (new ImageIcon("rei.gif"), 3);
		botao[4] = new carta (new ImageIcon("valete.gif"), 4);
		botao[5] = new carta (new ImageIcon("10.gif"), 0);
		botao[6] = new carta (new ImageIcon("As.gif"), 1);
		botao[7] = new carta (new ImageIcon("dama.gif"), 2);
		botao[8] = new carta (new ImageIcon("rei.gif"), 3);
		botao[9] = new carta (new ImageIcon("valete.gif"), 4);

		setTitle("Jogo da Memória");
		setSize(800,600);

		getContentPane().setLayout(new GridLayout());
		JPanel mainGrid = new JPanel (new GridLayout (2 , 5));

		for(int i =0; i<botao.length;i++){
			botao[i].addActionListener(this);
			mainGrid.add(botao[i]);
		}

		primeiroClick = true;
		itemCriarJogo = new JMenuItem("Criar jogo");
		itemCriarJogo.addActionListener(this);
		itemReiniJogo = new JMenuItem("Reiniciar jogo");
		itemReiniJogo.addActionListener(this);
		Highscore = new JMenuItem("Highscore");
		Highscore.addActionListener(this);
		itemAjuda = new JMenuItem("Ajuda");
		itemAjuda.addActionListener(this);
		itemSairJogo = new JMenuItem("Sair jogo");
		itemSairJogo.addActionListener(this);
		nTotal = new JTextField(4);
		nActual = new JTextField(4);
		nTempo = new JTextField(4);
		Som = new JCheckBox();
		Som.addActionListener(this);
		//sound = getAudioClip(this.getCodeBase(),"som.wav");

		JPanel painelCaixas = new JPanel (new FlowLayout());
		setLayout(new BorderLayout());

		painelCaixas.add(new JLabel(" Pontuação Actual "));
		painelCaixas.add(nActual);
		painelCaixas.add(new JLabel(" / "));
		painelCaixas.add(new JLabel(" Total "));
		painelCaixas.add(nTotal);
		painelCaixas.add(new JLabel(" Tempo "));
		painelCaixas.add(nTempo);
		painelCaixas.add(new JLabel(" Som "));
		painelCaixas.add(Som);

		JMenuBar barraMenu = new JMenuBar();
		JMenu ficheiro = new JMenu("Jogo");
		ficheiro.add(itemCriarJogo);
		ficheiro.add(itemReiniJogo);
		ficheiro.add(Highscore);
		ficheiro.add(itemAjuda);
		ficheiro.add(itemSairJogo);
		barraMenu.add(ficheiro);
		setJMenuBar(barraMenu);

		add(mainGrid, BorderLayout.CENTER);
		add(painelCaixas, BorderLayout.SOUTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		setVisible(true);
	}

	public void actionPerformed(ActionEvent ae) {


		for(int j = 0 ; j<botao.length; j++){
			if (ae.getSource() == botao[j] && contador == 0){
				a = botao[j].getIndex();							
				current = j;										
				botao[j].paraCima(true);					
				repaint();
				contador++;
				break;										
			}

			else if  (ae.getSource() == botao[j] && contador == 1){
				b = botao[j].getIndex(); 
				if (a == b){									
					botao[j].paraCima(true);
					repaint();
					contador = 0;
					break;
				}

				else{
					botao[j].paraCima(true);
					repaint();
					botao[current].paraCima(false);			
					botao[j].paraCima(false);				
					repaint();	
					contador = 0; 
					break;										
				}
			}
		}

		if(ae.getSource() == Som) {
			if(primeiroClick){
				sound.play();
				primeiroClick = false;
			}
			else{
				sound.stop();
				primeiroClick = true;
			}
		}

		if (ae.getSource() == itemAjuda){
			String message = "esta caixa de diálogo serve para ajudar o utilizador a navegar no jogo";
			JOptionPane pane = new JOptionPane(message);
			JDialog dialog = pane.createDialog(new JFrame(), "Ajuda");
			dialog.setSize(600, 400);
			dialog.show();
		}

		if (ae.getSource() == itemSairJogo) {
			if (JOptionPane.showConfirmDialog(new JFrame(),
					"Deseja realmente sair do jogo?", "Jogo da Memória",
					JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
				System.exit(0);
		}
	}
}

1 Resposta

dionat4n

Utilize Math.random() para gerar números randômicos entre [0;1)

Se quiser um inteiro, multiplique o número randômico e transforme-o para inteiro como preferir.

Criado 16 de junho de 2008
Ultima resposta 16 de jun. de 2008
Respostas 1
Participantes 2