/* Copyright 2009 Sebastiano Tronto This file is part of JBriscola. JBriscola is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. JBriscola is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with JBriscola; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import javax.swing.*; public class Main { public static int semeBriscola, punteggio, punteggioCPU, mano, cartaDaPescare; public static boolean toccaAllaCPU = false; public static boolean CPUHaGiocato = false; public static boolean haGiocatoPrima = true; public static Frame frame; public static void main( String args[] ) { frame = new Frame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setVisible( true ); frame.setResizable( false ); frame.setSize( 600, 550 ); } public void mischia() { mano = 0; cartaDaPescare = 6; ImageIcon aux; for ( int i = 0, k = 0; i < 10; i++ ) for ( int j = 0; j < 4; j++, k++ ) frame.arrayCarteMescolate[k] = frame.arrayCarte[i][j]; for ( int i = 0; i < 391; i++ ) { double a = Math.random() * 40; int x = (int) a; double b = Math.random() * 40; int y = (int) b; aux = frame.arrayCarteMescolate[x]; frame.arrayCarteMescolate[x] = frame.arrayCarteMescolate[y]; frame.arrayCarteMescolate[y] = aux; } char auxChar[] = frame.arrayCarteMescolate[39].toString().toCharArray(); semeBriscola = auxChar[auxChar.length-5] - 49; } public void giocaCPU() { int cartaDaGiocare; /*******************************************/ if ( frame.carteCPU[0] != null ) cartaDaGiocare = 0; else if ( frame.carteCPU[1] != null ) cartaDaGiocare = 1; else cartaDaGiocare = 2; /*******************************************/ frame.giocata[1] = frame.carteCPU[cartaDaGiocare]; frame.carteCPU[cartaDaGiocare] = null; frame.pannello.repaint(); CPUHaGiocato = true; if ( frame.giocata[0] == null ) toccaAllaCPU = false; } public void chiPrende() { char aux[], auxCPU[]; aux = frame.giocata[0].toString().toCharArray(); auxCPU = frame.giocata[1].toString().toCharArray(); giocatorePrende( ( (aux[aux.length-5] - 49 == semeBriscola) && (auxCPU[auxCPU.length-5] - 49 != semeBriscola) ) || ( (aux[aux.length-5] == auxCPU[auxCPU.length-5]) && laPrimaEPiuAlta( aux[aux.length-7] - 48, auxCPU[auxCPU.length-7] - 48 ) ) || ( (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && (aux[aux.length-5] != auxCPU[auxCPU.length-5]) && haGiocatoPrima ) ); } public boolean laPrimaEPiuAlta( int prima, int seconda ) { int a, b; switch ( prima ) { case 0: a = 10; break; case 1: a = 12; break; case 3: a = 11; break; default: a = prima; break; } switch ( seconda ) { case 0: b = 10; break; case 1: b = 12; break; case 3: b = 11; break; default: b = seconda; break; } return a > b; } public void giocatorePrende( boolean giocatore ) { if ( giocatore ) { toccaAllaCPU = CPUHaGiocato = false; haGiocatoPrima = true; } else { toccaAllaCPU = true; CPUHaGiocato = haGiocatoPrima = false; } daiPunti( giocatore ); if ( toccaAllaCPU ) giocaCPU(); } public void daiPunti( boolean giocatore ) { char aux[], auxCPU[]; aux = frame.giocata[0].toString().toCharArray(); auxCPU = frame.giocata[1].toString().toCharArray(); int punti = 0; switch( aux[aux.length-7] - 48 ) { case 0: punti += 4; break; case 1: punti += 11; break; case 3: punti += 10; break; case 8: punti += 2; break; case 9: punti += 3; break; default: break; } switch( auxCPU[auxCPU.length-7] - 48 ) { case 0: punti += 4; break; case 1: punti += 11; break; case 3: punti += 10; break; case 8: punti += 2; break; case 9: punti += 3; break; default: break; } frame.giocata[0] = frame.giocata[1] = null; if ( giocatore ) { punteggio += punti; if ( mano < 18 ) { if ( frame.carteInMano[0] == null ) frame.carteInMano[0] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteInMano[1] == null ) frame.carteInMano[1] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteInMano[2] == null ) frame.carteInMano[2] = frame.arrayCarteMescolate[cartaDaPescare]; cartaDaPescare++; if ( frame.carteCPU[0] == null ) frame.carteCPU[0] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteCPU[1] == null ) frame.carteCPU[1] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteCPU[2] == null ) frame.carteCPU[2] = frame.arrayCarteMescolate[cartaDaPescare]; cartaDaPescare++; } } else { punteggioCPU += punti; if ( mano < 18 ) { if ( frame.carteCPU[0] == null ) frame.carteCPU[0] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteCPU[1] == null ) frame.carteCPU[1] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteCPU[2] == null ) frame.carteCPU[2] = frame.arrayCarteMescolate[cartaDaPescare]; cartaDaPescare++; if ( frame.carteInMano[0] == null ) frame.carteInMano[0] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteInMano[1] == null ) frame.carteInMano[1] = frame.arrayCarteMescolate[cartaDaPescare]; if ( frame.carteInMano[2] == null ) frame.carteInMano[2] = frame.arrayCarteMescolate[cartaDaPescare]; cartaDaPescare++; } } frame.pannello.repaint(); frame.repaint(); } }