Suntuubi-palvelussa käytetään evästeitä. Palvelua käyttämällä hyväksyt evästeiden käytön. Lue lisää. OK

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>

#include <time.h>


unsigned int gamesave = 0;

unsigned short int x = 0;
char menuchoice = ' ';
char choice [15];
const char fname[30] = "C:\\textgamesave";


int menu (void);
void shoot (void);
void escapeatstart (void);
void startgame (void);
void loadgame (void);
void savegame (void);
void tanks (void);
void gamecontinue (void);


void cls (void) {
     system("cls");
     menu();
}


int main(int argc, char *argv[])
{
  srand((unsigned)time(0));
  menu();    
  return 0;
}


int menu (void) {
printf("GAME MENU\n\n");
printf("Press B to start a new game.\n");
printf("Press L to load an existing game.\n");
printf("Press S to save your game.\n");
printf("Press C to clear screen.\n");
printf("Press Q to quit.\n");
menuchoice = getch();
switch (menuchoice) {
       case 'B': case 'b':
            startgame();
            break;
       case 'L': case 'l':
            loadgame();
            break;
       case 'S': case 's':
            savegame();
            break;
       case 'Q': case 'q':
            return 0;
            break;
       case 'c': case 'C':
            system("cls");
            cls();
            break;
       default:
               printf("I cant understand your choice!\n");
               menu();
               break;
}
}


void startgame (void) {
system("cls");
printf("If you want return to menu, type Q anytime.\n");
printf("You are in WWII, at Berlin. The year is 1945 and russians are capturin the city!\n");
printf("You are a german civillian, 15 yers old boy.\n");
firstq:
printf("Do you want to defend the Berlin or trie to escape? E/D\n");
menuchoice = getch();
switch (menuchoice) {
       case 'e': case 'E':
            escapeatstart();
            break;
       case 'Q': case 'q':
            cls();
            break;
       case 'D': case 'd':
            tanks();
            break;
       default:
               printf("I dont understand your choice!\n");
               goto firstq;
               break;
}
}


void tanks (void) {
gamesave = 845;
saveone:
printf("So you decided to defend the city.\n");
printf("You have one shotgun and 45 rounds.\n");
printf("You see a russian tank, the enemy tanks have got through!!\n");
secondq:
printf("Theres a man on the tanks roof, at machine gun, do you want to trie to shoot him? S/D\n");
menuchoice = getch();
switch (menuchoice) {
       case 's': case 'S':
            shoot();
            break;
       case 'd': case 'D':
            puts("So you didnt trie to shoot him.");
            gamecontinue();
            break;
       case 'q': case 'Q':
            cls();
            break;
       default:
               printf("Invalid choice!!\n");
               goto secondq;
               break;
}
}


void gamecontinue (void) {
gamesave = 999;
puts("You survived!");
menu();
}


void savegame (void) {
  FILE *fp;
  printf("If you save game the old gamesave will be deleted unless you rename the savegame\n");
  puts("The savegame file is C:\\textgamesave\n");
  printf("Are you sure you want to remove the old savegame and create a new one? (Y/N)\n");
  menuchoice = getch();
  if ( (menuchoice = 'Y') && (menuchoice = 'y') ) {
       fp = fopen (fname, "w");
      fprintf( fp, "%d\n", gamesave);
}
  else {
  printf("Error!\n");
  fclose (fp);
  menu();
}
fclose (fp);
cls();
}


void loadgame (void) {
     FILE *fp;
     fp = fopen (fname, "r");
     fscanf(fp, "%d", &gamesave);
     switch (gamesave) {
            case 0:
                 startgame();
                 break;
            case 845:
                 tanks();
                 break;
            case 999:
                 gamecontinue();
                 break;
            default:
                    printf("Error while loading!");
                    menu();
                    break;
}
}


void escapeatstart (void)  {
    srand((unsigned)time(0));
    x = (int) (rand()% 2 + 1);
    if (x == 2) {
          printf("You were killed by a tank while escaping!\n");
          menu();
}
    if (x == 1) {
          printf("You were shot while escaping!\n");
          gamesave = 0;
          menu();
}
}


void shoot (void) {
x = (int) (rand()% 3 + 1);
if (x == 3) {
      puts("You got him!");
      gamecontinue();
}
else {
     puts("Enemy spotted you and killed you!");
     gamesave = 0;
     menu();
}
}


©2017 Ohjelmointi ja modaussivut - suntuubi.com