wickedxter has asked for the wisdom of the Perl Monks concerning the following question:
The code snipplet is part of a game for calulating battles, it goes ship by ship each searching (randomly) targets of the other side and caluating if the ship has more then one weapon. If this doesnt explain enough just ask and i'll go through and break the code down.
It's kinda long.
## Battleship if($d_batt){ for my $x (1..$d_batt){ my $rand_ss = int rand(4); if($rand_ss eq 1 && $a_crv ne 0){ #weak target my $weak = int rand 2; if($weak eq 0 || $weak eq 2){ for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $y = int rand $a_crv; $y++ if $y eq 0; push(@{$data->{shots}{defender}},"$y|crv|batt"); $data->fleet_d("defender","attacker"); } }else { for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $weakest = $data->damaged("crv",$config->{armor}{cr +v},"attacker"); my ($num,$class) = split(/\|/,$weakest); push(@{$data->{shots}{defender}},"$num|$class|batt"); $data->fleet_d("defender","attacker"); } } }elsif ($rand_ss eq 2 && $a_des ne 0){ #next if $data->{shots}{$y}; #weak target my $weak = int rand 2; if($weak eq 0 || $weak eq 2){ for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $y = int rand $a_des; $y++ if $y eq 0; push(@{$data->{shots}{defender}},"$y|des|batt"); $data->fleet_d("defender","attacker"); } }else { for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $weakest = $data->damaged("des",$config->{armor}{de +s},"attacker"); my ($num,$class) = split(/\|/,$weakest); push(@{$data->{shots}{defender}},"$num|$class|batt"); $data->fleet_d("defender","attacker"); } } }elsif($rand_ss eq 3 && $a_corvette ne 0){ #next if $data->{shots}{$y}; for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $y = int rand $a_corvette; $y++ if $y eq 0; push(@{$data->{shots}{defender}},"$y|corvette|batt"); $data->fleet_d("defender","attacker"); } }else { for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $r_shipD = $data->random_ship("attacker","defender"); my ($num, $ship) = split(/\|/,$r_shipD); push(@{$data->{shots}{defender}},"$num|$ship|batt"); $data->fleet_d("defender","attacker"); } } }
2006-04-02 Retitled by planetscape, as per Monastery guidelines
Original title: 'This a good way?'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is this a good way to implement a Battleship game?
by lima1 (Curate) on Mar 31, 2006 at 19:32 UTC | |
|
Re: Is this a good way to implement a Battleship game?
by CountZero (Bishop) on Mar 31, 2006 at 19:58 UTC | |
|
Re: Is this a good way to implement a Battleship game?
by nobull (Friar) on Apr 01, 2006 at 12:20 UTC |