Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Randomly generated rows and columns in Array

Wed Jan 27, 2010 3:32 am

Randomly generated rows and columns in array:
Im coding a program for a game of chess. The code isn't complete but...I'm having a problem in the constructor. Im trying to to assign Q to an element in the array with a randomly generated row and column and i receive a loss of precision error.....

Code:
public class DancingQueen
{
    public static int BOARD_SIZE = 8;
    public static char SPACE = ' ';
    private char [][] board;
    
    public DancingQueen
()
    {
       board = new char [BOARD_SIZE][BOARD_SIZE];
       //nested for loop
       for (int row =0; row< board.length; row++)
            for(int col =0; col<board.length; col++)
            {
                SPACE = board[row][col];
            }

         (ERROR COMES HERE) board(int)[(Math.random()*BOARD_SIZE)](int)[(Math.random()*BOARD_SIZE)] = 'Q' ;
          int x = (int)'Q';
        }
       
      
    
    
    public char
[][] getBoard()
    {
        char[][] temp = new char[board.length][board[0].length];
        for(int row = 0; row < board.length; row++)
            for(int col = 0; col < board[0].length; col++)
                temp[row][col] = board[row][col];
        return temp;
    }
  


    public String move
(int fromRow, int fromCol, int toRow, int toCol)
    {
        if(fromRow<|| fromRow>|| fromCol<|| fromCol>7){
            System.out.println("Invalid from coordinate.");}
        if(toRow<|| toRow>|| toCol<|| toCol>7){
            System.out.println("Invalid to coordinate.");}
            
            
        
        
//moves vertical, horizontal,diagnol
        //||of fromCol to toCol = || fromRo to toRow
    }
       

    
}

 




Re: java Array.! (help needed for brginner)

Wed Jan 27, 2010 11:14 am

Code:
board(int)[(Math.random()*BOARD_SIZE)](int)[(Math.random()*BOARD_SIZE)] = 'Q' ;


The int casting should be inside brackets :

board[(int)(Math.random()*BOARD_SIZE)][(int)(Math.random()*BOARD_SIZE)] = 'Q' ;


thanks .

Post a reply
  Related Posts  to : Randomly generated rows and columns in Array
 Uneven rows-2-dimenstionals-triangle look like 2D array     -  
 selecting a record randomly from table     -  
 Three different columns Frameset     -  
 Unique Row Count Across Columns     -  
 three rows frameset .     -  
 Select odd rows from table     -  
 Get affected rows for an update in php     -  
 delete duplicate rows in mysql using php     -  
 different colors for table Even- Odd rows and Cells     -  
 Listing All Rows and Fields in a Table     -  

Topic Tags

Java Arrays