• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert Into Java Code.

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know what Im asking here is too much work, but I want to give a try, can someone, translate this code for java. I already try to work with something, but I always get stuck with a lot of things, the only I could do right is the array for the list. Ok, here is all I have for now.

-----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

/* List of valid moves a knight can make from any square */
int [] [] list = {{0, 0, 0, 0, 0, 0, 0, 0, 0, },
{11, 18, 0, 0, 0, 0, 0, 0, 0}, //1
{12, 17, 19, 0, 0, 0, 0, 0, 0}, //2
{9, 13, 18, 20, 0, 0, 0, 0, 0}, //3
{10, 14, 19, 21, 0, 0, 0, 0, 0}, //4
{11, 15, 20, 22, 0, 0, 0, 0, 0}, //5
{12, 16, 21, 23, 0, 0, 0, 0, 0}, //6
{13, 22, 24, 0, 0, 0, 0, 0, 0}, //7
{14, 23, 0, 0, 0, 0, 0, 0, 0}, //8
{3, 19, 26, 0, 0, 0, 0, 0, 0}, //9
{4, 20, 25, 27, 0, 0, 0, 0, 0}, //10
{1, 5, 17, 21, 26, 28, 0, 0, 0}, //11
{2, 6, 18, 22, 27, 29, 0, 0, 0}, //12
{3, 7, 19, 23, 28, 30, 0, 0, 0}, //13
{4, 8, 20, 24, 29, 31, 0, 0, 0}, //14
{5, 21, 30, 32, 0, 0, 0, 0, 0}, //15
{6, 22, 31, 0, 0, 0, 0, 0, 0}, //16
{2, 11, 27, 34, 0, 0, 0, 0, 0}, //17
{1, 3, 12, 28, 33, 35, 0, 0, 0}, //18
{2, 4, 9, 13, 25, 29, 34, 36, 0}, //19
{3, 5, 10, 14, 26, 30, 35, 37, 0}, //20
{4, 6, 11, 15, 27, 31, 36, 38, 0}, //21
{5, 7, 12, 16, 28, 32, 37, 39, 0}, //22
{6, 8, 13, 29, 38, 40, 0, 0, 0}, //23
{7, 14, 30, 39, 0, 0, 0, 0, 0}, //24
{10, 19, 35, 42, 0, 0, 0, 0, 0}, //25
{9, 11, 20, 36, 41, 43, 0, 0, 0}, //26
{10, 12, 17, 21, 33, 37, 42, 44, 0}, //27
{11, 13, 18, 22, 34, 38, 43, 45, 0}, //28
{12, 14, 19, 23, 35, 39, 44, 46, 0}, //29
{13, 15, 20, 24, 36, 40, 45, 47, 0}, //30
{14, 16, 21, 37, 46, 48, 0, 0, 0}, //31
{15, 22, 38, 47, 0, 0, 0, 0, 0}, //32
{18, 27, 43, 50, 0, 0, 0, 0, 0}, //33
{17, 19, 28, 44, 49, 51, 0, 0, 0}, //34
{18, 20, 25, 29, 41, 45, 50, 52, 0}, //35
{19, 21, 26, 30, 42, 46, 51, 53, 0}, //36
{20, 22, 27, 31, 43, 47, 52, 54, 0}, //37
{21, 23, 28, 32, 44, 48, 53, 55, 0}, //38
{22, 24, 29, 45, 54, 56, 0, 0, 0}, //39
{23, 30, 46, 55, 0, 0, 0, 0, 0}, //40
{26, 35, 51, 58, 0, 0, 0, 0, 0}, //41
{25, 27, 36, 52, 57, 59, 0, 0, 0}, //42
{26, 28, 33, 37, 49, 53, 58, 60, 0}, //43
{27, 29, 34, 38, 50, 54, 59, 61, 0}, //44
{28, 30, 35, 39, 51, 55, 60, 62, 0}, //45
{29, 31, 36, 40, 52, 56, 61, 63, 0}, //46
{30, 32, 37, 53, 62, 64, 0, 0, 0}, //47
{31, 38, 54, 63, 0, 0, 0, 0, 0}, //48
{34, 43, 59, 0, 0, 0, 0, 0, 0}, //49
{33, 35, 44, 60, 0, 0, 0, 0, 0}, //50
{34, 36, 41, 45, 57, 61, 0, 0, 0}, //51
{35, 37, 42, 46, 58, 62, 0, 0, 0}, //52
{36, 38, 43, 47, 59, 63, 0, 0, 0}, //53
{37, 39, 44, 48, 60, 64, 0, 0, 0}, //54
{38, 40, 45, 61, 0, 0, 0, 0, 0}, //55
{39, 46, 62, 0, 0, 0, 0, 0, 0}, //56
{42, 51, 0, 0, 0, 0, 0, 0, 0}, //57
{41, 43, 52, 0, 0, 0, 0, 0, 0}, //58
{42, 44, 49, 53, 0, 0, 0, 0, 0}, //59
{43, 45, 50, 54, 0, 0, 0, 0, 0}, //60
{44, 46, 51, 55, 0, 0, 0, 0, 0}, //61
{45, 47, 52, 56, 0, 0, 0, 0, 0}, //62
{46, 48, 53, 0, 0, 0, 0, 0, 0}, //63
{47, 54, 0, 0, 0, 0, 0, 0, 0}} //64
;

