Perlをはじめてみた。

要点というか、なんと言うか。

 ちょっと仕事が、あまりにアレなので、Perlの練習のつもりで、Perlのコードをはじめて書いてみた。一応サブルーチンのパッケージ化とか、Whileによるループ処理とか、if文による分岐とかを盛り込んでみたつもり。ハッシュは使えるかどうかよく分かっていないので使っていない。それはそうと、そのPerlで実装している題材が、炎の虎とグリフォンでCruiser Warfare、だというのは、我ながらあまりに愚かだ。恥を承知でコードを公開してみたりする。定数の配列を10個別名で定義するとか、ぱっと見恥ずかしいことをやっているなあ。あとダメージの綴りをミスっている。動作チェックは軽く適当にやってあるよ。

メインルーチン

#========================
#
##メインルーチンの記述
#
#========================

#==========================
#自作ライブラリの呼び出し
#===========================
require 'Dice_1d.pl';
require 'Damege_Chart.pl';
require 'Fire_Chart.pl';
require 'BeamCannon.pl';

#========================
#定数の定義
#========================
use constant Right_Init => 5;
use constant Left_Init  => 6;

#========================
#変数の定義
#========================
	#炎の虎の変数
	@Right_Life;
	$Right_Fire;
	
	#グリフォンの変数
	$Left_Life;
	$Left_Fire;
	
	#ダイス
	$Dice_Val;
	#ダメージ
	$Damege_Val;

#========================
#変数の初期化
#========================
	$Right_Life = Right_Init;
	$Right_Fire = 0;
	
	$Left_Life = Left_Init;
	$Left_Fire = 0;

#========================
#ゲーム開始
#========================

print "ゲームを開始します......\n";

#先行後攻の決定
	print "先攻後攻の決定......\n";
	$Dice_Val = &Dice_1d::Dice_1d;
	print "ダイスの目は$Dice_Valです。\n";

#先行がグリフォンの場合は1回戦闘処理
if($Dice_Val % 2 == 1){
	print "グリフォンの先攻です。\n";
	print "\n";
	
	print "グリフォンの攻撃!\n";
	#グリフォン火力決定
	$Left_Fire = &Fire_Chart::Fire_Chart;
	
	#攻撃成功の場合
	if( $Left_Fire > 0 ){
		#グリフォンの攻撃
		$Damege_Val = &Damege_Chart::Damege_Chart($Left_Fire, $Right_Life);
		print "炎の虎に $Damege_Val のダメージ!\n";
	
		#炎の虎沈没チェック
		$Right_Life = $Right_Life - $Damege_Val;
		if( $Right_Life <= 0 ){
			print "炎の虎は沈没しました。\n";
		}
	}
	elsif( $Left_Fire < 0 ){
		#グリフォンの自爆攻撃
		$Damege_Val = &Damege_Chart::Damege_Chart(-$Left_Fire, $Left_Life);
		print "グリフォンに $Damege_Val のダメージ!\n";
	
		#グリフォン沈没チェック
		$Left_Life = $Left_Life - $Damege_Val;
		if( $Left_Life <= 0 ){
			print "グリフォンは沈没しました。\n";
		}
	}
	print "\n";

}
else{
	print "炎の虎の先攻です。\n";
	print "\n"
}

#メインループ
while(($Right_Life > 0) && ($Left_Life > 0)){

	print "炎の虎の攻撃!\n";
	#炎の虎の火力決定
	$Right_Fire = &Fire_Chart::Fire_Chart;
	
	#攻撃成功の場合
	if( $Right_Fire > 0 ){
		#炎の虎の攻撃
		$Damege_Val = &Damege_Chart::Damege_Chart($Right_Fire, $Left_Life);
		print "グリフォンに $Damege_Val のダメージ!\n";
	
		#グリフォン沈没チェック
		$Left_Life = $Left_Life - $Damege_Val;
		if( $Left_Life <= 0 ){
			print "グリフォンは沈没しました。\n";
			last;
		}
	}
	elsif ( $Right_Fire < 0 ){
		#炎の虎の自爆攻撃
		$Damege_Val = &Damege_Chart::Damege_Chart(-$Right_Fire, $Right_Life);
		print "炎の虎に $Damege_Val のダメージ!\n";
	
		#炎の虎沈没チェック
		$Right_Life = $Right_Life - $Damege_Val;
		if( $Right_Life <= 0 ){
			print "炎の虎は沈没しました。\n";
			last;
		}
	}
	
	print "\n";
	
	print "グリフォンの攻撃!\n";
	#グリフォン火力決定
	$Left_Fire = &Fire_Chart::Fire_Chart;

	#攻撃成功の場合
	if( $Left_Fire > 0 ){
		#グリフォンの攻撃
		$Damege_Val = &Damege_Chart::Damege_Chart($Left_Fire, $Right_Life);
		print "炎の虎に $Damege_Val のダメージ!\n";
	
		#炎の虎沈没チェック
		$Right_Life = $Right_Life - $Damege_Val;
		if( $Right_Life <= 0 ){
			print "炎の虎は沈没しました。\n";
			last;
		}
	}
	elsif( $Left_Fire < 0 ){
		#グリフォンの自爆攻撃
		$Damege_Val = &Damege_Chart::Damege_Chart(-$Left_Fire, $Left_Life);
		print "グリフォンに $Damege_Val のダメージ!\n";
	
		#グリフォン沈没チェック
		$Left_Life = $Left_Life - $Damege_Val;
		if( $Left_Life <= 0 ){
			print "グリフォンは沈没しました。\n";
			last;
		}
	}
	print "\n";

}
#メインループ終了

