All pastes #453528 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-04-23 02:02 UTC
rendered paste body
import java.util.Scanner;
public class SmokeRings
{
       String[][] grid;
       int x, y;
       int letter;
       final String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
       int dir; //0 right, 1 down, 2 left, 3 up
    public static void main(String args[]){
        
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter n for n by n  grid size: ");
        int n = reader.nextInt();
        System.out.print("Enter column number: ");
        int c = reader.nextInt();
        System.out.print("Enter row number: ");
        int r = reader.nextInt();

        SmokeRings rings = new SmokeRings(n);
        rings.fillGrid();
        System.out.println(rings.find(c, r));{ }
//         rings.showGrid();
       }

    public SmokeRings(int n){
        grid = new String[n][n];
        
        dir = 0;
        letter = 0;
        }
        
    public void showGrid(){
        for(String[] row: grid){
            for(String s: row)
                System.out.print(s);
            System.out.println();
            }
    }
    public void fillGrid(){fill();}   
    private void fill(){
        y = 0;
        x = -1;
        //Do the first row because it messes up numbers
        for (int i = 0; i < (2 * grid.length - 1); i++){
//             System.out.printf("%d, %d, %d --    ", x, y, dir);
            doLine();
            turn();
        }

    }
    
    public String find(int c, int r){return grid[c - 1][r - 1];}
    private void turn(){
        dir = (dir + 1) % 4;
       }
    private void doLine(){
        if (dir == 0){
            while(x + 1 < grid.length && grid[y][x + 1] == null){
                x++;
                grid[y][x] = alphabet[letter];
                letter = (letter + 1) % 26;                
            }
        }
        if (dir == 1){
                while(y + 1 < grid.length && grid[y + 1][x] == null){
                y++;
                grid[y][x] = alphabet[letter];
                letter = (letter + 1) % 26;                
        }
    }
        if (dir == 2){
            while(x != 0 && grid[y][x - 1] == null){
                x--;
                grid[y][x] = alphabet[letter];
                letter = (letter + 1) % 26;                
        }
    }
        if (dir == 3){
            while(y != 0 && grid[y - 1][x] == null){
                y--;
                grid[y][x] = alphabet[letter];
                letter = (letter + 1) % 26;                
        }
            
    }
}
}