Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <numeric>
  4.  
  5. /* DB */
  6.  
  7. #define DEBUG
  8.  
  9. #define color_default "\e[0;0m"
  10. #define color_red "\e[0;31m"
  11. #define color_green "\e[0;32m"
  12. #define color_yel "\e[0;33m"
  13.  
  14.  
  15. void color(const char * color){std::cout<<color;}
  16. void cdef(){color(color_default);}
  17. void err(const char * message){color(color_red);std::cout << message;color(color_default);}
  18.  
  19.  
  20. template<typename T>
  21. bool get_bit(T &value, int index){
  22.     int targetbyte = index / 8,targetbit = index - targetbyte * 8;
  23.     char * byte = reinterpret_cast<char*>(&value) + targetbyte;
  24.     return ((*byte) & (1<<targetbit)) >> targetbit;
  25. }
  26.  
  27.  
  28. template<typename T>
  29. void print_bit(T &value){
  30.     const int size = sizeof(T);
  31.  
  32.     for(int i = size*8-1;i>=0;i--){
  33.         std::cout << (get_bit<int>(value,i) ? '1' : '0');
  34.     }
  35. }
  36.  
  37. template<typename T>
  38. void print_bit(T &value, int bitcount){
  39.     for(int i = bitcount;i>=0;i--){
  40.         std::cout << (get_bit<int>(value,i) ? '1' : '0');
  41.     }
  42. }
  43.  
  44. template<typename T>
  45. bool get_highest_bit(T &value){
  46.     const int size = sizeof(T);
  47.  
  48.     for(int i = size*8-1;i>=0;i--){
  49.         if(get_bit<int>(value,i)){
  50.             return i;
  51.         }
  52.     }
  53.  
  54.     return 0;
  55. }
  56.  
  57. template<typename T>
  58. std::string to_binary(T &value){
  59.     const int size = sizeof(T);
  60.     std::string result = "";
  61.     int i;
  62.  
  63.     for(int i = size*8-1;i>=0;i--){
  64.         result+=(get_bit<int>(value,i) ? '1' : '0');
  65.     }
  66.  
  67.     return result;
  68. }
  69.  
  70.  
  71. void write_number_in_string(int number, std::string &str,int frompos){
  72.     char buffer[20];
  73.     sprintf(buffer,"%d",number);
  74.     int i = 0;
  75.     while(buffer[i]!='\0'){
  76.         str[frompos++]=buffer[i++];
  77.     }
  78. }
  79.  
  80. struct piles_stat{
  81.     int maxheapsize = 0;
  82.     int maxheapindex = 0;
  83.     int anyindex = 0; //Egy random kupac index, amin biztos van tegla
  84.     int countofpiles = 0; //aktiv kupacok szama
  85.     int countofones = 0;// egyes kupacok szama
  86.     int xorval = 0;
  87. };
  88.  
  89.  
  90.  
  91. class game_space{
  92. public:
  93.     friend class game_manager;
  94.     game_space(int size):piles(size,0),pilecount(size){}
  95.  
  96.     //0 ha ervenytelen lepes, kulonben 1
  97.     bool remove(const int pileindex, const int count){
  98.         if(pileindex<0 || pileindex >= pilecount || count < 0 || count > this->piles[pileindex]){
  99.             #ifdef DEBUG
  100.                 err("Ervenytelen lepes!, remove ");
  101.                 std::cout << pileindex << " ,count: "  << count << std::endl;
  102.                 std::cout << pileindex << " oszlopban osszesen : "<< piles[pileindex] << " db stone van\n";
  103.                 exit(-1);
  104.             #endif
  105.             return false;
  106.         }
  107.  
  108.         piles[pileindex]-=count;
  109.         return true;
  110.     }
  111.  
  112.  
  113.     //Legnagyobb helyiertek index
  114.     //A legnagyobb helyierteku setelt bit, tehat a legnagyobb segment meret
  115.     int get_highest_pile_segment_index(){
  116.         int highest = 0;
  117.         for(int elem : piles){
  118.             int h =get_highest_bit(elem);
  119.             if(h>highest){
  120.                 highest = h;
  121.             }
  122.         }
  123.         return highest;
  124.     }
  125.  
  126.     void print_parity_binary(){
  127.         std::cout << "#Binaris reprezentacio paritasa:\n";
  128.         int x = get_xor();
  129.         std::cout << "to_binary xor: " << to_binary(x);
  130.         int highest = get_highest_pile_segment_index();
  131.         std::cout << "\nhighest pile bit index: " << highest << "\n";
  132.         std::cout << "Roviditve: ";
  133.         print_bit(x,highest);
  134.         std::cout << "\n - - - - - \n";
  135.     }
  136.  
  137.     //TODO
  138.     piles_stat getStatistic(){
  139.         piles_stat stat;
  140.        
  141.         for(int i = 0; i < pilecount; i++){
  142.             int currpile = piles[i];
  143.  
  144.             stat.xorval^=piles[i];
  145.             if(currpile>0){
  146.                 stat.countofpiles++;
  147.                 if(currpile==1)stat.countofones++;
  148.  
  149.                 if(currpile > stat.maxheapsize){
  150.                     stat.maxheapindex=currpile;
  151.                     stat.maxheapindex = i;
  152.                     stat.anyindex = i;
  153.                 }
  154.             }
  155.         }
  156.  
  157.         return stat;
  158.     }
  159.  
  160.     int get_xor() const{
  161.         int ret = 0;
  162.         std::for_each(piles.begin(),piles.end(),[&](const int elem){ret^=elem;});
  163.         return ret;
  164.     }
  165.  
  166.     int get_count_in_pile(const int pileindex) const{
  167.         #ifdef DEBUG
  168.         if(pileindex<0 || pileindex >= pilecount){
  169.             err("Ervenytelen pileindex, getcountinpile");
  170.             std::cout << pileindex;
  171.             exit(-1);
  172.         }
  173.         #endif
  174.         return piles[pileindex];
  175.     }
  176.  
  177.  
  178.     int get_count_of_stones() const{
  179.         return std::accumulate(piles.begin(),piles.end(),0);
  180.     }
  181.  
  182.     int get_size() const{
  183.         return this->pilecount;
  184.     }
  185.  
  186.     int get_max() const {
  187.         return *std::max_element(piles.begin(),piles.end());
  188.     }
  189.  
  190.  
  191.     void generate_random_space(const int minheapsize,const int maxheapsize){
  192.         srand(time(NULL));
  193.         int len = maxheapsize - minheapsize;
  194.         for(int i = 0; i < pilecount;i++){
  195.             int randvalue = rand() % (len+1);
  196.             piles[i] = minheapsize + randvalue;
  197.         }
  198.     }
  199.  
  200.     void print_info(){
  201.         for(int i = 0; i < pilecount;i++){
  202.             std::cout << i+1 << " : " << piles[i] << "\n";
  203.         }
  204.         print_parity_binary();
  205.         std::cout << "\n";
  206.     }
  207.  
  208.     //Csak a ter kerul kirajzolasra
  209.     void print_space(){
  210.         const std::string margin = "     ";
  211.  
  212.         int rows = get_max();
  213.         std::string lines[rows];
  214.  
  215.         //init
  216.         for(int i = 0; i < rows;i++){
  217.             lines[i]="";
  218.             for(int j = 0; j < pilecount*2;j++){
  219.                 lines[i]+=margin;
  220.             }
  221.         }
  222.  
  223.         for(int i = 0; i < pilecount;i++){
  224.             int index = i*margin.size()+i;
  225.             lines[rows-1][index+1] = '|';
  226.             write_number_in_string(i+1,lines[rows-1],index+2);
  227.             for(int j = 0; j < piles[i];j++){
  228.                 lines[rows-1-j][index] = 'O';
  229.             }
  230.         }
  231.  
  232.         std::cout << "\n";
  233.         color(color_green);
  234.         for(int i = 0; i < rows;i++){
  235.             std::cout << lines[i] << "\n";
  236.         }        
  237.         cdef();        
  238.     }
  239.  
  240. private:
  241.     int pilecount;
  242.     std::vector<int> piles;
  243. };
  244.  
  245.  
  246. struct move{
  247.     move() = default;
  248.     move(const int pileindex, const int stonecount):pindex(pileindex),scount(stonecount){}
  249.     int pindex;
  250.     int scount;
  251. };
  252.  
  253. std::ostream& operator<<(std::ostream &os, const move &m){
  254.     os << "oszlop: " << m.pindex+1 << ", " << m.scount << " darab";
  255.     return os;
  256. }
  257.  
  258. //player interface
  259. class player{
  260. public:
  261.     virtual ~player() {}
  262.     virtual move get_move(const game_space&p) = 0;
  263.     virtual bool is_wanted(){return false;}
  264. };
  265.  
  266. class console_player : public player{
  267. public:
  268.     console_player(){}
  269.     virtual move get_move(const game_space &p){
  270.         int from,count;
  271.  
  272. #ifdef DEBUG
  273.         std::cout << "console_player: Melyik oszlopbol? ";
  274. #endif
  275.  
  276.         std::cin>>from;
  277.  
  278. #ifdef DEBUG
  279.         std::cout << "console_player: Hany darabot? ";
  280. #endif
  281.         std::cin>>count;
  282.  
  283.         from--;
  284.         return move{from,count};
  285.     }
  286.  
  287.     virtual ~console_player(){}
  288. };
  289.  
  290.  
  291. // teljesen random jatszik
  292. // TODO
  293. class random_player : public player{
  294. public:
  295.  
  296. };
  297.  
  298.  
  299. // - - - -- - - --------- --- - - ------ - -  ----- -- - - - -
  300.  
  301.  
  302. class smart_player : public player{
  303. public:
  304.     smart_player(){}
  305.     virtual move get_move(const game_space &p){
  306.         int currentxor = p.get_xor();
  307.        
  308.     }
  309.  
  310. private:
  311.  
  312.  
  313. };
  314.  
  315.  
  316. class game_manager{
  317. public:
  318.     game_manager(player * MYPLAYER, player * OTHERPLAYER, game_space &space):space(space),itsme(MYPLAYER),enemy(OTHERPLAYER){}
  319.  
  320.     void start_local(){
  321.         bool whomove =  itsme->is_wanted();
  322.  
  323.         if(whomove){
  324.             std::cout << "A jatekot mi kezdjuk\n";
  325.         }else{
  326.             std::cout << "A jatekot az ellenfel kezdi\n";
  327.         }
  328.  
  329.         while(space.get_count_of_stones()){
  330.             std::cout << "\n S- - - - - - - - - - - \n\n";
  331.             space.print_info();
  332.             space.print_space();
  333.  
  334.             move m;
  335.             if(whomove){
  336.                 m=itsme->get_move(space);
  337.             }else{
  338.                 m=enemy->get_move(space);
  339.             }
  340.            
  341.             color(color_yel);
  342.             std::cout << "~Lepes: " << m << "\n";
  343.             cdef();
  344.  
  345.             space.remove(m.pindex,m.scount);
  346.             whomove = !whomove;
  347.         }
  348.  
  349.         color(color_yel);
  350.         if(!whomove){
  351.             std::cout << "\n [ WIN ]\n";
  352.         }else{
  353.             std::cout << "\n [ LOOSE ]";
  354.         }
  355.         std::cout << "\n Jatek vege \n";
  356.     }
  357.  
  358.     void start_graphi(){
  359.  
  360.     }
  361.  
  362. private:
  363.     game_space &space;
  364.     player * itsme;
  365.     player * enemy;
  366. };
  367.  
  368.  
  369. void _game1(){
  370.     player * p1 = new console_player();
  371.     player * p2 = new console_player();
  372.     game_space s(5);
  373.  
  374.     s.generate_random_space(1,7);
  375.    
  376.     game_manager m(p1,p2,s);
  377.  
  378.     m.start_local();
  379.  
  380.     delete p1;
  381.     delete p2;
  382. }
  383.  
  384. int main(){
  385.     _game1();
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement