# Need Help with Animation Coding Puzzle

## Recommended Posts

I am trying to solve this  puzzle in PHP. I have found lots of help in Java and C++, but that isn't helping.

I have gotten my code to spit out the first line, but am not sure how to get it to continue until all the particles have cleared the string.

Any help would be appreciated!

Problem Statement      A collection of particles is contained in a linear chamber. They all have the same speed, but some are headed toward the right and others are headed toward the left. These particles can pass through each other without disturbing the motion of the particles, so all the particles will leave the chamber relatively quickly.

We will be given the initial conditions by a String init containing at each position an 'L' for a leftward moving particle, an 'R' for a rightward moving particle, or a '.' for an empty location. init shows all the positions in the chamber. Initially, no location in the chamber contains two particles passing through each other.

We would like an animation of the process. At each unit of time, we want a string showing occupied locations with an 'X' and unoccupied locations with a '.'. Create a class Animation that contains a method animate that is given an int speed and a String init giving the initial conditions. The speed is the number of positions each particle moves in one time unit.

The method will return a String[] in which each successive element shows the occupied locations at the next time unit. The first element of the return should show the occupied locations at the initial instant (at time = 0) in the 'X','.' format. The last element in the return should show the empty chamber at the first time that it becomes empty.

Definition      Class: Animation Method: animate Parameters: int, String Returns: String[] Method signature: String[] animate(int speed, String init) (be sure your method is public)          Constraints - speed will be between 1 and 10 inclusive - init will contain between 1 and 50 characters inclusive - each character in init will be '.' or 'L' or 'R'   Examples 0)
`2`
` "..R...." `
`Returns: { "..X....",  "....X..",  "......X",  "......." }`
The single particle starts at the 3rd position, moves to the 5th, then 7th, and then out of the chamber. 1)
`3`
`"RR..LRL" `
`Returns: { "XX..XXX",  ".X.XX..",  "X.....X",  "......." }`
At time 1, there are actually 4 particles in the chamber, but two are passing through each other at the 4th position 2)
`2`
`"LRLR.LRLR"`
`Returns: { "XXXX.XXXX",  "X..X.X..X",  ".X.X.X.X.",  ".X.....X.",  "........." }`
At time 0 there are 8 particles. At time 1, there are still 6 particles, but only 4 positions are occupied since particles are passing through each other. 3)
`10`
`"RLRLRLRLRL"`
`Returns: { "XXXXXXXXXX",  ".........." }`
These particles are moving so fast that they all exit the chamber by time 1. 4)
`1`
`"..."`
`Returns: { "..." }`
5)
`1`
`"LRRL.LR.LRR.R.LRRL."`
`Returns: { "XXXX.XX.XXX.X.XXXX.", "..XXX..X..XX.X..XX.", ".X.XX.X.X..XX.XX.XX", "X.X.XX...X.XXXXX..X", ".X..XXX...X..XX.X..", "X..X..XX.X.XX.XX.X.", "..X....XX..XX..XX.X", ".X.....XXXX..X..XX.", "X.....X..XX...X..XX", ".....X..X.XX...X..X", "....X..X...XX...X..", "...X..X.....XX...X.", "..X..X.......XX...X", ".X..X.........XX...", "X..X...........XX..", "..X.............XX.", ".X...............XX", "X.................X", "..................." }`

##### Share on other sites

What have you got so far? I don't think you're going to find anyone that is willing to code up your entire assignment, no matter how fun this "puzzle" might be. Do some work and then let us help you with where you get stuck.

##### Share on other sites

Please consider wrapping your code in BBCode "Code" tags. This will help with formatting and legibility.

What have you got so far? I don't think you're going to find anyone that is willing to code up your entire assignment, now matter how fun this "puzzle" might be. Do some work and then let us help you with where you get stuck.

True. Unless they're desperate. Which might be the case. Are you sure, OP, that you don't need to go back to match.com and stop phishing around here?

##### Share on other sites

For the record, if you're the real username you say you are, I'm not trying to be mean.

But there are plenty of bad peeps around who might happily appropriate a username from some other place, come to a busy programming forum, and do something malicious, and users here should be careful if you don't play by the community rules.

##### Share on other sites

<?php

class Animation {

function animate(\$speed, \$init){

\$startArray = str_split(\$init);
\$positionCounter = 0;
\$particleArray = array();

foreach(\$startArray as \$char){

if(\$char == 'R'){

\$position = \$positionCounter;
\$particle = new Particle();
\$particle->setSpeed(\$speed);
\$particle->setDirection('R');
\$particle->setPosition(\$

position);
\$particleArray[] = \$particle;

} elseif (\$char == 'L'){

\$position = \$positionCounter;
\$particle = new Particle();
\$particle->setSpeed(\$speed);
\$particle->setDirection('L');
\$particle->setPosition(\$position);
\$particleArray[] = \$particle;
}

else {

\$position = \$positionCounter;
\$particle = new Particle();
\$particle->setSpeed(0);
\$particle->setDirection('');
\$particle->setPosition(\$position);

}
\$positionCounter ++;

}

foreach (\$particleArray as \$particle) {
if (!\$particle == '.') {
\$currentLine .= (\$particle -> getValue());
}
else {
\$currentLine .= '.';
}
}

echo \$currentLine;

}

}

class Particle {
public \$speed;
public \$direction;
public \$position;

public function getValue(){

if (\$this->direction == 'R' || \$this->direction == 'L'){
return 'X';
} else {
return '.';
}

}

public function setSpeed(\$s){

\$this->speed = \$s;

}

public function setDirection(\$d){
\$this->direction = \$d;
}

public function setPositon(\$p){
\$this->position = \$p;
}
}

\$test = new Animation();
\$test -> animate(1, "R..L.");

?>
##### Share on other sites

I am still trying to figure out how to get the particles to move in the string output

##### Share on other sites

this code will print out the first line, but it wont do anything else

<?php

class Animation{

function animate(\$speed, \$init){

\$startArray = str_split(\$init);
\$currentLine = '';

foreach (\$startArray as \$char) {
if (\$char == 'R'){

\$currentLine .= 'X';

} elseif (\$char == 'L') {

\$currentLine .= 'X';

} elseif (\$char == '.') {

\$currentLine .= '.';

}
}

echo \$currentLine;
}
}

\$tester = new Animation;
\$tester -> animate(1, "R...L..")

?>

##### Share on other sites

You know each particles start position, speed and direction.

Each time period calculate the new position for each after N periods

```foreach particle
if direction='R'
dir = 1
else
dir = -1
end if
newpos = startpos + N * speed * dir
end foreach```
Edited by Barand
##### Share on other sites

Any advice on how to make the Update Array function update the position of each particle while removing where the old particle was and putting an umoving particle in its place???

Latest code:

<?php

class Animation {

public function animate(\$speed, \$init){

\$particleArray = \$this->initData(\$speed, \$init);

foreach (\$particleArray as \$p) {
\$currentLine .= (\$p->getValue());
}
\$result[] = \$currentLine;

while (strpos(\$currentLine, 'X') !== false) {
foreach (\$particleArray as \$p) {
\$p->moveParticle();
\$newArray[] = \$p;
}
\$particleArray = \$this->updateArray(\$newArray);

foreach (\$particleArray as \$p) {
\$currentLine .= (\$p->getValue());
}
\$result[] = \$currentLine;
}

print_r(\$result);

}

public function initData(\$speed, \$init){
\$startArray = str_split(\$init);
\$positionCounter = 0;
\$particleArray = array();
\$result = array();

foreach(\$startArray as \$char){

if(\$char == 'R'){

\$position = \$positionCounter;
\$particle = new Particle();
\$particle->setSpeed(\$speed);
\$particle->setDirection('R');
\$particle->setPosition(\$

position);
\$particle->setValue('X');

\$particleArray[] = \$particle;

} elseif (\$char == 'L'){

\$position = \$positionCounter;
\$particle = new Particle();
\$particle->setSpeed(\$speed);
\$particle->setDirection('L');
\$particle->setPosition(\$position);
\$particle->setValue('X');

\$particleArray[] = \$particle;
}

else {

\$position = \$positionCounter;
\$particle = new Particle();
\$particle->setSpeed(0);
\$particle->setDirection('');
\$particle->setPosition(\$position);
\$particle->setValue('.');

\$particleArray[] = \$particle;

}
\$positionCounter ++;

}
}

public function updateArray(\$array){
\$newArray = array();
foreach (\$array as \$p) {
if (\$p->getValue() == 'X'){
array_splice(\$newArray, \$p->getPosition(), 0, \$p);
} else {
\$particle = new Particle();
\$particle->setSpeed(0);
\$particle->setDirection('');
\$particle->setPosition(\$p->getPosition());
\$particle->setValue('.');
\$newArray[] = \$particle;
}
}
print_r(\$newArray);
return \$newArray;
}

}

class Particle {
public \$speed;
public \$direction;
public \$position;
public \$value;

public function getValue(){
return \$this->value;
}

public function setValue(\$v){
\$this->value = \$v;
}

public function setSpeed(\$s){
\$this->speed = \$s;
}

public function getSpeed(\$s){
return \$this->speed;
}

public function setDirection(\$d){
\$this->direction = \$d;
}

public function getDirection(){
return \$this->direction;
}

public function setPosition(\$p){
\$this->position = \$p;
}

public function getPosition(){
return \$this->position;
}

public function moveParticle(){
if (\$this->direction == 'R') {
\$this->position = \$this->position + 1 * \$this->speed;
} elseif (\$this->direction == 'L') {
\$this->position = \$this->position - 1 * \$this->speed;
}
}
}

\$test = new Animation();
\$test -> animate(1, "R....");

?>
##### Share on other sites

Here's my solution

```<?php
include("/animate.class.php");

\$anim = new animation('LRRL.LR.LRR.R.LRRL.', 1);
echo '<pre>' . join("\n", \$anim->animate()) . '</pre>';
?>
```

animate.class.php:

```<?php
class particle
{
private \$startpos;
private \$speed;

public function __construct(\$pos,\$dir,\$speed)
{
\$this->startpos = \$pos;
\$this->speed = \$dir=='R' ? \$speed : -\$speed;
}

public function getPos(\$n)
{
return \$this->startpos + \$n * \$this->speed;
}
}

class animation
{
private \$particles;
private \$speed;
private \$size;

public function __construct(\$particleStr, \$speed)
{
\$l = strlen(\$particleStr);
\$this->size = \$l;
for (\$i=0; \$i<\$l; \$i++) {
if (\$particleStr[\$i] != '.')
\$this->particles[] = new particle(\$i, \$particleStr[\$i], \$speed);
}
}

public function animate()
{
\$rows = array();
\$n = 0;
do {
\$output = \$this->getPositions(\$n);
\$rows[\$n++] = \$output;
} while (trim(\$output,'.'));
return \$rows;
}

private function getPositions(\$n)
{
\$str = str_repeat('.', \$this->size);
if (count(\$this->particles) > 0) {
foreach (\$this->particles as \$p) {
\$pos = \$p->getPos(\$n);
if (\$pos >= 0 && \$pos < \$this->size) {
\$str[\$pos] = 'X';
}
}
}
return \$str;
}
}

```
##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.