Created
January 31, 2018 00:13
-
-
Save karlramberg/661ec723f444bc7135a0d4710146aaf1 to your computer and use it in GitHub Desktop.
Perl Learning (Old)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
my $num1 = &getNum; | |
my $operation = &getOperator; | |
my $num2 = &getNum; | |
if($operation eq '+'){ | |
&add; | |
} | |
elsif($operation eq '-'){ | |
&subtract; | |
} | |
elsif($operation eq '*'){ | |
&multiply; | |
} | |
elsif($operation eq '/'){ | |
÷ | |
} | |
sub getNum{ | |
print "Enter a number: "; | |
my $temp = readline STDIN; | |
chomp $temp; | |
return $temp; | |
} | |
sub getOperator{ | |
print "Enter an operator: "; | |
my $temp = readline STDIN; | |
chomp $temp; | |
if($temp eq '+' || $temp eq '-' || $temp eq '*' || $temp eq '/'){ | |
return $temp; | |
} | |
else { | |
print "That was not a valid operator.\n"; | |
&getOperator; | |
} | |
} | |
sub add{ | |
my $ans = $num1 + $num2; | |
print "$num1 + $num2 = $ans\n"; | |
} | |
sub subtract{ | |
my $ans = $num1 - $num2; | |
print "$num1 - $num2 = $ans\n"; | |
} | |
sub multiply{ | |
my $ans = $num1 * $num2; | |
print "$num1 * $num2 = $ans\n"; | |
} | |
sub divide{ | |
my $ans = $num1 / $num2; | |
print "$num1 / $num2 = $ans\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
our $QUEEN = 8; | |
our $EMPTY = 0; | |
our $INVALID = -1; | |
our $BOARD_SIZE = 8; | |
our @contents; | |
dialogue(); | |
initContents(); | |
placeQueen(0); | |
printContents(); | |
sub dialogue { | |
my $input = ""; | |
print 'Nonattacking Queens is a chess puzzle where the goal is to place | |
eight queens on a chessboard with none of them in attack position. | |
Enter to continue... '; | |
$input = <STDIN>; | |
print 'In chess, a queen can attack other pieces that | |
are in the same row, column, or diagonal as itself. | |
> Enter to continue... '; | |
$input = <STDIN>; | |
print 'It\'s also a puzzle famous for its | |
elegant recursive solution, implemented here. | |
Enter N to quit or anything else to see the solution. | |
> '; | |
$input = <STDIN>; | |
if($input eq 'n' or $input eq 'N') { exit; } | |
} | |
sub initContents { | |
my $result = ""; | |
for(my $i = 0; $i < $BOARD_SIZE; $i++) { | |
for(my $j = 0; $j < $BOARD_SIZE; $j++) { | |
$contents[$i][$j] = $EMPTY; | |
} | |
} | |
} | |
sub placeQueen { | |
my $column = 0; | |
my $row = $_[0]; | |
while($column < $BOARD_SIZE) { | |
$contents[$row][$column] = $QUEEN; | |
if(noQueenConfliction($row, $column)) { | |
if($row == $BOARD_SIZE - 1) { return 1; } | |
if(placeQueen($row + 1) == 1) { | |
return 1; | |
} | |
} | |
$contents[$row][$column] = $EMPTY; | |
$column++; | |
} | |
return 0; | |
} | |
sub noQueenConfliction { | |
my $row = $_[0]; | |
my $column = $_[1]; | |
my $i = 1; | |
while($i < $BOARD_SIZE) { | |
#Check horizontals and verticals | |
if(getPiece($row + $i, $column) == $QUEEN) { return 0; } | |
if(getPiece($row, $column + $i) == $QUEEN) { return 0; } | |
if(getPiece($row - $i, $column) == $QUEEN) { return 0; } | |
if(getPiece($row, $column - $i) == $QUEEN) { return 0; }; | |
#Check diagonals | |
if(getPiece($row + $i, $column + $i) == $QUEEN) { return 0; } | |
if(getPiece($row - $i, $column + $i) == $QUEEN) { return 0; } | |
if(getPiece($row + $i, $column - $i) == $QUEEN) { return 0; } | |
if(getPiece($row - $i, $column - $i) == $QUEEN) { return 0; } | |
#And again | |
$i++; | |
} | |
return 1; | |
} | |
sub getPiece { | |
my $row = $_[0]; | |
my $column = $_[1]; | |
if($row < $[ or $column < $[ or $row >= $[ + $BOARD_SIZE or $column >= $[ + $BOARD_SIZE) { return $INVALID; } | |
return $contents[$row][$column]; | |
} | |
sub printContents { | |
print "\n\n" . "X" . " = Queen\n"; | |
for(my $i = 0; $i < $BOARD_SIZE; $i++) { | |
for(my $j = 0; $j < $BOARD_SIZE; $j++) { | |
if($contents[$i][$j] == $EMPTY and (($i + $j) % 2) == 0) { print "0"; } | |
if($contents[$i][$j] == $EMPTY and (($i + $j) % 2) == 1) { print "0"; } | |
if($contents[$i][$j] == $QUEEN) { print "X"; } | |
} | |
print "\n" | |
} | |
print "\n" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment