need help in C++, stack, queue and Array in game

Learn how to start making games.

need help in C++, stack, queue and Array in game

Postby Masky94 » Fri Apr 17, 2015 3:12 am

i have an idea to make a simple game from using stack queue and array or at least two of them, but i still need expert to tell me how to really code them into game, is there any tutorials?

for queue, i have an idea like building army/unit, that must have a queue to build them one by one, but i need an example of the code.

for stack, i have an idea about using power-up, that last-in first-out theory, also need an example of the coding.

appreciated your expert's suggestion if you willing to help, thank you
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in C++, stack, queue and Array in game

Postby koala » Fri Apr 17, 2015 11:05 am

Well, in C, which is pretty much like in C++, you can code stack and queue using array.

stack:
Code: Select all
#define MAX 100

int stack[MAX];
int top = 0; // first empty place

// add an element
int push (int x) {
    if (top == MAX) return 0; // stack is full, operation failed
    stack[top++] = x;
    return 1; // operation succeeded
}

// take an element
int pop () {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,
                                      // or 0, if stack contains natural numbers)
    x = stack[--top];
    return x;
}

// read the top, without removing the element
int read() {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed
    x = stack[top];
    return x;
}

// is stack empty
int empty() {
    if (top == 0) return 1;
    else     return 0;
}

// is stack full
int full() {
    if (top == MAX) return 1;
    else     return 0;
}


You can code these five functions to be unique for all stacks, if you have more of them. You just need one more argument, which stack you are referring to.
Code: Select all
#define MAX 100

typedef struct stack {
    int elements[MAX];
    int top;
} Stack;

Stack stack1, stack2, stack3, stack4, stack5;
stack1.top = 0; stack2.top = 0; stack3.top = 0; stack4.top = 0; stack5.top = 0;

// add an element
int push (stack1, int x) {
    if (stack1.top == MAX) return 0; // stack is full, operation failed
    stack1.elements[stack1.top++] = x;
    return 1; // operation succeeded
}

// take an element
int pop (stack2) {
    int x;
    if (stack2.top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,
                                      // or 0, if stack contains natural numbers)
    x = stack2.elements[--stack2.top];
    return x;
}

// read the top, without removing the element
int read(stack3) {
    int x;
    if (stack3.top == 0) return -1; // stack is empty, operation failed
    x = stack3.elements[stack3.top];
    return x;
}

// is stack empty
int empty(stack4) {
    if (stack3.top == 0) return 1;
    else     return 0;
}

// is stack full
int full(stack5) {
    if (stack4.top == MAX) return 1;
    else     return 0;
}


queue:
Code: Select all
#define MAX 100

int queue[MAX];
int top = 0; // here elements exit queue
int bottom = 0; // here elements enter queue
int N = 0; // number of elements

// insert an element
int insert(int x) {
    if (N == MAX) return 0; // queue is full, operation failed
    bottom = (bottom + 1) % MAX; // set bottom to next empty element
    queue[bottom] = x;
    N++;
    return 1; // operation succeeded
}

// take an element
int take() {
    int x;
    if (N == 0) return -1; // queue is empty, operation failed
    top = (top + 1) % MAX;
    x = queue[top];
    N--;
    return x;
}

// is queue empty
int empty() {
    if (N == 0) return 1;
    else     return 0;
}

// is queue full
int full() {
    if (N == MAX) return 1;
    else     return 0;
}

You can also make these functions universal, to work with all queues of this format.
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in C++, stack, queue and Array in game

Postby Masky94 » Sat Apr 18, 2015 7:03 am

Thanks for the help, i will look into it and try to implement them into the game
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in C++, stack, queue and Array in game

Postby bat78 » Sun May 03, 2015 2:28 am

A common standard is 0 to indicate success and 1 or above - an error (i.e the function returns either the number of errors or a specific error code)
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: need help in C++, stack, queue and Array in game

Postby koala » Sun May 03, 2015 5:44 am

bat78 wrote:A common standard is 0 to indicate success and 1 or above - an error (i.e the function returns either the number of errors or a specific error code)
Yes, bat78 is right. +1
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score


Return to Getting Started

Who is online

Users browsing this forum: No registered users and 1 guest