Total members 11890 |It is currently Wed Apr 24, 2024 1:17 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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
    }
       

    
}

 





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

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 .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  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 .     -  
 Get affected rows for an update in php     -  
 Select odd rows from table     -  
 Listing All Rows and Fields in a Table     -  
 Number of Rows Returned by query in php     -  
 Multi colored table rows     -  



Topic Tags

Java Arrays
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com