private static int lcs(){/*reads the characters in b[][] to determine the lcs*/ char lcs [] = new char[y.length];//characters are read from last to first into this array int count = 0;//counts the length of the lcs for(int i = y.length; i > 0;){//nested for loop with length and height of table for(int j = x.length; i > 0;){ if(b[i][j] == '\\'){//if this character is encountered lcs[i] = y[i];//read the corresponding matching letters into the array i -= 1; //go to the left j -= 1;//go up count++;//increments count } else if (b[i][j] == '<'){//if this character is read i-=1;//go to the left } else if(b[i][j] == '^'){//if this character is read j -= 1;//go up } else break; //no more characters to read } } lcsax = new char[lcs.length];//initialize character array for(int i = lcs.length; i > 0; i--){ lcsax[i] = lcs[i];//reads the characters from last to first into array } return count;//returns lcs length }
Now I'm encountering a bug in the nested loops I set up. These are supposed to determine the longest common sub sequence based on the characters \\, <, and ^
What is the bug you are running into? The code as written will print out the longest common extractable subsequence, skipping over characters in the y sequence that do not match.
If you are trying to find the longest common already intact subsequence, there are more than a few bugs.
edited 27th Apr '16 10:52:58 PM by war877
If anyone's interested, I tried my hand at the "longest common sequence" problem and came up with two solutions.
The first function browses the matrix intersection of x and y along its diagonals (and not along its rows or columns as usual), because this is where common sequences will be found.
The second function is slower but easier to understand : it browses the matrix as usual, and from each point tries to find a common sequence by following the diagonal.
edited 28th Apr '16 5:06:52 AM by Aetol
Worldbuilding is fun, writing is a choreI don't know how to implement that because It's written in python- I have more experience using c++ and java.
I was able to make my code work most of the time, but I keep running into this weird bug where instead of going up and to the left after encountering '\\', the loop instead goes up and to the right several spaces.
private static int lcs(){/*reads the characters in b[][] to determine the lcs*/ char lcs [] = new char[y.length];//characters are read from last to first into this array int count = 0;//counts the length of the lcs for(int i = y.length-1; i >= 1;){//nested for loop with length and height of table for(int j = x.length-1; i >= 1;){ if(b[i][j] == '\\'){//if this character is encountered System.out.print(b[i][j]); System.out.print(c[i][j]); lcs[i] = x[i];//read the corresponding matching letters into the array System.out.println(lcs[i]); i -= 1; //go to the left j -= 1;//go up count++;//increments count if(c[i][j] == 1){ break; } } else if (b[i][j] == '<'){//if this character is read System.out.print(b[i][j]); System.out.println(c[i][j]); j-=1;//go to the left } else if(b[i][j] == '^'){//if this character is read System.out.print(b[i][j]); System.out.println(c[i][j]); i -= 1;//go up } else break; //no more characters to read } } lcsax = new char[lcs.length];//initialize character array for(int i = lcs.length-1; i >= 0; i--){ lcsax[i] = lcs[i];//reads the characters from last to first into array } return count;//returns lcs length }
. j n d c a a . .0 .0 .0 .0 .0 .0 .0 a .0 ^0 ^0 ^0 ^0 \1 \1 n .0 ^0 \1 <1 <1 ^1 ^1 c .0 ^0 ^1 ^1 \2 <2 <2 d .0 ^0 ^1 \2 ^2 ^2 ^2 j .0 \1 ^1 ^2 ^2 ^2 ^2 a .0 ^1 ^1 ^2 ^2 \3 \3 \3a ^2 ^2 <2 \2c ^1 //this should be <1 \1a //wrong letter The longest common sub sequence length is 3 The longest common sub sequence is aca
edited 29th Apr '16 9:55:37 AM by Xopher001
I am curious. What do you get when you type in:
System.out.println(new String(x));in the above example. Assuming that x is of type char[].
edited 29th Apr '16 10:16:03 AM by war877
The sequence of letters in the left-most column
Converting from python to java isn't really hard. Most of the syntax is identical to other languages : you just need to put semicolons and curly brackets where needed, and add a type to variable declarations. You only need to be a bit creative for the argument of for loops, since in python they have the form for iterator in iterable (and even then the iterable is often xrange(), which gives a list of consecutive integers aka the standard use of for loops).
EDIT : anyway, here you go.
Regarding your code : first, you have an error in your second-level loop. It should be "j >= 1", nor "i". Second, if this doesn't fix the problem (I doubt it will), I'm thinking it may be caused by the non-standard use of for loops. How about this :
int i = y.length-1
int j = x.length-1
while(i >= 1 && j >= 1) {
/* code here */
}
Finally, I'm not sure I understand what you want to do : are the sequences you're looking for supposed to be continuous? In other words, should the solution in this case have for length 1 or 3?
edited 29th Apr '16 11:38:29 AM by Aetol
Worldbuilding is fun, writing is a choreThere is several errors. I cannot explain the behaviour you are getting, because none of them would cause this...... Oh crap. I see. Kill the two for loops and replace them with a while loop.
int i=x.length-1;int j=y.length-1;You should have an easier time finding the remaining bugs after that.
while(true)
-[snip]- s
edited 23rd May '16 2:10:50 PM by war877
If anyone knows the version of JavaScript that Khan Academy uses for its programming course, I'm trying to make a simple maze (well, path) game. I need the program to know when the ball is out of bounds, but the turns in the path make that difficult for a beginner. I know how to use If/Else, the question is how to apply it in this particular program. If I posted a link to the program here, might someone be able to help me out?
please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?I do not know if kahn academy uses a non-standard version of JS or not. However, I have used JS before, and the basics of C derived languages are surprisingly similar to each other.
All right, cool. Here it is. Code on the left, output on the right. Mouse controls are intentionally inverted.
edited 23rd May '16 2:21:18 PM by TooManyIdeas
please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?I see. I would store the edges in an array. Creating a massive ifelse tree will simply not work if you need to replace the maze with a new one.
Yeah, good point. I'll need to do the Array lessons again, then. Efficiency is hard to program.
Okay, weird thing I notice: If I program in normal mouse controls, it breaks the messages. Makes debugging frustrating.
edited 23rd May '16 2:47:28 PM by TooManyIdeas
please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?A separate issue: I assume you want the entire outside to be marked with out of bounds? Or do you only want to mark the touching of lines? The other way to do it would be to mark the crossing of lines.
If you uninvert the controls, you need to replace mouseX== with mouseY==
edited 23rd May '16 2:51:46 PM by war877
Yeah, the entire outside is ideal, but I don't know how to do that.
edited 23rd May '16 3:01:35 PM by TooManyIdeas
please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?Will your mazes be limited to one left turn and one right turn? If so, you can simply replace your equalities with inequalities.
Another solution which will always work is to create an array the size of the the playing field and then paint it. Either with the flood-fill method, which will work for a maze with no holes in it, or a scan and flip method if the maze only contains horizontal and vertical lines and no overdraw and no gaps.
A third solution is to count the number of lines to the left up if the maze contains no overdraw or gaps and only vertical and horizontal lines. If a line is drawn across the entrance and exit, I believe the inside will always have an even count. Oh. It will always be even. Both counts will be odd on the inside.
edited 23rd May '16 3:23:19 PM by war877
Okay, so here's why you shouldn't try to write Javascript after midnight...
I was working on my latest PCG program, and after testing a certain feature I noticed I was getting statistically bizarre results unlike anything the program had produced before. It all seemed to come down to a particular hashing algorithm. I had thought my hashing algorithm was reasonably good, but when I substituted a call to Math.random(), the bad results went away. I tested my hashing algorithm with some other outputs and those ones looked good too, very similar to what Math.random() output in the same tests. Finally, just to make sure it was in fact my algorithm that was behaving incorrectly, I wrote another completely different hashing algorithm from scratch and substituted that- and the bad results were still there. Either both algorithms somehow shared a similar weakness, or there was something else I was overlooking that was poisoning the seed values.
Finally, I opened up Firebug and dug through the DOM to see what the actual values were that the algorithm was returning- and found that, for every given run of the program, all the hash values related to the bad outputs were exactly the same number, even though hashes with very similar seeds were returning all sorts of different numbers. I finally went back and checked the original function call- and found that, when I had first copy+pasted it from elsewhere before changing the arguments, I had accidentally left in one extra argument, causing the seed I had wanted to use to be completely ignored. (Even though the function was only defined with 3 arguments, apparently Javascript doesn't care how many arguments you actually pass in when you call it, and doesn't report any errors if you add superfluous ones. This same mistake would have shown up instantly in a language like C++ where the compiler doesn't accept such nonsense.)
As usual, the bug was way simpler than I thought it was. Now someone get me an anvil so I can hit my head on it for being such a moron.
Join my forum game!What exactly were you using hashes for? I haven't used hashes outside hash tables (where a bad hash function would be merely a performance problem) and message integrity checks, so I'm genuinely curious what their other uses are.
edited 29th May '16 1:49:18 AM by Medinoc
"And as long as a sack of shit is not a good thing to be, chivalry will never die."~Too Many Ideas: one fundamental rule of programming is to ALWAYS separate logic from data. In your case :
- Data is how the mouse controls the ball and where the walls are.
- Logic is everything else: mostly the collision detection and the display.
To keep the mouse control function separate, you need to use two variables x and y that will represent the position of the ball. First you initialize x and y with mouseX and mouseY, and then you use them in the rest of the code.
To keep the position of the walls separate, you need to do as Tropers/war877 said: put them into an array. Then you can just use for loops to draw them and to check for collision.
Checking for collision is the tricky part. Right now, the collision will only be detected if the ball is exactly on the wall: if you move the mouse quickly enough, you can cross a wall without triggering the "you hit the wall" message. My solution would be to write a function that verifies whether two segments intersect (this isn't easy: there'll be some math involved), then compare each wall with the segment formed by the current and the last position of the ball (you'll need two more variable for that).
Here's what the main loop would look like (in pseudocode) :
store x,y in lastX,lastYWorldbuilding is fun, writing is a chore
set x,y in terms of mouseX,mouseY
// display
set the background
display the ball at the position x, y
FOR each wall :
display the wall
// collision detection
FOR each wall :
check whether the wall intersects the segment (x,y,lastX,lastY)
IF there was any collision :
display the "you lost" message
In this specific instance, the hash value was being used as the magnitude of a patch of color associated with a Voronoi point.
Join my forum game!In short, you're using the hashes as sources of pseudo-randomness?
"And as long as a sack of shit is not a good thing to be, chivalry will never die."Which hash were you using? FNV?
Do you highlight everything looking for secret messages?So this semester we're programming shell scripts in C, and I'm having trouble with this one simple program- it's supposed to take an inputted integer between 5 and 9 and print that number of rows in a pyramid- for example, if I entered , then the output would be
___1
___22
__333
_4444
_55555
666666
this is what my code looks like:
#!/bin/sh #task6c.sh read Input for ((i=1; $i <= $Input; i++)); do sp=`expr $Input - $i` #this is a variable used to help create the right number of spaces if [ `expr $sp % mod 2` -eq 0 ]; then aces=`expr $sp/2` else aces=`expr ($sp + 1)/2` #this is to prevent the program from trying to print out 1 1/2 or 7 1/2 spaces fi for ((h=1; $h <= $aces; h++)); do #This is where I am getting the syntax error- it says "unexpected ;" echo -n ""; #actual printing of the spaces done for((j=1; $j <= $Input; j++)); do echo -n "$i"; #prints the numbers done done exit 0;
edited 18th Sep '16 7:53:35 PM by Xopher001
Yes.