Snake and ladder: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 104: Line 104:
Player 3 on square 97 rolls a 3 and moves to square 100
Player 3 on square 97 rolls a 3 and moves to square 100
Player 3 wins!</pre>
Player 3 wins!</pre>

=={{header|Ruby}}==
<lang ruby>
NONE = 0; LADDER = 1; SNAKE = 2; STAY = 1; MOVE = 2; WIN = 3
class Cell
@type; @to; attr_reader :type, :to
def initialize; @type = NONE; @to = 0; end
def set( t, o ); @type = t; @to = o; end
end
class Player
@pos; @name; attr_accessor :pos; attr_reader :name
def initialize( n ); @pos = 0; @name = n; end
def play( dice )
s = dice.roll; return s, STAY if @pos + s > 99
@pos += s; return s, WIN if @pos == 99
return s, MOVE
end
end
class Die
@sides; def initialize( s = 6 ); @sides = s; end
def roll; return 1 + rand( @sides ); end
end
def initBoard
@board = Array.new( 100 ); for i in 0 .. 99; @board[i] = Cell.new(); end
@board[3].set( LADDER, 13 ); @board[8].set( LADDER, 30 ); @board[19].set( LADDER, 37 );
@board[27].set( LADDER, 83 );@board[39].set( LADDER, 58 ); @board[50].set( LADDER, 66 );
@board[62].set( LADDER, 80 ); @board[70].set( LADDER, 90 ); @board[16].set( SNAKE, 6 );
@board[61].set( SNAKE, 18 ); @board[86].set( SNAKE, 23 ); @board[53].set( SNAKE, 33 );
@board[63].set( SNAKE, 59 ); @board[92].set( SNAKE, 72 ); @board[94].set( SNAKE, 74 );
@board[98].set( SNAKE, 77 );
end
def initPlayers
@players = Array.new( 4 );
for i in 0 .. @playersCount - 1; @players[i] = Player.new( "player " << i + 49 ); end
end
def play
initBoard; initPlayers; @die = Die.new
while true
for p in 0 .. @playersCount - 1
puts; puts
if( 0 == p )
print "#{@players[p].name}, your turn. Your position is cell #{@players[p].pos + 1}.\n"<<
"Press [RETURN] to roll the die."
gets; np = @players[p].play( @die ); print "You rolled a #{np[0]}\n"
if np[1] == WIN
print "You reached position #{@players[p].pos + 1} and win the game!!!!\n"; return
elsif np[1] == STAY; print "Sorry, you cannot move!\n"
else print "Your new position is cell #{@players[p].pos + 1}.\n";
end
else
np = @players[p].play( @die ); print "#{@players[p].name} rolled a #{np[0]}.\n"
if np[1] == WIN
print "He reached position #{@players[p].pos + 1} and wins the game!!!!\n"; return
elsif np[1] == STAY; print "But he cannot move....\n"
else print "His new position is cell #{@players[p].pos + 1}.\n";
end
end
s = @board[@players[p].pos].type
next if s == NONE
@players[p].pos = @board[@players[p].pos].to
case s
when SNAKE; print "What a pitty, landed on a snake. "
when LADDER; print "Lucky move! Landed on a ladder. "
end
print "New position is cell #{@players[p].pos + 1}.\n"
end
end
end
@playersCount = 4; @board; @players; @die
play
</lang>
{{out}}<pre>
...
player 1, your turn. Your position is cell 97.
Press [RETURN] to roll the die.
You rolled a 5
Sorry, you cannot move!

player 2 rolled a 5.
His new position is cell 49.

player 3 rolled a 4.
But he cannot move....

player 4 rolled a 6.
His new position is cell 91.

player 1, your turn. Your position is cell 97.
Press [RETURN] to roll the die.
You rolled a 3
You reached position 100 and win the game!!!!
</pre>

Revision as of 05:15, 16 June 2017

Snake and ladder is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Let us create a Snakes and Ladders game where the computer plays against a human player.

For a board example you can use this image here: snakes & ledders.

The way you'll represent the board and the players' tokens is totally up to you: graphics or ASCII graphics or even just text.

Happy coding.

Perl 6

Works with: Rakudo version 2017.01

Snakes and ladders is entirely chance based, so human interaction is not really required. This version allows up to one human player against any number of computer players and asks for input... but doesn't really need or even use it. I didn't bother to implement a graphical interface.

<lang perl6> # board layout my %snl = 4, 14, 9, 31, 17, 7, 20, 38, 28, 84, 40, 59, 51, 67, 54, 34,

         62, 19, 63, 81, 64, 60, 71, 91, 87, 24, 93, 73, 95, 75, 99, 78;

my @players = 1, 1, 1; # three players, starting on square 1 my $human = 1; # player 1 is human. set to 0 for all computer players

loop {

   for ^@players -> $player {
       turn(@players[$player], $player + 1);
   }
   say ;

}

sub turn ($square is rw, $player) {

   if $player == $human {
       prompt "You are on square $square. Hit enter to roll the die.";
   }
   my $roll = (1..6).roll;
   my $turn = $square + $roll;
   printf "Player $player on square %2d rolls a $roll", $square;
   if $turn > 100 {
       say " but cannot move. Next players turn.";
       return $square;
   }
   if %snl{$turn} {
       $square = %snl{$turn};
       if $turn > $square {
           say ". Oops! Landed on a snake. Slither down to $square."
       } else {
           say ". Yay! Landed on a ladder. Climb up to $square."
       }
   } else {
       $square = $turn;
       say " and moves to square $square";
   }
   say "Player $player wins!" and exit if $square == 100;
   return $square;

}</lang>

Sample output:
You are on square 1. Hit enter to roll the die.
Player 1 on square  1 rolls a 1 and moves to square 2
Player 2 on square  1 rolls a 4 and moves to square 5
Player 3 on square  1 rolls a 1 and moves to square 2

You are on square 2. Hit enter to roll the die.
Player 1 on square  2 rolls a 1 and moves to square 3
Player 2 on square  5 rolls a 2 and moves to square 7
Player 3 on square  2 rolls a 3 and moves to square 5

You are on square 3. Hit enter to roll the die.
Player 1 on square  3 rolls a 4 and moves to square 7
Player 2 on square  7 rolls a 1 and moves to square 8
Player 3 on square  5 rolls a 2 and moves to square 7

You are on square 7. Hit enter to roll the die.
Player 1 on square  7 rolls a 2. Yay! Landed on a ladder. Climb up to 31.
Player 2 on square  8 rolls a 3 and moves to square 11
Player 3 on square  7 rolls a 2. Yay! Landed on a ladder. Climb up to 31.

  ... about 15 turns omitted ...

You are on square 90. Hit enter to roll the die.
Player 1 on square 90 rolls a 3. Oops! Landed on a snake. Slither down to 73.
Player 2 on square 55 rolls a 3 and moves to square 58
Player 3 on square 90 rolls a 4 and moves to square 94

You are on square 73. Hit enter to roll the die.
Player 1 on square 73 rolls a 6 and moves to square 79
Player 2 on square 58 rolls a 5. Yay! Landed on a ladder. Climb up to 81.
Player 3 on square 94 rolls a 3 and moves to square 97

You are on square 79. Hit enter to roll the die.
Player 1 on square 79 rolls a 3 and moves to square 82
Player 2 on square 81 rolls a 6. Oops! Landed on a snake. Slither down to 24.
Player 3 on square 97 rolls a 5 but cannot move. Next players turn.

You are on square 82. Hit enter to roll the die.
Player 1 on square 82 rolls a 5. Oops! Landed on a snake. Slither down to 24.
Player 2 on square 24 rolls a 6 and moves to square 30
Player 3 on square 97 rolls a 4 but cannot move. Next players turn.

You are on square 24. Hit enter to roll the die.
Player 1 on square 24 rolls a 4. Yay! Landed on a ladder. Climb up to 84.
Player 2 on square 30 rolls a 5 and moves to square 35
Player 3 on square 97 rolls a 4 but cannot move. Next players turn.

You are on square 84. Hit enter to roll the die.
Player 1 on square 84 rolls a 1 and moves to square 85
Player 2 on square 35 rolls a 2 and moves to square 37
Player 3 on square 97 rolls a 3 and moves to square 100
Player 3 wins!

Ruby

<lang ruby> NONE = 0; LADDER = 1; SNAKE = 2; STAY = 1; MOVE = 2; WIN = 3 class Cell

   @type; @to; attr_reader :type, :to
   def initialize; @type = NONE; @to = 0; end 
   def set( t, o ); @type = t; @to = o; end

end class Player

   @pos; @name; attr_accessor :pos; attr_reader :name
   def initialize( n ); @pos = 0; @name = n; end
   def play( dice )
       s = dice.roll; return s, STAY if @pos + s > 99
       @pos += s; return s, WIN if @pos == 99
       return s, MOVE
   end

end class Die

   @sides; def initialize( s = 6 ); @sides = s; end
   def roll; return 1 + rand( @sides ); end

end def initBoard

   @board = Array.new( 100 ); for i in 0 .. 99; @board[i] = Cell.new(); end
   @board[3].set( LADDER, 13 ); @board[8].set( LADDER, 30 ); @board[19].set( LADDER, 37 );
   @board[27].set( LADDER, 83 );@board[39].set( LADDER, 58 ); @board[50].set( LADDER, 66 );
   @board[62].set( LADDER, 80 ); @board[70].set( LADDER, 90 ); @board[16].set( SNAKE, 6 );
   @board[61].set( SNAKE, 18 ); @board[86].set( SNAKE, 23 ); @board[53].set( SNAKE, 33 );
   @board[63].set( SNAKE, 59 ); @board[92].set( SNAKE, 72 ); @board[94].set( SNAKE, 74 ); 
   @board[98].set( SNAKE, 77 );

end def initPlayers

   @players = Array.new( 4 );
   for i in 0 .. @playersCount - 1; @players[i] = Player.new( "player " << i + 49 ); end

end def play

   initBoard; initPlayers; @die = Die.new
   while true
       for p in 0 .. @playersCount - 1
           puts; puts
           if( 0 == p ) 
               print "#{@players[p].name}, your turn. Your position is cell #{@players[p].pos + 1}.\n"<<
               "Press [RETURN] to roll the die."
               gets; np = @players[p].play( @die ); print "You rolled a #{np[0]}\n"
               if np[1] == WIN
                   print "You reached position #{@players[p].pos + 1} and win the game!!!!\n"; return
               elsif np[1] == STAY; print "Sorry, you cannot move!\n"
               else print "Your new position is cell #{@players[p].pos + 1}.\n";
               end
           else
               np = @players[p].play( @die ); print "#{@players[p].name} rolled a #{np[0]}.\n"
               if np[1] == WIN 
                    print "He reached position #{@players[p].pos + 1} and wins the game!!!!\n"; return
               elsif np[1] == STAY; print "But he cannot move....\n"
               else print "His new position is cell #{@players[p].pos + 1}.\n";
               end
           end
           s = @board[@players[p].pos].type
           next if s == NONE
           @players[p].pos = @board[@players[p].pos].to
           case s
               when SNAKE; print "What a pitty, landed on a snake. "
               when LADDER; print "Lucky move! Landed on a ladder. "
           end
           print "New position is cell #{@players[p].pos + 1}.\n"
       end
   end

end @playersCount = 4; @board; @players; @die play </lang>

Output:

... player 1, your turn. Your position is cell 97. Press [RETURN] to roll the die. You rolled a 5 Sorry, you cannot move!

player 2 rolled a 5. His new position is cell 49.

player 3 rolled a 4. But he cannot move....

player 4 rolled a 6. His new position is cell 91.

player 1, your turn. Your position is cell 97. Press [RETURN] to roll the die. You rolled a 3 You reached position 100 and win the game!!!!