Snake and Ladder is a two player game, played on 2-D board, in which every playe
ID: 3854210 • Letter: S
Question
Snake and Ladder is a two player game, played on 2-D board, in which every player roll a dice on its turn and move forward according to number appears on dice.
In this assignment you have to implement a Snake and Ladder Game with some extra functionalities. In this game different cells of game board can contain different objects,
list of objects and their corresponding points are given below.
Gold Coins
10 Points for each
Silver Coins
5 Points for each
Diamond
50 Points for each
Prison cell
-20 Points
Sword
0 Points
Shield
0 Points
A basic layout of Game board is presented in figure below.
Rules of game are listed here.
Winner/Loser: A winner of game is decided on the basis of two things number of points earned and last cell. If a player reached in last cell but has less number of points than other player, then game will draw. A winner must have reached to last cell with more points than other player.
If a player found an object in any cell he can put that in his bag so that he can use it and gain points. Once a player picked an object, then it will be removed from that cell and game board should update accordingly.
Sword: When a player enters in a cell with snake head then, he can kill the snake with sword and move on without losing any points. Sword can be used only once in life of a player.
Shield: When a player enters in a cell with snake head then, he can use his shield to protect him against snake attack, this will also be used once.
Prison cell: If any player enters in prison cell his next three turns are skipped and he lose 20 points. After three turns of other player, he can come out of prison.
Diamond: If a player has diamond in the bag and he enters in a prison cell, then he can sell his diamond to come out of prison immediately but the player will lose points of diamond.
Gold and Silver Coins: If a player is attacked by the snake and he has gold or silver coins, then he can use 3 gold coins or 6 silver coins to move two rows up from cell where he reached by snake attack. Points of player will also be deducted for using gold and silver coins.
Other Requirements:
Dynamic Board: Your game should support dynamic size of board minimum 5×5 (25 cells) to
maximum 10×10 (100 cells).
Objects: Each time when a new game is started, number and placement of objects will be random. You can place minimum 5 and maximum 5+5 objects on the 5×5 (25 cells) board similar calculations will be used for larger board like minimum 10 to 20 objects on 10×10 (100 cells) board. The snakes and ladders will also be placed in random cells. Size of all snakes and ladders should be different and random too.
User Interaction: Your game should be user interactive and should provide proper options to player, which he can use for every move. Like pick an object from cell, kill the snake, use an object etc. The dice of user will be rolled by system using a random number and dice will be six sided.
You do not need to provide any graphical user interface for game, but create neat and clean board on console and you can write name of objects in cells where they will present. Similarly you will update both users Bags, points and cells information accordingly.
Happy Programming
Gold Coins
10 Points for each
Silver Coins
5 Points for each
Diamond
50 Points for each
Prison cell
-20 Points
Sword
0 Points
Shield
0 Points
Explanation / Answer
driver.cpp
#include <iostream>
#include "SnakesAndLadders.h"
#include <Windows.h>
using namespace std;
void print(SnakesAndLadders &game)
{
system("cls");
if (!game.getturn())
cout << "PLAYER 1 TURN";
else
cout << "PLAYER 2 TURN";
game.printboard();
}
int main() {
int size;
cout << "Enter the size of board: ";
cin >> size;
if (size < 5 || size > 50)
{
cout << "Please enter a valid size. ";
return 0;
}
SnakesAndLadders game(size);
bool p = false;
while (!game.won())
{
print(game);
cout << " Roll The Dice Now! ";
system("pause");
for (int i = 0; i < 10; i++)
{
print(game);
cout << " Roll The Dice Now! ";
cout << "DICE: " << 1 + rand() % 6 << endl;
Sleep(50);
}
print(game);
cout << " Roll The Dice Now! ";
game.rolldiece();
print(game);
cout << " " << "Points: " << "Player 1: "
<< game.getpoints(1) << " Player 2: "
<< game.getpoints(2)
<< " Time To Change The Turn." << endl;
system("pause");
game.changeturn();
}
system("cls");
if (game.won() == 1)
{
cout << "PLAYER 1 WON";
}
else if (game.won() == 2)
{
cout << "PLAYER 2 WON";
}
else
{
cout << "ITS A DRAW";
}
cout << endl;
return 0;
}
SnakesAndLadders.cpp
#include "SnakesAndLadders.h"
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
/*---------------------------------------------------------------------------------------------------------------
PUBLIC FUNCTIONS FOR THE BASE CLASS OF SNAKES AND LADDERS
---------------------------------------------------------------------------------------------------------------*/
// CONSTRUCTOR FOR THE BOARD THAT TAKES IN THE SIZE OF BOARD AND GENERATES IT DYNAMICALLY AND THEN CALL THE BOOTSTRAP FUNCTION
SnakesAndLadders::SnakesAndLadders(int boardsize)
{
srand(time(NULL));
board = new cell* [boardsize] {};
for (int i = 0; i < boardsize; i++)
{
board[i] = new cell[boardsize]{};
}
turn = 0;
ended = 0;
size = boardsize;
bootstrap();
}
int SnakesAndLadders::rolldiece()
{
int n = d.rolldice();
cout << "DICE: " << n << endl;
system("pause");
player * p;
if (turn == 0)
p = &p1;
else
p = &p2;
int j = (p->getY() + n) % size;
int i = p->getX();
if (p->getY() > j)
{
i++;
}
if (i*size + j < size*size)
{
p->updatePosition(i, j);
p->updateStatus(board[i][j]);
board[i][j].obj = 0;
}
if (i == size - 1 && j == size - 1)
{
ended = true;
}
return n;
}
void SnakesAndLadders::printboard()
{
for (int i = size - 1; i >= 0; i--)
{
cout << endl;
for (int j = 0; j < size; j++)
{
if (i == p1.getX() && j == p1.getY() && !turn)
{
cout << " P1";
}
else if (i == p2.getX() && j == p2.getY() && turn)
{
cout << " P2";
}
else if (board[i][j].obj == 0)
{
if (i*size + j + 1 < 10)
{
cout << " 0" << i*size + j + 1;
}
else
{
cout << " " << i*size + j + 1;
}
}
else if (board[i][j].obj == 1)
{
cout << " GC";
}
else if (board[i][j].obj == 2)
{
cout << " SC";
}
else if (board[i][j].obj == 3)
{
cout << " DD";
}
else if (board[i][j].obj == 4)
{
cout << " SW";
}
else if (board[i][j].obj == 5)
{
cout << " SH";
}
else if (board[i][j].obj == 6)
{
cout << " PC";
}
else if (board[i][j].obj == 7)
{
cout << " SN";
}
else if (board[i][j].obj == 2)
{
cout << " LD";
}
}
}
}
int SnakesAndLadders::won()
{
if (ended)
{
if (p1.getPoints() > p2.getPoints())
{
return 1;
}
else if (p1.getPoints() < p2.getPoints())
return 2;
else
return 3;
}
return 0;
}
bool SnakesAndLadders::getturn()
{
return turn;
}
void SnakesAndLadders::changeturn()
{
turn = !turn;
}
int SnakesAndLadders::getpoints(int p)
{
if (p == 1)
{
return p1.getPoints();
}
else
{
return p2.getPoints();
}
return 0;
}
SnakesAndLadders::~SnakesAndLadders()
{
}
/*---------------------------------------------------------------------------------------------------------------
PRIVATE FUNCTIONS FOR THE BASE CLASS OF SNAKES AND LADDERS
---------------------------------------------------------------------------------------------------------------*/
// BOOTSTRAP FUNCTION IS RESPONSIBLE FOR PLACING RANDOM OBJECTS AT DIFFERENT CELLS ON BOARD
// IT IS NOT RESPONSIBLE FOR THE AMOUNT OF OBJECT IN CASE OF COINS. COINS ARE RANDOMLY GIVIEN WHEN PLAYER COMES TO THAT CELL
void SnakesAndLadders::bootstrap()
{
int objs = rand() % (size + 1) + size;
int i = 0;
while (i < objs)
{
int row = rand() % (size - 2) + 1;
int col = rand() % (size - 2) + 1;
int item = 0;
if (board[row][col].obj == 0)
{
item = 1 + (rand() % 6); // 1-6 means some item
board[row][col].obj = item;
i++;
}
if (item == 1)
{
board[row][col].quantity = rand() % 3 + 1;
}
else if (item == 2)
{
board[row][col].quantity = rand() % 6 + 1;
}
else if (item == 7)
{
board[row][col].col = rand() % size;
board[row][col].row = rand() % row;
}
else if (item == 8)
{
board[row][col].col = rand() % size;
board[row][col].row = rand() % (size - row) + row;
}
if (item > 2)
{
board[row][col].quantity = 1;
}
}
}
}
SnakesAndLadders.h
#pragma once
#include "cell.h"
#include "player.h"
#include "dice.h"
class SnakesAndLadders
{
public:
SnakesAndLadders(int);
int rolldiece();
~SnakesAndLadders();
void printboard();
int won();
bool getturn();
void changeturn();
int getpoints(int);
private:
cell **board;
dice d;
player p1, p2;
bool turn;
int size; // size represents the size of number of rows or column as both are same.
void bootstrap();
bool ended;
};
bag.h
#pragma once
class Bag
{
int items[5];
public:
Bag()
{
for (int i = 0; i < 5; i++)
{
items[i] = 0;
}
}
void insertItem(int itemcode, int quantity)
{
items[itemcode - 1] += quantity;
}
int getQuantity(int itemcode)
{
return items[itemcode - 1];
}
void redeemItem(int itemcode, int quantity)
{
items[itemcode -1 ] -= quantity;
}
};
cell.h
#pragma once
// ------------------- REFRENCE TO OBJ IN CELL STRUCT ----------------------
/*
0 means there is nothing on that cell
1 means Gold coins number of gold coins will be random but max 3
2 means Silver coins number of silver coins will be random but max 6
3 means a Diamond
4 means a Sword
5 means a Shield
6 means a Prison cell
7 means a Snake
8 means a Ladder
*/
struct cell
{
int obj;
int quantity; // Only for coins
// If its a ladder or snake then the cell to where it goes
int row; int col;
cell()
{
obj = quantity = row = col = 0; // By default everything is 0
}
};
dice.h
#pragma once
#include <stdlib.h>
#include <ctime>
class dice
{
public:
dice()
{
srand(time(NULL));
}
int rolldice()
{
return rand() % 6 + 1;
}
};
player.h
#pragma once
#include <iostream>
#include "bag.h"
#include "cell.h"
using namespace std;
class player
{
Bag bag;
int points;
int posx, posy;
int inprison;
public:
player()
{
posx = posy = points = 0;
inprison = 0;
}
void updateStatus(cell item)
{
// Updating points
if (item.obj == 1)
{
points += item.quantity * 10;
}
else if (item.obj == 2)
{
points += item.quantity * 5;
}
else if (item.obj == 3)
{
points += item.quantity * 50;
}
else if (item.obj == 6)
{
cout << "Woops You are in jail. Do you want to give a bribe? Press Y or N: ";
char x;
cin >> x;
if (x == 'Y')
{
cout << "Currently you have " << bag.getQuantity(1) << " gold coins, " << bag.getQuantity(2) << " silver coins and "<< bag.getQuantity(3) << " diamons left ";
cout << "Press 1 for gold, 2 for silver or 3 for diamond ";
cin >> x;
if (x == '1')
{
if (bag.getQuantity(1) >= 3)
{
bag.redeemItem(1, 3);
points -= 30;
return;
}
else
{
cout << "You do not have enough coins.";
}
}
else if(x == '2')
{
if (bag.getQuantity(2) >= 6)
{
bag.redeemItem(2, 6);
points -= 30;
return;
}
else
{
cout << "You do not have enough silver coins.";
}
}
else
{
if (bag.getQuantity(3))
{
bag.redeemItem(3, 1);
points -= 50;
return;
}
else
{
cout << "You do not have any diamond.";
}
}
}
points += item.quantity * (-20);
inprison = 3;
}
// Storing in bag if item is storable
if (item.obj > 0 && item.obj < 6)
{
bag.insertItem(item.obj, item.quantity);
}
else if (item.obj == 7)
{
cout << "Woops There's a snake. Do you want to use an item? Press Y or N";
char x;
cin >> x;
if (x == 'Y')
{
cout << "Currently you have " << bag.getQuantity(4) << " swords and " << bag.getQuantity(5) << " shields left ";
cout << "Press 1 for sword or 2 for shield ";
cin >> x;
if (x == '1')
{
if (bag.getQuantity(4))
{
bag.redeemItem(4, 1);
return;
}
else
{
cout << "You do not have a sword.";
}
}
else
{
if (bag.getQuantity(5))
{
bag.redeemItem(5, 1);
return;
}
else
{
cout << "You do not have a shield.";
}
}
}
posx = item.col;
posy = item.row;
}
else if (item.obj == 8)
{
posx = item.col;
posy = item.row;
}
}
void updatePosition(int i, int j)
{
if (!inprison)
{
posx = i;
posy = j;
}
else
inprison--;
}
int getX()
{
return posx;
}
int getY()
{
return posy;
}
int getPoints()
{
return points;
}
};
sample output
13 14 15 16 GC 18
07 GC 09 PC 11 12
01 02 03 04 05 06
Roll The Dice Now!
DICE: 2
sh: cls: command not found
PLAYER 1 TURN
31 32 33 34 35 36
25 P1 27 PC 29 30
19 20 21 22 GC 24
13 14 15 16 GC 18
07 GC 09 PC 11 12
01 02 03 04 05 06
Roll The Dice Now!
DICE: 2
sh: cls: command not found
PLAYER 1 TURN
31 32 33 34 35 36
25 P1 27 PC 29 30
19 20 21 22 GC 24
13 14 15 16 GC 18
07 GC 09 PC 11 12
01 02 03 04 05 06
Roll The Dice Now!
DICE: 6
sh: cls: command not found
PLAYER 1 TURN
31 32 33 34 35 36
25 P1 27 PC 29 30
19 20 21 22 GC 24
13 14 15 16 GC 18
07 GC 09 PC 11 12
01 02 03 04 05 06
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.