Aller au contenu. | Aller à la navigation

Outils personnels

Navigation
Vous êtes ici : Accueil / Didaktik der Informatik / Hai-Qualle-Programm

Hai-Qualle-Programm

C source code icon shark.c — C source code, 1 ko (1231 bytes)

Contenu du fichier

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
  char f[100][100],state[100],r[3];
  int xs[100],ys[100], q,n,h,i,j,hl;
  srandom((long int)time(NULL));
  printf("Groesse Haie Quallen:");
  scanf("%i %i %i",&n,&h,&q);
  for (i=0;i<h+q+1;i++) {xs[i]=random()%n; ys[i]=random()%n; state[i]=i<h+1?'H':'Q';}
  state[0]='S';
  hl=h;
  while (hl>0)
  {
    for (i=0;i<n;i++) {for (j=0;j<n;j++){f[i][j]='.';}}
    for (i=0;i<q+h+1;i++){if (state[i]!='.'){f[xs[i]][ys[i]]=state[i];}}
    for (i=0;i<n;i++) {printf("\n");for (j=0;j<n;j++){printf("%c",f[j][i]);}}
    printf("\n\n Richtung:");
    scanf("%s",r);
    ys[0]+=(ys[0]<n-1)*(r[0]=='j')-(ys[0]>0)*(r[0]=='k');
    xs[0]+=(xs[0]<n-1)*(r[0]=='l')-(xs[0]>0)*(r[0]=='h');  
    for (i=1;i<h+1;i++)
    {
          xs[i]+=(xs[0]>xs[i])-(xs[0]<xs[i]);
          ys[i]+=(ys[0]>ys[i])-(ys[0]<ys[i]);
    }
    for (i=0;i<h+1;i++) 
    { 
      for(j=i+1;j<q+h+1;j++)
      {
        if (xs[i]==xs[j] && ys[i]==ys[j])
        {
          if (state[j]!='.' && state[i]!='.')
          {
            state[i]='.';
            if (i==0) {hl=-100;} else {hl--;}
          }
        }
      }
    } 
  }
  printf(hl<=-100?"Verloren\n":"Gewonnen\n");  
  return(0);
}