dhsharma27
BAN USERRedis is an in-memory database implemnted in ANSI C.
I have tried to give a basic idea how something like this can be structured in C++ , in the implementation things can be added further .
#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define redis_obj robj*
using namespace std;
class robj //base class for redis objects , rest classes are to be derived from it
{
public :
virtual string get()
{
}
virtual string append(char v)
{
}
virtual string append(string v)
{
}
virtual void lpush(string v)
{
}
virtual void rpush(string v)
{
}
virtual void sadd(string s)
{
}
};
class sstring : public robj //class for string in redis
{
string val;
public :
sstring() //constructor1
{
val.clear();
}
sstring(string v) //constructor2
{
val=v;
}
string get() //returns string version of the data_Structure
{
return val;
}
string append(string v) //func. to append string to the stored string.
{
val+=v;
return val;
}
string append(char v) //func. to append char to the stored string.
{
val.pb(v);
}
};
class llist:public robj
{
list<string> val;
public :
llist() //constructor
{
val.clear();
}
llist(list<string> v) //constructor 2
{
val=v;
}
void lpush(string v) //function to insert in begining
{
val.push_front(v);
}
void rpush(string v) //function to insert in the end
{
val.push_back(v);
}
string get()
{
string tmp;
for(list<string> :: iterator i=val.begin();i!=val.end();i++)
tmp+=(*i)+" ";
return tmp;
}
};
class sset:public robj //class for set in redis , this can be implented using trie , I just implemnted using the c++ default set
{ // in redis it is implemented using hash
set<string> val;
public :
sset() //cons.
{
val.clear();
}
sset(set<string> v) //cons.1
{
val=v;
}
void sadd(string s) //adding val to the set
{
val.insert(s);
}
string get()
{
string tmp;
for(set<string> :: iterator i=val.begin();i!=val.end();i++)
tmp+=(*i)+" ";
return tmp;
}
};
class s_set : public robj //class for sorted set in redis...
{
set<string> val;
public :
s_set()
{
val.clear();
}
s_set(set<string> v)
{
val=v;
}
void sadd(string s)
{
val.insert(s);
}
string get()
{
string tmp;
for(set<string> :: iterator i=val.begin();i!=val.end();i++)
tmp+=(*i)+" ";
return tmp;
}
};
class hash : public robj //although its O(1) in redis , i used map over here,
{
map<string,string> val;
public :
hash()
{
}
hash(map<string,string> s)
{
val=s;
}
string get()
{
string tmp ; tmp.clear();
for(map<string,string> :: iterator i=val.begin();i!=val.end();i++)
{
tmp+=i->ff+" "+i->ss+" ";
}
return tmp;
}
};
int main()
{
redis_obj s=new sstring();
cout<<s->append("check")<<"\n";
redis_obj li= new llist();
li->lpush("first"); li->rpush("second"); li->rpush("third");
cout<<li->get()<<"\n";
return 0;
}
#include<bits/stdc++.h>
#define ppi pair<int,int>
#define mp make_pair
#define ff first
#define ss second
using namespace std;
string mv;
class board
{
vector< vector<string> > brd;
string R,B; //players names
int cplayer; //current move
vector< vector<int> > undo_redo; //undo_redo_moves
int cntr,cntb;
public :
board()
{
}
board(string a,string b)
{
cntr=cntb=12;
R=a,B=b;
brd.resize(8,vector<string> (8,"__"));
cplayer=0;
init_board();
}
void player_is(int cp)
{
if(cp==0)
cout<<"[ Your turn Mr. "<<R<<" ] {RED}\n";
else
cout<<"[ Your turn Mr. "<<B<<" ] {BLUE}\n";
}
bool out_of_board(ppi rc)
{
if(rc.ff>7 || rc.ff<0 || rc.ss>7 || rc.ff<0)return 1;
return 0;
}
pair<ppi,ppi> parse_move(string mv)
{
return mp( mp(mv[0]-'a',mv[1]-'1') , mp(mv[3]-'a',mv[4]-'1') );
}
bool gameover()
{
if(cntr==0)
cout<<"Blue wins !! \n" ;
else if(cntb==0)
cout<<"Red wins !! \n";
if(cntb==0 || cntr==0)return 1;
return 0;
}
void play_game()
{
while(!gameover())
{
cout<<"here\n\n\n";
cmove();
}
}
void init_board();
void print_board();
void cmove();
int commit_move(pair<ppi,ppi> mv);
int is_valid_and_commit(int pturn,pair<ppi,ppi> mv);
void rules()
{
cout<<"\nEnter your move when prompted !! \nEnter string of length 5 .i.e [a-h][1-8] [a-h][1-8] \n\n";
}
};
/*
valid moves :
red -- rows should increase ..... if not crowned
*/
void board :: init_board()
{
for(int i=0;i<3;i++)
for(int j=0;j<8;j++)
{
if( (i+j)%2==1)
brd[i][j]="NR";
else
brd[i+5][j]="NB";
}
}
void board :: print_board()
{
cout<<"\n ";
for(int i=0;i<8;i++)
cout<<i+1<<" ";cout<<"\n";
for(int i=0;i<8;i++)
{
cout<<"\n "<<char('a'+i)<<" ";
for(int j=0;j<8;j++)
cout<<brd[i][j]<<" ";
cout<<" "<<char('a'+i);
}
cout<<"\n\n ";
for(int i=0;i<8;i++)
cout<<1+i<<" ";
cout<<"\n\n";
}
string tmp;
void board :: cmove()
{
player_is(cplayer);
print_board();
cin>>mv>>tmp;
mv+=" "+tmp;
int val=is_valid_and_commit( cplayer , parse_move(mv) );
if(val==0)
{
cout<<"Invalid move !! \n";
cmove();
return ;
}
else if(val==1)
{
//add_to_stack(parse_move(mv));
}
else
{
player_is(cplayer);
//add_to_stack(parse_move(mv));
cmove();
return ; //return used so that player mayn't change....
}
cplayer=(cplayer+1)%2;
}
int board :: is_valid_and_commit(int pturn,pair<ppi,ppi> mv)
{
cout<<mv.ff.ff<<" "<<mv.ff.ss<<" "<<mv.ss.ff<<" "<<mv.ss.ss<<"\n";
if(out_of_board(mv.ff) || out_of_board(mv.ss))
{
cout<<"out of box \n";
return 0;
}
int fx=mv.ff.ff , fy=mv.ff.ss , tx=mv.ss.ff , ty=mv.ss.ss ;
cout<<fx<<" "<<fy<<" "<<tx<<" "<<ty<<"\n";
if(pturn==0) //red turn or 0 implies RED
{
if(brd[fx][fy]=="NR")
{
if(tx-fx==1)
{
if(abs(fy-ty)!=1 || brd[tx][ty]!="__") return 0;
swap(brd[tx][ty],brd[fx][fy]); //commit the move here itself
return 1;
}
else if(tx-fx==2)
{
if(abs(fy-ty)!=2 || brd[tx][ty]!="__")return 0;
if(ty-fy==2 && (brd[fx+1][fy+1]=="NB" || brd[fx+1][fy+1]!="CB") )
{
swap(brd[tx][ty],brd[fx][fy]);
brd[fx+1][fy+1]="__";
cntb--;
return 2;
}
else if(ty-fy==-2 && (brd[fx+1][fy-1]=="NB" || brd[fx+1][fy-1]=="CB"))
{
swap(brd[tx][ty],brd[fx][fy]);
brd[fx+1][fy+1]="__";
cntb--;
return 2;
}
return 0;
}
else
return 0;
}
else if(brd[fx][fy]=="CR")
{
if( abs(fx-tx)==1)
{
if( abs(fy-ty)!=1 || brd[tx][ty]!="__" )return 0;
swap(brd[fx][fy],brd[tx][ty]);
return 1;
}
else if( abs(fx-tx)==2 )
{
if( abs(fy-ty)!=2 || brd[tx][ty]!="__")return 0;
if(tx-fx==2 && ty-fy==2 && (brd[fx+1][fy+1]=="NB" || brd[fx+1][fy+1]!="CB") )
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx+1][fy+1]="__";
cntb--;
return 2;
}
else if(tx-fx==2 && ty-fy==-2 && (brd[fx+1][fy-1]=="NB" || brd[fx+1][fy-1]=="CB"))
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx+1][fy-1]="__";
cntb--;
return 2;
}
else if(tx-fx==-2 && ty-fy==2 && ( brd[fx-1][fy+1]=="NB" || brd[fx-1][fy+1]=="CB"))
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx-1][fy+1]="__";
cntb--;
return 2;
}
else if(mv.ss.ff-mv.ff.ff==-2 && mv.ss.ss-mv.ff.ss==-2 && (brd[mv.ff.ff-1][mv.ff.ss-1]=="NB" || brd[mv.ff.ff-1][mv.ff.ss-1]=="CB"))
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx-1][fy-1]="__";
cntb--;
return 2;
}
return 0;
}
}
else
return 0;
}
else
{
if(brd[fx][fy]=="NB")
{
if(tx-fx==-1)
{
if(abs(ty-fy)!=1 || brd[tx][ty]!="__") return 0;
swap(brd[fx][fy],brd[tx][ty]);
return 1;
}
else if(tx-fx==-2)
{
if(abs(ty-fy)!=2 || brd[tx][ty]!="__")return 0;
if(ty-fy==2 && ( brd[fx-1][fy+1]=="NR" || brd[fx-1][fy+1]=="CR") )
{
swap(brd[tx][ty],brd[fx][fy]);
brd[fx-1][fy+1]="__";
cntr--;
return 2;
}
else if(ty-fy==-2 && (brd[fx-1][fy-1]!="NR" || brd[fx-1][fy-1]!="CR"))
{
swap(brd[tx][ty],brd[fx][fy]);
brd[fx-1][fy-1]="__";
cntr--;
return 2;
}
else
return 0;
}
else
return 0;
}
else if(brd[fx][fy]=="CB")
{
if( abs(fx-fy)==1)
{
if( abs(fy-ty)!=1 || brd[tx][ty]!="__" )return 0;
swap(brd[fx][fy],brd[tx][ty]);
return 1;
}
else if( abs(tx-fx)==2 )
{
if( abs(fy-ty)!=2 || brd[tx][ty]!="__")return 0;
if(tx-fx==2 && ty-fy==2 && (brd[fx+1][fy+1]=="NR" || brd[fx+1][fy+1]!="CR") )
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx+1][fy+1]="__";
cntr--;
return 2;
}
else if(tx-fx==2 && ty-fy==-2 && (brd[fx+1][fy-1]=="NR" || brd[fx+1][fy-1]=="CR"))
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx+1][fy-1]="__";
cntr--;
return 2;
}
else if(tx-fx==-2 && ty-fy==2 && ( brd[fx-1][fy+1]=="NR" || brd[fx-1][fy+1]=="CR"))
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx-1][fy+1]="__";
cntr--;
return 2;
}
else if(mv.ss.ff-mv.ff.ff==-2 && mv.ss.ss-mv.ff.ss==-2 && (brd[mv.ff.ff-1][mv.ff.ss-1]=="NR" || brd[mv.ff.ff-1][mv.ff.ss-1]=="CR"))
{
swap(brd[fx][fy],brd[tx][ty]);
brd[fx-1][fy-1]="__";
cntr--;
return 2;
}
else
return 0;
}
else
return 0;
}
else
return 0;
}
}
int main()
{
board o("d","p");
o.rules();
o.play_game();
return 0;
}
I hope it may give you some idea , although its a lot of code .
- dhsharma27 May 25, 2016#include<bits/stdc++.h>
#define pp pair<char,char>
#define ppi pair<int,int>
#define ff first
#define ss second
#define mp make_pair
#define move mv
using namespace std;
string move;
pair<pp,pp> parsed_move(string move)
{
return mp( mp(move[0]-'a',move[1]-'0') , mp(move[3]-'a',move[4]-'0') );
}
class piece
{
public :
string rep;
virtual string ret_rep()
{
return rep;
}
virtual bool is_valid(ppi frm,ppi to)
{
}
void add_color(string c)
{
rep=c+rep;
}
};
class empty:public piece
{
public :
empty()
{
rep="00";
}
string ret_rep()
{
return rep;
}
};
class pawn:public piece
{
public :
pawn()
{
rep="p";
}
string ret_rep()
{
return rep;
}
bool is_valid(ppi frm,ppi to)
{
}
};
class king:public piece
{
int moved;
public :
king()
{
rep="K";
moved=0;
}
bool is_valid(ppi frm,ppi to)
{
}
string ret_rep()
{
return rep;
}
};
class queen:public piece
{
public :
queen()
{
rep="Q";
}
bool is_valid(ppi frm,ppi to)
{
}
string ret_rep()
{
return rep;
}
};
class rook:public piece
{
public :
rook()
{
rep="R";
}
bool is_valid(ppi frm,ppi to)
{
}
string ret_rep()
{
return rep;
}
};
class knight:public piece
{
public :
knight()
{
rep="k";
}
bool is_valid(ppi frm,ppi to)
{
}
string ret_rep()
{
return rep;
}
};
class bishop:public piece
{
public :
bishop()
{
rep="B";
}
string ret_rep()
{
return rep;
}
bool is_valid(ppi frm,ppi to)
{
}
};
class chess_board
{
public :
vector<vector<piece*> > brd;
string white,black;
int moves;
vector<string> move_details;
chess_board()
{
}
chess_board(string a,string b)
{
brd.resize(8,vector<piece*> (8));
white=a,black=b;
moves=0;
initialize_board();
}
void initialize_board();
void add_piece(int r,int c,piece *obj)
{
brd[r][c]=obj;
}
void move_p1()
{
cout<<"Your move Mr."<<white<<"\n";
cin>>move;
if( isvalid_move(1,move) )
{
commit_to_board(1,move);
moves++;
}
else
move_p1();
}
void move_p2()
{
cout<<"Your move Mr."<<black<<"\n";
cin>>move;
if( isvalid_move(2,move) )
{
moves++;
commit_to_board(2,move);
}
else
move_p1();
}
void castling(int flag,string move)
{
}
bool isvalid_move(int flag,string mv)
{
}
void commit_to_board(int flag,string move)
{
move_details.push_back(move);
if(move=="0-0" || move=="0-0-0") //castling
castling(flag,move);
else
{
pair<ppi,ppi> fnl=parsed_move(move);
brd[fnl.ss.ff][fnl.ss.ss]=brd[fnl.ff.ff][fnl.ff.ss];
brd[fnl.ff.ff][fnl.ff.ss]=new empty();
}
}
void print_board()
{
cout<<" ";
for(int i=0;i<8;i++)
cout<<char('a'+i)<<" ";
for(int i=0;i<8;i++)
{
cout<<"\n"<<i<<" ";
for(int j=0;j<8;j++)
{
cout<<brd[i][j]->ret_rep()<<" ";
}
cout<<" "<<i<<" ";
}
cout<<"\n ";
for(int i=0;i<8;i++)
cout<<char('a'+i)<<" ";
}
};
void chess_board :: initialize_board()
{
for(int j=0;j<8;j++)
for(int i=0;i<8;i++)
brd[i][j]=new empty();
for(int i=0;i<8;i++)
brd[1][i]=new pawn(),
brd[6][i]=new pawn();
//4 rooks
brd[0][0]=new rook() , brd[0][7]=new rook();
brd[7][0]=new rook() , brd[7][7]=new rook();
//4 knights
brd[0][1]=new knight() , brd[0][6]=new knight();
brd[7][1]=new knight() , brd[7][6]=new knight();
//4 bishops
brd[0][2]=new bishop() , brd[0][5]=new bishop();
brd[7][2]=new bishop() , brd[7][5]=new bishop();
brd[0][3]=new king() , brd[7][3]=new king();
brd[0][4]=new queen() , brd[7][4]=new queen();
for(int i=0;i<2;i++)
{
for(int j=0;j<8;j++)
brd[i][j]->add_color("W") , brd[i+6][j]->add_color("B");
}
}
int main()
{
string a="dhruv",b="pankaj";
chess_board match(a,b);
match.print_board();
}
Here , I have made a class for the board and a base class for pieces .
The board consists of 64 pointers to the class pieces .
Classes like king,queen,rook,knight,pawn etc inherit from the pieces .
Made an extra class empty which indicates empty box and it also inherits from the class pieces .
Using polymorphism or virtual functions , piece* can point to different sort of pieces on the board .
This can be done using another approach .i.e
- dhsharma27 May 28, 2016For every pair of station , simply use brute force approach to calculate the path between the both .
This is a lot easy to implement .
Now the result can be stored in database easily and queried easily.
The scalability depends on number of queries the site is getting .