Help on program bugs

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 717 Views - Last Post: 11 December 2010 - 12:49 AM Rate Topic: -----

#1 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Help on program bugs

Posted 10 December 2010 - 09:15 PM

Hey guys, as some of you may have already known, im doing this Frigate game project for my semester class which is intro to C++. This was all required for me to do, so its not like i could have done my own thing or go and made a program with my own rules. Currently this is what i have. I am supposed to make a program that you can move your "ship" around a 10x10 grid and defeat the computer's ship by shooting it. There is four attributes: agility (how much things you can do in one turn), armor (how much damage you can take till u die), weapons (how much damage you can inflict per shot), and range (how far you can shoot from your position.

The game currently runs, but the problem im having is that for some reason, its not detecting the range between the ships correctly, and i know there's something in my code that im doing wrong, but no matter how much i tr to go through it, i just can figure out whats wrong. So can you guys help me debugging this and suggest what i should do? Thanks :D


#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>


using namespace std;

    struct ships
    {
           int agility; //the amount of moves it has each turn
           int armor; //the "health points" of each ship
           int weapons; //amount of weapons it has
           int range; //how far a ship can shoot from its current position
    };

void initGrid(char [][10], int, int, int, int);  //assign characters to different aspects of the grid
void printBoard (char[][10]);  //print the grid with the respective characters
void assignAttributes (ships & p1);  //asks players for the attributes of his/her ship
void cpuShooting (ships & p1, ships cpu);  //calculates how much damage a shot will do each time the cpu fires depending on weapons
void playerShooting (ships  p1, ships & cpu);  //calculates how much damage a shot will do each time the player fires depending on weapons
void movement (char [][10], int&, int&, int&, int&, ships&p1, ships&cpu);  //asks player for his moves
void cpumovement (char [][10], int&, int&, int&, int&, ships&cpu, ships&p1);  //makes cpu move randomly
bool gameOver (ships p1, ships cpu);  //makes the game end when armor for plaer or cpu is less than -5

int main(int argc, char *argv[])
{
    
    srand(time(0));
    
    char choice;
    ships p1;
    ships cpu;
    char gameboard[10][10];
    
    //attributes for the player's ship
    p1.agility;
    p1.armor;
    p1.weapons;
    p1.range;
    
    //randomly assigned attributes for the cpu ship
    cpu.agility = rand()%6+1;
    cpu.armor = rand()%6+1;
    cpu.range = rand()%6+1;
    cpu.weapons = rand()%4+1;
    
    //player and cpu starting positions
    int p1_row = 1;
    int p1_col = 1;
    int cpu_row = 8;
    int cpu_col = 8;
    
    
    cout << "\n\n";
    cout << "\t Heyo, wanna play the game? (Y = yes, N = exit): ";
    cin >> choice;
    cout << "\n\n";
    
    
    
    switch (choice)
    {
        case 'Y':
        case 'y':
                cout << " \t Instructions: ""w"" is up, ""a"" is left, ""d"" is right, and ""s"" is down \n";           
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                assignAttributes (p1);
                while(gameOver (p1, cpu))
                {
                    movement (gameboard, p1_row, p1_col, cpu_row, cpu_col, p1, cpu);
                    cpumovement (gameboard, p1_row, p1_col, cpu_row, cpu_col, cpu, p1);
                }
                cout << "Game over! \n";
            break;
            
        case 'N':
        case 'n':
            exit(0);
            break;
         
        default:
            cout << "Wrong input. It's either: Y to play, or N to quit.\n";
    }
    
    
         
    system("PAUSE");
    return EXIT_SUCCESS;
}


void initGrid(char gameboard[][10], int p1_row, int p1_col, int cpu_row, int cpu_col) 
{
    for(int r = 0; r < 10; r++)
    {
        for (int c = 0; c < 10; c++)
        {
            if (r == 0 || r == 9)
                gameboard[r][c] = 205;
            else if(c == 0 || c == 9)
                gameboard[r][c] = 186;
            else
                gameboard[r][c] = ' ';
        }

    } 
    
    
    gameboard[0][0] = 201;
    gameboard[9][0] = 200;
    gameboard[0][9] = 187;
    gameboard[9][9] = 188;
    
    
    
    gameboard[p1_row][p1_col] = 193; //single T- player
    gameboard[cpu_row][cpu_col] = 208; //double TT- computer
}

void printBoard (char gameboard[][10])
{
    for (int r = 0; r < 10; r++)
    {
        for (int c = 0; c < 10; c++)
        {
			cout << gameboard[r][c];
        }
        cout << endl; 
    }
}


void movement (char gameboard [][10], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & p1, ships & cpu)
{
    char move;
    
    for(int moves = 0; moves < p1.agility; moves++)
    {
        cout << "Please enter your moves : ";
        cin >> move;
        switch (move)
        {
            case 'w': //if "move" is assigned w or W .. move up on the board
            case 'W':
                p1_row = p1_row - 1;
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_row <= 0)
                {
                    p1_row = p1_row + 9;
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
                
            case 'a': //if "move" is assigned a or A.. move left on the board
            case 'A':
                p1_col = p1_col - 1;
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_col <= 0)
                {
                    p1_col = p1_col + 9;
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
                
            case 'd': //if "move" is assigned d or D.. move right on the board
            case 'D':
                p1_col = p1_col + 1;
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_col >= 9)
                {
                    p1_col = p1_col - 9;
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
                
            case 's': //if "move" is assigned s or S.. move down on the board
            case 'S':
                p1_row = p1_row + 1;
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_row >= 10)
                {
                    p1_row = p1_row - 9;
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
                
            case 'f':
            case 'F':
                if (cpu_row <= p1.range && cpu_col <= p1.range)
                {
                    playerShooting (p1, cpu);
                    cout << "CPU's armor is: " << cpu.armor;
                }
                else
                {
                    cout << "You are too far away! \n";
                }
                break;
                
            default: 
                cout << "You put in a wrong move. \n";
                break;
        }
    }
    
    
}

void assignAttributes (ships & p1)
{
    
    cout << "Please enter agility (only from 0 to 6): ";
    cin >> p1.agility;
    cout << endl;
    while(p1.agility < 0 || p1.agility > 6)
    {
        cout << "Too low or too high! PLease enter agility again (only from 0 to 6): ";
        cin >> p1.agility;
        cout << endl;
    }
    
    cout << "Please enter armor (only from 0 to 6): ";
    cin >> p1.armor;
    cout << endl;
    while(p1.armor < 0 || p1.armor > 6)
    {
        cout << "Too low or too high! PLease enter armor again (only from 0 to 6): ";
        cin >> p1.armor;
        cout << endl;
    }
    
    cout << "Please enter weapons (only from 0 to 4!!!): ";
    cin >> p1.weapons;
    cout << endl;
    while(p1.weapons < 0 || p1.weapons > 4)
    {
        cout << "Too low or too high! PLease enter weapons again (only from 0 to 4!!!): ";
        cin >> p1.weapons;
        cout << endl;
    }
    
    cout << "Please enter range (only from 0 to 6): ";
    cin >> p1.range;
    cout << endl;
    while(p1.range < 0 || p1.range > 6)
    {
        cout << "Too low or too high! PLease enter range again (only from 0 to 6): ";
        cin >> p1.range;
        cout << endl;
    }
    
    
    /*assigns random attributes to the commputers ship 
    cpu.agility = rand()%6+1;
    cpu.armor = rand()%6+1;
    cpu.weapons = rand()%4+1;
    cpu.range = rand()%6+1;
    
    */
    
}

void cpumovement (char gameboard [][10], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & cpu, ships & p1) //random cpu ship movement
{
    
    int s;
    
    for(int moves = 0; moves <= cpu.agility; moves++)
    {
            s = rand()%5+1;
            switch (s)
            {
                
                case 1: //if "s" is assigned 1.. move up on the board
                    cpu_row = cpu_row - 1;
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_row <= 0)
                    {
                        cpu_row = cpu_row + 9;
                        gameboard[cpu_row][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                    
                case 2: //if "s" is assigned 2.. move left on the board
                    cpu_col = cpu_col - 1;
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_col <= 0)
                    {
                        cpu_col = cpu_col + 9;
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                    
                case 3: //if "s" is assigned 3.. move right on the board
                    cpu_col = cpu_col + 1;
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_col >= 10)
                    {
                        cpu_col = cpu_col - 9;
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                    
                case 4: //if "s" is assigned 4.. move down on the board
                    cpu_row = cpu_row + 1;
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_row >= 10)
                    {
                        cpu_row = cpu_row - 9;
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                    
                case 5:
                    if (p1_row <= cpu.range && p1_col <= cpu.range)
                    {
                        cpuShooting (p1, cpu);
                        cout << "Player's armor is: " << cpu.armor;
                    }
                    else
                    {
                        cout << "The computer was too far away! \n";
                    }
                    break;
            }
    }
            


}

bool gameOver (ships p1, ships cpu)
{
    bool over = true;
    if(p1.armor < -6 || cpu.armor < -6)
    {
        over = false;
    }
    return over;
}

void cpuShooting (ships & p1, ships cpu)
{
    if(cpu.weapons == 1)
    {
        p1.armor = p1.armor - 1;
    }
    else if(cpu.weapons == 2)
    {
        p1.armor = p1.armor - 2;
    }
    else if(cpu.weapons == 3)
    {
        p1.armor = p1.armor - 3;
    }
    else if(cpu.weapons == 4)
    {
        p1.armor = p1.armor - 4;
    }
}
     
void playerShooting (ships p1, ships & cpu)
{
    if(p1.weapons == 1)
    {
        cpu.armor = cpu.armor - 1;
    }
    else if(p1.weapons == 2)
    {
        cpu.armor = cpu.armor - 2;
    }
    else if(p1.weapons == 3)
    {
        cpu.armor = cpu.armor - 3;
    }
    else if(p1.weapons == 4)
    {
        cpu.armor = cpu.armor - 4;
    }
}   


This post has been edited by halopower67: 10 December 2010 - 09:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help on program bugs

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Help on program bugs

Posted 10 December 2010 - 09:25 PM

What does this mean exactly?

View Posthalopower67, on 11 December 2010 - 01:15 PM, said:

its not detecting the range between the ships correctly,


Give us some concrete, specific examples of exactly what is going wrong.
Provide enough details of any inputs and other things we need to do to replicate the bug.
Tell us how the program output (or behaviour) is different to what you want/expect.

This post has been edited by janotte: 10 December 2010 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Help on program bugs

Posted 10 December 2010 - 09:30 PM

View Postjanotte, on 10 December 2010 - 08:25 PM, said:

What does this mean exactly?

View Posthalopower67, on 11 December 2010 - 01:15 PM, said:

its not detecting the range between the ships correctly,


Give us some concrete, specific examples of exactly what is going wrong.
Provide enough details of any inputs and other things we need to do to replicate the bug.
Tell us how the program output (or behaviour) is different to what you want/expect.


i think you quoted something wrong, but to answer your other questions... when i try to play the game and i try to shoot the computer, or the computer tries to randomly shoot me, sometimes i just cant shoot because it's detecting im too far away from it. What i need it to do is that once it detects that either the player is close enough to the cpu (if that value is less than or equal to the range of the player), or if the computer is close enough to the player (if that value is less than or equal to the range of the computer)... then i can fire and damage, or the computer can fire and damage equal to the value it has in the weapons attribute.
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1526
  • View blog
  • Posts: 5,514
  • Joined: 03-August 09

Re: Help on program bugs

Posted 10 December 2010 - 09:30 PM

why not just shorten those shooting functions to cpu.armor -= p1.weapons; and vise versa? where is the issue arsing from, what line/s? where is your program checking for the distance?

This post has been edited by ishkabible: 10 December 2010 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Help on program bugs

Posted 10 December 2010 - 09:33 PM

View Postishkabible, on 10 December 2010 - 08:30 PM, said:

why not just shorten those shooting functions to cpu.armor -= p1.weapons; and vise versa? where is the issue arsing from, what line/s?


well the lines that i think this is coming from is where i try to calculate how close a hip is so it can fire, which is the last cases in the
void movement
and the
void cpumovement
functions
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1526
  • View blog
  • Posts: 5,514
  • Joined: 03-August 09

Re: Help on program bugs

Posted 10 December 2010 - 09:38 PM

lets look at this
if (cpu_row <= p1.range && cpu_col <= p1.range)
this reads "if the row the cpu is in is less than or equal to the shooting range of the player and the column the cpu is in is less than or equale to the players range then you can fire". how much sense dose that make? what if the cpu is in row 9 and you in row 8 and your ships range is 3? it wont shoot becuase 9 is grater than 3.
try this instead
"if distance between cpu and player is less than or equal to the range of player then fire"

This post has been edited by ishkabible: 10 December 2010 - 09:40 PM

Was This Post Helpful? 1
  • +
  • -

#7 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Help on program bugs

Posted 10 December 2010 - 09:41 PM

View Postishkabible, on 10 December 2010 - 08:38 PM, said:

lets look at this
if (cpu_row <= p1.range && cpu_col <= p1.range)
this reads "if the row the cpu is in is less than or equal to the shooting range of the player and the column the cpu is in is less than or equale to the players range then you can fire". how much sense dose that make? what if the cpu is in row 9 and you in row 8 and your ships range is 3? it wont shoot becuase 9 is grater than 3.


Ah crap why didn't i see that. Ok well that's definitely why it's not calculating the range to the enemy (be it the player or the cpu). Now how would i go about calculating the range, is there any article taht would help em on this? Or if you guys can help? I can't think of a way of calculating the range :/
Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1526
  • View blog
  • Posts: 5,514
  • Joined: 03-August 09

Re: Help on program bugs

Posted 10 December 2010 - 09:47 PM

dont over complicate it finding the distance between 2 points is pre-algebra stuff. remember the distance formula?
Was This Post Helpful? 0
  • +
  • -

#9 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Help on program bugs

Posted 10 December 2010 - 09:54 PM

View Postishkabible, on 10 December 2010 - 08:47 PM, said:

dont over complicate it finding the distance between 2 points is pre-algebra stuff. remember the distance formula?

Im not sure how i would translate tat into cde, the distance formula is c^2 = a^2 - b^2 right?
Was This Post Helpful? 0
  • +
  • -

#10 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1526
  • View blog
  • Posts: 5,514
  • Joined: 03-August 09

Re: Help on program bugs

Posted 10 December 2010 - 09:57 PM

Google "the distance formula". ill even sweeten the deal and give you a neat function i found (only becuase cmath already provides sqrt but who wants to mess with floats and doubles) :wacko:
finds the integer square root of an integer:
unsigned int isqrt(unsigned int n) {
	unsigned short c = 0x8000;
	unsigned short g = 0x8000;
	for(;;)/> {
		if(g*g > n)
             g ^= c;
         c >>= 1;
         if(!c)
             return g;
         g |= c;
     }
 }



this isn't mine i got it off this site.

This post has been edited by ishkabible: 10 December 2010 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#11 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Help on program bugs

Posted 10 December 2010 - 10:35 PM

View Postishkabible, on 10 December 2010 - 08:57 PM, said:

Google "the distance formula". ill even sweeten the deal and give you a neat function i found (only becuase cmath already provides sqrt but who wants to mess with floats and doubles) :wacko:
finds the integer square root of an integer:
unsigned int isqrt(unsigned int n) {
	unsigned short c = 0x8000;
	unsigned short g = 0x8000;
	for(;;)/> {
		if(g*g > n)
             g ^= c;
         c >>= 1;
         if(!c)
             return g;
         g |= c;
     }
 }



this isn't mine i got it off this site.


I'm sorry but im not sure what that function does, it has things i have never even learnt of in the class. Care to explain what exactly it does?
Was This Post Helpful? 0
  • +
  • -

#12 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Help on program bugs

Posted 10 December 2010 - 11:03 PM

View Postishkabible, on 10 December 2010 - 08:57 PM, said:

Google "the distance formula". ill even sweeten the deal and give you a neat function i found (only becuase cmath already provides sqrt but who wants to mess with floats and doubles) :wacko:
finds the integer square root of an integer:
unsigned int isqrt(unsigned int n) {
	unsigned short c = 0x8000;
	unsigned short g = 0x8000;
	for(;;)/> {
		if(g*g > n)
             g ^= c;
         c >>= 1;
         if(!c)
             return g;
         g |= c;
     }
 }



this isn't mine i got it off this site.


I just did this and it works perfectly so far, am i missing something or overlooking a flaw?:

int distance (int p1_row, int p1_col, int cpu_row, int cpu_col)
{
    int distance;
    
    int distance_row = p1_row - cpu_row;
    
    int distance_col = p1_col - cpu_col;
    
    distance = sqrt( (distance_row * distance_row) + (distance_col * distance_col));
    
    return distance;
}

Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Help on program bugs

Posted 11 December 2010 - 12:17 AM

Care to explain what you you meant here?

View Posthalopower67, on 11 December 2010 - 01:30 PM, said:

i think you quoted something wrong,

I am just curious as to what you were trying to say.
Was This Post Helpful? 0
  • +
  • -

#14 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Help on program bugs

Posted 11 December 2010 - 12:30 AM

View Postjanotte, on 10 December 2010 - 11:17 PM, said:

Care to explain what you you meant here?

View Posthalopower67, on 11 December 2010 - 01:30 PM, said:

i think you quoted something wrong,

I am just curious as to what you were trying to say.



The first time i saw your message, i saw that what you tried to quote had the quote tags visible, and it was just information about my profile in the middle of those quote tags. So i figured you might have messed up quoting me.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Help on program bugs

Posted 11 December 2010 - 12:40 AM

View Posthalopower67, on 11 December 2010 - 04:30 PM, said:

i figured you might have messed up quoting me.


Correct. I did have a 'fat finger' moment.
But it was fixed by me by the time you quoted me so I wonder why you bothered to mention a problem that no longer existed. I clearly knew I had blundered because I had already gone back and fixed it. Seems like a waste of everyone's time.
Anyway, no matter.

Have you found any problems with your function from your unit testing?
Seems simple and robust to me.
Good old Pythagorus and his theorem.

I'd suggest doing yourself a favour and making the ints unsigned ints so it is clear to anyone reading it you had thought that part of the maths through. But that's a flourish really.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2