Main.java (16990B)
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 java.io.*; 23 import java.util.*; 24 import javax.swing.*; 25 import java.awt.*; 26 27 public class Main { 28 public static int semeBriscola, punteggio, punteggioCPU, mano, cartaDaPescare, cartaDaGiocare; 29 public static boolean toccaAllaCPU = false; 30 public static boolean CPUHaGiocato = false; 31 public static boolean haGiocatoPrima = true; 32 public static Frame frame; 33 public static String stringaBottonePrendi = "Prendi/Lascia carte"; 34 public static String stringaPrimaGioca = "Prima Gioca!"; 35 public static String stringaUltime4 = "Ultime 4 mani - Occhio alla briscola!"; 36 public static String stringaVinto = "Hai Vinto!"; 37 public static String stringaPerso = "Hai Perso"; 38 public static String stringaPareggio = "Pareggio"; 39 public static String stringaGiocatore = "Giocatore"; 40 public static String stringaComputer = "\nComputer"; 41 public static String stringaNuovaPartita = "Nuova Partita"; 42 public static String stringaVuoiDavveroIniziare = "Vuoi davvero iniziare una nuova partita?"; 43 public static String stringaVuoiDavveroUscire = "Vuoi davvero uscire?"; 44 public static String stringaSi = "Si"; 45 public static String stringaNo = "No"; 46 public static String stringaOpzioni = "Opzioni..."; 47 public static String stringaEsci = "Esci"; 48 public static String stringaImpossibileConfigurare = "Impossibile modificare il file di configurazione.\nSegnalare il bug a: sebastiano@luganega.org\n\nEccezione:\n"; 49 public static String stringaLingua = "Lingua"; 50 public static String stringaAnnulla = "Annulla"; 51 public static String stringaIt = "Italiano"; 52 public static String stringaLa = "Latino"; 53 public static String stringaNecessarioRiavviare = "E' necessario riavviare JBriscola se sono state\nmodificate impostazioni riguardanti la lingua.\nAlcune parti saranno comunque tradotte prima."; 54 public static String stringaModificaOpzioni = "Modifica Opzioni"; 55 public static void main( String args[] ) { 56 settaOpzioni(); 57 frame = new Frame(); 58 frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); 59 frame.setResizable( false ); 60 frame.setSize( 600, 600 ); 61 frame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 600) / 2, 62 (Toolkit.getDefaultToolkit().getScreenSize().height - 600) / 2); 63 64 frame.setVisible( true ); 65 } 66 public void mischia() { 67 mano = 0; 68 punteggio = 0; 69 punteggioCPU = 0; 70 cartaDaPescare = 6; 71 ImageIcon aux; 72 frame.mazzo = true; 73 toccaAllaCPU = false; 74 CPUHaGiocato = false; 75 haGiocatoPrima = true; 76 for ( int i = 0, k = 0; i < 10; i++ ) 77 for ( int j = 0; j < 4; j++, k++ ) 78 frame.arrayCarteMescolate[k] = frame.arrayCarte[i][j]; 79 for ( int i = 0; i < 391; i++ ) { 80 double a = Math.random() * 40; 81 int x = (int) a; 82 double b = Math.random() * 40; 83 int y = (int) b; 84 aux = frame.arrayCarteMescolate[x]; 85 frame.arrayCarteMescolate[x] = frame.arrayCarteMescolate[y]; 86 frame.arrayCarteMescolate[y] = aux; 87 } 88 char auxChar[] = frame.arrayCarteMescolate[39].toString().toCharArray(); 89 semeBriscola = auxChar[auxChar.length-5] - 49; 90 frame.carteInMano[0] = frame.arrayCarteMescolate[0]; 91 frame.carteInMano[1] = frame.arrayCarteMescolate[1]; 92 frame.carteInMano[2] = frame.arrayCarteMescolate[2]; 93 frame.carteCPU[0] = frame.arrayCarteMescolate[3]; 94 frame.carteCPU[1] = frame.arrayCarteMescolate[4]; 95 frame.carteCPU[2] = frame.arrayCarteMescolate[5]; 96 frame.ultimaCarta = frame.arrayCarteMescolate[39]; 97 frame.giocata[0] = null; 98 frame.giocata[1] = null; 99 } 100 public void giocaCPU() { 101 if ( frame.giocata[0] == null ) { 102 if ( vaiLiscio() ); 103 else if ( vaiPunti() ); 104 else if ( vaiBriscola() ); 105 else if ( vaiCarico() ); 106 else; 107 } 108 else { 109 if ( haLisciato() || haMessoPunti() ) { 110 if ( vaiSopra() ); 111 else if ( vaiLiscio() ); 112 else if ( vaiPunti() ); 113 else if ( vaiBriscola() ); 114 else if ( vaiCarico() ); 115 else System.out.println("Ha lisciato e non so cosa fare"); 116 } else if ( haMessoBriscola() ) { 117 if ( vaiLiscio() ); 118 else if ( vaiPunti() ); 119 else if ( vaiBriscola() ); 120 else if ( vaiCarico() ); 121 else System.out.println("Ha messo una briscola e non so cosa fare"); 122 } else if ( haCaricato() ) { 123 if ( vaiSopra() ); 124 else if ( vaiBriscola() ); 125 else if ( vaiLiscio() ); 126 else if ( vaiPunti() ); 127 else if ( vaiCarico() ); 128 else System.out.println("Ha caricato e non so cosa fare"); 129 } else System.out.println("Non so cosa ha fatto"); 130 } 131 frame.giocata[1] = frame.carteCPU[cartaDaGiocare]; 132 frame.carteCPU[cartaDaGiocare] = null; 133 frame.pannello.repaint(); 134 CPUHaGiocato = true; 135 if ( frame.giocata[0] == null ) 136 toccaAllaCPU = false; 137 } 138 public void chiPrende() { 139 char aux[], auxCPU[]; 140 aux = frame.giocata[0].toString().toCharArray(); 141 auxCPU = frame.giocata[1].toString().toCharArray(); 142 giocatorePrende( ( (aux[aux.length-5] - 49 == semeBriscola) && (auxCPU[auxCPU.length-5] - 49 != semeBriscola) ) 143 || ( (aux[aux.length-5] == auxCPU[auxCPU.length-5]) && laPrimaEPiuAlta( aux[aux.length-7] - 48, auxCPU[auxCPU.length-7] - 48 ) ) 144 || ( (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && (aux[aux.length-5] != auxCPU[auxCPU.length-5]) && haGiocatoPrima ) ); 145 } 146 public boolean laPrimaEPiuAlta( int prima, int seconda ) { 147 int a, b; 148 switch ( prima ) { 149 case 0: 150 a = 10; 151 break; 152 case 1: 153 a = 12; 154 break; 155 case 3: 156 a = 11; 157 break; 158 default: 159 a = prima; 160 break; 161 } 162 switch ( seconda ) { 163 case 0: 164 b = 10; 165 break; 166 case 1: 167 b = 12; 168 break; 169 case 3: 170 b = 11; 171 break; 172 default: 173 b = seconda; 174 break; 175 } 176 return a > b; 177 } 178 public void giocatorePrende( boolean giocatore ) { 179 if ( giocatore ) { 180 toccaAllaCPU = CPUHaGiocato = false; 181 haGiocatoPrima = true; 182 } 183 else { 184 toccaAllaCPU = true; 185 CPUHaGiocato = haGiocatoPrima = false; 186 } 187 daiPunti( giocatore ); 188 if ( toccaAllaCPU ) 189 giocaCPU(); 190 } 191 public void daiPunti( boolean giocatore ) { 192 char aux[], auxCPU[]; 193 aux = frame.giocata[0].toString().toCharArray(); 194 auxCPU = frame.giocata[1].toString().toCharArray(); 195 int punti = 0; 196 switch( aux[aux.length-7] - 48 ) { 197 case 0: 198 punti += 4; 199 break; 200 case 1: 201 punti += 11; 202 break; 203 case 3: 204 punti += 10; 205 break; 206 case 8: 207 punti += 2; 208 break; 209 case 9: 210 punti += 3; 211 break; 212 default: 213 break; 214 } 215 switch( auxCPU[auxCPU.length-7] - 48 ) { 216 case 0: 217 punti += 4; 218 break; 219 case 1: 220 punti += 11; 221 break; 222 case 3: 223 punti += 10; 224 break; 225 case 8: 226 punti += 2; 227 break; 228 case 9: 229 punti += 3; 230 break; 231 default: 232 break; 233 } 234 frame.giocata[0] = frame.giocata[1] = null; 235 if ( giocatore ) { 236 punteggio += punti; 237 if ( mano < 18 ) { 238 if ( frame.carteInMano[0] == null ) 239 frame.carteInMano[0] = frame.arrayCarteMescolate[cartaDaPescare]; 240 if ( frame.carteInMano[1] == null ) 241 frame.carteInMano[1] = frame.arrayCarteMescolate[cartaDaPescare]; 242 if ( frame.carteInMano[2] == null ) 243 frame.carteInMano[2] = frame.arrayCarteMescolate[cartaDaPescare]; 244 cartaDaPescare++; 245 if ( frame.carteCPU[0] == null ) 246 frame.carteCPU[0] = frame.arrayCarteMescolate[cartaDaPescare]; 247 if ( frame.carteCPU[1] == null ) 248 frame.carteCPU[1] = frame.arrayCarteMescolate[cartaDaPescare]; 249 if ( frame.carteCPU[2] == null ) 250 frame.carteCPU[2] = frame.arrayCarteMescolate[cartaDaPescare]; 251 cartaDaPescare++; 252 } 253 } 254 else { 255 punteggioCPU += punti; 256 if ( mano < 18 ) { 257 if ( frame.carteCPU[0] == null ) 258 frame.carteCPU[0] = frame.arrayCarteMescolate[cartaDaPescare]; 259 if ( frame.carteCPU[1] == null ) 260 frame.carteCPU[1] = frame.arrayCarteMescolate[cartaDaPescare]; 261 if ( frame.carteCPU[2] == null ) 262 frame.carteCPU[2] = frame.arrayCarteMescolate[cartaDaPescare]; 263 cartaDaPescare++; 264 if ( frame.carteInMano[0] == null ) 265 frame.carteInMano[0] = frame.arrayCarteMescolate[cartaDaPescare]; 266 if ( frame.carteInMano[1] == null ) 267 frame.carteInMano[1] = frame.arrayCarteMescolate[cartaDaPescare]; 268 if ( frame.carteInMano[2] == null ) 269 frame.carteInMano[2] = frame.arrayCarteMescolate[cartaDaPescare]; 270 cartaDaPescare++; 271 } 272 } 273 frame.pannello.repaint(); 274 frame.repaint(); 275 } 276 public boolean haLisciato() { 277 char auxCPU[]; 278 auxCPU = frame.giocata[0].toString().toCharArray(); 279 return ( (frame.giocata[0] != null) && (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && 280 (auxCPU[auxCPU.length-7] - 48 != 0 ) && (auxCPU[auxCPU.length-7] - 48 != 1 ) && 281 (auxCPU[auxCPU.length-7] - 48 != 3 ) && (auxCPU[auxCPU.length-7] - 48 < 8 ) ); 282 } 283 public boolean haMessoPunti() { 284 char auxCPU[]; 285 auxCPU = frame.giocata[0].toString().toCharArray(); 286 return ( (frame.giocata[0] != null) && (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && 287 ( (auxCPU[auxCPU.length-7] - 48 == 0 ) || (auxCPU[auxCPU.length-7] - 48 == 8 ) || 288 (auxCPU[auxCPU.length-7] - 48 == 9 ) ) ); 289 } 290 public boolean haMessoBriscola() { 291 char auxCPU[]; 292 auxCPU = frame.giocata[0].toString().toCharArray(); 293 return ( (frame.giocata[0] != null) && (auxCPU[auxCPU.length-5] - 49 == semeBriscola) ); 294 } 295 public boolean haCaricato() { 296 char auxCPU[]; 297 auxCPU = frame.giocata[0].toString().toCharArray(); 298 return ( (frame.giocata[0] != null) && (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && 299 ( (auxCPU[auxCPU.length-7] - 48 == 1 ) || (auxCPU[auxCPU.length-7] - 48 == 3 ) ) ); 300 } 301 public boolean vaiLiscio() { 302 char auxCPU[]; 303 for ( int i = 0; i < 3; i++ ) { 304 if ( frame.carteCPU[i] != null ) { 305 auxCPU = frame.carteCPU[i].toString().toCharArray(); 306 if ( (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && 307 (auxCPU[auxCPU.length-7] - 48 != 0 ) && (auxCPU[auxCPU.length-7] - 48 != 1 ) && 308 (auxCPU[auxCPU.length-7] - 48 != 3 ) && (auxCPU[auxCPU.length-7] - 48 < 8 ) ) { 309 cartaDaGiocare = i; 310 return true; 311 } 312 } 313 } 314 return false; 315 } 316 public boolean vaiPunti() { 317 char auxCPU[]; 318 for ( int i = 0; i < 3; i++ ) { 319 if ( frame.carteCPU[i] != null ) { 320 auxCPU = frame.carteCPU[i].toString().toCharArray(); 321 if ( (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && 322 ( (auxCPU[auxCPU.length-7] - 48 == 0 ) || (auxCPU[auxCPU.length-7] - 48 == 8 ) || 323 (auxCPU[auxCPU.length-7] - 48 == 9 ) ) ) { 324 cartaDaGiocare = i; 325 return true; 326 } 327 } 328 } 329 return false; 330 } 331 public boolean vaiBriscola() { 332 char auxCPU[]; 333 for ( int i = 0; i < 3; i++ ) { 334 if ( frame.carteCPU[i] != null ) { 335 auxCPU = frame.carteCPU[i].toString().toCharArray(); 336 if ( auxCPU[auxCPU.length-5] - 49 == semeBriscola ) { 337 cartaDaGiocare = i; 338 return true; 339 } 340 } 341 } 342 return false; 343 } 344 public boolean vaiCarico() { 345 char auxCPU[]; 346 for ( int i = 0; i < 3; i++ ) { 347 if ( frame.carteCPU[i] != null ) { 348 auxCPU = frame.carteCPU[i].toString().toCharArray(); 349 if ( (auxCPU[auxCPU.length-5] - 49 != semeBriscola) && 350 ( (auxCPU[auxCPU.length-7] - 48 == 1 ) || (auxCPU[auxCPU.length-7] - 48 == 3 ) ) ) { 351 cartaDaGiocare = i; 352 return true; 353 } 354 } 355 } 356 return false; 357 } 358 public boolean vaiSopra() { 359 if ( frame.giocata[0] == null ) 360 return false; 361 int a = -1; 362 int b = -1; 363 int c = -1; 364 boolean laPrimaPuoAndareSopra = false; 365 boolean laSecondaPuoAndareSopra = false; 366 boolean laTerzaPuoAndareSopra = false; 367 char[] auxCPU1 = new char[1024]; 368 char[] auxCPU2 = new char[1024]; 369 char[] auxCPU3 = new char[1024]; 370 char[] aux = frame.giocata[0].toString().toCharArray(); 371 if ( aux[aux.length-7] - 48 == 1 ) 372 return false; 373 if ( frame.carteCPU[0] != null ) { 374 auxCPU1 = frame.carteCPU[0].toString().toCharArray(); 375 a = auxCPU1[auxCPU1.length-7] - 48; 376 } 377 if ( frame.carteCPU[1] != null ) { 378 auxCPU2 = frame.carteCPU[1].toString().toCharArray(); 379 b = auxCPU2[auxCPU2.length-7] - 48; 380 } 381 if ( frame.carteCPU[2] != null ) { 382 auxCPU3 = frame.carteCPU[2].toString().toCharArray(); 383 c = auxCPU3[auxCPU3.length-7] - 48; 384 } 385 if ( (auxCPU1[auxCPU1.length-5] - 49 == aux[aux.length-5] - 49) && laPrimaEPiuAlta( a, aux[aux.length-7] - 48 ) ) 386 laPrimaPuoAndareSopra = true; 387 if ( (auxCPU2[auxCPU2.length-5] - 49 == aux[aux.length-5] - 49) && laPrimaEPiuAlta( b, aux[aux.length-7] - 48 ) ) 388 laSecondaPuoAndareSopra = true; 389 if ( (auxCPU3[auxCPU3.length-5] - 49 == aux[aux.length-5] - 49) && laPrimaEPiuAlta( c, aux[aux.length-7] - 48 ) ) 390 laTerzaPuoAndareSopra = true; 391 if ( !laPrimaPuoAndareSopra && !laSecondaPuoAndareSopra && !laTerzaPuoAndareSopra ) 392 return false; 393 else if ( laPrimaPuoAndareSopra && !laSecondaPuoAndareSopra && !laTerzaPuoAndareSopra ) 394 cartaDaGiocare = 0; 395 else if ( !laPrimaPuoAndareSopra && laSecondaPuoAndareSopra && !laTerzaPuoAndareSopra ) 396 cartaDaGiocare = 1; 397 else if ( !laPrimaPuoAndareSopra && !laSecondaPuoAndareSopra && laTerzaPuoAndareSopra ) 398 cartaDaGiocare = 2; 399 else if ( laPrimaPuoAndareSopra && laSecondaPuoAndareSopra && !laTerzaPuoAndareSopra ) 400 if ( laPrimaEPiuAlta( a, b ) ) 401 cartaDaGiocare = 0; 402 else 403 cartaDaGiocare = 1; 404 else if ( !laPrimaPuoAndareSopra && laSecondaPuoAndareSopra && laTerzaPuoAndareSopra ) 405 if ( laPrimaEPiuAlta( b, c ) ) 406 cartaDaGiocare = 1; 407 else 408 cartaDaGiocare = 2; 409 else if ( laPrimaPuoAndareSopra && !laSecondaPuoAndareSopra && laTerzaPuoAndareSopra ) 410 if ( laPrimaEPiuAlta( a, c ) ) 411 cartaDaGiocare = 0; 412 else 413 cartaDaGiocare = 2; 414 else if ( laPrimaPuoAndareSopra && laSecondaPuoAndareSopra && laTerzaPuoAndareSopra ) 415 if ( laPrimaEPiuAlta( a, b ) ) 416 if ( laPrimaEPiuAlta( a, c ) ) 417 cartaDaGiocare = 0; 418 else 419 cartaDaGiocare = 2; 420 else 421 if ( laPrimaEPiuAlta( b, c ) ) 422 cartaDaGiocare = 1; 423 else 424 cartaDaGiocare = 2; 425 else cartaDaGiocare = 55; 426 return true; 427 } 428 public static void settaOpzioni() { 429 String lang; 430 Scanner scanner = null; 431 File file = new File( "JBriscolaConfig.txt" ); 432 if ( file.exists() ) { 433 try { 434 scanner = new Scanner( file ); 435 } catch ( FileNotFoundException e ) { 436 lang = "it"; 437 } 438 lang = scanner.next(); 439 } 440 else { 441 lang = "it"; 442 } 443 if ( lang.equals( "it" ) ) { 444 stringaBottonePrendi = "Prendi/Lascia carte"; 445 stringaPrimaGioca = "Prima Gioca!"; 446 stringaUltime4 = "Ultime 4 mani - Occhio alla briscola!"; 447 stringaVinto = "Hai Vinto!"; 448 stringaPerso = "Hai Perso"; 449 stringaPareggio = "Pareggio"; 450 stringaGiocatore = "Giocatore"; 451 stringaComputer = "\nComputer"; 452 stringaNuovaPartita = "Nuova Partita"; 453 stringaVuoiDavveroIniziare = "Vuoi davvero iniziare una nuova partita?"; 454 stringaVuoiDavveroUscire = "Vuoi davvero dscire?"; 455 stringaSi = "Si"; 456 stringaNo = "No"; 457 stringaOpzioni = "Opzioni..."; 458 stringaEsci = "Esci"; 459 stringaImpossibileConfigurare = "Impossibile modificare il file di configurazione.\nSegnalare il bug a: sebastiano@luganega.org\n\nEccezione:\n"; 460 stringaLingua = "Lingua"; 461 stringaAnnulla = "Annulla"; 462 stringaIt = "Italiano"; 463 stringaLa = "Latino"; 464 stringaNecessarioRiavviare = "E' necessario riavviare JBriscola se sono state\nmodificate impostazioni riguardanti la lingua\nAlcune parti saranno comunque tradotte prima."; 465 stringaModificaOpzioni = "Modifica Opzioni"; 466 } 467 else if ( lang.equals( "la" ) ) { 468 stringaBottonePrendi = "CAPIS/RELINQVIS CHARTAS"; 469 stringaPrimaGioca = "ANTE CHARTAM PONE!"; 470 stringaUltime4 = "IV MANVS POSTREMAS - INTVERE BRISCOLAM!"; 471 stringaVinto = "VINXISTI!"; 472 stringaPerso = "PERDIDISTI"; 473 stringaPareggio = "AEQVATIO!"; 474 stringaGiocatore = "LVSOR: "; 475 stringaComputer = "\nCOMPVTER: "; 476 stringaNuovaPartita = "NOVA LVSO"; 477 stringaVuoiDavveroIniziare = "NOVAM LVSIONEM INCIPERE?"; 478 stringaVuoiDavveroUscire = "EXIRE?"; 479 stringaSi = "ID VOLO"; 480 stringaNo = "NOLO"; 481 stringaOpzioni = "OPTIONES..."; 482 stringaEsci = "EXIRE"; 483 stringaImpossibileConfigurare = "CONFIGVRATIONIS FILE SCRIBERE NON POTVI.\nERROREM ME CERTIOREM FACITE: sebastiano@luganega.org\n\nEXCEPTIO:\n"; 484 stringaLingua = "LINGVA"; 485 stringaAnnulla = "ABROGA"; 486 stringaIt = "ITALICE"; 487 stringaLa = "LATINE"; 488 stringaNecessarioRiavviare = "JBRISCOLAM RVRSVS INCIPERE NECESSE EST\nSI LINGVA PRAELATIONES NOVATAE SUNT.\nALIQVAE ANTE ID VERSA ERVNT"; 489 stringaModificaOpzioni = "OPTIONES NOVARE"; 490 } 491 } 492 }