Level 9 - Two-dimension arrays and lists 0 of 9 activities solved
  1. Rainfall collector Not yet solved
  2. Journey log Not yet solved
  3. Snakes and ladders Not yet solved
  4. Quote of the day Not yet solved
  5. Word game Not yet solved
  6. Grade book Not yet solved
  7. Checkout Not yet solved
  8. Missiles Not yet solved
  9. Noughts and crosses Not yet solved
Next series Assessment point - Levels 1-9
Level 9 - Two-dimension arrays and lists

Snakes and ladders

Sign in to hand in your solution and get feedback.

// Snakes and ladders program using System; using System.Collections.Generic; class Submission { // ------------------------- // Globals // ------------------------- static Random random_generator = new Random(); // ------------------------- // Subprograms // ------------------------- static void Play(int[] board, int[] players) { int current_player = 0; bool game_won = false; while (!game_won) { int player_square = players[current_player]; Console.WriteLine(); Console.WriteLine("------------------------------------"); Console.WriteLine($"Player {current_player + 1}, it's your turn."); Console.WriteLine($"You are on square {player_square}"); Console.WriteLine("Press Enter to roll the dice."); Console.ReadLine(); int dice = random_generator.Next(1, 7); Console.WriteLine($"You rolled a {dice}"); player_square += dice; if (player_square > board.Length - 1) { player_square = board.Length - 1; } Console.WriteLine($"You moved to square {player_square}"); int board_square = board[player_square]; if (board_square < player_square) { Console.WriteLine("Oh no, you landed on a snake."); player_square = board_square; Console.WriteLine($"You are now on square {player_square}"); } else if (board_square > player_square) { Console.WriteLine("Yay, you landed on a ladder."); player_square = board_square; Console.WriteLine($"You are now on square {player_square}"); } if (player_square >= board.Length - 1) { game_won = true; Console.WriteLine($"Player {current_player + 1} wins the game!"); } else { players[current_player] = player_square; Console.WriteLine("Press Enter for the next player to take their turn."); Console.ReadLine(); current_player = (current_player + 1) % players.Length; } } } static int[] initialise_board(int squares) { int[] board = new int[squares + 1]; for (int square = 0; square <= squares; square++) { board[square] = square; } board[4] = 7; board[6] = 15; board[18] = 23; board[19] = 2; board[24] = 17; return board; } static int[] Initialise_players() { int[] players = new int[2] { 1, 1 }; return players; } // ------------------------- // Main program // ------------------------- public static void Main(string[] args) { int[] board = initialise_board(25); int[] players = Initialise_players(); Play(board, players); } }