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.
EXAMPLE:

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"

PROBLEM
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........

sincerely,

stuck

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 John Tompson · September 9, 2015
c student, glad to hear from you!

that was an awesome game. it is definitely a classic. there is nothing wrong with that game.
yes, i don't mind writing a simple game. the reason why i wasn't going to use c++ is because i wanted to master c first. 
then i will learn c++. however, i figured that i didn't need c++ because that is mostly for high end (take a long time) graphics.
a simple game is fine; otherwise, for a high end game it takes 100 people to make it, so if i am going to make it myself,
it is okay for it to be a simple game (because it will take a billion years for a better one).

so you say there is more program code available for c++, when it comes to looking at examples?
thanks for the advice.

1. but as far as the program architecture, what do you think of the "structure" of my program that i have shown you in the previous post?

John
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.
0 John Tompson · September 2, 2015
hi saket,
nice to meet.

YOUR FRIENDS WITH BUCKY???
YOU GOT TO TALK TO HIM?

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.

:angel:John:angel:
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()
{
  do
  {
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;
          break;
      }
defeat monster
gain treasure an XP
      }
hero wins
      {if hero win->
         break;     
      }
    return 0;
}



friend programmer,

John 
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");
   break;}
   } while (turns!=1000);
  printf("\n program ends\n");

    return 0;
}


John:P
0 Saket Kulkarni · August 21, 2015
Hey Guys,

In all these forum discussions, you seemed like the C-Gods to me.... I'm just a newbie....

So, did u guys learn C from Bucky's tuts(which are freakin' awesome acc. to me...) and den go for advanced C courses elsewhere? Just need-to-know stuff so that I can be a pro like u guys 1 day(Hopefully soon!!)

Tysm...
Saket
0 c student · August 21, 2015
there seems to be a lot of things you don't understand about the language.  perhaps it would be better if you learned more before approaching a task of this caliber?
+1 Jon Z. · August 21, 2015
Not necessarily header files.  You can have the functions in the same file. Or you can have the functions in different files if you have a lot of functions and want to keep them more organized. 

Look at the following example (it's not proper C code, it's pseudocode).

void fight(){
//fight
life--;
exp++;
...
}

void run(){
//run function
energy--;
...
}

void takeANap(){
//take a nap
energy++;
...
}

int main(){
while(1){
 //ask what to do
 if(toDo == fight){
  fight();
 }
 else if(toDo == run){
  run();
 }
 else if(toDo == takeANap){
  takeANap();
 }
 else if(toDo == exit){
  break;
 }
}
return 0;
}


In the example they are all 1 big if statement, but if it wasn't you could use continue; to go back to the beginning of the loop.

The point is that this way you keep your main function simple and clean. And most of the code is in the functions.
0 John Tompson · August 16, 2015
c student,
hey bro, what's up man? nice to see you again!;)
/images/forum/upload/2015-08-16/1c3c8405fb923ca9e488233627afb6f4.jpg
no, i haven't seen a game written in c. how do i find one? i didn't know that it was possible to get this, as games are usually protected with some kind of block to prevent you from looking at it. i don't know how to unblock it to look at one. but on other languages, that is how i got good at the language, was by looking at other people's code. however, not as much in the new languages (or c) is available.

how do i "get familiar with the more advanced data structures which used structs"? -- i have done all the tutorials and logged the definition and HOW to make one. so your saying structs are similar to functions, in that they keep all of a set of variables together into a cluster and make it easier to implement this set of variables into the program? i didn't know that you could do that -- if that's what it does. if i am wrong, then in a single sentence, how would you describe what the struct does. my sentence and understanding currently about struct and what it does currently is: 
"a struct is only used to combine floats and ints together so that they can be compatible with eachother when normally you might not be able to use them together." 

what do you mean its "too much work to implement. the labels DUNGEONXX"? how so? (and by the way, i thought that it was DUNGEONX [DUNGEONLEFTX], not DUNGEONXX, as a double array with 2 "X"'s, because by "double array", you meant 1 array with x and array 2 with y, not a double array for x, right -- or was this just a typo?). wouldn't it just be a simple coordinate change such as x=x+1, now x=99,y=5 (map). it sounds to me like this is what you did: you took the entire game map and divided into 4 equal parts (4 squares, as the entire map is a square). so you mean "easier" because you have to worry about a long array size or split it into a bunch of arrays for x (and the same for y).

currently my map is a giant long rectangle because it was easier to program and to look at later (look at the code later, as it (each x row) could be see on a single line and not scattered everywhere, making it difficult to see the map in the code). but i just got the idea, that i can create many more of these giant long rectangles for each room and still make the code simple to program.

so in the actual code, this is exactly how you would use the void main command: "int main (void) {"?. note the use of the ( ) brackets around void, and the possible confusion that i might have as to its use: as i have never seen this before. note: i am not saying that its not possible, just that i have not seen it. i thought void was only to assign functions. so this would mean that main becomes a function? that is confusing: i don't understand what this does (main void) that is different from a normal function or WHY you would use this instead of a "normal funtion". how does this help you doing it differently [i.e. main void instead of void function]?

you said, "use goto to simplify your life." does this mean that you can use the "goto" command in the c language? currently my program is written in another programming language. i am going to convert the program from one language to another . the "goto" command that i suggested, is what you can use in another language, but as far as i know it is not even available in the c language, nor is there anything like it.....

"a function which uses empty parenthesis, e.g. functionOne() takes in no parameters." okay, didn't know that. that's strange why they write this programming language this way then!

so what is the purpose of using a "typedef": is it to assign less bits (space) to a variable to make your program smaller (and therefore take up less space and run faster)?

a pointer pointing at another pointer? dude, i didn't know that you could do that. and you use this to make a double array. using a pointer method with this computer language is a more convulted way of using arrays then other programming languages! [example: "**pointer point to map(x,y)" instead of "map(x,y)". i've noticed that this language takes up an enormous more of an amount of size than the programs have to be with all of this additional unnessary stuff added to simple functions], (i.e. the programs run slower).

"Player->posX": i don't understand how to use this. unless, "->" is an actual c command? and then this statement, "p->posX += x;" (and "y") is saying "i am referring to player 1(p1) in the program, if p1is not equal to the current location, then posX(y) will be the new location"? in the actual code, this is how you write it, "p->posX += x;"? or maybe if player = x5, then the pointer posX = 5, so you are assigning the value of posX to the pointer player(p) then? i've only seen this type thing done before with an ampersand& or *pointer (note the confusion).

so this is what the actual code would look like (or that it CAN be written this way, in this format): "int position posX;"? and this assigns a value to the variable, "position posX"? note: is this 1 value or a single variable with two values, or a single int assignment with two separate variables assigned to 1 int? and your saying that the c language programming has the same format, like in the command of void, where two variables can be  assigned to a single void [function], as in "void updateMapArea (Player p)"? [note: the two variables are, 1 variable (updateMapArea), and 2 variable ((Player p))].

sincerely,
yo bro
0 John Tompson · August 16, 2015
hi jon,
thanks for getting back to me.
how you been?

i thought putting all 500 lines in a single loop was your idea on HOW to get back to the beginning of the program?

and by "call functions", you mean #include header files.h, or voids?

so you were suggesting to use the continue and break functions. have you used them yourself in programming? have you gotten them to work in all of the loops. and if so, do you have a small example program of how to use them in the other loops (i'm not talking about using break in a do while [in other loops such as for or if else], nor am i talking about using the continue with the for loop [but the other loops such as if or do while]).
if you know how to do this [IF ITS POSSIBLE], then this would be really useful to me.

thanks,
friend john

/images/forum/upload/2015-08-16/04fd091c00cd2bb28c527a060edab58d.jpg
  • 1
  • 2

C

107,060 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator