Thread: Dynamic 2D Array.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    58

    Dynamic 2D Array.

    Hello world. I am new at programming. I have started programming at C(in Ubuntu) just when I entered univercity last October. It is hard but I like it. But I have a project where I need to make a, basically a game. The game must be played in the terminal. I have 9 days to complete it and send it. I cannot think of anything else. The game has a board(the board must be at the terminal and must be created and filled dynamically as a 2d array)either a square or a rectangular. The dimencions must be between a certain min N*M(maybe a 3*5) and a max N*M(maybe a 10*15). The main hero is a stingy knight(must be shown as $) and doesn't want to lose much money(which also must be shown at the end how much he lost). He fights enemies(must be shown 1,2,3 depending on how many hits they need till death). In the board there must be, also, some obstacles(must be shown as #). The pathes from where the knight can move must be shown as (. a dot). When the knight kills all the enemies the game updates and the board gets bigger by 1 in both dimencions and if one of the dimencions is at max then it changes only the other. The game ends when the player wins the max N*M board. The player must in the start give to the programme the starting N*M dimencions which the game must start from.

    The game has three difficulty levels.
    Easy: where both the enemies and the obstacles take a 5% of the N*Mof each level

    Medium: where obstacles take a 5% and enemies take 10%.

    Hard: where enemies and obstacles take 10%.

    The obstacles can be in differenr positions but each enemy must be next to at least one other enemy (up, down, left, right and NOT diagonal).This must be done with a rand().

    To show the board each line and each column must have above and in the left the number of each line and column.

    Now, about the moving.
    To move the knight the user must give (U/u, D/d, L/l, R/r)and a number(1-9). For example, U7 d4 L3 r2. If the programme finds out that there is an obstacle or an enemy it asks again for moving. That costs 5.

    Punch.
    Punch costs 50. To do a punch the user writes (F or f) and removes 1 health point from the enemy next to the knight. After that if behind the enemy there is nothing it moves one position behind. If not enemy doesn't move.

    Chop with an axe.
    That costs 70. To do that the user must write (A or a). That removes 1 health point from the enemy next to the knight and also the on behind that enemy. If chop is done correctly and hit the enemies they go back one block back if no obstacles or other enemy is behind. Also, if the second enemy dies and the one next to the knight doesn't that enemy goes back where the dead enemy was.

    Slash.
    That costs 80. To do that the user must write (S or s). That removes 1 health point from the 3 enemies who are in front of the knight(Up,down,left,right).If there is an obstacle or an enemy behind them then they don't go back else they do.

    Bow Attack.
    That costs 60. To do that the user must write (B or b). That removes 1 health point from an enemy who is one block far from the knight. If there is an obstacle or an enemy behind the enemy does NOT go back else they do.

    If the attacks does not hit an enemy then the cost is 0.

    To end the game the player must write (X or x).

    Also, the knight can move from the one end of the board to the other.

    If the player gives different letters the program must ask again.

    Here are one example.

    1 2 3 4 5 6 7 8 91011
    ------------------------------
    1 | . #. . . 1 3 . # . .
    2 | . . . . 2 2 1 . . . .
    3 | . . . . . . 3 . . . .
    4 | # . . . . . . . . . .
    5 | . . . . . . . . . . .
    6 | . . . . . . . . . . .
    7 | . . . . . . . . . . .
    8 | . . . . . $ . . . . .
    9 | . . . . . . . . . . .
    10| # . . . . . . . . . .
    stingy
    stingy


    Level money spent: 25
    Game money spent: 300
    Make your move:


    Thanks for the help.

  2. #2
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    I need help. This is very difficult.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,175
    > The dimencions must be between a certain min N*M(maybe a 3*5) and a max N*M(maybe a 10*15).
    So start with 10x15 as a fixed sized array, then get everything else working.

    You can still choose any size up to those limits, and all you're wasting is a bit of memory.

    Changing it to be dynamic later on is pretty easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Thanks. But I have another question, also. In the array how can I put the enemies and the obstacles there?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,175
    You start with a simple static board of say
    Code:
    char board[5][5] = {
       "..1..",
       ".#...",
       ".....",
       "...$.",
       ".....",
    };
    You can test ALL of this without changing anything about the board.
    Now, about the moving.
    To move the knight the user must give (U/u, D/d, L/l, R/r)and a number(1-9). For example, U7 d4 L3 r2. If the programme finds out that there is an obstacle or an enemy it asks again for moving. That costs 5.

    Punch.
    Punch costs 50. To do a punch the user writes (F or f) and removes 1 health point from the enemy next to the knight. After that if behind the enemy there is nothing it moves one position behind. If not enemy doesn't move.

    Chop with an axe.
    That costs 70. To do that the user must write (A or a). That removes 1 health point from the enemy next to the knight and also the on behind that enemy. If chop is done correctly and hit the enemies they go back one block back if no obstacles or other enemy is behind. Also, if the second enemy dies and the one next to the knight doesn't that enemy goes back where the dead enemy was.

    Slash.
    That costs 80. To do that the user must write (S or s). That removes 1 health point from the 3 enemies who are in front of the knight(Up,down,left,right).If there is an obstacle or an enemy behind them then they don't go back else they do.

    Bow Attack.
    That costs 60. To do that the user must write (B or b). That removes 1 health point from an enemy who is one block far from the knight. If there is an obstacle or an enemy behind the enemy does NOT go back else they do.

    If the attacks does not hit an enemy then the cost is 0.

    To end the game the player must write (X or x).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    But how can I use the rand() to fill the board with the enemies, the obstacles and the knight?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,175
    > But how can I use the rand() to fill the board with the enemies, the obstacles and the knight?
    Why do you even care about that at the moment, when you can't even work with a fixed map?
    A development process

    Stop trying to eat the entire elephant in one meal.

    You need to split the problem into sub-tasks.

    If you can make everything work with the fixed map I posted, you'll be in pretty good shape for then adding
    - randomised positions
    - expanding maps

    But until then, they're distractions you need to ignore.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Can you give me a guideline and some tips(especially if that are in a piece of programme) so I can make it good?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,175
    Well you don't write code just by picking up your assignment and start typing.

    A key skill (which you just haven't recognised you need) is to be able to look at a long list of requirements (like the ones you posted) and break that down into a series of smaller steps.

    > I have 9 days to complete it
    You've made a good start by asking early.
    It's better than showing up with only 3 hours to go, hoping for miracles.

    How about these activities per day
    1 Lots of paper, research, making notes about the things you need
    2 Basic map, knight can move around anywhere
    3 knight recognises obstacles and enemies (you just print "there is a ? here")
    4 Implement punch and chop
    5 Implement slash and bow
    6 Add the scoring and costs
    7 Add easy/med/hard random positioning of enemies and obstacles
    8 Add increasingly large play areas until max NxM is reached.
    9 Polish user interface and submit
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    How can I print the static array you made before?

  11. #11
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Just found out. No need to do it.

  12. #12
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    307
    You should start by learning what loops can do.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  2. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  3. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  4. I want a dynamic array.
    By MK27 in forum C Programming
    Replies: 10
    Last Post: 08-27-2008, 11:47 PM
  5. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM

Tags for this Thread