#勝者の表示
if( $Right_Life <= 0 ){
	print "グリフォンの勝利です!\n";
}
else{
	print "炎の虎の勝利です!\n";
}

#ゲーム終了
	print "ゲームを終了します......\n"

6面ダイスのサブルーチン Dice_1d.pl

package Dice_1d;

sub Dice_1d{
	$Result;
	$Max = 6;
	
	$Result = int (rand($Max)) + 1;
	
	return $Result;
	
}
1;

Cruiserの武器チャート Fire_Chart.pl

package Fire_Chart;

sub Fire_Chart{
	$Weapon;
	$Fire;
	
	#==========================
	#武器の種類を決定
	#==========================
	$Weapon = &Dice_1d::Dice_1d + &Dice_1d::Dice_1d;
	print "ダイスの目は$Weapon\n";
	
	#D弾の場合
	if( $Weapon == 2 ){
		print "D弾を発射!\n";
		$Fire = 50;
		print "火力:$Fireで攻撃!\n";
	}
	#G弾の場合
	elsif( $Weapon == 3 ){
		print "G弾を発射!\n";
		$Fire = 30;
		print "火力:$Fireで攻撃!\n";
	}
	#ビームキャノンの場合火力を決定
	elsif(( $Weapon >= 4 ) && ( $Weapon <= 6 )){
		print "ビームキャノン発射!\n";
		$Fire = &BeamCannon::BeamCannon;
		print "火力:$Fireで攻撃!\n";
	} 
	#誤爆の場合はマイナスで火力を決定
	elsif( $Weapon == 12 ){
		print "自機を誤爆!\n";
		$Fire = &BeamCannon::BeamCannon;
		print "火力:$Fireで攻撃!\n";
		$Fire = - $Fire;
	}
	#上記以外は何もなし
	else{
		print "攻撃に失敗!\n";
		$Fire = 0;
	}
	
	return $Fire;
}
1;

ビームキャノンの火力チャート BeamCannon.pl

package BeamCannon;

sub BeamCannon{
	$Result;
	$Fire;
	
	$Result = &Dice_1d::Dice_1d;
	print "ダイスの目は$Resultです。\n";
	
	$Fire = 8 + 2 * $Result;
	
	return $Fire;
}
1;

戦闘結果チャート Damege_Chart.pl

package Damege_Chart;

sub Damege_Chart{
	$Fire = @_[0];
	$Life = @_[1];
	@Damege10_1 = ( 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 1);
	@Damege09_1 = ( 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1);
	@Damege08_1 = ( 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1);
	@Damege07_1 = ( 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0);
	@Damege06_1 = ( 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0);
	@Damege05_1 = ( 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0);
	@Damege04_1 = ( 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0);
	@Damege03_1 = ( 3, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0);
	@Damege02_1 = ( 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0);
	@Damege01_1 = ( 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0);
	@Damege1_02 = ( 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	$Damege;
	$Ratio;
	$Result;
	
	# 火力と耐久力の比率を計算
	$Ratio = $Fire / $Life;
	print "火力:$Fire\n";
	print "耐久力:$Life\n";
	print "Ratio: $Ratio\n";
	
	#ダイスを振る
	$Result = &Dice_1d::Dice_1d + &Dice_1d::Dice_1d;
	print "ダイスの目は$Resultです。\n";
	$Result = $Result - 2;
	
	# 10:1以上の処理
	if( $Ratio >= 10 ){
		$Damege = $Damege10_1[$Result];
		print "10:1です\n";
	}
	# 9:1の処理
	elsif ( $Ratio >= 9 ){
		$Damege = $Damege09_1[$Result];
		print "9:1です\n";
	}
	# 8:1の処理
	elsif ( $Ratio >= 8 ){
		$Damege = $Damege08_1[$Result];
		print "8:1です\n";
	}
	# 7:1の処理
	elsif ( $Ratio >= 7 ){
		$Damege = $Damege07_1[$Result];
		print "7:1です\n";
	}
	# 6:1の処理
	elsif ( $Ratio >= 6 ){
		$Damege = $Damege06_1[$Result];
		print "6:1です\n";
	}
	# 5:1の処理
	elsif ( $Ratio >= 5 ){
		$Damege = $Damege05_1[$Result];
		print "5:1です\n";
	}
	# 4:1の処理
	elsif ( $Ratio >= 4 ){
		$Damege = $Damege04_1[$Result];	
		print "4:1です\n";
	}
	# 3:1の処理
	elsif ( $Ratio >= 3 ){
		$Damege = $Damege03_1[$Result];
		print "3:1です\n";
	}
	# 2:1の処理
	elsif ( $Ratio >= 2 ){
		$Damege = $Damege02_1[$Result];
		print "2:1です\n";
	}
	# 1:1の処理
	elsif ( $Ratio >= 1 ){
		$Damege = $Damege01_1[$Result];
		print "1:1です\n";
	}
	# 1:2以上の処理
	elsif ( $Ratio <= 0.5 ){
		$Damege = $Damege1_02[$Result];
		print "1:2です\n";
	}
	
	print "ダメージは$Damegeです!\n";
	return $Damege;
}
1;