Hejsan
Sitter här med ett program och det har blivit lite fel. Ska försöka beskriva problemet och inte skriva all kod som blir för mycket.
Men jag har en funktion void handleDices(Dice dices[], bool locked[]);
Sen i main ska jag anropa den. Dice dices[] är ju en array i en klass.
Så jag vet inte hur jag ska anropa funktionen i main då.
jag skriver handleDices(dices, locked);
men det blir fel, sen skrev jag:
Dice *myDices = new Dice(6);
det fungerade med handleDices(myDices, locked);
men då blev värden helt fel från funktionen då jag kastade tärningarna. Så insåg att det inte blev samma variabel. Fick minusvärden.
Så hur ska jag skriva för att kunna använda samma värden som jag fick när jag slumpade ut värden i klassfunkionen toss.
dice.toss();
verkar vara problemet med handleDices(dices[], locked); går inte skriva tex. Någon som har nåt förslag.
Hoppas jag har förklarat tillräckligt bra, i värsta fall får jag väl ha hela koden.
Okej, här kommer även koden
#ifndef _DICE_H
#define _DICE_H
#include <iostream>
#include <ctime>
using namespace std;
class Dice
{
private:
int values;
int nrOfDices;
public:
Dice();
Dice(int nrOfDices);
void toss();
int getValue();
int dices[];
};
#endif
#include "Dice.h"
Dice::Dice()
{
values = 0;
nrOfDices = 5;
dices[5];
}
Dice::Dice(int nrOfDices)
{
nrOfDices = 5;
}
void Dice::toss()
{
srand((unsigned int)time(0));
for(int i = 0; i < nrOfDices; i++)
{
dices[nrOfDices] = rand() % 6 + 1;
cout << dices[i] << endl;
}
}
int Dice::getValue()
{
for(int i = 0; i < nrOfDices; i++)
{
values += dices[i];
}
return values;
}
#include "Dice.h";
void resetProtocol(int protocol[]);
int valueOfDiceFace(int diceFace, Dice dices[]);
void addToProtocol(int diceFace, int protocol[], Dice dices[]);
void toss(Dice dices[], bool locked[]);
void showDices(Dice dices[]);
void unlockDices(bool locked[]);
void handleDices(Dice dices[], bool locked[]);
void showProtocol(int protocol[]);
void resetProtocol(int protocol[])
{
for(int i = 1; i < 7; i++)
{
protocol[i] = 0;
}
}
int valueOfDiceFace(int diceFace, Dice dices[])
{
for(int i = 1; i < 7; i++)
{
dices[i];
}
return 0;
}
void addToProtocol(int diceFace, int protocol[], Dice dices[])
{
for(int i = 0; i < 7; i++)
{
}
}
void toss(Dice dices[], bool locked[])
{
const int SIZE = 6;
srand((unsigned int)time(0));
for(int i = 0; i < 7; i++)
{
if(locked[i] == true)
{
dices[i];
}
else
{
dices[i] = rand() % 6 + 1;
}
}
}
void showDices(Dice dices[])
{
}
void unlockDices(bool locked[])
{
for(int i = 0; i < 7; i++)
{
locked[i] = 0;
cout << locked[i];
}
}
void handleDices(Dice dices[], bool locked[])
{
const int SIZE = 5;
int nr = 0;
int dice[5];
cout << "Skriv in vilken siffra du vill samla på: ";
cin >> nr;
cin.ignore();
for(int i = 0; i < SIZE+1; i++)
{
dices[i];
}
}
void showProtocol(int protocol[])
{
for(int i = 1; i < 7; i++)
{
cout << i << "or: ";
cout << protocol[i] << endl;
}
}
int main()
{
setlocale(0, "swedish");
Dice dice;
srand((unsigned int)time(0));
bool run = true;
int dicesFace = 0;
Dice dices[6];
bool locked[5];
int protocol[5];
resetProtocol(protocol);
unlockDices(locked);
while(run)
{
showProtocol(protocol);
system("pause");
dice.toss();
handleDices(dices, locked);
}
return 0;
}