7 e mezzo


This is some text prior to the author information. You can change this text from the admin section of WP-Gravatar  To change this standard text, you have to enter some information about your self in the Dashboard -> Users -> Your Profile box. Read more from this author


//Created by mathieu albanese
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
public class SetteMezzo extends JPanel implements Runnable {
	// Variabili
	private JLabel giocatore, computer;
 
	private JPanel panel1, panel2, panel3;
 
	private JPanel[] emptyPanels1, emptyPanels2, emptyPanels3;
 
	private Carta[] carte;
 
	private int carta, counterGiocatore, counterComputer;
 
	private Thread thread;
 
	private boolean active;
 
	// Costruttore
	public SetteMezzo() {
		thread = new Thread(this);
		thread.start();
		thread.suspend();
 
		panel1 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel1.setBackground(new Color(0, 128, 0));
 
		panel2 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel2.setBackground(new Color(0, 128, 0));
 
		panel3 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel3.setBackground(new Color(0, 128, 0));
 
		carte = new Carta[41];
		for (int i = 0; i < 41; i++)
			carte[i] = new Carta(i);
 
		emptyPanels1 = new JPanel[10];
		for (int i = 0; i < 10; i++) {
			emptyPanels1[i] = new JPanel();
			emptyPanels1[i].setBackground(new Color(0, 128, 0));
		}
 
		emptyPanels2 = new JPanel[10];
		for (int i = 0; i < 10; i++) {
			emptyPanels2[i] = new JPanel();
			emptyPanels2[i].setBackground(new Color(0, 128, 0));
		}
 
		emptyPanels3 = new JPanel[10];
		for (int i = 0; i < 10; i++) {
			emptyPanels3[i] = new JPanel();
			emptyPanels3[i].setBackground(new Color(0, 128, 0));
		}
 
		setBackground(new Color(0, 128, 0));
		setPreferredSize(new Dimension(1020, 600));
		setBorder(BorderFactory.createLoweredBevelBorder());
		setLayout(new GridLayout(3, 1, 1, 1));
 
		// Components
		giocatore = new JLabel("");
		giocatore.setFont(new Font("Verdana", 1, 13));
		giocatore.setHorizontalAlignment(JLabel.CENTER);
		giocatore.setForeground(Color.white);
 
		computer = new JLabel("");
		computer.setFont(new Font("Verdana", 1, 13));
		computer.setHorizontalAlignment(JLabel.CENTER);
		computer.setForeground(Color.white);
 
		panel1.add(giocatore, 0);
		for (int i = 1; i < 11; i++) {
			panel1.add(emptyPanels1[i - 1], i);
		}
 
		panel2.add(carte[0], 0);
		for (int i = 1; i < 11; i++) {
			panel2.add(emptyPanels2[i - 1], i);
		}
 
		panel3.add(computer, 0);
		for (int i = 1; i < 11; i++) {
			panel3.add(emptyPanels3[i - 1], i);
		}
 
		add(panel1);
		add(panel2);
		add(panel3);
	}
 
	public void startGame() {
		generaCarta();
		counterGiocatore++;
		giocatore.setText("Giocatore");
		panel1.remove(counterGiocatore);
		panel1.add(carte[carta], counterGiocatore);
		updateScoreGiocatore();
	}
 
	public void updateScoreGiocatore() {
		SetteMezzoMain.punteggioG.setForeground(Color.red);
		double score = Double.valueOf(SetteMezzoMain.punteggioG.getText())
				.doubleValue();
		if (carta == 30) {
			score = 7.5;
			SetteMezzoMain.punteggioG.setText("" + score);
			verifyValue(score);
			return;
		} else if (carta < 8)
			score = score + carta;
		else if (carta == 8 || carta == 9 || carta == 10)
			score = score + 0.5;
		else {
			StringBuffer s = new StringBuffer("" + carta);
			String lastNString = new String("" + s.charAt(1));
			int lastN = Integer.valueOf(lastNString).intValue();
			if (lastN > 0 && lastN < 8)
				score = score + lastN;
			else
				score = score + 0.5;
		}
		SetteMezzoMain.punteggioG.setText("" + score);
		verifyValue(score);
	}
 
	public void updateScoreComputer() {
		double score = Double.valueOf(SetteMezzoMain.punteggioC.getText())
				.doubleValue();
		if (carta == 30) {
			score = 7.5;
			SetteMezzoMain.punteggioC.setText("" + score);
			verifyValueComputer(score);
			return;
		} else if (carta < 8)
			score = score + carta;
		else if (carta == 8 || carta == 9 || carta == 10)
			score = score + 0.5;
		else {
			StringBuffer s = new StringBuffer("" + carta);
			String lastNString = new String("" + s.charAt(1));
			int lastN = Integer.valueOf(lastNString).intValue();
			if (lastN > 0 && lastN < 8)
				score = score + lastN;
			else
				score = score + 0.5;
		}
		SetteMezzoMain.punteggioC.setText("" + score);
		verifyValueComputer(score);
 
	}
 
