trying to return to the beginning of the program....

0 John Tompson · July 29, 2015
i have studied this c language and find no way to get BACK to the beginning of a program.

create character hero
walk around {infinite until life==0}
[don't want the program to end after making 1 step into a dungeon]: need to explore the rest of the map. data 1,1,2,1,1,1,1. read data. get a$. map(x,y). A$="hero's name"

need to return to the walk around section.
500 lines of code and 3 other loops separate needing to return to this section.
don't know how to do this without a "goto 10": ?"this is not possible with the 'continue' command...": A$="this c language is not that good of a language and takes more space (?print){}()"" to write and in program size)".

?"do you know how to solve this problem?";get how$

::i don't see how it is possible to fit 500 lines of code and 3 separate loops within a single "do while" or "if else" statement.
when a sample was run for mixing a for and if statement together for example, it completely ERASED the values of all of my variables within these brackets........



Post a Reply


- page 2
Oldest  Newest  Rating
0 c student · July 29, 2015
are you doing all 500 lines inside main?  if so, i'd strongly suggest you split your code into functions and into smaller functions again if it's appropriate.  call the required function within main and loop that.  something like this? (assume map origin (0, 0) from bottom left):

#define SUCCESSFUL 1

#define DUNGEONLEFTX ...
#define DUNGEONTOPY ...

typedef short position;
typedef struct player *Player;

struct player {
   char *name;
   short level;
   int **inventory;
   position posX;
   position posY;
   char *mapArea;

Player playerOne;

//update player position on map
void updateMapArea (Player p) {
   if ((p->posX >= DUNGEONLEFTX && p->posX < = DUNGEONRIGHTX)
        && (posY >= DUNGEONBOTTOMY && p->posY < = DUNGEONTOPY)) {
       p->mapArea = "Dungeon";
   } else if ...


// update player position
int walk (Player p, position x, position y) {
   p->posX += x;
   p->posY += y;
   updateMapArea (p);
   return SUCCESSFUL;

int main (void) {
   initializePlayer();    // set values into player
   while (1) {
       switch (keyboard) {    // fetch key pressed from keyboard
           case 'w':
               walk (playerOne, 1, 0);    // move up

not sure if this helped.  please provide an example or explain further.
0 Jon Z. · July 29, 2015
Probably use a loop.

//use 'continue' to skip the rest of the code in the loop and jump to the beginning

//use 'break' to exit the loop
0 John Tompson · September 2, 2015
hi jon,
nice to hear from you!

yes, i agree. you said to keep it "simple and clean" and "to keep them more organized"; so that is why i said header file. keep you program simple. all extra data is in the header files.
->okay, so it doesn't matter if the break is in an if statement, as long as it is within a do while. -->>SUCCESS
didn't realize that you could use it in an if statement. well, without a do while you cant use a break in a if. thanks!!!

#include <stdio.h>
#include <stdlib.h>
//i am putting all my data and void functions in a header file to clean up and simplify this program
//#include <headerFile.h>

int main()
    int turns =1000;
    int toDo = 1;
    int fight = 4;
    int run = 2;
    int takeANap = 3;
    int exit = 1;
    char action[20];

    do {
        printf("\n player takes their turn\n");
  //ask what to do
  if(toDo == fight){
        action[20]= "fight";
  printf("\n player fights a monster");
printf("\n\t player does not run");
printf("\n\t player does not stand there looking around as monster is beating him up");}

  else if(toDo==run) {
   printf("monster is gone");}

  else if(toDo==takeANap){
   printf("the earth is healed +1 HP");}

  else if(toDo==exit){
        printf("\n the world has ended and you stood around and did nothing");
   } while (turns!=1000);
  printf("\n program ends\n");

    return 0;

0 John Tompson · September 2, 2015
hey there c student:angel:
thanks for getting back to me.

1. do you know where i can look at c game code: how to find it so i can learn and get better **{from examples}&; && || model code.

yes, everyone can always learn more.
however, the sticking point is not the specific details, but rather the lack of possibility and probable error in the c language code itself, as the program always goes forward, but cannot go back: therefore the entire program is one giant loop with a bunch of other loops in between. as soon as you end the big loop, the program ends -- it doesn't keep going, nor can you get "back" into the loop. so i am trying to figure out a strategy to do this. i figure that i will use a model similar to what you have shown though.
the other difficulty is the lack of compatibility in the c language code for built in c language functions.

i really like how you simplified it though. you simplified it even further than i had been planning to do. i like that idea.
i didn't know that you could fetch a keyboard input outside of a for loop.

the map is a long rectangle because when i look at the code, the map will fit all on the same line, in that i can navigate the map with my eyes (and so i can see if there are any errors).
but i can fit more digits on the screen with c, so my map can be bigger (for the first peice {will add a bunch of peices together}): otherwise i risk error and a hero runs into a dead end and cannot complete the map {if there is an error}.

so with the sample game architecture that i have included, the entire game where a player will play for 100 hours is run with a single do while loop with 6 sub loops within a single loop.
//possible error: if i cannot find a way to implement this, then i will not be able to get back to (no goto) the "encounter monster section" (as the program keeps going forward and cannot go back several lines), unless i manually write 1000 encounter monster sections (as a single game does not have more encounters than this). or a do while within a do while loop. or a for statement with a do while inside of it (assuming this is possible): as in for turns = 1 to 100000; {{do (if encounter) {fight} while...}} next i (end for statement, i++)...

#include <stdio.h>
#include <stdlib.h>
#include <variables.h>
#include <mapData.h>
void print new coordinates of map on screen
main void fetch keyboard command

int main()
roll hero stats
      {if new hero->
          save temp hero stats
begin game
print map on screen
      {//fetch keyboard command
      {if xy!=same->
          walk across map
          //print new coordinates of map on screen
encounter monster
      {if monster->
          hit, miss, miss, hit
      {if earth HP==0
          hero killed;
defeat monster
gain treasure an XP
hero wins
      {if hero win->
    return 0;

friend programmer,

0 John Tompson · September 2, 2015
hi saket,
nice to meet.


awesome dude. i'd like to talk to bucky. he asked for advice on one of his tutorials and i was going to help him.
but he's so popular, why would he talk me? no one knows me and i'm not a pro at programming, and i know less than 4 programming languages.
anyways, i don't know HOW to talk to him, assumming he wasn't too busy to do so.

yes, i watched ALL of the tuts, some of them twice for review. but i have not read the book yet. guess that is the next step...
i'm not lookin to be a pro, just write my own game programs (think i'll shy from c# as it takes too long for 1 person to complete *{graphics}), and to get a good computer job (though it probably wont be as a programmer because i don't want to get carpel tunnel (unless a speech to text is allowed, and then i would take a programming job).
i want to be successful in life someday. tired of flippin burgers in the fast food joints.

0 c student · September 2, 2015
games arent usually programmed in c, at least, not very complex ones.  most games are done on c++ and also java, however, they might include techniques such as classes to maintain their code.  if you're not familiar with these things then it might be a bit hard to understand how games of such complexity are done.  there are many simple games done in c and a quick google search for the source codes should point them out.  the only decent game i know of that was coded in c is doom.
  • 1
  • 2



One of the most popular languages of all time.

Bucky Roberts Administrator