Flipkart Interview Question
SDE-2sCountry: India
Interview Type: In-Person
#include "SnakesnLadders.h"
#include<math.h>
#define MAX_PLAYERS 4
#define MAX_BLOCK 100
class Player
{
int id;
std::basic_string<char> name;
int position;
public:
Player(int id,std::basic_string<char> name)
{
this->id = id;
this->name = name;
}
int throw_dice()
{
return random()%6;
}
Player(const Player &p)
{
this->id = p.id;
this->name = p.name;
}
int get_position()
{return position;}
void set_position(int n)
{
position = n;
}
};
class Board
{
int n;//no of blocks 100 common case
int move[];
public:
Board()
{
n = MAX_BLOCK;
for(int i =0;i<n;i++)
{
move[i] = -1;
}
}
Board(int n , int move[])
{
this->n = n;
for(int i =0;i<n;i++)
{
this->move[i] = move[i];
}
}
int get_steps()
{
return n;
}
int* get_moves()
{
return move;
}
};
class Game
{
Player players[MAX_PLAYERS];
Board board;
int n;
public: Game(Player p[],Board b,int n);
int play();
};
Game :: Game(Player p[],Board b,int n)
{
this-> n = n;
for(int i = 0;i<n;i++)
this->players[i](&p[i])
this->board = board;
}
int Game::play()
{
int chance = 0;
int dice_value = 0;
while(1)
{
dice_value = players[chance].throw_dice();
int move = 0;
int new_position = players[chance].get_position() + dice_value;
if(move = board.get_moves()[new_position] != -1)
{
new_position += move;
}
if(new_position <= 100)
{
players[chance].set_position(new_position);
}
if(new_position == 100)
return chance;
if(dice_value != 6)
chance = chance == 5 ? 0 : chance+1;
}
return -1;
}
In design rounds, full working code was expected or class diagrams ?
- AJ May 14, 2015