Task: Snake and ladder





Java: import java.util.Map;
import java.util.Random;

public class Game {
    private static final Map<Integer, Integer> snl = Map.ofEntries(
        Map.entry(4, 14),
        Map.entry(9, 31),
        Map.entry(17, 7),
        Map.entry(20, 38),
        Map.entry(28, 84),
        Map.entry(40, 59),
        Map.entry(51, 67),
        Map.entry(54, 34),
        Map.entry(62, 19),
        Map.entry(63, 81),
        Map.entry(64, 60),
        Map.entry(71, 91),
        Map.entry(87, 24),
        Map.entry(93, 73),
        Map.entry(95, 75),
        Map.entry(99, 78)
    );
    private static final boolean sixesThrowAgain = true;
    private static Random rand = new Random();

    private static int turn(int player, int square) {
        int square2 = square;
        while (true) {
            int roll = rand.nextInt(6) + 1;
            System.out.printf("Player %d, on square %d, rolls a %d", player, square2, roll);
            if (square2 + roll > 100) {
                System.out.println(" but cannot move.");
            } else {
                square2 += roll;
                System.out.printf(" and moves to square %d\n", square2);
                if (square2 == 100) return 100;
                Integer next = snl.getOrDefault(square2, square2);
                if (square2 < next) {
                    System.out.printf("Yay! Landed on a ladder. Climb up to %d.\n", next);
                    if (next == 100) return 100;
                    square2 = next;
                } else if (square2 > next) {
                    System.out.printf("Oops! Landed on a snake. Slither down to %d.\n", next);
                    square2 = next;
                }
            }
            if (roll < 6 || !sixesThrowAgain) return square2;
            System.out.println("Rolled a 6 so roll again.");
        }
    }

    public static void main(String[] args) {
        
        int[] players = {1, 1, 1};
        while (true) {
            for (int i = 0; i < players.length; ++i) {
                int ns = turn(i + 1, players[i]);
                if (ns == 100) {
                    System.out.printf("Player %d wins!\n", i + 1);
                    return;
                }
                players[i] = ns;
                System.out.println();
            }
        }
    }
}



Python: import random
import sys

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
}
sixesRollAgain = True

def turn(player, square):
    while True:
        roll = random.randint(1,6)
        sys.stdout.write("Player {0} on square {1}, rolls a {2}".format(player, square, roll))
        if square + roll > 100:
            print " but cannot move."
        else:
            square += roll
            print " and moves to square {0}".format(square)
            if square == 100:
                return 100
            next = snl.get(square, square)
            if square < next:
                print "Yay! landed on a ladder. Climb up to {0}.".format(next)
                if square == 100:
                    return 100
                square = next
            elif square > next:
                print "Oops! Landed on a snake. Slither down to {0}.".format(next)
                square = next
        if roll < 6 or not sixesRollAgain:
            return square
        print "Rolled a 6 so roll again."

def main():
    players = [1, 1, 1]
    while True:
        for i in range(0, 3):
            ns = turn(i+1, players[i])
            if ns == 100:
                print "Player {0} wins!".format(i+1)
                return
            players[i] = ns;
            print

main()