void play( int );
void winner( void );


int board[65] = {0}; /* the Chess board */
int moves[65]; /* list of moves */
int k = 0; /* move counter */
long wincount = 0; /* winning counter */
long iterl = 0; /* iteration counter */
long iterh = 0; /* iteration counter */

void main(int argc, char *argv[])
{
if (argc != 2)
{
printf("\nPlease provide the starting square: 1 to 64\n");
return;
}
if (atoi(argv[1]) < 1) return;
if (atoi(argv[1]) > 64) return;
play(atoi(argv[1]));
printf("%ld%ld Total Iterations!\n", iterh, iterl);
printf("%ld Total Winning combinations!\n", wincount);
}

void play( int square )
{
int i;

if (++iterl == (long) 100000000)
{
iterl = 0; iterh++;
if (((iterh/10)*10) == iterh)
printf("%ld Billion iterations %ld Winners\n",iterh/10, wincount);
else
printf("%ld Hundred Million iterations %ld Winners\n",iterh, wincount);
}
board[square] = 1;
moves[k++] = square;

if (k == 64)
{
/*
winner();
*/
wincount++;
board[square] = 0;
k--;
return;
}
for (i = 0; list[square][i] != 0; i++)
{
if (board[list[square][i]] == 0)
play(list[square][i]);
}
board[square] = 0;
k--;
return;
}

/* Print the winning info */
void winner( void )
{

/*
wincount++;
printf("Winner %d iteration %ld%ld\n", wincount, iterh, iterl);
int i;
for (i = 0; i < 16; i++) printf("%2d ",moves[i]);
printf("\n");
for (i = 16; i < 32; i++) printf("%2d ",moves[i]);
printf("\n");
for (i = 32; i < 48; i++) printf("%2d ",moves[i]);
printf("\n");
for (i = 48; i < 64; i++) printf("%2d ",moves[i]);
printf("\n\n");
}
-------------------------------------------------------------------------

I hope someone can help me, I really need this.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We like to help people learn, but we don't really like the idea of just doing people's work for them.

It sounds to me like you're really trying to get ahead of yourself here. If you want to learn to program in Java, then you'll need a book (like, say Head First Java to teach you. You can come here to ask questions about parts you had trouble with.

If, on the other hand, you don't want to learn, and just want to get this done, then you need to hire someone -- a student, perhaps -- to do this for you. Once you decide how much money it's worth to you, you're welcome to use our Jobs Offered forum to advertise.

But no one here is just going to do this for you, for free. That's just not what we do here.
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not my intention, actually, Im a student. But, I dont have the knowledge neceserealy to do this in java, I did it on Pascal, but when I try to take something from Pascal and take into java, it gives me an error, so, I was asking, how can i do the code in java. It's ok, I understand, nobody is going to do the work for me. I already did the work, I just only need to take into java. Sorry for bother you, it won't happen again.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manuel,

You mentioned that you are knowledgeable in Pascal. And you also created code that shows you know C.

Why the resistence to picking up a book and learning Java? With a working knowledge of both C and Pascal, Java should be incredibly easy.

Henry
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manuel,
I understand you are a student, and I also understand everybody else's opinion...I will try to be 'in the middle' and what I ask you is to post ALSO whatever code you have translated in Java.

If you show the community you want to learn AND have this thing done, I am sure a lot of people will help you!

Giovanni
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program is pretty straightforward. It took about 15 minutes last night, to port to Java... and *no*, I am not listing it...

It actually will be a good first experience with Java if you port it yourself. Here are a few pointers.

- C is procedural and Java is OO. The easiest way to move it is to wrap all the code in a class. And declare everything public and static.

- C main method seems to count the program as one of the argument. Java only counts the arguments. Just adjust the parameter count and array index in the main method. The program only takes one parameter.

- Array are not implicitely allocated in Java. You need to allocated with the new operator. There is also one operation that set values to zero initially. Don't bother, Java allocation of ints automatically does that.

- Don't bother with the winner() function. It seems to just be a testing debugging function... do it for extra credit.

- And finally, I ran the program and had no idea what it does. I don't know if it works correctly since I don't know what is it doing.

But... when I ran it with a value of 5, this is what I got.

1 Hundred Million iterations 13 Winners
2 Hundred Million iterations 21 Winners
3 Hundred Million iterations 1101 Winners
4 Hundred Million iterations 1263 Winners
5 Hundred Million iterations 1276 Winners
6 Hundred Million iterations 1276 Winners
7 Hundred Million iterations 1276 Winners
8 Hundred Million iterations 1276 Winners
9 Hundred Million iterations 1290 Winners
1 Billion iterations 1369 Winners
11 Hundred Million iterations 1369 Winners
12 Hundred Million iterations 1443 Winners
13 Hundred Million iterations 1460 Winners
14 Hundred Million iterations 1460 Winners
15 Hundred Million iterations 1471 Winners
16 Hundred Million iterations 1487 Winners
17 Hundred Million iterations 1506 Winners
18 Hundred Million iterations 1509 Winners
19 Hundred Million iterations 1511 Winners
2 Billion iterations 1514 Winners
...

The program seems to run forever.

Henry
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this program is for the Knight's Tour. In the program you specified the first square and the program will try to find all the possibles moves to find the solution. Like you said, it never stop. I want to learn Java, Im reading at school and all that stuffs, it takes a lot of time to learn it. As you can see, all I did was the array part (list) to list all the moves per square the knight can do. But when I try to use the other code, I have a lot of error. I was just asking if someone could help me with this, maybe if one of you contribute with a little part in the code, I can reach my goal. Im just 18, so I have a long way to be like one of you (good in Java), that's my goal. Ok, I think I said my point.

THANKS!
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manuel Diaz:
I was just asking if someone could help me with this, maybe if one of you contribute with a little part in the code, I can reach my goal. Im just 18, so I have a long way to be like one of you (good in Java), that's my goal. Ok, I think I said my point.



Sounds like you're trying to run before you can walk. How can any sane person try to convert a program written in language A to B if they have no knowledge of B? Why not, as someone else suggested, actually try to learn Java? If not, investigate the thought of paying someone to do it for you if you want a quick runnable solution (e.g. RentACoder). We will be more inclined to help if you ask questions about the language in general, rather than pasting lots of code in another language and waiting for someone to convert it for you (and as a student, you would probably want to take all the credit). You said you have tried and failed, but have made no effort to post what you wrote for us to help.
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is NOT ABOUT CREDIT!!. And I always try to do my best. In the code I posted, there is only one part that I have done, (list array), I know is almost nothing, but is only what I can do. Ok, I was just asking for help, it's ok, I understand. Sorry to bother!.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manuel Diaz:
That is not my intention, actually, Im a student. But, I dont have the knowledge neceserealy to do this in java, I did it on Pascal, but when I try to take something from Pascal and take into java, it gives me an error, so, I was asking, how can i do the code in java. It's ok, I understand, nobody is going to do the work for me. I already did the work, I just only need to take into java. Sorry for bother you, it won't happen again.



So what error did you get? If you post the EXACT error message, along with the code causing the error, we'll be glad to explain what the error means and give you some ideas how to fix it.

Layne
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manuel,
in my previous post I said I would have been 'in the middle'...but you seem to confirm the idea that you are trying to have someone else do your job...
simply POST YOUR CODE WHATEVER IT IS AND TELL US WHICH ERRORS YOU GET!

Giovanni
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic