私はPLayer1とPlayer2の単純なシステムを作成していますが、システムは30ラウンドが完了し、両方のプレーヤーがまだ生きている場合は30ラウンドまでしか制限されないため、ドローと呼ばれます。30ラウンド前にプレーヤー1が0または0未満になり、プレーヤー2がまだ生きている場合、プレーヤー2がゲームなどに勝ちます...
問題は、コードに何が問題なのか、まだ負の値が残っている理由です。私はすでにそこにifステートメントを設定しました。どんなアイデアでも私にとって大きな助けになります。私はまだ初心者プログラマーなので、改善の余地があります。ありがとうございます。
<?php
//Player 1
$p1Health = 100;
$p1Attack = 5;
$p1Speed = 3;
//Player 2
$p2Health = 70;
$p2Attack = 8;
$p2Speed = 5;
//Greater speed attack first
$speed1=0;
$speed2=0;
echo '<td>'.$p1Health.'</td><td>'.$p1Attack.'</td><td>'.$p1Speed.'</td>';
echo '<td>'.$p2Health.'</td><td>'.$p2Attack.'</td><td>'.$p2Speed.'</td>';
//Compare speed
if($p1Speed<$p2Speed){
$speed1=1; //start first
$speed2=0;
}
else {
$speed1=0; //start first
$speed2=1;
}
$rounds = 30; //maximum rounds
$count = 0;
while($count<=30){
if($p1Health<=0 || $p2Health<=0){ //if any of the players health is equal or below zero loop stop and declare winner
break;
}
else if($speed1==1){
$p2Health = $p2Health - $p1Attack;
echo 'Player 2 damaged by '.$p1Attack.' points.Health points left: '.$p2Health.'<br>';
//turn to other player to attack
$speed1=0;
$speed2=1;
}
else if($speed2==1){
$p1Health = $p1Health - $p2Attack;
echo 'Player 1 damaged by '.$p2Attack.' points.Health points left: '.$p1Health.'<br>';
//turn to other player to attack
$speed1=1;
$speed2=0;
}
$count++;
}
if($p1Health>0 && $p2Health<=0){
echo 'Player 1 wins the battle';
}
else if($p2Health>0 && $p1Health<=0){
echo 'Player 2 wins the battle';
}
else if($p1Health>0 && $p2Health>0){
echo 'Battle draw';
}
?>
私のコードが正しいかどうかはわかりませんが、これは私の理解に基づいており、これを改善するためのアイデアは私にとって本当に大きな助けになります。