Welcome to Dream.In.Code
Become a Java Expert!

Join 150,372 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,789 people online right now. Registration is fast and FREE... Join Now!




Cant create 2d array:

 
Reply to this topicStart new topic

Cant create 2d array:

needhelpbadly
7 Feb, 2008 - 11:04 AM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 13

**Fixed my array**

I only have 2 errors.

Why is Knight check = new Knight; not legal? If it isnt legal, why are my check.SOMEMETHOD statements OK?

blink.gif

CODE
public class Knight
{
  boolean valid_pos;
//Global variables                                                                                                                                                                    
int size = 6;//Board size
int[][] board = new int[size][size]; //create board

public boolean main(String []args)
{
  Knight check = new Knight;
  int row, col, count=0;


  for(int a=0;a<=size;a++) //clear board to 0                                                                                                                                    
    {
      for(int b=0;b<=size;b++)
        {
          board[a][b]=0;
        }
    }

  //Take input
  row = 0;
  col = 0;

  check.valid_pos(row, col, size);

  //Output results                                                                                                                                                                

  check.recursive(row, col, size, count);
  check.display_board(size);

  return false;
}

public boolean valid_pos(int row, int col, int size){
  if(row>=size || col>=size ||  row<0 || col<0){//Test for chosen location being within board dimensions                                                                          
    return true;

  }
  else{//Check to see if location was not previously visited                                                                                                                      
    if((board[row][col]) == 0){
      return false;

    }
    else{//returns 1 if location previously visited                                                                                                                              
      return true;

    }
  }
}

public void display_board(int size)
{
  for(int c=0;c<size;c++)
    {
      for(int d=0;d<size;d++)
        {
          if(d==size-1)
            {
              System.out.println("| "+board[c][d]+" |");
            }
          else
{
              System.out.println("| "+board[c][d]+" ");
            }
        }
    }
}


public int check_board(int size)
{
  int rows, cols, checker;

  for(rows=0;rows<size;rows++)
    {
      for(cols=0;cols<size;cols++)
        {
          if(valid_pos(rows,cols,size)!= true)
            {
              checker=0;
            }
        }
    }
  return(checker);
}

public void recursive(int row, int col, int size, int count)
{

  int checked;

  board[row][col]=count;

  if(valid_pos (row+2, col+1, size)!= true)
  {
    count++;
    recursive(row+2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col+1]=0;
      count--;
    }
  }
  if(valid_pos(row+2, col-1, size)!= true)
  {
    count++;
    recursive(row+2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col-1]=0;
      count--;

    }
  }
if(valid_pos (row+1, col+2, size)!= true)
{
    count++;
    recursive(row+1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row+1, col-2, size)!= true)
  {
    count++;
    recursive(row+1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col-2]=0;
      count--;
    }
  }
  if(valid_pos(row-2, col+1, size)!= true)
  {
    count++;
    recursive(row-2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col+1]=0;
      count--;
    }
  }

if(valid_pos (row-2, col-1, size)!= true)
{
    count++;
    recursive(row-2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col-1]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col+2, size)!= true)
  {
    count++;
    recursive(row-1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col-2, size)!= true)
  {
    count++;
    recursive(row-1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col-2]=0;
      count--;
    }
  }

checked=check_board(size);
  if(checked==0){
    board[row][col]=0;
    count--;
  }
}
}


This post has been edited by needhelpbadly: 7 Feb, 2008 - 11:45 AM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Cant Create 2d Array:
7 Feb, 2008 - 12:17 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Try Knight check = new Knight();
User is offlineProfile CardPM
+Quote Post

needhelpbadly
RE: Cant Create 2d Array:
7 Feb, 2008 - 12:25 PM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 13

Oh wow that was it. Its compiling but I guess I have no method being called right away. I'm gonna have to take a look at what to do next. Thanks! any ideas?
User is offlineProfile CardPM
+Quote Post

needhelpbadly
RE: Cant Create 2d Array:
7 Feb, 2008 - 07:50 PM
Post #4

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 13

Program is compiling but wont run, what am I leaving out?
User is offlineProfile CardPM
+Quote Post

capty99
RE: Cant Create 2d Array:
7 Feb, 2008 - 08:14 PM
Post #5

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,259



Thanked: 16 times
Dream Kudos: 550
My Contributions
code?
User is online!Profile CardPM
+Quote Post

needhelpbadly
RE: Cant Create 2d Array:
7 Feb, 2008 - 10:26 PM
Post #6

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 13

Yea i know - i just cant figure out what exactly to try next.

Ive created an instance of the class.

I've tried check.EVERYMETHOD.


Nothing is actually producing any output.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Cant Create 2d Array:
8 Feb, 2008 - 04:22 AM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Cross posting is bad.

Isn't this a near identical question to the one you posted here? The answer I offered there is nearly complete.

And here you're asking for someone else's answer to be translated to Java?

Now this thread is the same problem with completely different code again? Points for creative problem solving and industry. Perhaps it's time to actually write some code of your own? blink.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month