Minecraft wep

So i want to make a program that will tell how many hearts of damage a certain weapon will do when a user types the name of the tool or weapon in. for example if a user typed "diamond sword" the program would respond with "3.5". Other people have told me to use arrays, but I'm not sure. Could I have a bit of help here?

(by the way I am very new to C++)

(also here are all the tool damage values http://gaming.stackexchange.com/questions/64525/how-much-damage-does-each-tool-do )
Last edited on
You should use std::map, depending on how you get input. How will you be getting the input from the user? Will you list out the choices with numbers, or just the choices and they have to retype them?
what i hope to have is that the user will be asked to type in a tool name, then it will display the damage. The user will already know the choices and they wont be listed
Well then you have the issue of Pick vs Pickaxe, Spade vs Shovel, etc.
An easy way to do it would be to have the program output your options:

TYPE
(1) for Diamond Sword
(2) for Diamond Axe
(3) for Diamond Pickaxe
(4) for Diamond Shovel
(5) for Diamond Hoe
(6) for Iron Sword
(7) for.... and so on

then use cin, and use the case statement to make a decision on the output.
The issue of Pick vs pick vs PICK vs Pick Axe vs pick axe vs Pick Axe vs Pick_Axe... The (1) for Diamond sword (2) for Diamond Axe is a good suggestion.
int _tmain()
{ int number;
cout << " (1) for Diamond Sword \n (2) Diamond Axe \n (3) Diamond Pickaxe \n (4) Diamond Shovel \n (5) Iron Sword \n (6) Iron Axe \n (7) Iron Pickaxe \n (8) Iron Shovel \n";
cout<< "Please choose a number and press [Enter]" <<endl;
cin >> number;
return 0;
}

// So this is what I ended up with, but I'm not sure how to set up that "Case Statement", please go into more detail?
(also thank you you all are being extremely helpful)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# include <iostream>
# include <cstring>
# include <cstdio>
using namespace std;

int getStrIndex(const char *);
float getResult(int,int);

int main()
{
    char array[50];
    string firstWord,secondWord;

    cout << "Type your weapon -> ";gets(array);
    char *ptr;

    // Spliting those two words
    ptr = strtok(array," ");
    firstWord = ptr;
    ptr = strtok(NULL," ");
    secondWord = ptr;

    int x = getStrIndex(firstWord.c_str());
    int y = getStrIndex(secondWord.c_str());

    if(x!=9&&y!=9)
    {
        cout << getResult(x,y);
    }


    return 0;
}

int getStrIndex(const char *ptr)
{
    const char *szBuffer[] = {"Diamond","Iron","Ston","Gold","Wood"};
    const char *szBuffer2[] = {"Sword","Axe","Pick","Shovel"};

    for(int i=0;i<4;i++)
    {
        if(!strcmp(ptr,szBuffer[i])||!strcmp(szBuffer2[i],ptr))
            return i;
    }if(!strcmp(ptr,szBuffer[4]))return 3;

    return 9;
}

float getResult(int x,int y)
{
    float map[4][4] = {{3.5,3,2.5,2},
                       {3,2.5,2,1.5},
                       {2.5,2,1.5,1},
                       {2,1.5,1,0.5}};

    return map[x][y];
}


PS : watch out!! I capitalized the first letters

hope that helps
Why are you using c-style strings? Use std::string, there is no need for char [50] anyways what if you don't use 50 characters? You have a bunch of wasted memory.. I think you made the program way too complicated.

OP when he said use a case statement he meant a switch

1
2
3
4
5
6
7
switch( number ) //input variable you used..
{
    case 1: //do stuff
        break;
    case 2: //stuff
        break;
}


http://www.cplusplus.com/doc/tutorial/control/
Check out the bottom of the page.

*edit btw also you could use a map for something like this
http://www.cplusplus.com/reference/map/map/map/
Last edited on
Ew. You're using C style arrays. What if you want to extend without recompiling? You could just make an option to add a value, and use vectors to make a dynamic array.
OP wrote:
So i want to make a program that will tell how many hearts of damage a certain weapon will do when a user types the name of the tool or weapon in. for example if a user typed "diamond sword" the program would respond with "3.5". Other people have told me to use arrays, but I'm not sure. Could I have a bit of help here?


You could use an enum in combination with heyyouyesyouiloveyou's suggestion.
1
2
3
4
5
6
7
8
9
10
11
enum Weapon{
   sword = 1,
   gun = 2
};
//...
switch(user_choice){
   case sword:
//...
      break;
//...
};


Or, if you write a weapon class or something, you can use std::map as L B suggested to match names up with objects.
1
2
3
4
5
6
Weapon sword, gun, /*...*/ cannon;

std::map<std::string, Weapon> choices;

choices["sword"] = sword;
//... 




Random Notes:
Techno's code looks like C with C++ I/O.

...You have a bunch of wasted memory..
Ew. You're using C style arrays
That awkward moment when you remember std::string and std::vector both internally use C-style arrays.
Last edited on
If he knows all the values ahead of time why not use a map they are meant for that a key , and a value. The weapon being key and the damage being value.

I don't play the game so I don't know the damage but heres an example..
1
2
3
4
5
6
7

int main()
{
    std::map<std::string , short> weapons = { { "Sword" , 10 } , { "Dagger" , 7 } };
    weapon["Bow"] = 9;
    //Might be best to make it all lowercase or uppercase then make the input all upper/lower
}
It depends on whether or not he wants the user to enter in a number or a string. If it's a string, it would be easier to use a map as you (and L B) have suggested. If it's a number, then a switch-case, vector, array, or enum would be simpler.
Techno01, that works almost perfectly!!!! though i would like if you would post it again, but add lots of comments! I'm very new to C++ and did this little project in order to learn a bit more about the language, so if you could explain what each bit does in detail it would be much more helpful than just simply using the program. THANK YOU ALL FOR YOUR HELP!!!!!
Last edited on
You should try and code it yourself and not just copy/paste...How about you ask us what parts you do not understand? Also I wouldn't recommend doing it his way anyways if you are going to code it in c++. Especially since he is using char arrays ( of a size of 50? ). What happens if the weapon name is greater than 50? Also if it is less than 50 then you have all that wasted space.
closed account (3qX21hU5)
As giblit said please ask if you don't understand anything about this code. I would be more then willing to explain anything you don't understand. Also look below for links to all the things I have used. This code also uses C++11 so if you don't have a compiler that supports it or you don't have it turned on this code won't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// This is where you can declare all the tools
namespace Tools
{
    enum
    {
        DiamondPick,
        DiamondHoe,
        NumOfTools
    };
}

// This is where you declare all the stats for each tool.
struct Tool
{
    std::string name;
    unsigned    damage;
};

// Just so we have a central place to init the tools
// We use this function to initialize each tools data.
std::vector<Tool> initTools();

int main()//currently stucked.
{
    // Create a vector for our tools and initialize it with all our tools and their
    // respective data.
    std::vector<Tool> tools = initTools();
    std::string selection;

    // Ask the user to enter which tool he is looking for
    std::cout << "Enter a tools name. Hint: Don't use spaces" << std::endl;
    std::cin >> selection;

    // Make it lowercase so we don't have to working about it not working with uppercase letters
    for (char letter : selection)
        letter = std::tolower(letter);

    // Search our vector for the tool the user wants to find
    auto found = std::find_if(tools.begin(), tools.end(), [&selection] (const Tool& tool) {return tool.name == selection;});

    // If we have found it print out the damage.
    // If not it will equal tools.end() so let the user know we didn't find it.
    if (found != tools.end())
        std::cout << found->name << ": " << found->damage << std::endl;
    else
        std::cout << "That tool was not found." << std::endl;
}

// This is a central place where you can change and add data to each tool
// as you see fit.
std::vector<Tool> initTools()
{
    std::vector<Tool> data(Tools::NumOfTools);

    data[Tools::DiamondPick].name = "diamondpick";
    data[Tools::DiamondPick].damage = 50;

    data[Tools::DiamondHoe].name = "diamondhoe";
    data[Tools::DiamondHoe].damage = 20;

    return data;
}


enum - http://www.cprogramming.com/tutorial/enum.html
vectors - http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/
struct/class - http://www.cplusplus.com/doc/tutorial/classes/
ranged based for loops - http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
find_if - http://www.cplusplus.com/reference/algorithm/find_if/
tolower() - http://www.cplusplus.com/reference/cctype/tolower/
auto keyword - http://en.cppreference.com/w/cpp/language/auto

And if there is anything else again feel free to ask.
Last edited on
Topic archived. No new replies allowed.