ancient-projects

My earliest programs (2006-2010)
git clone https://git.tronto.net/ancient-projects
Log | Files | Refs | README

Main.java (6028B)


      1 /*
      2 Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org>
      3 
      4 This file is part of JBriscola.
      5 
      6     JBriscola is free software; you can redistribute it and/or modify
      7     it under the terms of the GNU General Public License as published by
      8     the Free Software Foundation; either version 2 of the License, or
      9     (at your option) any later version.
     10 
     11     JBriscola is distributed in the hope that it will be useful,
     12     but WITHOUT ANY WARRANTY; without even the implied warranty of
     13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14     GNU General Public License for more details.
     15 
     16     You should have received a copy of the GNU General Public License
     17     along with JBriscola; if not, write to the Free Software
     18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     19 
     20 */
     21 
     22 import javax.swing.*;
     23 
     24 public class Main {
     25 	public static int semeBriscola, punteggio, punteggioCPU, mano, cartaDaPescare;	
     26 	public static boolean toccaAllaCPU = false;
     27 	public static boolean CPUHaGiocato = false;
     28 	public static boolean haGiocatoPrima = true;
     29 	public static Frame frame;
     30 	public static void main( String args[] ) {
     31 		frame = new Frame();
     32 		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     33 		frame.setVisible( true );
     34 		frame.setResizable( false );
     35 		frame.setSize( 600, 550 );
     36 	}
     37 	public void mischia() {
     38 		mano = 0;
     39 		cartaDaPescare = 6;
     40 		ImageIcon aux;
     41 		for ( int i = 0, k = 0; i < 10; i++ )
     42 			for ( int j = 0; j < 4; j++, k++ )
     43 				frame.arrayCarteMescolate[k] = frame.arrayCarte[i][j];
     44 		for ( int i = 0; i < 391; i++ ) {
     45 			double a = Math.random() * 40;
     46 			int x = (int) a;
     47 			double b = Math.random() * 40;
     48 			int y = (int) b;
     49 			aux = frame.arrayCarteMescolate[x];
     50 			frame.arrayCarteMescolate[x] = frame.arrayCarteMescolate[y];
     51 			frame.arrayCarteMescolate[y] = aux;
     52 		}
     53 		char auxChar[] = frame.arrayCarteMescolate[39].toString().toCharArray();
     54 		semeBriscola = auxChar[auxChar.length-5] - 49;
     55 	}
     56 	public void giocaCPU() {
     57 		int cartaDaGiocare;
     58 		/*******************************************/
     59 		if ( frame.carteCPU[0] != null )
     60 			cartaDaGiocare = 0;
     61 		else if ( frame.carteCPU[1] != null )
     62 			cartaDaGiocare = 1;
     63 		else
     64 			cartaDaGiocare = 2;
     65 		/*******************************************/
     66 		frame.giocata[1] = frame.carteCPU[cartaDaGiocare];
     67 		frame.carteCPU[cartaDaGiocare] = null;
     68 		frame.pannello.repaint();
     69 		CPUHaGiocato = true;
     70 		if ( frame.giocata[0] == null )
     71 			toccaAllaCPU = false;
     72 	}
     73 	public void chiPrende() {
     74 		char aux[], auxCPU[];
     75 		aux = frame.giocata[0].toString().toCharArray();
     76 		auxCPU = frame.giocata[1].toString().toCharArray();
     77 		giocatorePrende(   ( (aux[aux.length-5] - 49 == semeBriscola) && (auxCPU[auxCPU.length-5] - 49 != semeBriscola) )
     78 			|| ( (aux[aux.length-5] == auxCPU[auxCPU.length-5]) && laPrimaEPiuAlta( aux[aux.length-7] - 48, auxCPU[auxCPU.length-7] - 48 ) )
     79 			|| ( (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && (aux[aux.length-5] != auxCPU[auxCPU.length-5]) && haGiocatoPrima ) );
     80 	}
     81 	public boolean laPrimaEPiuAlta( int prima, int seconda ) {
     82 		int a, b;
     83 		switch ( prima ) {
     84 			case 0:
     85 				a = 10;
     86 				break;
     87 			case 1:
     88 				a = 12;
     89 				break;
     90 			case 3:
     91 				a = 11;
     92 				break;
     93 			default:
     94 				a = prima;
     95 				break;
     96 		}
     97 		switch ( seconda ) {
     98 			case 0:
     99 				b = 10;
    100 				break;
    101 			case 1:
    102 				b = 12;
    103 				break;
    104 			case 3:
    105 				b = 11;
    106 				break;
    107 			default:
    108 				b = seconda;
    109 				break;
    110 		}
    111 		return a > b;
    112 	}
    113 	public void giocatorePrende( boolean giocatore ) {
    114 		if ( giocatore ) {
    115 			toccaAllaCPU = CPUHaGiocato = false;
    116 			haGiocatoPrima = true;
    117 		}
    118 		else {
    119 			toccaAllaCPU = true;
    120 			CPUHaGiocato = haGiocatoPrima = false;
    121 		}
    122 		daiPunti( giocatore );
    123 		if ( toccaAllaCPU )
    124 			giocaCPU();
    125 	}
    126 	public void daiPunti( boolean giocatore ) {
    127 		char aux[], auxCPU[];
    128 		aux = frame.giocata[0].toString().toCharArray();
    129 		auxCPU = frame.giocata[1].toString().toCharArray();
    130 		int punti = 0;
    131 		switch( aux[aux.length-7] - 48 ) {
    132 			case 0:
    133 				punti += 4;
    134 				break;
    135 			case 1:
    136 				punti += 11;
    137 				break;
    138 			case 3:
    139 				punti += 10;
    140 				break;
    141 			case 8:
    142 				punti += 2;
    143 				break;
    144 			case 9:
    145 				punti += 3;
    146 				break;
    147 			default:
    148 				break;
    149 		}
    150 		switch( auxCPU[auxCPU.length-7] - 48 ) {
    151 			case 0:
    152 				punti += 4;
    153 				break;
    154 			case 1:
    155 				punti += 11;
    156 				break;
    157 			case 3:
    158 				punti += 10;
    159 				break;
    160 			case 8:
    161 				punti += 2;
    162 				break;
    163 			case 9:
    164 				punti += 3;
    165 				break;
    166 			default:
    167 				break;
    168 		}
    169 		frame.giocata[0] = frame.giocata[1] = null;
    170 		if ( giocatore ) {
    171 			punteggio += punti;
    172 			if ( mano < 18 ) {
    173 				if ( frame.carteInMano[0] == null )
    174 					frame.carteInMano[0] = frame.arrayCarteMescolate[cartaDaPescare];
    175 				if ( frame.carteInMano[1] == null )
    176 					frame.carteInMano[1] = frame.arrayCarteMescolate[cartaDaPescare];
    177 				if ( frame.carteInMano[2] == null )
    178 					frame.carteInMano[2] = frame.arrayCarteMescolate[cartaDaPescare];
    179 				cartaDaPescare++;
    180 				if ( frame.carteCPU[0] == null )
    181 					frame.carteCPU[0] = frame.arrayCarteMescolate[cartaDaPescare];
    182 				if ( frame.carteCPU[1] == null )
    183 					frame.carteCPU[1] = frame.arrayCarteMescolate[cartaDaPescare];
    184 				if ( frame.carteCPU[2] == null )
    185 					frame.carteCPU[2] = frame.arrayCarteMescolate[cartaDaPescare];
    186 				cartaDaPescare++;
    187 			}
    188 		}
    189 		else {
    190 			punteggioCPU += punti;
    191 			if ( mano < 18 ) {
    192 				if ( frame.carteCPU[0] == null )
    193 					frame.carteCPU[0] = frame.arrayCarteMescolate[cartaDaPescare];
    194 				if ( frame.carteCPU[1] == null )
    195 					frame.carteCPU[1] = frame.arrayCarteMescolate[cartaDaPescare];
    196 				if ( frame.carteCPU[2] == null )
    197 					frame.carteCPU[2] = frame.arrayCarteMescolate[cartaDaPescare];
    198 				cartaDaPescare++;
    199 				if ( frame.carteInMano[0] == null )
    200 					frame.carteInMano[0] = frame.arrayCarteMescolate[cartaDaPescare];
    201 				if ( frame.carteInMano[1] == null )
    202 					frame.carteInMano[1] = frame.arrayCarteMescolate[cartaDaPescare];
    203 				if ( frame.carteInMano[2] == null )
    204 					frame.carteInMano[2] = frame.arrayCarteMescolate[cartaDaPescare];
    205 				cartaDaPescare++;
    206 			}
    207 		}
    208 		frame.pannello.repaint();
    209 		frame.repaint();
    210 	}
    211 }