	public void verifyValueComputer(double score) {
		if (score > 7.5) {
			active = false;
 
			SetteMezzoMain.ris.setText("Il computer ha sballato - Hai vinto!");
			SetteMezzoMain.btnGiu.setEnabled(false);
			SetteMezzoMain.btnSto.setEnabled(false);
			SetteMezzoMain.btnNew.setEnabled(true);
			int puntata = Integer.valueOf(SetteMezzoMain.puntata1.getText())
					.intValue();
			int cassa = Integer.valueOf(SetteMezzoMain.cassa1.getText())
					.intValue();
			SetteMezzoMain.cassa1.setText("" + (cassa + puntata * 2));
			SetteMezzoMain.puntata1.setText("0");
			SetteMezzoMain.giu.setText("");
			thread.suspend();
		} else if (score > 4.5) {
			active = false;
 
			SetteMezzoMain.giu.setText("Sto!");
			double scoreGiocatore = Double.valueOf(
					SetteMezzoMain.punteggioG.getText()).doubleValue();
			if (score < scoreGiocatore) {
				SetteMezzoMain.ris.setText("Hai vinto!");
				SetteMezzoMain.btnGiu.setEnabled(false);
				SetteMezzoMain.btnSto.setEnabled(false);
				SetteMezzoMain.btnNew.setEnabled(true);
				int puntata = Integer
						.valueOf(SetteMezzoMain.puntata1.getText()).intValue();
				int cassa = Integer.valueOf(SetteMezzoMain.cassa1.getText())
						.intValue();
				SetteMezzoMain.cassa1.setText("" + (cassa + puntata * 2));
				SetteMezzoMain.puntata1.setText("0");
			} else {
				SetteMezzoMain.ris.setText("Hai perso");
				SetteMezzoMain.btnGiu.setEnabled(false);
				SetteMezzoMain.btnSto.setEnabled(false);
				SetteMezzoMain.btnNew.setEnabled(true);
				SetteMezzoMain.puntata1.setText("0");
			}
			thread.suspend();
		}
	}
 
	public void verifyValue(double score) {
		if (score > 7.5) {
			SetteMezzoMain.ris.setText("Sballato!");
			SetteMezzoMain.btnGiu.setEnabled(false);
			SetteMezzoMain.btnSto.setEnabled(false);
			SetteMezzoMain.btnNew.setEnabled(true);
			SetteMezzoMain.puntata1.setText("0");
			active = false;
			thread.suspend();
		}
	}
 
	public void generaCarta() {
		carta = (int) ((Math.random() * 40) + 1);
		if (!carte[carta].state)
			carte[carta].state = true;
		else
			generaCarta();
	}
 
	public void giu() {
		if (counterGiocatore < 10) {
			generaCarta();
			counterGiocatore++;
			panel1.remove(counterGiocatore);
			panel1.add(carte[carta], counterGiocatore);
			updateScoreGiocatore();
		}
	}
 
	public void sto() {
		computer.setText("Computer");
		generaCarta();
		counterComputer++;
		panel3.remove(counterComputer);
		panel3.add(carte[carta], counterComputer);
		updateScoreComputer();
		thread.resume();
		active = true;
	}
 
	public void reset() {
		counterGiocatore = 0;
		counterComputer = 0;
		carta = 0;
		giocatore.setText("");
		computer.setText("");
 
		remove(panel1);
		remove(panel2);
		remove(panel3);
 
		panel1 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel1.setBackground(new Color(0, 128, 0));
 
		panel2 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel2.setBackground(new Color(0, 128, 0));
 
		panel3 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel3.setBackground(new Color(0, 128, 0));
 
		panel1.add(giocatore, 0);
		for (int i = 1; i < 11; i++) {
			panel1.add(emptyPanels1[i - 1], i);
		}
 
		panel2.add(carte[0], 0);
		for (int i = 1; i < 11; i++) {
			panel2.add(emptyPanels2[i - 1], i);
		}
 
		panel3.add(computer, 0);
		for (int i = 1; i < 11; i++) {
			panel3.add(emptyPanels3[i - 1], i);
		}
		add(panel1);
		add(panel2);
		add(panel3);
 
		carte = new Carta[41];
		for (int i = 0; i < 41; i++)
			carte[i] = new Carta(i);
 
		updateUI();
 
	}
 
	public void run() {
		while (true) {
			if (active) {
				try {
					thread.sleep(200);
				} catch (Exception e) {
				}
				if (counterComputer < 10) {
					generaCarta();
					counterComputer++;
					panel3.remove(counterComputer);
					panel3.add(carte[carta], counterComputer);
					updateScoreComputer();
				}
			}
		}
	}
 
	public static void main(String[] args) {
		JFrame frame = new JFrame("SetteMezzo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new SetteMezzo());
		frame.pack();
		frame.setVisible(true);
	}
}

Articoli simili

  • Articoli simili
  1. Posted dicembre 19, 2007 at 10:54 am

    in fase di compilazione, questo programma da SOLO 41 errori! :D

  2. Posted gennaio 3, 2009 at 1:23 am

    Ma dov\’è la classe Carta?? Da un sacco di errori in compilazione.

Lascia un commento

Your email is never published nor shared. Required fields are marked *

*
*

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a