Posted by : Unknown domingo, 28 de septiembre de 2014

Miércoles 17 de septiembre de 2014

Ejercicio 02 de SDP03

Por: Eduardo Zavala Ramírez

Este día resolvimos el ejercicio número dos del curso de programación de iCarnegie.
El objetivo fue leer desde una aplicación JAVA un archivo de texto y cambiar los caracteres por números acorde a las instrucciones dadas.

/**
 *
 */
package maze;

import java.io.File;
import java.util.Scanner;

public class Exercise02 {
    private static final String dataPath = "mazedata.txt";
    private static int[][] cell;
    private static int maxRow = 0;        // These are the height and width of the maze
    private static int maxCol = 0;
    private static final int WALL = -1;        // These constants are the encoding used to represent the maze
    private static final int START = -2;    // A zero and positive values are pathways.  A positive value
    private static final int GOAL = -3;        // specifies how many times the MazeRunner has left this position
    private static char c;

    /**
     * @param args
     */
    public static void main(String[] args) {
        //Run the simple test first
        try {
            Scanner scanner = new Scanner(new File(dataPath));
            String line = scanner.nextLine();
            Scanner s = new Scanner(line);
            maxCol = s.nextInt();
            maxRow = s.nextInt();
            cell = new int[maxRow][maxCol];
           
            //while(s.hasNextLine())
            for(int j=0; j<maxRow; j++)
            {
                line = scanner.nextLine();
                for(int i=0; i<maxCol; i++){
                   
                    c=line.charAt(i);
                   
                    switch(c){
                   
                    case '*': cell[j][i] = WALL;
                    break;
                    case 'S': cell[j][i] = START;
                    break;
                    case 's': cell[j][i] = START;
                    break;
                    case 'G': cell[j][i] = GOAL;
                    break;
                    case 'g': cell[j][i] = GOAL;
                    break;
                    }
                 }
            }
           
           
            System.out.println(mazeToString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    /**
     * Create a String representation of the maze
     */
    private static String mazeToString(){
        String str = "";
        for (int i = 0; i < maxRow; i++) {
            for (int j = 0; j < maxCol; j++)
                str += cell[i][j];
            str += '\n';
        }
        return str;
    }

}


Lo anterior nos permitió que los símbolos como * se cambiara por -1, la s o S por -2 y la g o G por -3.
La ejecución fue exitosa. 
Lo más importante para la resolución de la aplicación fue ir leyendo las líneas y, en cada una, obtener a partir del atributo charAt de la clase String un caracter para comparar a traves de un SWITCH y generar el intercambio por otros caracteres.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Popular Post

Blogger templates

Con tecnología de Blogger.

- Copyright © Zavala Zavala - Computing and Problem Solving -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -