0% found this document useful (0 votes)
86 views4 pages

Snake Game

This C++ program implements a two-player snakes and ladders game. It initializes an array to represent the game board with spaces for ladders and snakes. It prompts the user to input the number and positions of ladders and snakes on the board. The main game loop alternates turns for each player, rolling dice and moving their marker while checking for ladders or snakes. The game ends when either player reaches the final space, declaring that player the winner.

Uploaded by

Hamza Latif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views4 pages

Snake Game

This C++ program implements a two-player snakes and ladders game. It initializes an array to represent the game board with spaces for ladders and snakes. It prompts the user to input the number and positions of ladders and snakes on the board. The main game loop alternates turns for each player, rolling dice and moving their marker while checking for ladders or snakes. The game ends when either player reaches the final space, declaring that player the winner.

Uploaded by

Hamza Latif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <ctime>
#include<cstdlib>

using namespace std;


void setupBoard(char snakes_ladders[]);
int rollDice();
void displayLaddersAndSnakes(char snakes_ladders[]);

int main()
{
bool gameOver = false;
bool playerTurn = true; // true = 1st players turn, false = second players
turn.
char player1[] = "F";
int playerOneIndex = 0;
char player2[] = "S";
int playerTwoIndex = 0;
int TotalLadders;
int TotalSnakes;
int initialBox;
int finalBox;
char snakes_ladders[101];
char snake_letters[3] = {'x','y','z'};
char ladder_letters[4] = {'a','b','c','d'};

setupBoard(snakes_ladders);

cout<<"Enter no of total ladders you want to place on board:"<<endl;


cin>> TotalLadders;

if(TotalLadders > 4){


cout << "Maximum 4 ladders allowed, setting it to 4";
TotalLadders = 4;
}

// We represent ladder by 'b'


for (int k=1;k<=TotalLadders;k++) // Note a maximum of 5 ladders are allowed
say, lets show them with a,b,c,d,e & f characters.
{
cout << "Enter Initial Box# to place ladder "<<k<<" on the board"<<"\n";
cin>>initialBox;
snakes_ladders[initialBox] = ladder_letters[k-1];
cout << "Enter Final Box# to ladder "<<k<<" on the board"<<"\n";
// Embed your code here
cin >> finalBox;
snakes_ladders[finalBox] = ladder_letters[k-1];
}

cout<<"Enter no of total snakes you want to place on board:"<<endl;


cin>> TotalSnakes;

if(TotalSnakes > 3){


cout << "Maximum 3 snakes allowed, setting it to 3";
TotalSnakes = 3;
}
// We represent snake by 'x'
for (int k=1;k<=TotalSnakes;k++) // Note a maximum of 3 snakes are allowed,
lets show them by x,y & z snake.
{
cout << "Enter Initial Box# to place Sanke "<<k<<" on the board"<<"\n";
// Embed your code here
cin>>initialBox;
snakes_ladders[initialBox] = snake_letters[k-1];
cout << "Enter Final Box# to place Snake "<<k<<" on the board"<<"\n";
// Embed your code here
cin >> finalBox;
snakes_ladders[finalBox] = snake_letters[k-1];
}

displayLaddersAndSnakes(snakes_ladders);

int turnNumber;
int rolledNumber;
int currentIndex;
// next we need a game loop.
while(!gameOver){

// if first players turn


if(playerTurn){

cout << "Player 1's Turn, input 0 and press enter!";


cin >> turnNumber;
rolledNumber = rollDice(); // 6
currentIndex = playerOneIndex;
// playerOneIndex = 6;
playerOneIndex = playerOneIndex + rolledNumber;

// if both players same index, display both (not so important bug).....

// if the player lands on a ladder, move up, if player lands on snake,


move down

snakes_ladders[playerOneIndex] = player1[0];

// setting previous index to empty


snakes_ladders[currentIndex] = '\0';

playerTurn = false;
displayLaddersAndSnakes(snakes_ladders);
// otherwise second players turn
}else{
cout << "Player 2's Turn, input 0 and press enter!";
cin >> turnNumber;
rolledNumber = rollDice(); // 6
currentIndex = playerTwoIndex;
// playerOneIndex = 6;
playerTwoIndex = playerTwoIndex + rolledNumber;
// if both players same index, display both

snakes_ladders[playerTwoIndex] = player2[0];

// setting previous index to empty


snakes_ladders[currentIndex] = '\0';

playerTurn = true;
displayLaddersAndSnakes(snakes_ladders);

// winning condition to break out of loop!


if(playerOneIndex == 99){
cout << "Player 1 won the game!";
break;

}else if(playerTwoIndex == 99){


cout << "Player 2 won the game!";
break;
}

return 0;
}

void displayLaddersAndSnakes(char snakes_ladders[]){

cout<< "STATUS OF SNAKES & LADDERS BOARD IS:"<<endl;

for (int i=0 ; i<=99; i++) // displaying snakes & ladders board array
{
cout<<" "<< snakes_ladders[i] <<" | " ;
if((i+1)%10==0)
{
cout<< endl;
for (int j=0;j<=9*4+11;j++)
cout<<"_";
cout<<endl;
}
}

void setupBoard(char snakes_ladders[]){

for (int i=0; i<=100; i++) // initializing snakes & ladders board array
snakes_ladders[i]=' ';
snakes_ladders[100]='\0'; // character array terminated with null character

cout<< "STATUS OF SNAKES & LADDERS BOARD IS:"<<endl;

for (int i=0 ; i<=99; i++) // displaying snakes & ladders board array
{
cout<<" "<< snakes_ladders[i] <<" | " ;
if((i+1)%10==0)
{
cout<< endl;
for (int j=0;j<=9*4+11;j++)
cout<<"_";
cout<<endl;
}
}
}

int rollDice(){
int random;
rand(time(0) );
return (rand() % 6) + 1;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy