Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use warnings;
- =head1
- Known problems:
- -I need to fucking bless this shit everywhere in order to make it work properly.
- Crap. That's why it's not working ..at all. *sigh*
- -No logic for the auto-discovery of empty cells. It's a new game, it's called
- roflsweeper for a reason y'know.
- -Who fucking cares?
- -Why can't the internet be up? Why why why why why why why...
- =cut
- # ConstructNewEmptyField
- # ..
- sub ConstructNewEmptyField {
- my ($x,$y) = @_;
- my $FIELD;
- for(my $a = 0; $a <= $x; $a++){
- for(my $b = 0; $b <= $y; $b++){
- $FIELD->[$a]->[$b]->['MinesNear'] = 0;
- $FIELD->[$a]->[$b]->['IsMine'] = 0;
- $FIELD->[$a]->[$b]->['Flagged'] = 0;
- $FIELD->[$a]->[$b]->['Discovered'] = 0;
- }
- }
- $FIELD->['MaxWidth'] = $x;
- $FIELD->['MaxHeight'] = $y;
- }
- # FlagCell
- # Allow the player to flag a cell ($x,$y) as a mine, whether
- # the cell is a mine or not.
- sub FlagCell {
- my ($FIELD, $x, $y) = @_;
- $FIELD->[$x]->[$y]->['Flagged'] = 1;
- return $FIELD;
- }
- # FindMinesClose
- # Find and assign to 'MinesNear' the number of mines directly beside
- # cell($x,$y). Return the result.
- sub FindMinesClose {
- my ($FIELD,$x,$y) = @_;
- my $mines = 0;
- for(my $a = $x-1; $a <= $x+1; $a++){
- for(my $b = $y-1; $b <= $y+1; $b++){
- if($FIELD->FindIfMine($a,$b)){
- if($a != $x && $b != $y){ # Don't add current cell
- $mines++;
- }
- }
- }
- }
- $FIELD->[$x]->[$y]->['MinesNear'] = $mines;
- return $FIELD;
- }
- # FindIfMine
- # Returns 1 if the given cell ($x, $y) has a mine, 0 otherwise.
- sub FindIfMine {
- my ($FIELD, $x, $y) = @_;
- return $FIELD->[$x]->[$y]->['IsMine'] == 1 ? 1 : 0;
- }
- # FindIfDiscovered
- # Returns 1 if the given cell ($x, $y) has a mine, 0 otherwise.
- sub FindIfDiscovered {
- my ($FIELD, $x, $y) = @_;
- return $FIELD->[$x]->[$y]->['Discovered'] ? 1 : 0;
- }
- # FindIfFlagged
- # Returns 1 if the given cell ($x,$y) has been flagged, 0 otherwise
- sub FindIfFlagged {
- my ($FIELD, $x, $y) = @_;
- return $FIELD->[$x]->[$y]->['Flagged'] ? 1 : 0;
- }
- # GetMinesNear
- # Get and return the number of mines near given cell ($x,$y).
- sub GetMinesNear {
- my ($FIELD,$x,$y) = @_;
- return $FIELD->[$x]->[$y]->['MinesNear'];
- }
- # GetFieldWidth
- # Get and return the width of the field.
- sub GetFieldWidth {
- my ($FIELD) = @_;
- return $FIELD->['MaxWidth'];
- }
- # GetFieldHeight
- # Get and return the height of the field.
- sub GetFieldHeight {
- my ($FIELD) = @_;
- return $FIELD->['MaxHeight'];
- }
- # PrintField
- # Print out the field to console.
- sub PrintField {
- my ($FIELD) = @_;
- print "\n\n\n\n\nROFLSWEEPER: TACTICAL MINE REMOVAL GAME\n",
- "WITH NO CLEARING LOGIC IMPLEMENTED. FOR REAL MEN ONLY v.v";
- print " ";
- for(1..$FIELD->GetFieldWidth()){
- print " $_ ";
- }
- for(my $a = 1; $a <= $FIELD->GetFieldWidth(); $a++){
- print "\n";
- for(my $b = 1; $b <= $FIELD->GetFieldHeight(); $b++){
- if ($FIELD->FindIfFlagged($a,$b) ||
- $FIELD->FindIfDiscovered($a,$b) ||
- $FIELD->GetMinesNear($a,$b)){
- if($FIELD->FindIfFlagged($a,$b)){
- print " F ";
- }
- elsif($FIELD->GetMinesNear($a,$b)){
- print " ",$FIELD->GetMinesNear($a,$b)," ";
- }
- elsif($FIELD->FindIfDiscovered($a,$b)){
- print " x ";
- }
- }
- else {
- print " . ";
- }
- }
- print " $a";
- }
- }
- # InsertMines
- # Randomly places a player-set number of mines in the field.
- sub InsertMines {
- my $FIELD = shift;
- print "How many mines to put in? ";
- my $number = <>;
- chomp $number;
- for(0..$number){
- my $x = int(rand($FIELD->GetFieldWidth()));
- my $y = int(rand($FIELD->GetFieldHeight()));
- redo if $FIELD->FindIfMine($x,$y);
- $FIELD->[$x]->[$y]->['IsMine'] = 1;
- }
- return $FIELD;
- }
- # DetermineLoss
- # If player chooses a cell with a mine, tell them they fail at roflsweeper.
- sub DetermineLoss {
- my ($FIELD, $x, $y) = @_;
- if($FIELD->FindIfMine($x,$y)){
- print "You lose! v.v ";
- return 1;
- }
- return 0;
- }
- # PlayGame
- # Play a game of roflsweeper. Good fucking luck rofl
- sub PlayGame {
- my $field = ConstructNewEmptyField;
- $field = InsertMines($field);
- PrintField($field);
- GAME:while(1){
- print "SELECT THE HORIZONTAL COORDINATE: ";
- my $x = <>;
- chomp $x;
- print "SELECT THE VERTICAL COORDINATE: ";
- my $y = <>;
- chomp $y;
- print "CHOOSE AN ACTION:\n",
- "\t\t (F) FLAG CELL\n",
- "\t\t (V) OPEN CELL\n",
- "\t\t (Z) REDO SELECTION";
- my $act = <>;
- chomp $act;
- if($act =~ m/Z/){
- redo GAME;
- }
- elsif($act =~ m/F/){
- $field = FlagCell($field, $x , $y);
- }
- elsif($act =~ m/V/){
- if(DetermineLoss($field, $x, $y)){
- last GAME;
- }
- $field->[$x]->[$y]->['Discovered'] = 1;
- }
- PrintField($field);
- }
- }
- PlayGame();
Add Comment
Please, Sign In to add comment