| 1 | import java.util.Scanner; |
| 2 | import java.text.NumberFormat; |
| 3 | import java.lang.Math; |
| 4 | import java.io.*; |
| 5 | |
| 6 | /** Trida BitevniPole je implementaci hry lode pro jednoho hrace |
| 7 | * @author hejnama2 |
| 8 | * @version 1.0 |
| 9 | */ |
| 10 | |
| 11 | class BitevniPole implements Serializable { |
| 12 | /** pole znaku, ve kterem je ulozen stav bitevniho pole */ |
| 13 | public char[][] pole; |
| 14 | public final int size; //velikost pole (x je stejne jako y) |
| 15 | //private Random rnd = new Random(); |
| 16 | private Scanner sc = new Scanner(System.in); |
| 17 | private long draws = 0, impacts = 0, total_impacts = 0; //pocet tahu, pocet zasahu, celkovy pocet okupovanych policek |
| 18 | private int x,y; //pomocne promenne |
| 19 | public boolean debug = false; //urcuje, jestli se budou lode zobrazovat (takovy cheat pro odladeni programu) |
| 20 | |
| 21 | public char //tady jsou ruzne znaky reprezentujici ruzne stavy jednotlivych bunek pole: |
| 22 | neznamo = '_', |
| 23 | lod = '#', |
| 24 | voda = '~', |
| 25 | zasah = 'X'; |
| 26 | |
| 27 | /** vypise napovedu ke hre */ |
| 28 | public void printHelp() { |
| 29 | System.out.print( |
| 30 | "Pri zadavani souradnic dodrzujte mala a velka pismenka.\n"+ |
| 31 | neznamo + " - zatim neproskoumane policko\n"+ |
| 32 | zasah + " - policko se zasazenou lodi\n"+ |
| 33 | voda + " - policko se splouchnutim vody\n"+ |
| 34 | lod + " - lod (zobrazuje se jen pri debug = true)\n" |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | /** konstruktor - vytvori prazdne bitevni pole o velikosti s */ |
| 39 | BitevniPole(int s) { |
| 40 | int max_size = 59; |
| 41 | if(s < 1 || s > max_size) { |
| 42 | System.err.println("Can't make battlefield smaller than 1x1 or bigger than "+max_size+"x"+max_size+"!"); |
| 43 | System.exit(max_size); |
| 44 | } |
| 45 | size = s; |
| 46 | pole = new char[size][size]; |
| 47 | for(x = 0;x < size;x++) for(y = 0;y < size;y++) pole[x][y] = neznamo; |
| 48 | } |
| 49 | |
| 50 | /** druhy konstruktor - vytvori pole o vychozi s pouzitim prvniho konstruktoru velikosti (bude zavolan, pokud neni zadna velikost zadana) */ |
| 51 | BitevniPole() { |
| 52 | this(10); |
| 53 | } |
| 54 | |
| 55 | /** metoda zajistujici rozmisteni lodi - ve skutecnosti zatim rozmistuje jen nahodne ctverecky, ne cele lode*/ |
| 56 | public void rozmistiLode(int i) { |
| 57 | for(;i>0;i--) { |
| 58 | x = (int)(Math.random()*(size-1)); |
| 59 | y = (int)(Math.random()*(size-1)); |
| 60 | pole[x][y] = lod; |
| 61 | total_impacts++; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** metoda, ktera vytvori string znazornujici toto bitevni pole */ |
| 66 | public String toString() { |
| 67 | String out = new String("\n"); |
| 68 | out += " \tPocet tahu: "+draws+"\n"; |
| 69 | out += " \tPocet zasahu: "+impacts+" z "+total_impacts+"\n"; |
| 70 | out += " \t"; |
| 71 | for(y = 0;y < size;y++) out += "|"+(char)((int)'A'+y); |
| 72 | out += "|\n\n"; |
| 73 | for(x = 0;x < size;x++) { |
| 74 | out += x+"\t"; |
| 75 | for(y = 0;y < size;y++) { |
| 76 | if(!debug && pole[x][y] == lod) { |
| 77 | out += "|"+neznamo; |
| 78 | continue; |
| 79 | } |
| 80 | out += "|"+pole[x][y]; |
| 81 | } |
| 82 | out += "|\n"; |
| 83 | } |
| 84 | return out; |
| 85 | } |
| 86 | |
| 87 | /** zjisti, jestli uz byly zniceny vsechny lode a vrati jako boolean */ |
| 88 | public boolean jeKonec() { |
| 89 | for(x = 0;x < size;x++) for(y = 0;y < size;y++) if(pole[x][y] == lod) return false; |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** strili na souradnice x,y */ |
| 94 | public boolean strilej(int a, int b) { |
| 95 | if(a >= size || b >= size || a < 0 || b < 0) { |
| 96 | System.out.println("No such cell!"); |
| 97 | return false; |
| 98 | } |
| 99 | if(pole[a][b] == voda || pole[a][b] == zasah) { |
| 100 | System.out.println("This cell was already impacted!"); |
| 101 | return false; |
| 102 | } |
| 103 | if(pole[a][b] == lod) { |
| 104 | pole[a][b] = zasah; |
| 105 | impacts++; |
| 106 | } |
| 107 | if(pole[a][b] == neznamo) pole[a][b] = voda; |
| 108 | draws++; |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | /** provede hrace dalsim tahem */ |
| 113 | public void dalsiTah() { |
| 114 | System.out.println(this); |
| 115 | System.out.print("pismenko: "); |
| 116 | y = (int)(sc.next().charAt(0)-'A'); |
| 117 | System.out.print("cislicko: "); |
| 118 | x = sc.nextInt(); |
| 119 | strilej(x, y); |
| 120 | } |
| 121 | |
| 122 | /** provadi tahy, dokud neni pravda, ze jeKonec(), pak vypise gratulace a zkonci */ |
| 123 | public void hrat() { |
| 124 | while(!jeKonec()) dalsiTah(); |
| 125 | System.out.println("\n!!! CONGRATULATIONS !!!"); |
| 126 | System.out.println("You have defeated "+impacts+" ships within "+draws+" draws!"); |
| 127 | System.out.println("!!! CONGRATULATIONS !!!\n"); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /** trida lode vytvori BytevniPole a provede vse potrebne ke spusteni hry */ |
| 133 | public class lode { |
| 134 | public static void main(String[] argv) { |
| 135 | System.out.println("Lode (verze pro jednoho hrace)"); |
| 136 | |
| 137 | BitevniPole bp = new BitevniPole(2); //nove bitevni pole (pokud neni zadana velikost, bude pouzita vychozi velikost) |
| 138 | bp.printHelp(); //vypis napovedu |
| 139 | //bp.debug = true; //zapne zobrazeni lodi (to je pri hre nezadouci podvod) |
| 140 | bp.rozmistiLode(2); //kolik lodi chceme? |
| 141 | |
| 142 | bp.hrat(); //hrajeme |
| 143 | |
| 144 | } |
| 145 | } |
| 146 | |