Guest User

Untitled

a guest
Apr 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.42 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4.  
  5. =head1
  6. Known problems:
  7.  
  8. -I need to fucking bless this shit everywhere in order to make it work properly.
  9. Crap. That's why it's not working ..at all. *sigh*
  10.  
  11. -No logic for the auto-discovery of empty cells. It's a new game, it's called
  12. roflsweeper for a reason y'know.
  13.  
  14. -Who fucking cares?
  15.  
  16. -Why can't the internet be up? Why why why why why why why...
  17.  
  18. =cut
  19.  
  20.  
  21. # ConstructNewEmptyField
  22. # ..
  23. sub ConstructNewEmptyField {
  24.     my ($x,$y) = @_;
  25.     my $FIELD;
  26.     for(my $a = 0; $a <= $x; $a++){
  27.         for(my $b = 0; $b <= $y; $b++){
  28.             $FIELD->[$a]->[$b]->['MinesNear'] = 0;
  29.             $FIELD->[$a]->[$b]->['IsMine'] = 0;
  30.             $FIELD->[$a]->[$b]->['Flagged'] = 0;
  31.             $FIELD->[$a]->[$b]->['Discovered'] = 0;
  32.         }
  33.     }
  34.     $FIELD->['MaxWidth'] = $x;
  35.     $FIELD->['MaxHeight'] = $y;
  36.  
  37. }
  38.  
  39. # FlagCell
  40. # Allow the player to flag a cell ($x,$y) as a mine, whether
  41. # the cell is a mine or not.
  42. sub FlagCell {
  43.     my ($FIELD, $x, $y) = @_;
  44.     $FIELD->[$x]->[$y]->['Flagged'] = 1;
  45.     return $FIELD;
  46. }
  47.  
  48. # FindMinesClose
  49. # Find and assign to 'MinesNear' the number of mines directly beside
  50. # cell($x,$y). Return the result.
  51. sub FindMinesClose {
  52.     my ($FIELD,$x,$y) = @_;
  53.     my $mines = 0;
  54.     for(my $a = $x-1; $a <= $x+1; $a++){
  55.         for(my $b = $y-1; $b <= $y+1; $b++){
  56.             if($FIELD->FindIfMine($a,$b)){
  57.                 if($a != $x && $b != $y){ # Don't add current cell
  58.                     $mines++;
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     $FIELD->[$x]->[$y]->['MinesNear'] = $mines;
  64.     return $FIELD;
  65. }
  66.  
  67. # FindIfMine
  68. # Returns 1 if the given cell ($x, $y) has a mine, 0 otherwise.
  69. sub FindIfMine {
  70.     my ($FIELD, $x, $y) = @_;
  71.     return $FIELD->[$x]->[$y]->['IsMine'] == 1 ? 1 : 0;
  72. }
  73.  
  74. # FindIfDiscovered
  75. # Returns 1 if the given cell ($x, $y) has a mine, 0 otherwise.
  76. sub FindIfDiscovered {
  77.     my ($FIELD, $x, $y) = @_;
  78.     return $FIELD->[$x]->[$y]->['Discovered'] ? 1 : 0;
  79. }
  80.  
  81. # FindIfFlagged
  82. # Returns 1 if the given cell ($x,$y) has been flagged, 0 otherwise
  83. sub FindIfFlagged {
  84.     my ($FIELD, $x, $y) = @_;
  85.     return $FIELD->[$x]->[$y]->['Flagged'] ? 1 : 0;
  86. }
  87.  
  88. # GetMinesNear
  89. # Get and return the number of mines near given cell ($x,$y).
  90. sub GetMinesNear {
  91.     my ($FIELD,$x,$y) = @_;
  92.     return $FIELD->[$x]->[$y]->['MinesNear'];
  93. }
  94. # GetFieldWidth
  95. # Get and return the width of the field.
  96. sub GetFieldWidth {
  97.     my ($FIELD) = @_;
  98.     return $FIELD->['MaxWidth'];
  99. }
  100.  
  101. # GetFieldHeight
  102. # Get and return the height of the field.
  103. sub GetFieldHeight {
  104.     my ($FIELD) = @_;
  105.     return $FIELD->['MaxHeight'];
  106. }
  107.  
  108. # PrintField
  109. # Print out the field to console.
  110. sub PrintField {
  111.     my ($FIELD) = @_;
  112.     print "\n\n\n\n\nROFLSWEEPER: TACTICAL MINE REMOVAL GAME\n",
  113.     "WITH NO CLEARING LOGIC IMPLEMENTED. FOR REAL MEN ONLY v.v";
  114.     print "   ";
  115.     for(1..$FIELD->GetFieldWidth()){
  116.         print " $_ ";
  117.     }
  118.     for(my $a = 1; $a <= $FIELD->GetFieldWidth(); $a++){
  119.         print "\n";
  120.         for(my $b = 1; $b <= $FIELD->GetFieldHeight(); $b++){
  121.             if ($FIELD->FindIfFlagged($a,$b) ||
  122.                 $FIELD->FindIfDiscovered($a,$b) ||
  123.                 $FIELD->GetMinesNear($a,$b)){
  124.                 if($FIELD->FindIfFlagged($a,$b)){
  125.                     print " F ";
  126.                 }
  127.                 elsif($FIELD->GetMinesNear($a,$b)){
  128.                     print " ",$FIELD->GetMinesNear($a,$b)," ";
  129.                 }
  130.                 elsif($FIELD->FindIfDiscovered($a,$b)){
  131.                     print " x ";
  132.                 }
  133.                
  134.             }
  135.             else {
  136.                 print " . ";
  137.             }
  138.         }
  139.         print "  $a";
  140.     }
  141. }
  142.  
  143. # InsertMines
  144. # Randomly places a player-set number of mines in the field.
  145. sub InsertMines {
  146.     my $FIELD = shift;
  147.    
  148.     print "How many mines to put in? ";
  149.     my $number = <>;
  150.     chomp $number;
  151.    
  152.     for(0..$number){
  153.         my $x = int(rand($FIELD->GetFieldWidth()));
  154.         my $y = int(rand($FIELD->GetFieldHeight()));
  155.         redo if $FIELD->FindIfMine($x,$y);
  156.         $FIELD->[$x]->[$y]->['IsMine'] = 1;
  157.     }
  158.     return $FIELD;
  159. }
  160.  
  161. # DetermineLoss
  162. # If player chooses a cell with a mine, tell them they fail at roflsweeper.
  163. sub DetermineLoss {
  164.     my ($FIELD, $x, $y) = @_;
  165.    
  166.     if($FIELD->FindIfMine($x,$y)){
  167.         print "You lose! v.v ";
  168.         return 1;
  169.     }
  170.     return 0;
  171. }
  172.  
  173. # PlayGame
  174. # Play a game of roflsweeper. Good fucking luck rofl
  175. sub PlayGame {
  176.     my $field = ConstructNewEmptyField;
  177.     $field = InsertMines($field);
  178.     PrintField($field);
  179.    
  180.     GAME:while(1){
  181.         print "SELECT THE HORIZONTAL COORDINATE: ";
  182.         my $x = <>;
  183.         chomp $x;
  184.        
  185.         print "SELECT THE VERTICAL COORDINATE: ";
  186.         my $y = <>;
  187.         chomp $y;
  188.        
  189.         print "CHOOSE AN ACTION:\n",
  190.         "\t\t (F) FLAG CELL\n",
  191.         "\t\t (V) OPEN CELL\n",
  192.         "\t\t (Z) REDO SELECTION";
  193.        
  194.         my $act = <>;
  195.         chomp $act;
  196.        
  197.         if($act =~ m/Z/){
  198.             redo GAME;
  199.         }
  200.         elsif($act =~ m/F/){
  201.             $field = FlagCell($field, $x , $y);
  202.         }
  203.         elsif($act =~ m/V/){
  204.             if(DetermineLoss($field, $x, $y)){
  205.                 last GAME;
  206.             }
  207.             $field->[$x]->[$y]->['Discovered'] = 1;
  208.         }      
  209.         PrintField($field);
  210.     }
  211. }
  212. PlayGame();
Add Comment
Please, Sign In to add comment