All pastes #46705 Raw Edit

Something

public text v1 · immutable
#46705 ·published 2006-03-23 17:41 UTC
rendered paste body
/* robots.c
 * ein einfaches Spiel, mit Text-Graphik realisiert ("rogue-like")
 *
 *
 * B.Glavina, 9.12.2005 (nur Programm-Rahmen)
 */

#include <stdio.h>
#include <string.h>

#define bool int
#define false 0
#define true 1

/* unsere Welt besteht aus einem Rechteck mit */
/* Breite XRANGE und Hoehe YRANGE; links unten ist Zelle (0;0) */
#define XRANGE 50
#define YRANGE 24

/* die Welt kann (nur) folgende Objekte enthalten: */
#define EMPTY '.'
#define WALL '#'
#define ME '@'
#define ROBOT 'R'
#define JUNK 'X'


void init_world(char world[XRANGE][YRANGE], int level)
/* fuelle Welt mit Waenden und Robotern */
{
  int x, y;

  for (x=0; x<XRANGE; x++)
    for (y=0; y<YRANGE; y++)
      world[x][y] = WALL;
  for (x=1; x<XRANGE-1; x++)
    for (y=1; y<YRANGE-1; y++)
      world[x][y] = EMPTY;
  switch (level)
  {
  case 0: /* test level */
    world[5][5] = ROBOT;
    world[10][5] = ROBOT;
    break;
  default: /* nothing */
    break;
  } /* end switch */
}


void show_world(char world[XRANGE][YRANGE])
{
  int x, y;


for (y=0;y<YRANGE;y++)
{if (y!=YRANGE)//Damit am schluss keine Leerzeile erzeugt wird
printf("\n");
    for (x=0;x<XRANGE;x++)
    {
    printf("%c",world[x][y]);
    }    
}
  printf("\n\n");

//  printf("[[ hier soll das Spielfeld angezeigt werden ]]\n");

  return;
}


void beep(void)
{
  printf("\a");
  return;
}


void move_robots(char world[XRANGE][YRANGE], 
                 int goal_x, int goal_y, int *n_rob)
{

  printf("[[ jetzt ziehen die Roboter ... ]]\n");
  *n_rob = 1; /* damit main nicht auf undefinierten Wert zugreift */

  return;
}

void compute_random_free_cell(char w[XRANGE][YRANGE], int *x, int *y)
{
int xempty;
int yempty;
srand( time(0));
do {
    xempty=(rand() % XRANGE + 1);
    yempty=(rand() % YRANGE + 1);
} 
while (w[xempty][yempty]!=EMPTY);

*x=xempty;
*y=yempty;
 
}



int main(void)
{
  char world[XRANGE][YRANGE]; /* Spielfeld */
  int me_x, me_y; /* Position des Spielers */
  char commandline[10], command, msg[100];
  int level;
  bool alive, ok;
  int number_of_robots;
  int *x;
  int *y;
  
  printf("Robots, ein geniales Produkt von Jan Knobloch\n");
  printf("\n");
  printf("Start-Level? ");
  scanf("%d", &level);
  init_world(world, level);
  me_x = XRANGE/2; me_y = YRANGE/2;
  world[me_x][me_y] = ME;
  alive = true;

  do
  {
    show_world(world);
    printf("Now what? (h for help) >> ");
    gets(commandline);
    command = commandline[0];
    ok = false;
    switch (command)
    {
    case '2': /* go south */
      if (world[me_x][me_y+1]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_y++;
        world[me_x][me_y] = ME;
        ok = true;
      }
      break;
    
    case '4': /* go west */
      if (world[me_x-1][me_y]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_x--;
        world[me_x][me_y] = ME;
        ok = true;
      }
      break;
      
    case '6': /* go east */
      if (world[me_x+1][me_y]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_x++;
        world[me_x][me_y] = ME;
        ok = true;
      }
      break;
    
    case '8': /* go north */
      if (world[me_x][me_y-1]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_y--;
        world[me_x][me_y] = ME;
        ok = true;
      }    
      break;
      
      case '7': /* go northwest */
      if (world[me_x-1][me_y-1]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_y--;
        me_x--;
        world[me_x][me_y] = ME;
        ok = true;
      }    
      break;
      
      case '9': /* go northeast */
      if (world[me_x+1][me_y-1]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_y--;
        me_x++;
        world[me_x][me_y] = ME;
        ok = true;
      }    
      break;
      
      case '1': /* go southwest */
      if (world[me_x-1][me_y+1]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_y++;
        me_x--;
        world[me_x][me_y] = ME;
        ok = true;
      }    
      break;
      
      case '3': /* go southeast */
      if (world[me_x+1][me_y+1]==EMPTY)
      {
        world[me_x][me_y] = EMPTY;
        me_y++;
        me_x++;
        world[me_x][me_y] = ME;
        ok = true;
      }
      break;
      
      case '5': /* dont move */
      world[me_x][me_y] = ME;
      ok = true;
      break;   
      
      case '0': /* dont move */
      //compute_random_free_cell(world[XRANGE][YRANGE], *x, *y);
      world[me_x][me_y] = EMPTY;
      ok = true;
      break; 
      

      break;
    case 'h': /* Hilfe: Erklaerung moeglicher Eingaben */
      printf("\n");
      printf("Moegliche Eingaben sind\n");
      printf("x = Spiel abbrechen\n");
      printf("[12345679h<space> ebenfalls erklaeren]\n");
      printf("... usw.\n");
      printf("\n");
      printf("(RET druecken zum Weiterspielen)");
      gets(commandline);
      break;
    case 'x': /* Abbruch */
      alive = false;
      strcpy(msg, "Hey, is there really anything better than ROBOTS???");
      break;
    default:
      break;
    } /* end switch */
    if (ok)
    { 
      move_robots(world, me_x, me_y, &number_of_robots);
      if (world[me_x][me_y]!=ME)
      {
        alive = false; /* player is dead */
        strcpy(msg, "Sorry, loser! Have more luck next time.");
      }
      else if (number_of_robots==0)
      {
        alive = false; /* player has won */
        strcpy(msg, "Wow! You are a genuine robot buster.");
      }
    }
    else 
      beep(); /* invalid command */
  } while (alive);

  show_world(world);
  printf("\n");
  printf("%s\n", msg);
  printf("... exiting this exciting game ...\n");
  printf("\n");

  return 0;
}
/* end of file robots.c */