我正在试验一个游戏程序。我正在尝试生成随机数量的项目。该代码将多次生成相同的项目。我可以设置一系列 switch 语句,这些语句将生成多个搜索区域供玩家浏览,从而每个区域获得一个新的随机项目,但我想学习如何处理我的问题在这里做错了。没有什么比错误更能帮助人们学习了。
我正在使用结构、链表、类和指针。
//genItem.h
#pragma once
struct item
{
char itemName[50];
int itemDamage;
int itemStability;
item* Next;
};
class genItem
{
public:
genItem(void);
~genItem(void);
int count();
int add_item(item* currentItem);
void generate_item(int d2, int s2);
item *Head;
item *Retrieve(int pos);
private:
int size;
int damage;
int stability;
};
//genItem.cpp
#include <iostream>
#include "genItem.h"
#include <stdio.h> // NEED THIS FOR NULL TO WORK
#include <ctime>
using namespace std;
genItem::genItem(void)
:size(0), Head(NULL)
{
}
genItem::~genItem(void)
{
}
int genItem::count()
{
return size;
}
int genItem::add_item(item *thisItem)
{
item *itemObject = new item;
itemObject = thisItem;
itemObject -> Next = Head;
Head = itemObject;
return size++;
}
item *genItem::Retrieve(int position)
{
item *current = Head;
for (int i = count() -1; i > position && current != NULL; i--)
{
current = current -> Next;
}
return current;
}
void genItem::generate_item(int d2, int s2)
{
genItem *listItems = new genItem();
item *listItem;
srand (time(0));
int rn = 0;
int total_in_cat = 10;
int cat_item = 0;
int rand_dam = rand();
int rand_sta = rand();
int per = rand();
int base_d2 = 10;
int base_s2 = 10;
int rand_dam2 = rand();
int rand_sta2 = rand();
cat_item = per % total_in_cat;
d2 = (rand_dam2 % base_d2) +2;
s2 = (rand_sta2 % base_s2) + 2;
if (rn == 0) // mushrooms
{
if(cat_item == 0)
{
listItem = new item;
strcpy_s(listItem -> itemName, "an earthball mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 1)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a devil's bolete mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 2)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a rotting jack o'lantern mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 3)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a fly agaric mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 4)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a poison pie mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 5)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a mature deathcap mushroom");
listItem -> itemDamage = 50;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 6)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a shaggy inkcap mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 7)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a bleeding milkcap mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 8)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a velvet shank mushroom");
listItem -> itemDamage = d2;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
else if (cat_item == 9)
{
listItem = new item;
strcpy_s(listItem -> itemName, "a destroying angel mushroom");
listItem -> itemDamage = 100;
listItem -> itemStability = s2;
listItems -> add_item(listItem);
}
} //end group 0
damage = d2;
stability = s2;
int j = rand();
for (int j =0; j <= 3; j++)
{
cout << "\tJ equals: " << j << endl;
for (int i =0; i < listItems -> count(); i++)
{
item *found = listItems -> Retrieve(i);
cout << "\tYou have found " << found -> itemName << "." << endl;
cout << "\tIt has a damage rating of " << found -> itemDamage;
cout << " and a stability rating of " << found -> itemStability << "."<< endl;
cout << endl;
}
}
}
//main.cpp
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctime>
#include "genItem.h"
using namespace std;
int main()
{
genItem *findItem = new genItem;
int d2 = 0;
int s2 = 0;
findItem ->generate_item(d2, s2);
cout << "\t"; system("pause");
return 0;
}