| 1 | import java.io.*; |
| 2 | import java.util.*; |
| 3 | import java.text.*; |
| 4 | |
| 5 | /** |
| 6 | * trida reprezentujici pole na hrani piskvorek |
| 7 | * @author Honza |
| 8 | */ |
| 9 | class PiskvorciPole { |
| 10 | public int size; |
| 11 | public char pole[][]; |
| 12 | public char prazdno='_'; |
| 13 | private int x, y; |
| 14 | public boolean kriz = true; |
| 15 | |
| 16 | PiskvorciPole(int s) { //konstruktor |
| 17 | size = s; |
| 18 | pole = new char[size][size]; |
| 19 | for(x=0;x<size;x++) { |
| 20 | for(y=0;y<size;y++) { |
| 21 | pole[x][y]=prazdno; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | PiskvorciPole() { //pretizeni konstruktoru (pouzije se, pokud neni zadana velikost) |
| 27 | this(10); |
| 28 | } |
| 29 | |
| 30 | public String toString() { |
| 31 | char ascii = 'a'; |
| 32 | |
| 33 | String out = new String("\n \t|"); |
| 34 | for(char c=ascii;c<size+ascii;c++) out += (c+"|"); |
| 35 | out += "\n\n"; |
| 36 | for(x=0;x<size;x++) { |
| 37 | out += (x+":\t|"); |
| 38 | for(y=0;y<size;y++) { |
| 39 | out += pole[x][y]+"|"; |
| 40 | } |
| 41 | out += "\n"; |
| 42 | } |
| 43 | |
| 44 | return out; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | public void vykresli() { // udelat to metodou toString jinak je to chyba.... |
| 49 | System.out.print(this); |
| 50 | } |
| 51 | */ |
| 52 | |
| 53 | public void vykresliDoSouboru(String outputfile) { |
| 54 | double x, y; |
| 55 | FileOutputStream out; |
| 56 | PrintStream p; |
| 57 | |
| 58 | |
| 59 | try { |
| 60 | out = new FileOutputStream(outputfile, true); //true znamena, ze se bude vzdy pridavat na konec souboru |
| 61 | p = new PrintStream(out); |
| 62 | p.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date())); |
| 63 | p.println(this); |
| 64 | p.close(); |
| 65 | } catch(Exception e) { |
| 66 | System.err.println("Error writing to file"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | public void hrat() { |
| 72 | char hrac; |
| 73 | boolean chyba = false; |
| 74 | Scanner sc = new Scanner(System.in); |
| 75 | while(true) { |
| 76 | System.out.println(this); |
| 77 | vykresliDoSouboru("piskvorky.txt"); |
| 78 | kriz = !kriz; |
| 79 | hrac = 'O'; if(kriz) hrac = 'X'; |
| 80 | System.out.println("Hraje hrac s "+hrac); |
| 81 | |
| 82 | System.out.print("pismeno: "); |
| 83 | y=(int)(sc.next().charAt(0)-'a'); |
| 84 | |
| 85 | try { |
| 86 | System.out.print("cisilko: "); |
| 87 | x=sc.nextInt(); |
| 88 | } catch(Exception e) { |
| 89 | chyba = true; |
| 90 | } |
| 91 | |
| 92 | if(chyba || x < 0 || x >= size || y < 0 || y >= size || pole[x][y] != prazdno) { |
| 93 | chyba = false; |
| 94 | System.out.println("\t\t\tneplatne pole!"); |
| 95 | kriz = !kriz; continue; //dalsi pokus |
| 96 | } |
| 97 | pole[x][y]=hrac; |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * trida demonstrujici pouziti tridy PiskvorciPole |
| 105 | * @author Honza |
| 106 | */ |
| 107 | public class piskvorky { |
| 108 | public static void main (String argv[]) { |
| 109 | System.out.println("\t\t===> PISKVORKY <==="); |
| 110 | System.out.println("\tHru lze prerusit stisknutim ctrl+c, nebo pres netbeans."); |
| 111 | PiskvorciPole pp = new PiskvorciPole(20); //vytvorime nove pole 20x20 |
| 112 | pp.hrat(); //spustime hru |
| 113 | } |
| 114 | } |