GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2009, 06:27
ZackCampbell5 ZackCampbell5 is offline
New Member
 
Join Date: Oct 2009
Posts: 8
ZackCampbell5 is an unknown quantity at this point

2 dimensional arrays and number generating


Thank you for your help. We were able to figure out a method that works and follows our guidelines. I now have to create a chess senario with 1 queen and 1 pawn using 2 dimensional arrays and number generating. I don't want u to code it for me, but i was wondering if you could explain how to encorporate 2D arrays into a program, and how they work. Thank you!
  #2  
Old 14-Nov-2009, 20:04
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,234
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Here's a small example that fills and displays a 2D array within the program:
JAVA Code:
public class TwoDimensionArrayExample
{
   final short ROWS = 3;
   final short COLS = 3;

   short two_dimension_array[][] = new short[ROWS][COLS];

   public static void main(String[] args)
   {
      new TwoDimensionArrayExample();
   }

   public TwoDimensionArrayExample()
   {
      fill_array();
      show_array();
   }

   private void fill_array()
   {
      short fill = 1;

      for ( short row = 0; row < ROWS; ++row )
      {
         for ( short col = 0; col < COLS; ++col)
         {
            two_dimension_array[row][col] = fill;
            fill++;
         }
      }
   }

   private void show_array()
   {
      System.out.println("The array:");

      for ( short row = 0; row < ROWS; ++row )
      {
         System.out.print("\t");

         for ( short col = 0; col < COLS; ++col)
         {
            System.out.print("[ " + two_dimension_array[row][col] + " ]");
         }

         System.out.println();
      }
   }
}
...the output:
Code:
The array: [ 1 ][ 2 ][ 3 ] [ 4 ][ 5 ][ 6 ] [ 7 ][ 8 ][ 9 ]
Post another reply if there may be any questions.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 18-Nov-2009, 06:30
ZackCampbell5 ZackCampbell5 is offline
New Member
 
Join Date: Oct 2009
Posts: 8
ZackCampbell5 is an unknown quantity at this point

Re: Converting Octal to Decimal and then Decimal to Octal


awesome.... thank you, i have another question. this is our code so far

JAVA Code:
public class Chess
{
  public static void main (String args[])
  {
    double i = (Math.random()*7);  // queen's row cooridnates
    double j = (Math.random()*7); // queen's column coordinates
    double g = (Math.random()*7); // pawn's  row coordinates
    double h = (Math.random()*7); // pawn's column coordinates
    System.out.println ("Queen's corrdinates: " + (int)i + ", " + (int)j);
    System.out.println ("Pawn's corrdinates: " + (int)g + ", " + (int)h);
    
    if ( (int)i == (int)g) // if in the same row
      System.out.println ("Queen takes pawn");
    else if ((int)h == (int)j) // if in the same column
      System.out.println ("Queen takes pawn");
    else
      System.out.println ("Pawn is safe");
    char board [] [] = new char [8] [8];
    
    for(int x = 0; x < 8; x++)
    {
      for (int y = 0; y < 8; y++)
      {
        board [x][y] = '-';
        board [(int)i][(int)j] = 'Q';
        board [(int)g][(int)h] = 'P';
        
      System.out.print (board [x][y] + " ");
      }
    System.out.println();
    }
   
  }
}

print out:
Code:
Queen's corrdinates: 3, 4 Pawn's corrdinates: 6, 0 Pawn is safe - - - - - - - - - - - - - - - - - - - - - - - - - - - - Q - - - - - - - - - - - - - - - - - - - P - - - - - - - - - - - - - - -



as of now our 8x8 board is being displayed with "-"...... we r striving to have each space print as a box with straight lines on each side and lines on top and bottom but no brackets if you could help us figure out how to do that using the code we already have, that would be cool.

also we were a little confused as to how to relate the diagonal positions of the pieces, we know what to do if the are in the same row or column but what about diagonally??
Last edited by admin : 18-Nov-2009 at 06:58. Reason: Please insert your example Java codes between [JAVA] and [/JAVA] tags
  #4  
Old 18-Nov-2009, 11:12
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,234
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Note to moderator:

This thread should probably be split now, starting from #9, as the topic has shifted to "Two Dimensional Arrays" (then delete this post)


Thank you, TurboPT. Your wish is my command. Thread split from here.

Admin.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 18-Nov-2009, 11:37
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,234
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Quote:
Originally Posted by ZackCampbell5
...striving to have each space print as a box with straight lines on each side and lines on top and bottom but no brackets if you could help us figure out how to do that using the code we already have, that would be cool.
That would be at done at your looping: (note the comments)
JAVA Code:
    for(int x = 0; x < 8; x++)
    {
  //  print something here before the columns here. (box tops)
      for (int y = 0; y < 8; y++)
      {
        board [x][y] = '-';
        board [(int)i][(int)j] = 'Q';
        board [(int)g][(int)h] = 'P';
        
      System.out.print (board [x][y] + " ");  // insert vertical dividers here (box left/right)
      }
    System.out.println(); // print something after the columns here. (box bottoms)
    }
Note that instead of x/y, that row/col might make the intent more clear.

Quote:
Originally Posted by ZackCampbell5
also we were a little confused as to how to relate the diagonal positions of the pieces, we know what to do if the are in the same row or column but what about diagonally??
Simply relative position checking. Considering the Queen's position:
Code:
Queen's corrdinates: 3, 4 Pawn's corrdinates: 6, 0 Pawn is safe - - - - - - - - - - - - - - - - - - - - - - - - - - - - Q - - - - - - - - - - - - - - - - - - - P - - - - - - - - - - - - - - -
...create a way to check the diagonal directions. Consider using separate variables to avoid clobbering the current piece's position values. Plus additional care will be required to avoid going out of bounds!

So, for example, upper left diagonal checking from the queen is:
x/y: 2,3
x/y: 1,2
x/y: 0,1
Need to stop here, x < 0 (in this specific direction) will be out of bounds!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 19-Nov-2009, 06:06
ZackCampbell5 ZackCampbell5 is offline
New Member
 
Join Date: Oct 2009
Posts: 8
ZackCampbell5 is an unknown quantity at this point

Re: Converting Octal to Decimal and then Decimal to Octal


Quote:
Originally Posted by TurboPT
Note to moderator:

This thread should probably be split now, starting from #9, as the topic has shifted to "Two Dimensional Arrays" (then delete this post)


yes u r right.... i would definetly do that except i really don't know how : /.....
  #7  
Old 19-Nov-2009, 07:01
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,234
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Quote:
Originally Posted by ZackCampbell5
definitely do that except i really don't know how : /.....
When the topic changes, just start a new thread [just as you did with your very first post] so that it is easier for others to follow, that's all.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert row elements from two dimensional array to one dimensional arrays ahmetemir MS Visual C++ / MFC Forum 0 01-Feb-2009 04:08
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 16:19
Fgets() in two dimensional arrays - C projectwoa C Programming Language 2 17-Apr-2007 00:02
comparing multiple character arrays in C alucard C Programming Language 4 03-Feb-2006 00:47
two dimensional arrays in C seabreeze C Programming Language 5 21-Aug-2005 11:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:13.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.