Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <numeric>
- /* DB */
- #define DEBUG
- #define color_default "\e[0;0m"
- #define color_red "\e[0;31m"
- #define color_green "\e[0;32m"
- #define color_yel "\e[0;33m"
- void color(const char * color){std::cout<<color;}
- void cdef(){color(color_default);}
- void err(const char * message){color(color_red);std::cout << message;color(color_default);}
- template<typename T>
- bool get_bit(T &value, int index){
- int targetbyte = index / 8,targetbit = index - targetbyte * 8;
- char * byte = reinterpret_cast<char*>(&value) + targetbyte;
- return ((*byte) & (1<<targetbit)) >> targetbit;
- }
- template<typename T>
- void print_bit(T &value){
- const int size = sizeof(T);
- for(int i = size*8-1;i>=0;i--){
- std::cout << (get_bit<int>(value,i) ? '1' : '0');
- }
- }
- template<typename T>
- void print_bit(T &value, int bitcount){
- for(int i = bitcount;i>=0;i--){
- std::cout << (get_bit<int>(value,i) ? '1' : '0');
- }
- }
- template<typename T>
- bool get_highest_bit(T &value){
- const int size = sizeof(T);
- for(int i = size*8-1;i>=0;i--){
- if(get_bit<int>(value,i)){
- return i;
- }
- }
- return 0;
- }
- template<typename T>
- std::string to_binary(T &value){
- const int size = sizeof(T);
- std::string result = "";
- int i;
- for(int i = size*8-1;i>=0;i--){
- result+=(get_bit<int>(value,i) ? '1' : '0');
- }
- return result;
- }
- void write_number_in_string(int number, std::string &str,int frompos){
- char buffer[20];
- sprintf(buffer,"%d",number);
- int i = 0;
- while(buffer[i]!='\0'){
- str[frompos++]=buffer[i++];
- }
- }
- struct piles_stat{
- int maxheapsize = 0;
- int maxheapindex = 0;
- int anyindex = 0; //Egy random kupac index, amin biztos van tegla
- int countofpiles = 0; //aktiv kupacok szama
- int countofones = 0;// egyes kupacok szama
- int xorval = 0;
- };
- class game_space{
- public:
- friend class game_manager;
- game_space(int size):piles(size,0),pilecount(size){}
- //0 ha ervenytelen lepes, kulonben 1
- bool remove(const int pileindex, const int count){
- if(pileindex<0 || pileindex >= pilecount || count < 0 || count > this->piles[pileindex]){
- #ifdef DEBUG
- err("Ervenytelen lepes!, remove ");
- std::cout << pileindex << " ,count: " << count << std::endl;
- std::cout << pileindex << " oszlopban osszesen : "<< piles[pileindex] << " db stone van\n";
- exit(-1);
- #endif
- return false;
- }
- piles[pileindex]-=count;
- return true;
- }
- //Legnagyobb helyiertek index
- //A legnagyobb helyierteku setelt bit, tehat a legnagyobb segment meret
- int get_highest_pile_segment_index(){
- int highest = 0;
- for(int elem : piles){
- int h =get_highest_bit(elem);
- if(h>highest){
- highest = h;
- }
- }
- return highest;
- }
- void print_parity_binary(){
- std::cout << "#Binaris reprezentacio paritasa:\n";
- int x = get_xor();
- std::cout << "to_binary xor: " << to_binary(x);
- int highest = get_highest_pile_segment_index();
- std::cout << "\nhighest pile bit index: " << highest << "\n";
- std::cout << "Roviditve: ";
- print_bit(x,highest);
- std::cout << "\n - - - - - \n";
- }
- //TODO
- piles_stat getStatistic(){
- piles_stat stat;
- for(int i = 0; i < pilecount; i++){
- int currpile = piles[i];
- stat.xorval^=piles[i];
- if(currpile>0){
- stat.countofpiles++;
- if(currpile==1)stat.countofones++;
- if(currpile > stat.maxheapsize){
- stat.maxheapindex=currpile;
- stat.maxheapindex = i;
- stat.anyindex = i;
- }
- }
- }
- return stat;
- }
- int get_xor() const{
- int ret = 0;
- std::for_each(piles.begin(),piles.end(),[&](const int elem){ret^=elem;});
- return ret;
- }
- int get_count_in_pile(const int pileindex) const{
- #ifdef DEBUG
- if(pileindex<0 || pileindex >= pilecount){
- err("Ervenytelen pileindex, getcountinpile");
- std::cout << pileindex;
- exit(-1);
- }
- #endif
- return piles[pileindex];
- }
- int get_count_of_stones() const{
- return std::accumulate(piles.begin(),piles.end(),0);
- }
- int get_size() const{
- return this->pilecount;
- }
- int get_max() const {
- return *std::max_element(piles.begin(),piles.end());
- }
- void generate_random_space(const int minheapsize,const int maxheapsize){
- srand(time(NULL));
- int len = maxheapsize - minheapsize;
- for(int i = 0; i < pilecount;i++){
- int randvalue = rand() % (len+1);
- piles[i] = minheapsize + randvalue;
- }
- }
- void print_info(){
- for(int i = 0; i < pilecount;i++){
- std::cout << i+1 << " : " << piles[i] << "\n";
- }
- print_parity_binary();
- std::cout << "\n";
- }
- //Csak a ter kerul kirajzolasra
- void print_space(){
- const std::string margin = " ";
- int rows = get_max();
- std::string lines[rows];
- //init
- for(int i = 0; i < rows;i++){
- lines[i]="";
- for(int j = 0; j < pilecount*2;j++){
- lines[i]+=margin;
- }
- }
- for(int i = 0; i < pilecount;i++){
- int index = i*margin.size()+i;
- lines[rows-1][index+1] = '|';
- write_number_in_string(i+1,lines[rows-1],index+2);
- for(int j = 0; j < piles[i];j++){
- lines[rows-1-j][index] = 'O';
- }
- }
- std::cout << "\n";
- color(color_green);
- for(int i = 0; i < rows;i++){
- std::cout << lines[i] << "\n";
- }
- cdef();
- }
- private:
- int pilecount;
- std::vector<int> piles;
- };
- struct move{
- move() = default;
- move(const int pileindex, const int stonecount):pindex(pileindex),scount(stonecount){}
- int pindex;
- int scount;
- };
- std::ostream& operator<<(std::ostream &os, const move &m){
- os << "oszlop: " << m.pindex+1 << ", " << m.scount << " darab";
- return os;
- }
- //player interface
- class player{
- public:
- virtual ~player() {}
- virtual move get_move(const game_space&p) = 0;
- virtual bool is_wanted(){return false;}
- };
- class console_player : public player{
- public:
- console_player(){}
- virtual move get_move(const game_space &p){
- int from,count;
- #ifdef DEBUG
- std::cout << "console_player: Melyik oszlopbol? ";
- #endif
- std::cin>>from;
- #ifdef DEBUG
- std::cout << "console_player: Hany darabot? ";
- #endif
- std::cin>>count;
- from--;
- return move{from,count};
- }
- virtual ~console_player(){}
- };
- // teljesen random jatszik
- // TODO
- class random_player : public player{
- public:
- };
- // - - - -- - - --------- --- - - ------ - - ----- -- - - - -
- class smart_player : public player{
- public:
- smart_player(){}
- virtual move get_move(const game_space &p){
- int currentxor = p.get_xor();
- }
- private:
- };
- class game_manager{
- public:
- game_manager(player * MYPLAYER, player * OTHERPLAYER, game_space &space):space(space),itsme(MYPLAYER),enemy(OTHERPLAYER){}
- void start_local(){
- bool whomove = itsme->is_wanted();
- if(whomove){
- std::cout << "A jatekot mi kezdjuk\n";
- }else{
- std::cout << "A jatekot az ellenfel kezdi\n";
- }
- while(space.get_count_of_stones()){
- std::cout << "\n S- - - - - - - - - - - \n\n";
- space.print_info();
- space.print_space();
- move m;
- if(whomove){
- m=itsme->get_move(space);
- }else{
- m=enemy->get_move(space);
- }
- color(color_yel);
- std::cout << "~Lepes: " << m << "\n";
- cdef();
- space.remove(m.pindex,m.scount);
- whomove = !whomove;
- }
- color(color_yel);
- if(!whomove){
- std::cout << "\n [ WIN ]\n";
- }else{
- std::cout << "\n [ LOOSE ]";
- }
- std::cout << "\n Jatek vege \n";
- }
- void start_graphi(){
- }
- private:
- game_space &space;
- player * itsme;
- player * enemy;
- };
- void _game1(){
- player * p1 = new console_player();
- player * p2 = new console_player();
- game_space s(5);
- s.generate_random_space(1,7);
- game_manager m(p1,p2,s);
- m.start_local();
- delete p1;
- delete p2;
- }
- int main(){
- _game1();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement