Simple Hangman in PHP
This is the simple game of hangman. Guess letters and six wrong letters means you're dead.
This took me about 20 hours. And I learned enough to be certain I could make something better now in half the time. Still, give me your honest (and brutal) opinion, since that will help me improve the most.
<?php
//init
$woordenlijst = array(
"knie",
"rug",
"nek",
"elleboog",
);
$gekozen = '';
$woordstatus = array('*',);
$aantalfouten = 0;
//function
function tekengalg($arg1)
//tekent de juiste galg
{
if ($arg1 == 0) {
//blanco galg
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 1) {
// galg1
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 2) {
//galg2
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 3){
//galg3
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 4){
//galg4
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 5){
//galg5
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 6){
//galg6
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
}
function toonwoord($letter)
//print de bekende letters in het woord, met verder sterretjes
{
global $galgwoord, $woordstatus;
$galgarray = str_split($galgwoord);
for ($i=0; $i < strlen($galgwoord); $i++) {
if ($galgarray[$i] == $letter) {
$woordstatus[$i] = $letter;
}
}
for ($i=0; $i < strlen($galgwoord); $i++) {
echo $woordstatus[$i] . " ";
}
echo PHP_EOL;
}
function doorgaan($int){
//true is doorgaan
//zes fouten dan return false
//woord is af dan return false,
global $length, $woordstatus;
$boolret = false;
if ($int == 6) {
} else {
# stop als geen asterisk
for ($i=0; $i < $length; $i++) {
if ($woordstatus[$i] == '*') {
$boolret = true;
}
}
}
return $boolret;
}
//kies een woord
$galgwoord = $woordenlijst[rand(1, sizeof($woordenlijst)) - 1];
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
echo "het woord heeft $length letters" . PHP_EOL;
//blanco statusscherm
tekengalg($aantalfouten);
//gekozen letters, woordstatus, foute letters, stand van de galg
echo "U heeft de volgende letters al gekozen: " . $gekozen . PHP_EOL;
echo "U weet dit van het woord: " . $woordstatus[0] . PHP_EOL;
echo "De volgende letters staan niet in het woord: " . PHP_EOL;
//deel 3, computer vraagt een letter,
while (doorgaan($aantalfouten)) {
echo "Geef uw keuze voor een letter." . PHP_EOL;
echo "> ";
$input = trim(fgets(STDIN));
//is het een letter? is die letter eerder gebruikt?
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
echo "De door u gekozen letter is: $input" . PHP_EOL;
if (is_numeric(strpos($gekozen, $input))) {
echo "deze letter heeft u al eerder geprobeerd" . PHP_EOL;
}
else {
echo "U heeft deze letter niet eerder gekozen" . PHP_EOL;
$gekozen .= $input;
if (is_numeric(strpos($galgwoord, $input))) {
echo "goedzo, deze letter komt voor in het woord." . PHP_EOL;
// toon woord met alle bekende letters
toonwoord($input);
} else {
echo "jammer, deze letter komt niet voor in het woord." . PHP_EOL;
$aantalfouten += 1;
tekengalg($aantalfouten);
// nieuw plaatje
}
}
$gekozen .= $input;
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
}
if ($aantalfouten == 6) {
echo "sorry, u heeft verloren" . PHP_EOL;
}else{
echo "gefeliciteerd, u heeft gewonnen" . PHP_EOL;
}
?>
beginner php game console hangman
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
This is the simple game of hangman. Guess letters and six wrong letters means you're dead.
This took me about 20 hours. And I learned enough to be certain I could make something better now in half the time. Still, give me your honest (and brutal) opinion, since that will help me improve the most.
<?php
//init
$woordenlijst = array(
"knie",
"rug",
"nek",
"elleboog",
);
$gekozen = '';
$woordstatus = array('*',);
$aantalfouten = 0;
//function
function tekengalg($arg1)
//tekent de juiste galg
{
if ($arg1 == 0) {
//blanco galg
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 1) {
// galg1
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 2) {
//galg2
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 3){
//galg3
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 4){
//galg4
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 5){
//galg5
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 6){
//galg6
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
}
function toonwoord($letter)
//print de bekende letters in het woord, met verder sterretjes
{
global $galgwoord, $woordstatus;
$galgarray = str_split($galgwoord);
for ($i=0; $i < strlen($galgwoord); $i++) {
if ($galgarray[$i] == $letter) {
$woordstatus[$i] = $letter;
}
}
for ($i=0; $i < strlen($galgwoord); $i++) {
echo $woordstatus[$i] . " ";
}
echo PHP_EOL;
}
function doorgaan($int){
//true is doorgaan
//zes fouten dan return false
//woord is af dan return false,
global $length, $woordstatus;
$boolret = false;
if ($int == 6) {
} else {
# stop als geen asterisk
for ($i=0; $i < $length; $i++) {
if ($woordstatus[$i] == '*') {
$boolret = true;
}
}
}
return $boolret;
}
//kies een woord
$galgwoord = $woordenlijst[rand(1, sizeof($woordenlijst)) - 1];
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
echo "het woord heeft $length letters" . PHP_EOL;
//blanco statusscherm
tekengalg($aantalfouten);
//gekozen letters, woordstatus, foute letters, stand van de galg
echo "U heeft de volgende letters al gekozen: " . $gekozen . PHP_EOL;
echo "U weet dit van het woord: " . $woordstatus[0] . PHP_EOL;
echo "De volgende letters staan niet in het woord: " . PHP_EOL;
//deel 3, computer vraagt een letter,
while (doorgaan($aantalfouten)) {
echo "Geef uw keuze voor een letter." . PHP_EOL;
echo "> ";
$input = trim(fgets(STDIN));
//is het een letter? is die letter eerder gebruikt?
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
echo "De door u gekozen letter is: $input" . PHP_EOL;
if (is_numeric(strpos($gekozen, $input))) {
echo "deze letter heeft u al eerder geprobeerd" . PHP_EOL;
}
else {
echo "U heeft deze letter niet eerder gekozen" . PHP_EOL;
$gekozen .= $input;
if (is_numeric(strpos($galgwoord, $input))) {
echo "goedzo, deze letter komt voor in het woord." . PHP_EOL;
// toon woord met alle bekende letters
toonwoord($input);
} else {
echo "jammer, deze letter komt niet voor in het woord." . PHP_EOL;
$aantalfouten += 1;
tekengalg($aantalfouten);
// nieuw plaatje
}
}
$gekozen .= $input;
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
}
if ($aantalfouten == 6) {
echo "sorry, u heeft verloren" . PHP_EOL;
}else{
echo "gefeliciteerd, u heeft gewonnen" . PHP_EOL;
}
?>
beginner php game console hangman
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
This is the simple game of hangman. Guess letters and six wrong letters means you're dead.
This took me about 20 hours. And I learned enough to be certain I could make something better now in half the time. Still, give me your honest (and brutal) opinion, since that will help me improve the most.
<?php
//init
$woordenlijst = array(
"knie",
"rug",
"nek",
"elleboog",
);
$gekozen = '';
$woordstatus = array('*',);
$aantalfouten = 0;
//function
function tekengalg($arg1)
//tekent de juiste galg
{
if ($arg1 == 0) {
//blanco galg
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 1) {
// galg1
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 2) {
//galg2
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 3){
//galg3
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 4){
//galg4
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 5){
//galg5
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 6){
//galg6
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
}
function toonwoord($letter)
//print de bekende letters in het woord, met verder sterretjes
{
global $galgwoord, $woordstatus;
$galgarray = str_split($galgwoord);
for ($i=0; $i < strlen($galgwoord); $i++) {
if ($galgarray[$i] == $letter) {
$woordstatus[$i] = $letter;
}
}
for ($i=0; $i < strlen($galgwoord); $i++) {
echo $woordstatus[$i] . " ";
}
echo PHP_EOL;
}
function doorgaan($int){
//true is doorgaan
//zes fouten dan return false
//woord is af dan return false,
global $length, $woordstatus;
$boolret = false;
if ($int == 6) {
} else {
# stop als geen asterisk
for ($i=0; $i < $length; $i++) {
if ($woordstatus[$i] == '*') {
$boolret = true;
}
}
}
return $boolret;
}
//kies een woord
$galgwoord = $woordenlijst[rand(1, sizeof($woordenlijst)) - 1];
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
echo "het woord heeft $length letters" . PHP_EOL;
//blanco statusscherm
tekengalg($aantalfouten);
//gekozen letters, woordstatus, foute letters, stand van de galg
echo "U heeft de volgende letters al gekozen: " . $gekozen . PHP_EOL;
echo "U weet dit van het woord: " . $woordstatus[0] . PHP_EOL;
echo "De volgende letters staan niet in het woord: " . PHP_EOL;
//deel 3, computer vraagt een letter,
while (doorgaan($aantalfouten)) {
echo "Geef uw keuze voor een letter." . PHP_EOL;
echo "> ";
$input = trim(fgets(STDIN));
//is het een letter? is die letter eerder gebruikt?
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
echo "De door u gekozen letter is: $input" . PHP_EOL;
if (is_numeric(strpos($gekozen, $input))) {
echo "deze letter heeft u al eerder geprobeerd" . PHP_EOL;
}
else {
echo "U heeft deze letter niet eerder gekozen" . PHP_EOL;
$gekozen .= $input;
if (is_numeric(strpos($galgwoord, $input))) {
echo "goedzo, deze letter komt voor in het woord." . PHP_EOL;
// toon woord met alle bekende letters
toonwoord($input);
} else {
echo "jammer, deze letter komt niet voor in het woord." . PHP_EOL;
$aantalfouten += 1;
tekengalg($aantalfouten);
// nieuw plaatje
}
}
$gekozen .= $input;
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
}
if ($aantalfouten == 6) {
echo "sorry, u heeft verloren" . PHP_EOL;
}else{
echo "gefeliciteerd, u heeft gewonnen" . PHP_EOL;
}
?>
beginner php game console hangman
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This is the simple game of hangman. Guess letters and six wrong letters means you're dead.
This took me about 20 hours. And I learned enough to be certain I could make something better now in half the time. Still, give me your honest (and brutal) opinion, since that will help me improve the most.
<?php
//init
$woordenlijst = array(
"knie",
"rug",
"nek",
"elleboog",
);
$gekozen = '';
$woordstatus = array('*',);
$aantalfouten = 0;
//function
function tekengalg($arg1)
//tekent de juiste galg
{
if ($arg1 == 0) {
//blanco galg
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 1) {
// galg1
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 2) {
//galg2
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 3){
//galg3
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 4){
//galg4
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 5){
//galg5
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
if ($arg1 == 6){
//galg6
echo (" +---+") . PHP_EOL;
echo (" | |") . PHP_EOL;
echo (" o |") . PHP_EOL;
echo ("/| |") . PHP_EOL;
echo ("/ |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo (" |") . PHP_EOL;
echo ("=======") . PHP_EOL;
}
}
function toonwoord($letter)
//print de bekende letters in het woord, met verder sterretjes
{
global $galgwoord, $woordstatus;
$galgarray = str_split($galgwoord);
for ($i=0; $i < strlen($galgwoord); $i++) {
if ($galgarray[$i] == $letter) {
$woordstatus[$i] = $letter;
}
}
for ($i=0; $i < strlen($galgwoord); $i++) {
echo $woordstatus[$i] . " ";
}
echo PHP_EOL;
}
function doorgaan($int){
//true is doorgaan
//zes fouten dan return false
//woord is af dan return false,
global $length, $woordstatus;
$boolret = false;
if ($int == 6) {
} else {
# stop als geen asterisk
for ($i=0; $i < $length; $i++) {
if ($woordstatus[$i] == '*') {
$boolret = true;
}
}
}
return $boolret;
}
//kies een woord
$galgwoord = $woordenlijst[rand(1, sizeof($woordenlijst)) - 1];
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
echo "het woord heeft $length letters" . PHP_EOL;
//blanco statusscherm
tekengalg($aantalfouten);
//gekozen letters, woordstatus, foute letters, stand van de galg
echo "U heeft de volgende letters al gekozen: " . $gekozen . PHP_EOL;
echo "U weet dit van het woord: " . $woordstatus[0] . PHP_EOL;
echo "De volgende letters staan niet in het woord: " . PHP_EOL;
//deel 3, computer vraagt een letter,
while (doorgaan($aantalfouten)) {
echo "Geef uw keuze voor een letter." . PHP_EOL;
echo "> ";
$input = trim(fgets(STDIN));
//is het een letter? is die letter eerder gebruikt?
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
echo "De door u gekozen letter is: $input" . PHP_EOL;
if (is_numeric(strpos($gekozen, $input))) {
echo "deze letter heeft u al eerder geprobeerd" . PHP_EOL;
}
else {
echo "U heeft deze letter niet eerder gekozen" . PHP_EOL;
$gekozen .= $input;
if (is_numeric(strpos($galgwoord, $input))) {
echo "goedzo, deze letter komt voor in het woord." . PHP_EOL;
// toon woord met alle bekende letters
toonwoord($input);
} else {
echo "jammer, deze letter komt niet voor in het woord." . PHP_EOL;
$aantalfouten += 1;
tekengalg($aantalfouten);
// nieuw plaatje
}
}
$gekozen .= $input;
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
}
if ($aantalfouten == 6) {
echo "sorry, u heeft verloren" . PHP_EOL;
}else{
echo "gefeliciteerd, u heeft gewonnen" . PHP_EOL;
}
?>
beginner php game console hangman
beginner php game console hangman
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Dec 18 at 17:20
Sᴀᴍ Onᴇᴌᴀ
8,33261853
8,33261853
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Dec 18 at 9:51
marcel
113
113
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marcel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use elseif for mutually exclusive conditions
In tekengalg there are multiple if statements that cannot be true at the same time:
if ($arg1 == 0) {
// ...
}
if ($arg1 == 1) {
// ...
}
if ($arg1 == 2) {
// ...
}
// ...
That is, after $arg1 is known to be 0, then there's no need to evaluate $arg == 1, and so on. Replace the second and later if with elseif.
Avoid flag variables when possible
In doorgaan, $boolret is set to true when an '*' is found.
Its value never changes again.
Instead of setting $boolret = true, you could return true at this point.
This will have two nice effects:
- Stop looping as soon as possible. It's pointless to continue in the loop, it won't change the outcome of the function.
- Eliminate a variable. If the end of the loop is reached, that means we never returned because we haven't found
'*', so you canreturn false. No need for the variable$boolret.
Avoid deeply nested statements
Deeply nested statements like this can be hard to read:
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
// ...
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
In particular, when the main branch of an if is a long piece of code, then by the time you read the else, you might not remember well what it was about.
In such cases it can be interesting to invert the conditions, making the code flatter, and perhaps easier to understand:
if (!ctype_alpha($input)) {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
continue;
}
if (strlen($input) != 1) {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
continue;
}
// ...
Use more helper functions
I find the echo "..." . PHP_EOL boilerplate tedious... I would create a helper function that appends PHP_EOL, so I don't have to repeatedly type that.
Use better names
It's important to use names that describe the values they represent,
and help readers understand the code.
For example $arg1 doesn't describe that it's the number of failed guesses.
1
To improve on "use better names": don't program in Dutch. Stick to English. That's what most of the rest of the world does too and makes it a lot more readable.
– Mast
Dec 18 at 17:09
add a comment |
Avoid global variables
This may be difficult to explain but there are many reasons to avoid global variables, many of which are explained in this article. The reasons that stand out the most to me are Implicit coupling and Testing and Confinement (testing becomes a lot more difficult when global variables are used).
Empty if statement
I see the following in doorgaan():
if ($int == 6) {
} else {
It would be simpler to just use a negated condition from the if condition:
if ($int !== 6) {
//statements currently in the else block
}
Use Nowdoc syntax for multi-line text that is static
Each of the gallows representations could be stored in a constant - e.g. with define() or const
const GALLOWS_0 = <<<'GALLOWS'
+---+
| |
|
|
|
|
|
=======
GALLOWS;
Then GALLOWS_0 could be used instead of the 8 echo() statements with PHP_EOL appended. Another option would be to construct the gallows dynamically based the number in $arg1. Of the six variations with the eight lines, there are only three lines that really change - i.e. the third, fourth and fifth lines.
Utilize more string functions
Code like
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
Could be simplified with str_repeat():
$woordstatus = str_repeat('*', strlen($galgwoord));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
marcel is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209893%2fsimple-hangman-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use elseif for mutually exclusive conditions
In tekengalg there are multiple if statements that cannot be true at the same time:
if ($arg1 == 0) {
// ...
}
if ($arg1 == 1) {
// ...
}
if ($arg1 == 2) {
// ...
}
// ...
That is, after $arg1 is known to be 0, then there's no need to evaluate $arg == 1, and so on. Replace the second and later if with elseif.
Avoid flag variables when possible
In doorgaan, $boolret is set to true when an '*' is found.
Its value never changes again.
Instead of setting $boolret = true, you could return true at this point.
This will have two nice effects:
- Stop looping as soon as possible. It's pointless to continue in the loop, it won't change the outcome of the function.
- Eliminate a variable. If the end of the loop is reached, that means we never returned because we haven't found
'*', so you canreturn false. No need for the variable$boolret.
Avoid deeply nested statements
Deeply nested statements like this can be hard to read:
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
// ...
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
In particular, when the main branch of an if is a long piece of code, then by the time you read the else, you might not remember well what it was about.
In such cases it can be interesting to invert the conditions, making the code flatter, and perhaps easier to understand:
if (!ctype_alpha($input)) {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
continue;
}
if (strlen($input) != 1) {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
continue;
}
// ...
Use more helper functions
I find the echo "..." . PHP_EOL boilerplate tedious... I would create a helper function that appends PHP_EOL, so I don't have to repeatedly type that.
Use better names
It's important to use names that describe the values they represent,
and help readers understand the code.
For example $arg1 doesn't describe that it's the number of failed guesses.
1
To improve on "use better names": don't program in Dutch. Stick to English. That's what most of the rest of the world does too and makes it a lot more readable.
– Mast
Dec 18 at 17:09
add a comment |
Use elseif for mutually exclusive conditions
In tekengalg there are multiple if statements that cannot be true at the same time:
if ($arg1 == 0) {
// ...
}
if ($arg1 == 1) {
// ...
}
if ($arg1 == 2) {
// ...
}
// ...
That is, after $arg1 is known to be 0, then there's no need to evaluate $arg == 1, and so on. Replace the second and later if with elseif.
Avoid flag variables when possible
In doorgaan, $boolret is set to true when an '*' is found.
Its value never changes again.
Instead of setting $boolret = true, you could return true at this point.
This will have two nice effects:
- Stop looping as soon as possible. It's pointless to continue in the loop, it won't change the outcome of the function.
- Eliminate a variable. If the end of the loop is reached, that means we never returned because we haven't found
'*', so you canreturn false. No need for the variable$boolret.
Avoid deeply nested statements
Deeply nested statements like this can be hard to read:
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
// ...
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
In particular, when the main branch of an if is a long piece of code, then by the time you read the else, you might not remember well what it was about.
In such cases it can be interesting to invert the conditions, making the code flatter, and perhaps easier to understand:
if (!ctype_alpha($input)) {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
continue;
}
if (strlen($input) != 1) {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
continue;
}
// ...
Use more helper functions
I find the echo "..." . PHP_EOL boilerplate tedious... I would create a helper function that appends PHP_EOL, so I don't have to repeatedly type that.
Use better names
It's important to use names that describe the values they represent,
and help readers understand the code.
For example $arg1 doesn't describe that it's the number of failed guesses.
1
To improve on "use better names": don't program in Dutch. Stick to English. That's what most of the rest of the world does too and makes it a lot more readable.
– Mast
Dec 18 at 17:09
add a comment |
Use elseif for mutually exclusive conditions
In tekengalg there are multiple if statements that cannot be true at the same time:
if ($arg1 == 0) {
// ...
}
if ($arg1 == 1) {
// ...
}
if ($arg1 == 2) {
// ...
}
// ...
That is, after $arg1 is known to be 0, then there's no need to evaluate $arg == 1, and so on. Replace the second and later if with elseif.
Avoid flag variables when possible
In doorgaan, $boolret is set to true when an '*' is found.
Its value never changes again.
Instead of setting $boolret = true, you could return true at this point.
This will have two nice effects:
- Stop looping as soon as possible. It's pointless to continue in the loop, it won't change the outcome of the function.
- Eliminate a variable. If the end of the loop is reached, that means we never returned because we haven't found
'*', so you canreturn false. No need for the variable$boolret.
Avoid deeply nested statements
Deeply nested statements like this can be hard to read:
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
// ...
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
In particular, when the main branch of an if is a long piece of code, then by the time you read the else, you might not remember well what it was about.
In such cases it can be interesting to invert the conditions, making the code flatter, and perhaps easier to understand:
if (!ctype_alpha($input)) {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
continue;
}
if (strlen($input) != 1) {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
continue;
}
// ...
Use more helper functions
I find the echo "..." . PHP_EOL boilerplate tedious... I would create a helper function that appends PHP_EOL, so I don't have to repeatedly type that.
Use better names
It's important to use names that describe the values they represent,
and help readers understand the code.
For example $arg1 doesn't describe that it's the number of failed guesses.
Use elseif for mutually exclusive conditions
In tekengalg there are multiple if statements that cannot be true at the same time:
if ($arg1 == 0) {
// ...
}
if ($arg1 == 1) {
// ...
}
if ($arg1 == 2) {
// ...
}
// ...
That is, after $arg1 is known to be 0, then there's no need to evaluate $arg == 1, and so on. Replace the second and later if with elseif.
Avoid flag variables when possible
In doorgaan, $boolret is set to true when an '*' is found.
Its value never changes again.
Instead of setting $boolret = true, you could return true at this point.
This will have two nice effects:
- Stop looping as soon as possible. It's pointless to continue in the loop, it won't change the outcome of the function.
- Eliminate a variable. If the end of the loop is reached, that means we never returned because we haven't found
'*', so you canreturn false. No need for the variable$boolret.
Avoid deeply nested statements
Deeply nested statements like this can be hard to read:
if (ctype_alpha($input)) {
if (strlen($input) == 1) {
// ...
} else {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
}
} else {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
}
In particular, when the main branch of an if is a long piece of code, then by the time you read the else, you might not remember well what it was about.
In such cases it can be interesting to invert the conditions, making the code flatter, and perhaps easier to understand:
if (!ctype_alpha($input)) {
echo "dit is geen geldige letter voor galgje." . PHP_EOL;
continue;
}
if (strlen($input) != 1) {
echo "U heeft meer dan één letter gekozen." . PHP_EOL;
continue;
}
// ...
Use more helper functions
I find the echo "..." . PHP_EOL boilerplate tedious... I would create a helper function that appends PHP_EOL, so I don't have to repeatedly type that.
Use better names
It's important to use names that describe the values they represent,
and help readers understand the code.
For example $arg1 doesn't describe that it's the number of failed guesses.
answered Dec 18 at 12:27
janos
97.1k12124350
97.1k12124350
1
To improve on "use better names": don't program in Dutch. Stick to English. That's what most of the rest of the world does too and makes it a lot more readable.
– Mast
Dec 18 at 17:09
add a comment |
1
To improve on "use better names": don't program in Dutch. Stick to English. That's what most of the rest of the world does too and makes it a lot more readable.
– Mast
Dec 18 at 17:09
1
1
To improve on "use better names": don't program in Dutch. Stick to English. That's what most of the rest of the world does too and makes it a lot more readable.
– Mast
Dec 18 at 17:09
To improve on "use better names": don't program in Dutch. Stick to English. That's what most of the rest of the world does too and makes it a lot more readable.
– Mast
Dec 18 at 17:09
add a comment |
Avoid global variables
This may be difficult to explain but there are many reasons to avoid global variables, many of which are explained in this article. The reasons that stand out the most to me are Implicit coupling and Testing and Confinement (testing becomes a lot more difficult when global variables are used).
Empty if statement
I see the following in doorgaan():
if ($int == 6) {
} else {
It would be simpler to just use a negated condition from the if condition:
if ($int !== 6) {
//statements currently in the else block
}
Use Nowdoc syntax for multi-line text that is static
Each of the gallows representations could be stored in a constant - e.g. with define() or const
const GALLOWS_0 = <<<'GALLOWS'
+---+
| |
|
|
|
|
|
=======
GALLOWS;
Then GALLOWS_0 could be used instead of the 8 echo() statements with PHP_EOL appended. Another option would be to construct the gallows dynamically based the number in $arg1. Of the six variations with the eight lines, there are only three lines that really change - i.e. the third, fourth and fifth lines.
Utilize more string functions
Code like
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
Could be simplified with str_repeat():
$woordstatus = str_repeat('*', strlen($galgwoord));
add a comment |
Avoid global variables
This may be difficult to explain but there are many reasons to avoid global variables, many of which are explained in this article. The reasons that stand out the most to me are Implicit coupling and Testing and Confinement (testing becomes a lot more difficult when global variables are used).
Empty if statement
I see the following in doorgaan():
if ($int == 6) {
} else {
It would be simpler to just use a negated condition from the if condition:
if ($int !== 6) {
//statements currently in the else block
}
Use Nowdoc syntax for multi-line text that is static
Each of the gallows representations could be stored in a constant - e.g. with define() or const
const GALLOWS_0 = <<<'GALLOWS'
+---+
| |
|
|
|
|
|
=======
GALLOWS;
Then GALLOWS_0 could be used instead of the 8 echo() statements with PHP_EOL appended. Another option would be to construct the gallows dynamically based the number in $arg1. Of the six variations with the eight lines, there are only three lines that really change - i.e. the third, fourth and fifth lines.
Utilize more string functions
Code like
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
Could be simplified with str_repeat():
$woordstatus = str_repeat('*', strlen($galgwoord));
add a comment |
Avoid global variables
This may be difficult to explain but there are many reasons to avoid global variables, many of which are explained in this article. The reasons that stand out the most to me are Implicit coupling and Testing and Confinement (testing becomes a lot more difficult when global variables are used).
Empty if statement
I see the following in doorgaan():
if ($int == 6) {
} else {
It would be simpler to just use a negated condition from the if condition:
if ($int !== 6) {
//statements currently in the else block
}
Use Nowdoc syntax for multi-line text that is static
Each of the gallows representations could be stored in a constant - e.g. with define() or const
const GALLOWS_0 = <<<'GALLOWS'
+---+
| |
|
|
|
|
|
=======
GALLOWS;
Then GALLOWS_0 could be used instead of the 8 echo() statements with PHP_EOL appended. Another option would be to construct the gallows dynamically based the number in $arg1. Of the six variations with the eight lines, there are only three lines that really change - i.e. the third, fourth and fifth lines.
Utilize more string functions
Code like
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
Could be simplified with str_repeat():
$woordstatus = str_repeat('*', strlen($galgwoord));
Avoid global variables
This may be difficult to explain but there are many reasons to avoid global variables, many of which are explained in this article. The reasons that stand out the most to me are Implicit coupling and Testing and Confinement (testing becomes a lot more difficult when global variables are used).
Empty if statement
I see the following in doorgaan():
if ($int == 6) {
} else {
It would be simpler to just use a negated condition from the if condition:
if ($int !== 6) {
//statements currently in the else block
}
Use Nowdoc syntax for multi-line text that is static
Each of the gallows representations could be stored in a constant - e.g. with define() or const
const GALLOWS_0 = <<<'GALLOWS'
+---+
| |
|
|
|
|
|
=======
GALLOWS;
Then GALLOWS_0 could be used instead of the 8 echo() statements with PHP_EOL appended. Another option would be to construct the gallows dynamically based the number in $arg1. Of the six variations with the eight lines, there are only three lines that really change - i.e. the third, fourth and fifth lines.
Utilize more string functions
Code like
$length = strlen($galgwoord);
for ($i=0; $i < $length; $i++) {
$woordstatus[$i] = '*';
}
Could be simplified with str_repeat():
$woordstatus = str_repeat('*', strlen($galgwoord));
answered Dec 18 at 17:20
Sᴀᴍ Onᴇᴌᴀ
8,33261853
8,33261853
add a comment |
add a comment |
marcel is a new contributor. Be nice, and check out our Code of Conduct.
marcel is a new contributor. Be nice, and check out our Code of Conduct.
marcel is a new contributor. Be nice, and check out our Code of Conduct.
marcel is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209893%2fsimple-hangman-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown