mercredi 2 décembre 2015

Addition in Recursion Function

My code is to +1 to score when the number in the array is 1 and -1 when the number in the array is -1. But when I run the program it only returns 0. The recursion itself doesn't have an infinite loop, and I don't know what I'm doing wrong here.

#include <iostream>

using namespace std;

/* Mario can only jump forward by 1 step or 2. When he collects a coin he     instantly +1
but if he gets a mushroom he instantly -1. Mario cannot fall off the     track so zero is
the start and end. Mario has to guess whats ahead of him. */

/* Run through the track by jumping */
int jumping( int track[], int size, int location ){
static int score = 0;

if ( track[ location ] == 1 ){
    score++;
}
else if ( track[ location ] == -1 ){
    score--;
}

if (( location = size - 1 ) || ( track[ location + 1 ] == 0 )){
    return score;
}
else{
    jumping( track, size, location + 2 );
}
}

/* Run through the track by taking steps */
int stepping( int track[], int size, int location ){
static int score = 0;

if ( track[ location ] == 1 ){
    score++;
}
else if ( track[ location ] == -1 ){
    score--;
}

if ( location = size - 1 ){
    return score;
}
else{
    stepping( track, size, location++ );
}
}

/* Is to calculate the maxium possible score for any given track, with any arrangement
of coins and mushroom using recursion. */
int maxScore( int track[], int size, int location ){
int score = 0; //Keep track of score
int step = stepping( track, size, location );//score by stepping
int jump = jumping( track, size, location );//score by jumping

/* If step is higher or jump and replace according to the highest */
if ( step > jump ){
    score = step;
    return score;
}
else if ( jump > step ){
    score = jump;
    return score;
}
else if ( jump == step ){
    return score;
}
}

int main(){

int simple[2] = { 0, 0 };
int easy[8] = { 0, -1, 1, 1, -1, 1, -1, 0 };
int medium[25] = { 0, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 0 };
int mediumhard[3] = { 0, 1, 0 };
int impossible[20] = { 0, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1,     1, 1, 1, -1, -1, 0 };
int insane[40] = { 0, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1,
                  1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 0 };

cout << "1. SIMPLE............................................... " << maxScore( simple, 2, 0 ) << endl;
cout << "2. EASY................................................. " << maxScore( easy, 8, 0 ) << endl;
cout << "3. MEDIUM............................................... " << maxScore( medium, 25, 0 ) << endl;
cout << "4. MEDIUM HARD.......................................... " << maxScore( mediumhard, 3, 0 ) << endl;
cout << "5. IMPOSSIBLE........................................... " << maxScore( impossible, 20, 0 ) << endl;
cout << "6. INSANE............................................... " << maxScore( insane, 40, 0 ) << endl;

return 0;
}

Aucun commentaire:

Enregistrer un commentaire