Snake and Ladder Game in C Last Updated : 25 Sep, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Snake and Ladder game is a traditional game that involves two or more players and the winner is the guy who reaches the final square on the game board at the earliest. Components of the GameA square board with a series of numbered squares arranged in m x m grid.A dice to be rolled usually uses a six-faced die.Each player uses different color tokens in order to represent them.Rules of the GamePlayers take turns rolling the die and move their token forward by the number that appears on the top of the die. For example, if a player rolls a 3, they move three squares forward.If a player lands on a square with the base of a ladder, then they must climb the ladder to the square which is at the top of the ladder.If a player lands on the square in which there is a mouth of a snake then the player must slide down to the square which is at the snake's tail.Players take the turn clockwise and the game terminates until one player reaches the final square. If a player rolls a number that counts past the final square, then the player must wait until their next turn to try again.The first player to reach the final square is declared the winner.ImplementationDefining a function to roll a six-faced die we will use rand() here in order to generate the random integer value in each roll of die.Defining a function to decide the move of the player based on the number that appeared on the top of the die. newPosition is the sum of the current position and the number on top of the die.newSquare, the square where the player lands is basically the sum of newPosition and the board[new position] .If newSquare is greater than the final value of the square board then try again else return newSquare.Initialize the players in the main function further check for the moves and return the winner.C Program for Snake and Ladder Game C // C Program to implement Snake and Ladder Game #include <stdio.h> #include <stdlib.h> #include <time.h> // Function to roll a six-sided die int rollDie() { return rand() % 6 + 1; } // global variables to store postions of player1 and player2 int player1 = 0, player2 = 0; // Function to print the board void printBoard() { // logic to print a snake-ladder Game board // programmer can implement their own logic for the board, // this is one way to print a snake ladder board. int board[101]; for (int i = 1; i <= 100; i++) { board[i] = i; } int alt = 0; // to switch between the alternate nature of the board int iterLR = 101; // iterator to print from left to right int iterRL = 80; // iterator to print from right to left int val = 100; while (val--) { if (alt == 0) { iterLR--; if (iterLR == player1) { printf("#P1 "); } else if (iterLR == player2) { printf("#P2 "); } else printf("%d ", board[iterLR]); if (iterLR % 10 == 1) { printf("\n\n"); alt = 1; iterLR -= 10; } } else { iterRL++; if (iterRL == player1) { printf("#P1 "); } else if (iterRL == player2) { printf("#P2 "); } else printf("%d ", board[iterRL]); if (iterRL % 10 == 0) { printf("\n\n"); alt = 0; iterRL -= 30; } } if (iterRL == 10) break; } printf("\n"); } // Function to move the player int movePlayer(int currentPlayer, int roll) { int newPosition = currentPlayer + roll; // Define the positions of snakes and ladders on the // board int snakesAndLadders[101]; for (int i = 0; i <= 100; i++) { snakesAndLadders[i] = 0; } // here positive weights represent a ladder // and negative weights represent a snake. snakesAndLadders[6] = 40; snakesAndLadders[23] = -10; snakesAndLadders[45] = -7; snakesAndLadders[61] = -18; snakesAndLadders[65] = -8; snakesAndLadders[77] = 5; snakesAndLadders[98] = -10; int newSquare = newPosition + snakesAndLadders[newPosition]; if (newSquare > 100) { return currentPlayer; // Player cannot move beyond // square 100 } return newSquare; } int main() { srand(time(0)); // Initialize random seed int currentPlayer = 1; int won = 0; printf("Snake and Ladder Game\n"); while (!won) { printf( "\nPlayer %d, press Enter to roll the die...", currentPlayer); getchar(); // Wait for the player to press Enter int roll = rollDie(); printf("You rolled a %d.\n", roll); if (currentPlayer == 1) { player1 = movePlayer(player1, roll); printf("Player 1 is now at square %d.\n\n", player1); printBoard(); if (player1 == 100) { printf("Player 1 wins!\n"); won = 1; } } else { player2 = movePlayer(player2, roll); printf("Player 2 is now at square %d.\n\n", player2); printBoard(); if (player2 == 100) { printf("Player 2 wins!\n"); won = 1; } } // Switch to the other player currentPlayer = (currentPlayer == 1) ? 2 : 1; } return 0; } Output 1: Output 2: Output 3: Output 4 Ultimately the game continues and the instant any of the players reaches the last square that player is declared the winner. Comment More infoAdvertise with us Next Article Bank account system in C using File handling A anirudhkumarchaudhary01 Follow Improve Article Tags : C Language Geeks Premier League C Projects Geeks Premier League 2023 Similar Reads Top 25 C Projects with Source Codes for 2025 Ready to improve your C programming skills? youâre in the right place! This article is packed with C project ideas for all skill levels whether youâre a beginner, intermediate learner, or looking for advanced challenges. Working on these projects will not only boost your programming skills but also 12 min read Rock Paper Scissor in C Rock Paper Scissor (which is also called Stone Paper Scissor) is a hand game and played between two people, in which each player simultaneously forms one of three shapes. The winner of the game is decided as per the below rules: Rock vs Paper -> Paper wins.Rock vs Scissor -> Rock wins.Paper vs 4 min read Hangman Game in C Hangman game is a popular and simple game in which the player has to guess the word based on the given hint. In this article, we will write a program for the hangman game using C programming language. What is the Hangman Game? Hangman is a word puzzle game that involves: A secret word: A word is sel 6 min read C Program to Make a Simple Calculator A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C.ExampleInput: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is addition, 3 min read Snake and Ladder Game in C Snake and Ladder game is a traditional game that involves two or more players and the winner is the guy who reaches the final square on the game board at the earliest. Components of the GameA square board with a series of numbered squares arranged in m x m grid.A dice to be rolled usually uses a six 5 min read Bank account system in C using File handling This article focuses on how to create a bank account system using C language and File handling in C. Approach:Let's discuss the approach in detail, covering all the functions and their explanation in detail- Create a Menu in the main function and create different functions for the Menu, which will b 12 min read Student Information Management System Prerequisites: Switch Case in C/C++ Problem Statement: Write a program to build a simple Software for Student Information Management System which can perform the following operations: Store the First name of the student.Store the Last name of the student.Store the unique Roll number for every studen 14 min read E -Library Management System In this article, we will discuss the approach to creating an E-Library Management System where the user has the following options: Add book information.Display book information.To list all books of a given author.To list the count of books in the library. E -Library Management System Functionalities 10 min read Program for Employee Management System A Employeeâs Management System (EMS) is a software built to handle the primary housekeeping functions of a company. EMS help companies keep track of all the employees and their records. It is used to manage the company using computerized system. Aim of Employeeâs Management System: Built The Employe 14 min read Hospital Management System in C A Hospital Management System is software that facilitates its users some basic operations that take place in a typical hospital. This Hospital Management System in C language is a project for Hospitals having the following functionalities: Printing Hospital DataPrint Patients DataSort by Bed PriceSo 10 min read Like