Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

NetHack death statistics

This shell script reads in a NetHack logfile & outputs a list of all the things that have killed you, with their frequencies.

This script was originally posted to rec.games.roguelike.nethack by Faux_Pseudo.
   1  grep killed /usr/games/lib/nethackdir/logfile | awk -F killed '{print"killed"$NF}' | sort | uniq -c | sort -r

Dice roller

Given a di(c)e roll in standard d20 format on the command line, this script will roll the dice & output the result.
   1  
   2  #!/usr/bin/perl -wl
   3  use strict;
   4  $_ = shift or die 'Invalid argument passed';
   5  s/\s+//g;
   6  /^(\d+)[dD](\d+)([+-]\d+)?$/ or die 'Invalid argument passed';
   7  my $sum = 0;
   8  $sum += 1 + int rand $2 for 1..$1;
   9  $sum += $3 if defined $3;
  10  print $sum;

Go game

Algun juego que alguna ves realice... llamado go, pero que ya no recuerdo.

   1  
   2  #include <stdio.h>
   3  #include <stdlib.h>
   4  #include <time.h>
   5  
   6  
   7  int menu(){
   8  
   9      int select=0;
  10      do{
  11          printf("Menu:\n");
  12          printf("1. Score\n");
  13          printf("2. Jugar\n");
  14          printf("3. Salir\n");
  15          printf("Entre la opcion:\n");
  16          scanf("%i",&select);
  17      }while(!(select>0 && select<4));
  18    
  19      return select;    
  20  }
  21  
  22  void mostrar_score(int puntaje){
  23       //Si puntaje <0 entonces no agrega jugador
  24       //Sino puede agregarlo
  25  }
  26  
  27  void jugada_maquina(int **tablero, int f, int c){
  28  	 int i,j;
  29  	 do{
  30  	 	 i=rand()%f;
  31  	 	 j=rand()%c;
  32  	 } while (!tablero[i][j]==0);
  33  	 tablero[i][j]=2;	 
  34  }
  35  
  36  void mostrar_tablero(int **tablero,int f, int c){
  37  	 int i,j;
  38  	 for (i=0;i<f;i++){
  39  	 	 for (j=0;j<c;j++){
  40  
  41  		 	 switch(tablero[i][j]){
  42  		   	 			 case 0: printf("*");break;
  43  			 			 case 1: printf("X");break;
  44  						 case 2: printf("O");break;			 					   
  45  		     }
  46  	     }	
  47  		 printf("\n");
  48       }	 	 
  49  }
  50           
  51  
  52  int evaluar(int **tablero,int f,int c){
  53  	int i,j;
  54  	int cambio=0;
  55  	int tablas=1;
  56     	int gana=0;
  57  	do{
  58  	   cambio=0;
  59  		for (i=0;i<f;i++){
  60  			for (j=0;j<c;j++){
  61  				if(tablero[i][j]==3){
  62  					if (i-1>0)
  63  					   if (tablero[i-1][j]==1)
  64  					   	  tablero[i-1][j]=3,cambio=1;
  65  					if (i+1<f)
  66  					   if (tablero[i+1][j]==1)
  67  					   	  tablero[i+1][j]=3,cambio=1;
  68  					if (j-1>0)
  69  					   if (tablero[i][j-1]==1)
  70  					   	  tablero[i][j-1]=3,cambio=1;
  71  					if (i+1<c)
  72  					   if (tablero[i][j+1]==1)
  73  					   	  tablero[i][j+1]=3,cambio=1;											 
  74  			    } else if (tablero[i][j]==1){
  75  					if (i-1>0)
  76  					   if (tablero[i-1][j]==0)
  77  					   	  tablero[i][j]=3,cambio=1;
  78  					if (i+1<f)
  79  					   if (tablero[i+1][j]==0)
  80  					   	  tablero[i][j]=3,cambio=1;
  81  					if (j-1>0)
  82  					   if (tablero[i][j-1]==0)
  83  					   	  tablero[i][j]=3,cambio=1;
  84  					if (i+1<c)
  85  					   if (tablero[i][j+1]==0)
  86  					   	  tablero[i][j]=3,cambio=1;				  	   
  87  			    }
  88  			}	
  89  		}
  90  	}while(cambio);
  91  
  92  		for (i=0;i<f;i++){
  93  			for (j=0;j<c;j++){
  94  				if (tablero[i][j]==1)
  95  				   gana=1;
  96  			}
  97  		}
  98  	   for (i=0;i<f;i++){
  99  		  for (j=0;j<c;j++){
 100  			if(tablero[i][j]==3)
 101  			  tablero[i][j]=1;
 102  		  }
 103  	   }		   			 
 104  
 105  		if (gana==1){
 106  		   return 2;
 107          }
 108  
 109     	gana=0;	
 110  	do{
 111  	   cambio=0;
 112  		for (i=0;i<f;i++){
 113  			for (j=0;j<c;j++){
 114  				if(tablero[i][j]==4){
 115  					if (i-1>0)
 116  					   if (tablero[i-1][j]==2)
 117  					   	  tablero[i-1][j]=4,cambio=1;
 118  					if (i+1<f)
 119  					   if (tablero[i+1][j]==2)
 120  					   	  tablero[i+1][j]=4,cambio=1;
 121  					if (j-1>0)
 122  					   if (tablero[i][j-1]==2)
 123  					   	  tablero[i][j-1]=4,cambio=1;
 124  					if (i+1<c)
 125  					   if (tablero[i][j+1]==2)
 126  					   	  tablero[i][j+1]=4,cambio=1;											 
 127  			    } else if (tablero[i][j]==2){
 128  					if (i-1>0)
 129  					   if (tablero[i-1][j]==0)
 130  					   	  tablero[i][j]=4,cambio=1;
 131  					if (i+1<f)
 132  					   if (tablero[i+1][j]==0)
 133  					   	  tablero[i][j]=4,cambio=1;
 134  					if (j-1>0)
 135  					   if (tablero[i][j-1]==0)
 136  					   	  tablero[i][j]=4,cambio=1;
 137  					if (i+1<c)
 138  					   if (tablero[i][j+1]==0)
 139  					   	  tablero[i][j]=4,cambio=1;				  	   
 140  			    }
 141  			}	
 142  		}
 143  	}while(cambio);	
 144  
 145  		for (i=0;i<f;i++){
 146  			for (j=0;j<c;j++){
 147  				if (tablero[i][j]==2)
 148  				   gana=1;
 149  			}
 150  		}
 151  	   for (i=0;i<f;i++){
 152  		  for (j=0;j<c;j++){
 153  			if(tablero[i][j]==4)
 154  			  tablero[i][j]=2;
 155  		  }
 156  	   }		   			 
 157  		if (gana==1){
 158  		   return 1;
 159          }
 160  
 161  
 162  	
 163     for (i=0;i<f;i++){
 164  	  for (j=0;j<c;j++){
 165  		if(tablero[i][j]==0)
 166  		  tablas=0;
 167          }
 168  	}		   	
 169  	
 170  	if (tablas==1)
 171  	   return 4;		 
 172  
 173  	return 0;	
 174  }
 175  
 176  
 177  int jugar(){
 178      int i,j,f,c;
 179      int **tablero;
 180      int puntaje=0;
 181      
 182      printf ("Entre el numero de filas y columnas del tablero:\n");
 183      scanf("%i%i",&f,&c);
 184  
 185      tablero = (int**)malloc(f*sizeof(int*));
 186      for (i=0;i<f;i++){
 187          tablero[i]= (int*)malloc(c*sizeof(int)); 
 188          for (j=0;j<c;j++){
 189  			tablero[i][j]=0;	
 190  		 }
 191      }
 192      
 193      
 194      do{
 195          int ni,nj;
 196         
 197          mostrar_tablero(tablero,f,c);
 198          do{
 199             printf("Entre el numero de la coordenada:\n");
 200             scanf("%i%i",&ni,&nj);
 201             if (ni==0 && nj==0){
 202                printf("Desea Salir (s/n)\n");
 203                if(getch()=='s'){
 204  			      for (i=0;i<f;i++){
 205  			          free(tablero[i]);
 206  			      }
 207  				  free(tablero);
 208                    return -1;
 209  			  } else continue;
 210             }    
 211             ni--;nj--;
 212  		   if(!(ni>=0 && ni<f && nj>=0 && nj<c)){
 213  		   		   printf("La coordenana esta fuera de los limites\n");
 214  		   		   continue;
 215  		   }
 216  		   if (tablero[ni][nj]!=0){
 217  		   	  		printf("La coordenada colocada esta ocupada\n");	   
 218  		   	  		continue;
 219  		   }
 220  		   break;
 221          }while (1);
 222          
 223          tablero[ni][nj]=1;
 224          if(evaluar(tablero,f,c)==0){
 225  		        jugada_maquina(tablero,f,c);
 226          }else {
 227  	    //gano el humano
 228  	    }        
 229          puntaje=evaluar(tablero,f,c);
 230          printf("puntaje: %i\n",puntaje);
 231      }while(puntaje==0);
 232      
 233      for (i=0;i<f;i++){
 234          free(tablero[i]);
 235      }
 236      free(tablero);
 237     
 238  }
 239  
 240  int main(){
 241      int salir=0;
 242      do{
 243  
 244          switch (menu()){
 245                 case 1: 
 246                      mostrar_score(-1);
 247                      break;
 248                 case 2:
 249                      mostrar_score(jugar());
 250                      break;
 251                 case 3:
 252                      salir=1;    
 253                      break;
 254          }
 255      }while(!salir);
 256  
 257      printf("Gracias por utilizar el programa");
 258      
 259      return 0;
 260  }
 261  
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS