Data Storage Question, and Dynamic variables?
Ok iv got 2 questions off the bat, and would also like my code to be looked at to see if you can see any major problems offhand. I mean like style problems, but first, questions hehe.
1.) 'Data Storage'
I am making a little text based game just to get the syntax of basic functions into my head, its all going fine but i came up to a little problem. In this game you roll Six dice, well to keep track of each die i used an array like "array[7]" and used 1-6 to keep track of the dice. 0 is an empty value just cuz this seemed easier lol. Well now i need to check the data to see if any of the dice randomly generated exist 3 times. Here is an example.
I roll: 3, 1, 2, 3, 5, 3.
I need to check how many times each number exists (numbers 1-6, on dice ofcourse.). But i cant quite think of a good way to do it. in C++ that is lol. Im not too sure my array data storage is the best the way im using it already. So how would i do that? A simple loop running through each number and die ( number[1] dice[1] ) and storing its number then after the loop is done checking how many numbers exist, then looping through the number array and seeing if any of the values in each element is more than 3 would work i think.. (still with me?) but is all this the best way?
2.) 'Dynamic Variables'
Is there such an operator to allow umm joining? like "myStringVar = word1 $+ word2" would be "myStringVar = word1word2", or in such a case, dynamic variable naming, like "myVarOne $+ x = SomeValue", where x would be for example a number running in a loop. So if the loop ran 6 times you would have 6 variables. "myVarOne1, myVarOne2, myVarOne3" ect.
3.) 'My Horrible Code'
If you got a sec, view my horrible code lol. Keep in mind i am a TOTAL C++ NUB lol. And that this code IS NOT DONE! The bottom is left undone, i relize there are loops that have to break in them yet. But none the less just little things or big things. If your confused by anything, like what the point of the game is, or what for example "x" is representing, dont worry about it. Im not in need of examination yet, but since im posting i figured id post and see whats happends. Thanks!
*Warning, n00b code below. Shield your Eyes*
Code:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
int Dice_Ret(int numOfDie);
int Random_Range(int lowest_number, int highest_number);
int Rand_Test();
int menu();
void Game_Stage1();
int main()
{
cout<<"Welcome to Zonc Alpha\nDisplaying Main Menu..\n";
int exit = 0;
while(1){
switch(menu()){
case 1:
Rand_Test();
break;
case 2:
Game_Stage1();
break;
case 4:
cout<<"Exiting...\n";
exit = 1;
break;
case 5:
cout<<"Random Dice Generating: "<<Random_Range(1,6)<<"\n\n";
break;
default:
cout<<"Invalid Response, Please choose again.\n";
break;
}
if(exit == 0)
continue;
else
break;
}
cout<<"Exited.\n";
}
int Rand_Test()
{
int arreh[7];
arreh[1] = 0, arreh[2] = 0, arreh[3] = 0, arreh[4] = 0, arreh[5] = 0, arreh[6] = 0;
int c, r = 60000;
cout<<"Out of "<<r<<" rolls of one die, these are the numbers generated.\n";
for(int a = 0; a <= r; a++){
c = Random_Range(1,6);
arreh[c] = arreh[c]++;
}
for(int a = 1; a <= 6; a++){
cout<<a<<" Was Generated \""<<arreh[a]<<"\" Times.\n";
}
}
int Random_Range(int x, int y)
{
if(x > y){
swap(x,y);
}
int range = y - x + 1;
return x + int(range * rand()/(RAND_MAX + 1.0));
}
int Dice_Ret(int numofDie)
{
}
int menu()
{
cout<<"\n\n";
cout<<"]----------------------------[\n";
cout<<" 1: Displays the dice randomization test\n";
cout<<" 2: Enter Beta Zonc 1\n";
cout<<" 3: Display Menu Again\n";
cout<<" 4: Exit\n";
cout<<" 5: Roll Die\n";
cout<<"]----------------------------[\n";
int a;
cout<<"What do you choose: ";
cin>>a;
return a;
}
void Game_Stage1() {
int dice[1] = 0, dice[2] = 0, dice[3] = 0, dice[4] = 0, dice[5] = 0, dice[6] = 0;
unsigned int totalscore = 0, side = 0, totalcurrent = 0, input, zonc = 0;
cout<<"\n\nWelcome to Zonc Beta 1, This is a single player game.\n"
"The object of this is to test the coding of the game.\nIf you dont know how to play,\ninstructions should be in the main menu.\n"
"if not, well your out of luck then because Zeus hasent coded them yet.\n\n";
cout<<"Please choose a Gameplay Goal (the number you win if you reach, must be higher than 1k): ";
unsigned int goal;
cin>>goal;
while((goal <= 1000) || (goal > 50000)){
cout<<"\n\nError, Your Goal is too small, Please input a new Goal: ";
cin>>goal;
}
cout<<"\n\nCommands;\nQ - Type at anytime to exit.\nRoll - Rolls the dice, assuming you can by the rules.\n"
"Keep - Keeps a single die, use multiple times for multiple die.\nScore - Displays your total score (including your current roll and on the side).\n";
int continue = 1, roll = 1; // Roll checks if the user has put away atleast one die, allowing them to roll again. On be default.
while(continue){
//abandoned this type of tracking multiples of the same dice number. int zc-one = 0, zc-two = 0, zc-three = 0, zc-four = 0, zc-five = 0, zc-six = 0; //Zonc Check Variables, each one stands for a dice number, a loop checks for 3 of one number.
for(int a = 1; a <= 6; a++)
dice[a] = Random_Range(1,6);
cout<<"Rolling..\nDisplaying Roll Results..\n"
"Dice1: "<<dice[1]<<", Dice2: "<<dice[2]<<", Dice3: "<<dice[3]<<", Dice4: "<<dice[4]<<", Dice5: "<<dice[5]<<", Dice6: "<<dice[6]<<"\n";
for(int a = 1; a <= 6; a++){ //Check if the player Zonced
zonc = 1;
if((dice[a] == 1) || (dice[a] == 5))
zonc = 0;
}
}
}