Daily Diaries Of CTK

.

Thứ Bảy, 3 tháng 9, 2016

Lap trinh game tho va rua tren man hinh console


#include<iostream>
#include<ctime>
#include<cstdlib>
#include<string>

using namespace std;

void delay( float sec ){ // cho nay dung float or int deu dc.
 clock_t be, fin;
 be = clock();
 fin = be;
 while( fin - be <= sec )
  fin = clock();
}

void tho( int &t, int & n, int &toi_dich_t, string &str ){
 n= rand() %10 + 1;
 if( n == 4 ){
  str[t] = '.';
  t += 9;
  if( t >= 70 ) t = 70, toi_dich_t = 1;
  str[t] = 'T';
 }
 if( n == 5 ){
  str[t] = '.';
  t -= 12;
  if( t < 1 ) t = 1;
  str[t] ='T';
 }
 
 if( n == 6 || n == 7 || n == 8 ){
  str[t] = '.';
  t += 1;
  if( t >= 70 ) t = 70, toi_dich_t = 1;
  str[t] = 'T';
 }
 if( n == 9 || n == 10 ){
  str[t] = '.';
  t-= 2;
  if( t < 1 ) t = 1;
  str[t] = 'T';
 }
}

void rua( int &r, int & n, int &toi_dich_r, string &str ){
 n = rand() % 10 + 1;
 if( n <= 5 ){
  str[r] = '.';
  r+= 3;
  if( r >= 70 ) r = 70, toi_dich_r = 1;
  str[r] = 'R';
 }
 if( n == 6 || n == 7 ){
  str[r] = '.';
  r -= 6;
  if( r < 1 ) r= 1;
  str[r] ='R';
 }
 
 if( n == 8 || n == 9 || n == 10 ){
  str[r] = '.';
  r+= 1;
  if( r>= 70 ) r = 70, toi_dich_r = 1;
  str[r] = 'R';
 }
}

void tho_rua(){
 srand( time ( 0 ) );
 int t, r, toi_dich_t, toi_dich_r, n;
 t = r = 1; toi_dich_t = toi_dich_r = 0;
 string str= " "; str+= 'O';
 for( int i = 1; i < 70; i++ ) str += '.';
 cout<<"\t\t\t--------- Bang ----------"<< endl;
 cout<<"\t\t\t------- Ouch!!! -------";
 cout<< endl << str;
 while( toi_dich_t == 0 && toi_dich_r == 0 ){
  rua( r, n, toi_dich_r, str );
  tho( t, n, toi_dich_t, str );
  if( t == r ){
   cout<<"\t\t\t------- Ouch!!! -------";
   str[r] = 'O';
   cout<< endl << str; // in ra vi tri hai ae can nhau.
  }
  if( toi_dich_t == 0 && toi_dich_r == 0 && ( r != t ) ){
   str[t] = 'T'; str[r] = 'R'; // dat lai vi tri rua, tho.
   cout<< endl << str;
  }
  if( toi_dich_t == 1 && toi_dich_r == 1 ){
   cout<< endl << str;
   cout<<"\t\t\t------------ Tho & Rua Dong Hang! ------------";
  }
  if( toi_dich_t == 1 ){
   cout<< endl << str;
   cout<<"\t\t\t-----------Tho Thang! -----------";
  }
  if( toi_dich_r == 1 ){
   cout << endl <<str;
   cout<<"\t\t\t-----------Rua Thang! -----------";
  }
  delay( 100 );
 }
}

int main(){
 tho_rua();
 cout<< endl;
 return 0;
}




0 nhận xét:

Đăng nhận xét