Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <conio.h>
  5. using namespace std;
  6. bool spelesBeigas;
  7. const int platums = 30;
  8. const int augstums = 16;
  9. int x,y,xAuglim,yAuglim,punkti,virziens, nAstei;
  10. int xAstei[1000];
  11. int yAstei[1000];
  12. void Uzstadijumi(){
  13.     spelesBeigas=false;
  14.     virziens =0;
  15.     x=15;
  16.     y=8;
  17.     xAuglim=rand()%30+1;
  18.     yAuglim=rand()%16+1;
  19.     punkti =0;
  20.     nAstei=0;
  21. }
  22. void Laukums(){
  23. system("cls");
  24. for(int i=0;i<platums+2;i++){
  25.     cout<<"#";
  26. }
  27. cout<<"\n";
  28.  
  29. for(int j=0;j<augstums;j++){
  30.     for(int k=0;k<platums;k++){
  31.         if(k==0){
  32.             cout<<"#";
  33.         }
  34.         if(j==y && k==x){
  35.             cout<<"D";
  36.         }
  37.         else{
  38.             if(j==yAuglim && k==xAuglim ){
  39.                 cout<<"A";
  40.             }else{
  41.                 bool druka=false;
  42.         for(int f=0; f<nAstei; f++){   
  43.         if(yAstei[f]==j && xAstei[f]==k){
  44.                 cout<<"o"; 
  45.                 druka=true;
  46.             }
  47.         }
  48.         if(druka==false) cout<<" ";
  49.     }
  50.  
  51.             }
  52.        
  53.         if(k==platums){
  54.             cout<<"#";
  55.         }
  56.     }
  57.     cout<<"\n";
  58. }
  59.  
  60. for(int i=0;i<platums+2;i++){
  61.     cout<<"#";
  62. }
  63. cout<<"\nTavs punktu skaits : "<<punkti;
  64.  
  65.  
  66. }
  67. void Taustins(){
  68.     if(_kbhit()){ //ja veikta darb?bas no tastat?ras
  69.         switch(_getch()){ //analiz? piespiest? tausti?a v?rt?bu
  70.             case 'a': virziens=1; break; //ja piespiests tausti?š 'a', virziens v?rt?bu uzst?da uz 1 (pa kreisi)
  71.             case 'd': virziens=2; break; //ja piespiests tausti?š 'd', virziens v?rt?bu uzst?da uz 2 (pa labi)
  72.             case 'w': virziens=3; break; //ja piespiests tausti?š 'w', virziens v?rt?bu uzst?da uz 3 (uz augšu)
  73.             case 's': virziens=4; break; //ja piespiests tausti?š 's', virziens v?rt?bu uzst?da uz 4 (uz leju)
  74.         }
  75.     }
  76.  
  77. }
  78. void Logika(){
  79.     if(virziens==1){
  80.         x=x-1;
  81.     }
  82.     else if(virziens==2){
  83.         x=x+1;
  84.     }
  85.     else if(virziens==3){
  86.         y=y-1;
  87.     }
  88.     else if(virziens==4){
  89.         y=y+1;
  90.     }
  91.    
  92.     if(x>=platums){
  93.         x=0;
  94.     }else if(x<0){
  95.         x=platums-1;
  96.     }
  97.     else if(y>=augstums){
  98.         y=0;
  99.     }else if(y<0){
  100.         y=augstums-1;
  101.     }
  102.     for(int i=0; i<nAstei; i++){
  103.     if(xAstei[i]==x && yAstei[i]==y) {
  104.     spelesBeigas=true;
  105.     }
  106.     }
  107.        
  108.  
  109.    
  110.    
  111.     if(x==xAuglim && y==yAuglim){
  112.         for(int i=nAstei; i>0; i--){
  113.         xAstei[i]=xAstei[i-1];
  114.         yAstei[i]=yAstei[i-1];
  115.     }
  116.         xAstei[0]=x;
  117.         yAstei[0]=y;
  118.  
  119.         punkti=punkti+10;
  120.         xAuglim=rand()%30+1;
  121.         yAuglim=rand()%16+1;
  122.         nAstei = nAstei + 1;
  123.     }
  124. }
  125. int main() {
  126.      srand(time(0));
  127.      Uzstadijumi();
  128.      while(spelesBeigas==false){
  129.         Laukums();
  130.         Taustins();
  131.         Logika();
  132.      }
  133.      cout<<"speles beigas!";
  134.      getch();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement