<repo_name>vinamramaliwal/DesignPatterns<file_sep>/TicTacToe.cpp
Pubic class User{
    string name;
    CellType c;
}
public class Game{
    Board* board;
    User* a;
    User* b;
    Game(User* a,User* b)
    {
        this.a=a;
        this.b=b;
        board=new Board();
    }
    bool checkifUserWon(User* user)
    {
        
    }
    void endGame{
        
    }
}
public class Board{
    public:
    list<Cell*> listofCell;
    list<EmptyCell*> lisofEmptyCell;
    Board()
    {
        int i,j;
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                Cell* c=new Cell(i,j);
                listofCell.push_back(c);
                lisofEmptyCell.push_back(c);
            }
        }
    }
    Cell* getCell(int i,int j)
    {
        for(auto it=listofCell.begin();it!=listofCell.end();it++)
        {
            cell *c=*it;
            if(c->row==i&&c->col==j)
                return c;
        }
        return NULL;
    }
    bool move(int i,int j,CellType c)
    {
        Cell *cell=getCell(i,j);
        if(cell==NULL)
            return false;
        cell->celltype=c;
        listofemptycell.erase(cell);
        return true;
    }
}
enum CellType{CROSS,CIRCLE,EMPTY};
public class Cell{
    public:
    CellType celltype;   
    int row;
    int col;
    Cell(int i,int j)
    {
        row=i;
        col=j;
        cellType=CellType::EMPTY;   
    }
    
}
<file_sep>/MovieBooking.cpp
#include<bits/stdc++.h>
using namespace std;
public class Movie{
    int id;
    string name;
    //other metadata
}
public class SeatLock {
    private Seat seat;
    private Show show;
    private Integer timeoutInSeconds;
    private Date lockTime;
    private String lockedBy;

    public boolean isLockExpired() {
        final Instant lockInstant = lockTime.toInstant().plusSeconds(timeoutInSeconds);
        final Instant currentInstant = new Date().toInstant();
        return lockInstant.isBefore(currentInstant);
    }
}
public class Booking{
    Movie* movie;
    Threatre* theatre;
    Screen* screen;
    Show* show;
    List<seats*> seatsBooked;
    
}
public class Theatre{
    int id;
    string name;
    list<Screen*> lisofScreens;
    set<Movie*> setofMovies;
}
public class Screen{
    int id;
    string name;
    list<Show*> lisofShows;
    Theatre* Theatre;
    
}
public class Show{
    Movie* curr_movie;
    int startTime;
    int endTime;
    Screen* screen;
    List<Seats*> listofSeats;
};
public class Seat{
    int rownum;
    int colnum;
    Seatstatus st;
    int price;
};
public enum Seatstatus{Booked,Available,Not_Available};

int main()
{
    
      
 
}
<file_sep>/RideSharingApp.cpp
#include<bits/stdc++.h>
using namespace std;
enum RideStatus {IDLE,CREATED,WITHDRAWN,COMPLETED};
class Ride;
class Driver
{
    public:
    string name;
    Ride* ride_assigned;
     Driver(string name)
    {
        this->name=name;
        ride_assigned=NULL;
    }
    
};
class Ride{
    public:
    int id;
    int origin;
    int destination;
    int seats;
    int amount;
    static const int AMT_PER_KM= 20;
    RideStatus rideStatus;
    Driver* driverAssigned;
    Ride(int id,int origin,int destination,int seats)
    {
        this->id=id;
        this->origin=origin;
        this->destination=destination;
        this->seats=seats;
    }
    int calculateAmout(bool isPriorityRider)
    {
        
        int numerOfKilometers=destination-origin;
        
        if(seats>=2)
            amount=numerOfKilometers*seats*(isPriorityRider?0.75:1);
        else if(seats==1)
            amount=numerOfKilometers*AMT_PER_KM*(isPriorityRider?0.75:1);
        
        return amount;
    }
    
};

class Rider
{
    public:
    string name;
    list<Ride*> ridesBooked;
    Rider(string name)
    {
        this->name=name;
    }
    Ride* createRide(int id,int origin,int destination,int seats)
    {
        Ride* newRide=new Ride(id,origin,destination,seats);
        newRide->rideStatus=CREATED;
        ridesBooked.push_back(newRide);
        return newRide;
    }
    void updateRide(int id,int origin,int destination,int seats)
    {
        auto it=ridesBooked.rbegin();
        for(;it!=ridesBooked.rend();it++)
        {
            if((*it)->id==id)
                break;
        }
        if((*it)->rideStatus!=RideStatus::CREATED)
        {
            cout<<"Ride Not in Progress,Can't Update"<<endl;
            return;
        }
        (*it)->origin=origin;
        (*it)->destination=destination;
        (*it)->seats=seats;
    }
    void withdrawRide(Ride* ride)
    {
        ride->rideStatus=RideStatus::WITHDRAWN;
        ridesBooked.remove(ride);
    }
    int closRide(Ride* ride)
    {
        int amount=ride->calculateAmout(ridesBooked.size()>=10);
        Driver* d1=ride->driverAssigned;
        d1->ride_assigned=NULL;
        ride->rideStatus=RideStatus::COMPLETED;
        return amount;
    }
};
class RideSharingApp
{
    public:
    list<Rider*> listOfRiders;
    list<Driver*> listOfDrivers;
    Driver* addDriver(string name)
    {
        Driver* d1=new Driver(name);
        listOfDrivers.push_back(d1);
        return d1;
    }
    Rider* addRider(string name)
    {
        Rider* r1=new Rider(name);
        listOfRiders.push_back(r1);
        return r1;
    }
    void showdrivers()
    {
        cout<<"Drivers Available"<<endl;
        for(auto it=listOfDrivers.begin();it!=listOfDrivers.end();it++)
        {
            cout<<(*it)->name<<" ";
        }
        cout<<endl;
    }
};

int main() {
    RideSharingApp* Uber=new RideSharingApp();
    Uber->addDriver("Ayush");
    Uber->addDriver("Abhishek");
    Uber->addDriver("Avanish");
    Uber->addDriver("Kuldeep");
    Uber->addDriver("Nikhil");
    Uber->showdrivers();
    
    Rider* r1=Uber->addRider("Vinamra");
    cout<<"Enter details for Rider Vinamra"<<endl;
    Ride* ride1=r1->createRide(0,50,60,2);
    cout<<"Ride started"<<endl;
    auto it=Uber->listOfDrivers.begin();
    for(;it!=Uber->listOfDrivers.end();it++)
    {
        if((*it)->ride_assigned==NULL)
        {
            (*it)->ride_assigned=ride1;
            break;
        }
    }
    ride1->driverAssigned=*it;
    cout<<"Driver assigned="<<ride1->driverAssigned->name<<endl;
    int e=r1->closRide(ride1);
    cout<<"Ride ended:Amout charged="<<e<<endl;
    
    
}
<file_sep>/MeetingScheduler.cpp
#include<bits/stdc++.h>
using namespace std;
class Room;
class Meeting
{
    int start;
    int end;
    Room* assigned_room;
    public:
    Meeting(int start,int end,Room* assigned_room)
    {
        this->start=start;
        this->end=end;
        this->assigned_room=assigned_room;
    }
    int getStart(){return start;}
    int getEnd(){return end;}
    
};
class Room{
    int id;
    string name;
    vector<Meeting*> calendar;
    public:
    Room(int id,string name)
    {
        this->id=id;
        this->name=name;
    }
    int getId(){return id;};
    vector<Meeting*> getCalendar(){return calendar;};
    void updateCalendar(Meeting* m)
    {
        calendar.push_back(m);
        return;
    }
    bool bookRoom(int start,int end)
    {
        int i;
        for(i=0;i<calendar.size();i++)
        {
            int x=calendar[i]->getStart();
            int y=calendar[i]->getEnd();
            if(end<=x||start>=y)
                continue;
            else
            {
                cout<<"Room not available for these timings. Choose another Room"<<endl;
                return false;
            }
        }
        Meeting *newMeeting=new Meeting(start,end,this);
        this->updateCalendar(newMeeting);
        cout<<"Meeting scheduled successfully"<<endl;
        return true;
    }
    
};

class Scheduler
{
    vector<Room*> listOfRooms;
    public:
    Room* createRoom(int id,string name)
    {
        Room* newRoom=new Room(id,name);
        listOfRooms.push_back(newRoom);
        return newRoom;
    }
    bool bookRoom(int start,int end,int id)
    {
        Room *r;
        int i;
        for(i=0;i<listOfRooms.size();i++)
        {
            r=listOfRooms[i];
            if(r->getId()==id)
                break;
        }
        if(i==listOfRooms.size())
        {
            cout<<"Invalid Room id"<<end;
            return false;
        }
        return r->bookRoom(start,end);
        
    }
    void showScheduleOfAllRooms()
    {
        for(int i=0;i<listOfRooms.size();i++)
        {
            Room *p=listOfRooms[i];
            cout<<"Room Id="<<p->getId()<<endl<<"Meeting timings are"<<endl;
            vector<Meeting*> v=p->getCalendar();
            for(int j=0;j<v.size();j++)
            {
                cout<<v[j]->getStart()<<" "<<v[j]->getEnd()<<endl;
            }
        }
    }
    
};
int main()
{
    Scheduler* Jeera=new Scheduler();
    Jeera->createRoom(1,"Conference Hall-1");
    Jeera->createRoom(2,"Conference Hall-2");
    Jeera->createRoom(3,"Conference Hall-3");
    Jeera->bookRoom(9,11,1);
    Jeera->bookRoom(10,12,1);
    Jeera->bookRoom(10,15,2);
    Jeera->bookRoom(15,16,2);
    Jeera->showScheduleOfAllRooms();
    
}
<file_sep>/SnakeGame.cpp
enum Celltype{EMPTY,FOOD,SNAKE_NODE};
class cell
{
    public:
    Celltype ctp;
    int i;
    int j;
    cell();
    cell(int i,int j)
    {
        this->i=i;
        this->j=j;
        this->ctp=EMPTY;
    }
    void setCelltype(Celltype name)
    {
        this->ctp=name;
    }
    Celltype getCelltype()
    {
        return this->ctp;
    }
};
class Snake
{
    public:
    list<cell*> mysnake;
    cell* head;
    Snake(cell* initpos)
    {
        head=initpos;
        initpos->setCelltype(SNAKE_NODE);
        mysnake.push_back(initpos);
        
    }
    void grow(cell* nextcell)
    {
        cout<<"Snake is growing to "<<nextcell->i<<" "<<nextcell->j<<endl;
        nextcell->setCelltype(SNAKE_NODE);
        mysnake.push_front(nextcell);
        head=nextcell;
        
    }
        bool checkcrash(cell* nextcell)
        {
            cout<<"Going to check for crash"<<endl;
            for(auto it=mysnake.begin();it!=mysnake.end();it++)
            {
               if((*it)->i==nextcell->i&&(*it)->j==nextcell->j)
                        return true;
            }
            return false;
        }
    void move(cell* nextcell)
    {
        
        cout<<"Snake is moving to "<<nextcell->i<<" "<<nextcell->j<<endl;
        head=nextcell;
        cell *c=mysnake.back();
        (*c).setCelltype(EMPTY);
        nextcell->setCelltype(SNAKE_NODE);
        mysnake.push_front(nextcell);
        mysnake.pop_back();
    }
    
    
};


class Board
{
    public:
    int n,m;
    vector<vector<cell*>> matrix;
    Board(int n,int m)
    {
        this->n=n;
        this->m=m;
        int i,j;
        matrix.resize(n);
        for(i=0;i<n;i++)
        {
            matrix[i].resize(j);
            for(j=0;j<m;j++)
                matrix[i][j]=new cell(i,j);
        }
    }
    void generateFood()
    {
        cout<<"Going to generate food"<<endl;
        int x,y;
        while(1)
        {
            x=rand()%n;
            y=rand()%m;
            if((*matrix[x][y]).getCelltype()!=SNAKE_NODE||(*matrix[x][y]).getCelltype()!=FOOD)
            {
                break;
            }
        }
        matrix[x][y]->setCelltype(FOOD);
        return;
    }
};
class Game
{
    public:
    static const int DIRECTION_NONE=0,DIRECTION_RIGHT = 1,DIRECTION_LEFT = -1,DIRECTION_UP = 2,DIRECTION_DOWN = -2;
    Snake* snake;
    Board* board;
    int direction;
    bool gameOver;
    Game(Snake* snake,Board *board)
    {
        this->snake=snake;
        this->board=board;
    }
    void update()
    {
        cout<<"Going to update game"<<endl;
        if(!gameOver&&direction!=DIRECTION_NONE)
        {
            cell* nextcell=getNextCell(snake->head);
            if(gameOver)
                return;
            if(snake->checkcrash(nextcell))
            {
                direction=DIRECTION_NONE;
                gameOver=true;
            }
            else
            {
                
                if (nextcell->ctp==FOOD) {
                        snake->grow(nextcell);
                        board->generateFood();
                    }
                else
                    snake->move(nextcell);
            }
        }
    }
    cell* getNextCell(cell* currentPosition)
    {
        cout<<"Going to find next cell"<<endl;
        int row=currentPosition->i;
        int col=currentPosition->j;
         if (direction == DIRECTION_RIGHT) {
            col++;
        }
        else if (direction == DIRECTION_LEFT) {
            col--;
        }
        else if (direction == DIRECTION_UP) {
            row--;
        }
        else if (direction == DIRECTION_DOWN) {
            row++;
        }
        if(row<0||row>=10||col<0||col>=10)
        {
            gameOver=true;
            cout<<"NextCell out of Range::Game Over"<<endl;
            return NULL;
        }
        return board->matrix[row][col];
    }
    
};
int main() {
    std::cout <<"Going to start game";
       cell* initPos = new cell(0, 0);
       Snake *initSnake = new Snake(initPos);
        Board* board = new Board(10, 10);
        Game* newGame = new Game(initSnake, board);
        newGame->gameOver = false;
        newGame->direction = Game::DIRECTION_RIGHT;
    while(1)
    {
            for (int i = 0; i < 5; i++) 
            {
                if (i == 0)
                newGame->direction = Game::DIRECTION_DOWN;
            if (i == 2)
                newGame->board->generateFood();
             newGame->update();
            if (i == 3)
                newGame->direction = Game::DIRECTION_RIGHT;
            if (newGame->gameOver == true)
                return 0;
            }
    }
    return 0;
}
<file_sep>/README.md
# DesignPatterns

This Repository contains the DesignPattern Questions code.
<file_sep>/SnakeLadder.cpp
#include <bits/stdc++.h>
using namespace std;
class Dice
{
	public:
	int throwDice()
	{
	return rand()%6+1;
	}
};
class Player:public Dice
{
	private:
	int position;
	public:
	string name;
	Player(string name)
	{
		this->position=0;
		this->name=name;
	}
	void setPosition(int x)
	{
		this->position=x;
	}
	int getPosition()
	{
		return this->position;
	}

};
class Board
{
   
	public:
	map<int,int> ladder;
	map<int,int> snake;
	
	void addSnake(int a ,int b)
	{
		this->snake[a]=b;
	}
	void addLadder(int a,int b)
	{
		this->ladder[a]=b;
	}
};
int main() {
	
	Board bd;
	cout<<"Enter the number of snakes"<<endl;
	int i,j,k,l,m,n,a,b,p,q;
	cin>>l;
	for(i=1;i<=l;i++)
	{
		cin>>a>>b;
		bd.addSnake(a,b);
		
	}
	cout<<"Enter the number of ladders"<<endl;
	cin>>l;
	for(i=1;i<=l;i++)
	{
		cin>>a>>b;
		bd.addLadder(a,b);
	}
	Player x("Gaurav");
	Player y("Sagar");
	srand(time(0)); 
	m=1;
	while(1)
	{
		a=x.getPosition();
		b=y.getPosition();
		if(a==100||b==100)
			break;
		if(m==1)
		{
				m=0;
				k=x.throwDice();
				if(a+k>100)
					{
						x.setPosition(a);
					}
				else{
						j=a+k;
					   x.setPosition(j);
						if(bd.snake.find(j)!=bd.snake.end())
						{
							x.setPosition(bd.snake[j]);
						}
						if(bd.ladder.find(j)!=bd.ladder.end())
						{
							x.setPosition(bd.ladder[j]);
						}
					}
			p=x.getPosition();
		cout<<x.name<<" rolled a "<<k<<" and moved from "<<a<<" to "<<p<<endl;
			
		}
		else{
			m=1;
				k=y.throwDice();
				if(b+k>100)
					{
						y.setPosition(b);
					}
				else{
						j=b+k;
					 y.setPosition(j);
						if(bd.snake.find(j)!=bd.snake.end())
						{
							y.setPosition(bd.snake[j]);
						}
						if(bd.ladder.find(j)!=bd.ladder.end())
						{
							y.setPosition(bd.ladder[j]);
						}
					}
			p=y.getPosition();
		cout<<y.name<<" rolled a "<<k<<" and moved from "<<b<<" to "<<p<<endl;
			
			
		}
		
		
	}
	if(a==100)
		cout<<"Gaurav won the game"<<endl;
	else if(b==100)
		cout<<"Sagar won the game"<<endl;
	
	
	return 0;
}
