tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
0
ZUGRIFFE
274
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
  1. #1
    hasta_jesaja hasta_jesaja ist offline Mitglied Gold
    Registriert seit
    Feb 2005
    Ort
    Berlin
    Beiträge
    130
    Hier meine Lösung. Bin ich zu spät?

    [EDIT]
    Mir ist grad aufgefallen das ich in der run.bat im zip sleep(5) verwende anstatt pause, warum auch immer...
    Hier noch der klartext:
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    
    import java.io.*;
    import java.util.HashMap;
     
    public class Gol {
        
        private static HashMap<Integer, Boolean[]> rows = new HashMap<Integer, Boolean[]>();
        private static int rowNum = 0;
        private static  int colNum = 0;
        private static int cycles = 0;
        
        Gol(){
            
            //Start here
            outln("Willkommen zum Hefe Simulator!");
            outln("Kurzanleitung:");
            outln("1. Die Fragen beantworten bis 4.");
            outln("2. Ein Hefe wird markiert mit \"x\", alles andere bleibt unbewohnt.");
            outln("   Beispiel: \"Typen für Zeile 1: xcxbxm\" ergibt x0x0x0.");
            outln("Viel Spass!");
            
            getRows();
            getCols();
            getCycles();
            
            //Fill the Rows
            for(int i = 0; i < rowNum; i++){
                
                try {
                    out((i + 4) + ". Typen für Zeile " + (i + 1) + ": ");
                    
                    BufferedReader lineInput = new BufferedReader(new InputStreamReader(System.in));
                    
                    String lineTypes = lineInput.readLine();
                    Boolean[] rowTypes = new Boolean[colNum];
                    
                    if(lineTypes.length() > colNum){
                        lineTypes = lineTypes.substring(0, colNum);
                    }
                    
                    for(int j = 0; j < colNum; j++){
                        if(j < lineTypes.length()){
                             if((lineTypes.substring(j, j + 1).indexOf("x")) > -1){
                                 rowTypes[j] = true;
                             } else {
                                 rowTypes[j] = false;
                             }
                        } else {
                            rowTypes[j] = false;
                        }
                    }
                    
                    rows.put(i, rowTypes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
            
            String strBuff = "";
            int countNeighbours = 0;
            int countLiving = 0;
            //int countAll = rowNum * colNum;
            Boolean[] rowTypesBuffUp = new Boolean[colNum];
            Boolean[] rowTypesBuffNext = new Boolean[colNum];
            
            for(int cycle = 0; cycle < cycles; cycle++){
                outln("cls");
                countLiving = 0;
                outln("Durchgang " + (cycle + 1) + ":"); 
                //Copy / Buffer
                HashMap<Integer, Boolean[]> rowsBefore = new HashMap<Integer, Boolean[]>();
                for(int i = 0; i < rowNum; i++){
                    rowsBefore.put(i, new Boolean[colNum]);
                    for(int j = 0; j < colNum; j++){
                        ((Boolean[])rowsBefore.get(i))[j] = ((Boolean[])rows.get(i))[j];
                    }
                }
                for(int i = 0; i < rowNum; i++){
                    Boolean[] rowTypesBuff = rowsBefore.get(i);
                    if(i > 0){
                        rowTypesBuffUp = rowsBefore.get(i - 1);
                    }
                    if(i < rowNum - 1){
                        rowTypesBuffNext = rowsBefore.get(i + 1);
                    }
                    for(int j = 0; j < colNum; j++){
                        if(i > 0){
                            //Check up left
                            if(j > 0){
                                if(rowTypesBuffUp[j - 1]){
                                    countNeighbours++;
                                }
                            }
                            //Check up
                            if(rowTypesBuffUp[j]){
                                countNeighbours++;
                            }
                            //Check up right
                            if(j < colNum - 1){
                                if(rowTypesBuffUp[j + 1]){
                                    countNeighbours++;
                                }
                            }
                        }
                        //Check left
                        if(j > 0){
                            if(rowTypesBuff[j - 1]){
                                countNeighbours++;
                            }
                        }
                        //Check right
                        if(j < colNum - 1){
                            if(rowTypesBuff[j + 1]){
                                countNeighbours++;
                            }
                        }
                        if(i < rowNum - 1){
                            //Check next left
                            if(j > 0){
                                if(rowTypesBuffNext[j - 1]){
                                    countNeighbours++;
                                }
                            }
                            //Check next
                            if(rowTypesBuffNext[j]){
                                countNeighbours++;
                            }
                            //Check next right
                            if(j < colNum - 1){
                                if(rowTypesBuffNext[j + 1]){
                                    countNeighbours++;
                                }
                            }
                        }
                        
                        //What to do?
                        if(countNeighbours <= 1 || countNeighbours > 3){
                            //rowTypesBuff[j] = false;
                            ((Boolean[])rows.get(i))[j] = false;
                        }
                        else if(countNeighbours == 3 && rowTypesBuff[j] == false){
                            //rowTypesBuff[j] = true;
                            ((Boolean[])rows.get(i))[j] = true;
                        }
                        
                        strBuff += (((Boolean[])rows.get(i))[j]) ? "x" : "0";
                        if(((Boolean[])rows.get(i))[j]) countLiving++;
                        countNeighbours = 0;
                    }
                    outln(strBuff);
                    
                    strBuff = "";
                    //rows.put(i, rowTypesBuff);
                    //rows.
                }
                outln(countLiving + " Überlebende\n");
                if(countLiving == 0){
                    break;
                }
                /*try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
            if(countLiving == 0){
                outln("Die Hefe ist leider eingegangen!");
            } else {
                outln("Die Hefe hat überlebt!");
            }
            
        }
        
        private static void out(String s){
            try {
                PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, "Cp850"));
                out.print(s);
                out.flush();
            } catch (UnsupportedEncodingException e1) {
                //e1.printStackTrace();
                System.out.println("Printwriter Fehler! (Kein CP850?)");
            } 
        }
        
        private static void outln(String s){
            try {
                PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, "Cp850"));
                out.println(s);
                out.flush();
            } catch (UnsupportedEncodingException e1) {
                //e1.printStackTrace();
                System.out.println("Printwriter Fehler! (Kein CP850?)");
            } 
        }
        
        private void getRows(){
            //  prompt for Row Number
            out("1. Wieviele Zeilen?: ");
            
            BufferedReader rowNumInput = new BufferedReader(new InputStreamReader(System.in));
            
            try {
                rowNum = Integer.parseInt(rowNumInput.readLine());
            } catch (IOException e) {
                outln("Es gab einen Fehler.");
                getRows();
            } catch (java.lang.NumberFormatException e) {
                outln("Bitte eine Zahl eingeben.");
                getRows();
            }
        }
        
        private void getCols(){
            //  prompt for Column Number
            out("2. Wieviele Spalten?: ");
            BufferedReader colNumInput = new BufferedReader(new InputStreamReader(System.in));
            
            try {
                colNum = Integer.parseInt(colNumInput.readLine());
            } catch (IOException e) {
                outln("Es gab einen Fehler.");
                getCols();
            } catch (java.lang.NumberFormatException e) {
                outln("Bitte eine Zahl eingeben.");
                getCols();
            }
        }
        
        private void getCycles(){
            //  prompt for Cycles Number
            out("3. Wieviele Durchgänge?: ");
            BufferedReader cycleNumInput = new BufferedReader(new InputStreamReader(System.in));
            
            try {
                cycles = Integer.parseInt(cycleNumInput.readLine());
            } catch (IOException e) {
                outln("Es gab einen Fehler.");
                getCycles();
            } catch (java.lang.NumberFormatException e) {
                outln("Bitte eine Zahl eingeben.");
                getCycles();
            }
        }
        
        public static void main(String[] args){
            new Gol();      
        }
        
    }
    Angehängte Dateien Angehängte Dateien
    Geändert von hasta_jesaja (07.12.08 um 20:12 Uhr)
     
    Der Mensch ist ein Tier das zuviel denkt!

Thema nicht erledigt

Ähnliche Themen

  1. Quiz XML mit Java DOM
    Von Simon Berger im Forum XML Technologien
    Antworten: 1
    Letzter Beitrag: 16.06.10, 08:20
  2. [QUIZ#11] zeja (Java)
    Von zeja im Forum Archiv
    Antworten: 0
    Letzter Beitrag: 24.10.09, 13:55
  3. [QUIZ#9]HonniCilest(Java)
    Von HonniCilest im Forum Archiv
    Antworten: 2
    Letzter Beitrag: 05.08.09, 08:41
  4. [QUIZ#04] zeja (Java)
    Von zeja im Forum Archiv
    Antworten: 1
    Letzter Beitrag: 12.10.08, 18:38
  5. Quiz mit Java?
    Von fraus im Forum Javascript & Ajax
    Antworten: 2
    Letzter Beitrag: 13.07.01, 10:22