Sunday, October 5, 2025
HomePHPPHP Coding Puzzle 9: Phrase Ladder

PHP Coding Puzzle 9: Phrase Ladder


Welcome to the ninth PHP Coding Puzzle !

Phrase Ladder Recreation

Recreation Description

Phrase Ladder is a phrase recreation during which two gamers attempt to remodel one phrase into one other by altering one letter at a time, with every intermediate step being a sound phrase. For instance, to remodel the phrase “CAT” into the phrase “DOG”, one attainable phrase ladder is:

CAT -> COT -> DOT -> DOG

Gamers take turns making a transfer. On every transfer, a participant can change one letter of the phrase to create a brand new legitimate phrase. The primary participant who efficiently transforms the beginning phrase into the ending phrase wins the sport.

Necessities

Write a PHP perform known as wordLadder that enables two gamers to play a recreation of Phrase Ladder. The perform ought to take two parameters: the beginning phrase and the ending phrase.

The perform ought to immediate the primary participant for his or her transfer (the brand new phrase to create), validate the transfer (the brand new phrase must be one letter completely different from the earlier phrase and must be a sound English phrase), show the present state of the sport, and swap to the second participant if the transfer is legitimate.

The perform ought to proceed the sport till one participant efficiently transforms the beginning phrase into the ending phrase or no legitimate transfer might be made. The perform ought to return the successful participant (‘Participant 1’ or ‘Participant 2’) or ‘Tie’ if the sport is a tie.

The perform ought to use a dictionary file to validate phrases. You may obtain a dictionary file from http://www.gutenberg.org/ebooks/29765.

Instance Utilization

$consequence = wordLadder('CAT', 'DOG');
if ($consequence == 'Tie') {
  echo "It is a tie!n";
} else {
  echo "$consequence wins!n";
}

Instance Output

Participant 1's flip. Enter a phrase: COT
CAT -> COT
Participant 2's flip. Enter a phrase: DOT
CAT -> COT -> DOT
Participant 1's flip. Enter a phrase: DOG
Invalid transfer. Please strive once more.
Participant 1's flip. Enter a phrase: FOT
Invalid transfer. Please strive once more.
Participant 1's flip. Enter a phrase: COG
CAT -> COT -> COG
Participant 1 wins!

The answer is already out there within the subsequent web page 😉 Be happy to share with us your answer right here in feedback or Github.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments