Ascii Tables Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VickyA95
    New Member
    • Apr 2015
    • 1

    Ascii Tables Java

    Hi, so I'm having trouble creating a table that is used for a game. It needs to have 3 Human charaters (@), 2 Monsters (#), 3 Treasure Chests (*) and 4 Obstacles (O). The table needs to be 10 by 10 squares and gaps should be filled with .
    It should kinda look something like this:
    Code:
        1    2    3    4    5    6    7    8    9    10
    1  .    @    .      *    .     .     .    .      .      .
    2  .     .    .       .    .     #    .    .      O     .
    3  .     .    .       .    .     .     .    .      .      .
    4  .     .    .       .    .     .     .    .      .      .
    5  .     .    .       #   .     .     @   .      .      .
    6  .     .    O      .    .     .     .    .      .      .
    7  .     .    .       .    O    .     .    .      O     .
    8  .     .    .       .    .     .     *    .      .      .
    9  .     *    .       .    .     .     .    .      .      .
    10.     .    .       .    .     .     .    @     .      .
    I also need to be able to move the chapters in the end. This is my currant code. But i can only get it so that the . and @ appear otherwise i just get errors.
    Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
    import java.util.Scanner;
    /**
     *
     * @author vickyanderson
     */
    public class Test {
    
        
        public static void show(boolean[][] grid){
            String s = "";
            for(boolean[] row : grid){
                for(boolean col : row)
                    if(col)
                        s += "@  ";
                    else
                        s += ".  ";
                s += "\n";
            }
            System.out.println(s);
        }
        
        public static boolean[][] gen(){
            boolean[][] grid = new boolean[10][10];
            for(int r = 0; r < 10; r++)
                for(int c = 0; c < 10; c++)
                    if( Math.random() > 0.2 )
                        grid[r][c] = true;
            return grid;
        }
        
        public static void main(String[] args){
            boolean[][] world = gen();
            show(world);
            System.out.println();
            world = nextGen(world);
            show(world);
            Scanner s = new Scanner(System.in);
            while(s.nextLine().length() == 0){
                System.out.println();
                world = nextGen(world);
                show(world);
                
            }
        }
        
        public static boolean[][] nextGen(boolean[][] world){
            boolean[][] newWorld 
                = new boolean[world.length][world[0].length];
            int num;
            for(int r = 0; r < world.length; r++){
                for(int c = 0; c < world[0].length; c++){
                    num = numNeighbors(world, r, c);
                    if( occupiedNext(num, world[r][c]) )
                        newWorld[r][c] = true;
                }
            }
            return newWorld;
        }
        
        public static boolean occupiedNext(int numNeighbors, boolean occupied){
            if( occupied && (numNeighbors == 2 || numNeighbors == 3))
                return true;
            else if (!occupied && numNeighbors == 3)
                return true;
            else
                return false;
        }
    
        private static int numNeighbors(boolean[][] world, int row, int col) {
            int num = world[row][col] ? -1 : 0;
            for(int r = row - 1; r <= row + 1; r++)
                for(int c = col - 1; c <= col + 1; c++)
                    if( inbounds(world, r, c) && world[r][c] )
                        num++;
    
            return num;
        }
    
        private static boolean inbounds(boolean[][] world, int r, int c) {
            return r >= 0 && r < world.length && c >= 0 &&
            c < world[0].length;
        }
    }
    Does anyone have any ideas on what will help me get all the symbols in?
    Thanks
    Last edited by zmbd; Apr 16 '15, 08:17 PM. Reason: [z{Please use the [CODE/] button to format tables and script/code}]
Working...