Chain Game in C++

Source Code in C++

1: #include <iostream>

 2: #include <cstdlib>
 3: 
 4: using namespace std;
 5: //------------------------------------------------------
 6: class Summetexon
 7: {
 8: private:
 9:     int itsid;
 10:     int itsthesi;
 11: public:
 12:     Summetexon(){;}
 13:     ~Summetexon(){;}
 14:     Summetexon *next;
 15:     void set_Summetexon(int id,int thesi);
 16:     void set_thesi(int thesi){itsthesi = thesi ;}
 17:     int get_thesi(){return itsthesi;}
 18:     int get_id(){return itsid;}
 19: };
 20: 
 21: class Alusida
 22: {
 23: private:
 24:     int itsmegethos;
 25:     int itsdiagrafon_node;
 26:     int itsdiagrafon_thesi;
 27:     int itsnode_arxis;
 28:     Summetexon *arxi;
 29: public:
 30:     Alusida(int megethos);
 31:     ~Alusida(){;}
 32:     Summetexon *nodes;
 33:     void hit();
 34:     void stop_tune();
 35:     void resume();
 36:     void print_chain();
 37:     int game_over();
 38:     void winner();
 39: 
 40: };
 41: 
 42: //----------------------------------------------------
 43: 
 44: void Summetexon::set_Summetexon(int id,int thesi)
 45: {
 46:     itsid=id;
 47:     itsthesi=thesi;
 48: }
 49: 
 50: //-----------------------------------------------------
 51: 
 52: Alusida::Alusida(int megethos)
 53: {
 54:     int i,id;
 55:     itsmegethos=megethos;
 56:     nodes = new Summetexon[megethos];
 57:     id=rand();
 58:     for(i=0;i<itsmegethos;i++)
 59:     {
 60:         if(i==itsmegethos -1)
 61:         {
 62:             nodes[i].set_Summetexon(id++,i+1);
 63:             break;
 64:         }
 65:         nodes[i].set_Summetexon(id++,i+1);
 66:     }
 67:     for(i=0;i<itsmegethos;i++)
 68:     {
 69:         if(i==itsmegethos -1)
 70:         {
 71:             nodes[i].next = &nodes[0];
 72:             break;
 73:         }
 74:         nodes[i].next = &nodes[i+1];
 75:     }
 76:     arxi = &nodes[0];
 77:     itsnode_arxis = 0;
 78: }
 79: 
 80: void Alusida::hit()
 81: {
 82:     int i,thesi;
 83:     for(i=0;i<itsmegethos;i++)
 84:     {
 85:         if(nodes[i].get_thesi() == 1)
 86:         {
 87:             nodes[i].set_thesi(itsmegethos);
 88:             i++;
 89:         }
 90:         thesi = nodes[i].get_thesi();
 91:         nodes[i].set_thesi(--thesi);
 92:     }
 93:     for(i=0;i<itsmegethos;i++)
 94:     {
 95:         if(nodes[i].get_thesi() == 1)
 96:         {
 97:             arxi=&nodes[i];
 98:             itsnode_arxis = i;
 99:         }
 100:     }
 101: }
 102: 
 103: void Alusida::stop_tune()
 104: {
 105:     itsdiagrafon_node = rand() % itsmegethos;
 106:     cout<<"Diagrafike o Summetexon me id:" << nodes[itsdiagrafon_node].get_id() << "\n\n";
 107:     itsdiagrafon_thesi = nodes[itsdiagrafon_node].get_thesi();
 108: 
 109:     itsmegethos--;
 110:     if(itsdiagrafon_thesi == 1)
 111:     {
 112:         arxi = &nodes[itsdiagrafon_node + 1];
 113:         itsnode_arxis=itsdiagrafon_node + 1;
 114:     }
 115: }
 116: 
 117: void Alusida::resume()
 118: {
 119: 
 120:     int i=0,j=0,thesi=1;
 121:     Summetexon *buf = new Summetexon[itsmegethos];
 122:     if(itsdiagrafon_thesi == 1)
 123:     {
 124: 
 125:         for(i=itsdiagrafon_node + 1;i<itsmegethos + 1;i++)
 126:         {
 127:             buf[j] = nodes[i];
 128:             buf[j].set_thesi(thesi++);
 129:             j++;
 130:         }
 131:         for(i=0;i<itsdiagrafon_node;i++)
 132:         {
 133:             buf[j] = nodes[i];
 134:             buf[j].set_thesi(thesi++);
 135:             j++;
 136:         }
 137:         for(i=0;i<itsmegethos;i++)
 138:         {
 139:             if(i == itsmegethos -1)
 140:             {
 141:                 buf[i].next = &buf[0];
 142:                 break;
 143:             }
 144:             buf[i].next = &buf[i+1];
 145:         }
 146:     }
 147:     if(itsdiagrafon_thesi == itsmegethos + 1)
 148:     {
 149:         for(i=itsdiagrafon_node + 1;i<itsmegethos+1;i++)
 150:         {
 151:             buf[j] = nodes[i];
 152:             j++;
 153:         }
 154:         for(i=0;i<itsdiagrafon_node;i++)
 155:         {
 156:             buf[j] = nodes[i];
 157:             j++;
 158:         }
 159:         for(i=0;i<itsmegethos;i++)
 160:         {
 161:             if(i == itsmegethos - 1)
 162:             {
 163:                 buf[i].next = &buf[0];
 164:                 break;
 165:             }
 166:             buf[i].next = &buf[i+1];
 167:         }
 168:     }
 169: 
 170:     if(itsdiagrafon_thesi != itsmegethos + 1 && itsdiagrafon_thesi != 1)
 171:     {
 172:         for(i=itsnode_arxis;i<itsmegethos + 1 && i<itsdiagrafon_node;i++)
 173:         {
 174:             buf[j] = nodes[i];
 175:             buf[j].set_thesi(thesi++);
 176:             j++;
 177:         }
 178:         if(i==itsnode_arxis)
 179:         {
 180:             for(i=i;i<itsmegethos + 1;i++)
 181:             {
 182:                 buf[j] = nodes[i];
 183:                 buf[j].set_thesi(thesi++);
 184:                 j++;
 185:             }
 186:             for(i=0;i<itsdiagrafon_node;i++)
 187:             {
 188:                 buf[j]=nodes[i];
 189:                 buf[j].set_thesi(thesi++);
 190:                 j++;
 191:             }
 192:             for(i=itsdiagrafon_node+1;i<itsnode_arxis;i++)
 193:             {
 194:                 buf[j]=nodes[i];
 195:                 buf[j].set_thesi(thesi++);
 196:                 j++;
 197:             }
 198: 
 199:         }
 200:         if(i==itsmegethos+1)
 201:         {
 202:             for(i=0;i<itsdiagrafon_node;i++)
 203:             {
 204:                 buf[j]=nodes[i];
 205:                 buf[j].set_thesi(thesi++);
 206:                 j++;
 207:             }
 208:             for(i=itsdiagrafon_node+1;i<itsnode_arxis;i++)
 209:             {
 210:                 buf[j]=nodes[i];
 211:                 buf[j].set_thesi(thesi++);
 212:                 j++;
 213:             }
 214:         }
 215:         else if(i==itsdiagrafon_node)
 216:         {
 217:             for(i=itsdiagrafon_node+1;i<itsmegethos +1;i++)
 218:             {
 219:                 buf[j]=nodes[i];
 220:                 buf[j].set_thesi(thesi++);
 221:                 j++;
 222:             }
 223:             for(i=0;i<itsnode_arxis;i++)
 224:             {
 225:                 buf[j]=nodes[i];
 226:                 buf[j].set_thesi(thesi++);
 227:                 j++;
 228:             }
 229: 
 230:         }
 231:         for(i=0;i<itsmegethos;i++)
 232:         {
 233:             if(i == itsmegethos -1)
 234:             {
 235:                 buf[i].next = &buf[0];
 236:                 break;
 237:             }
 238:             buf[i].next = &buf[i+1];
 239:         }
 240:     }
 241: 
 242:     for(i=0;i<itsmegethos;i++)
 243:     {
 244:         nodes[i] = buf[i];
 245:     }
 246: 
 247:     arxi = &nodes[0];
 248:     itsnode_arxis = 0;
 249: }
 250: 
 251: int Alusida::game_over()
 252: {
 253:     if(itsmegethos == 1)
 254:         return 1;
 255:     else
 256:         return 0;
 257: }
 258: 
 259: void Alusida::print_chain()
 260: {
 261:     int i;
 262:     for(i=0;i<itsmegethos;i++)
 263:     {
 264:         cout << "Summetexon stin thesi "<<nodes[i].get_thesi()<<" me id: "<<nodes[i].get_id()<<"\n";
 265:     }
 266: }
 267: 
 268: void Alusida::winner()
 269: {
 270:     if(game_over() == 1)
 271:         cout <<"To paixnidi teleiose!\n********************\nThe Winner is Summetexon with ID:"<<nodes[0].get_id()<<"\n********************\n\n";
 272:     else
 273:         cout<<"To paixnidi den teleiose akoma!\n\n";
 274: }
 275: 
 276: 
 277: 
 278: /********************************************************************/
 279: 
 280: void main(void)
 281: {
 282:     int i,k,plithos;
 283:     cout << "Doste to plithos ton summetexonton:\n";
 284:     cin >> plithos;
 285:     Alusida myAlusida(plithos);
 286:     cout << "ARXIKH ALYSIDA.\n";
 287:     myAlusida.print_chain();
 288:     cout << "***************\n\n";
 289:     for(i=0;i<plithos-1;i++)
 290:     {
 291:         for(k=0;k<rand() % 10;k++)
 292:         {
 293:             cout<<"->HIT\n";
 294:             myAlusida.hit();
 295:             myAlusida.print_chain();
 296:             cout << "\n";
 297:         }
 298:         cout << "->STOP TUNE\n";
 299:         myAlusida.stop_tune();
 300:         cout << "->RESUME\n";
 301:         myAlusida.resume();
 302:         myAlusida.print_chain();
 303:         cout << "\n";
 304:     }
 305:     myAlusida.winner();
 306: 
 307: }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }