hello, could i get some advice?
Aug 23, 2011 at 3:12am UTC
ive recently started programming again, and as i had been told a long time ago, im trying to program a copy of the game mastermind. currently my code is supposed to pick a random number between 1 and 6 and then based on that, designate what the colors for the "mastermind's code" will be.ive stopped at the point where its selecting the colors, and am having it output the results for testing purposes, the only problem is that its not random enough, and if it is random, then it is horrifically slow. if anybody cares to help, heres my code.
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include <iostream>
#include <time.h>
#include <string>
int main()
{
int counter, cycle, selection;
std::string color1,color2,color3,color4,guess1,guess2,guess3,guess4;
cycle=0;
while (cycle <= 3)
{
for (counter=0;counter<=100;counter++)
{
srand((unsigned )time(NULL));
selection = ((rand() % 6)+1);
}
std::cout << selection << std::endl;
if (cycle==0&&selection==1)
{
color1 = "red" ;
}
else if (cycle==0&&selection==2)
{
color1 = "blue" ;
}
else if (cycle==0&&selection==3)
{
color1="green" ;
}
else if (cycle==0&&selection==4)
{
color1="yellow" ;
}
else if (cycle==0&&selection==5)
{
color1="black" ;
}
else if (cycle==0&&selection==6)
{
color1="white" ;
}
else if (cycle==1&&selection==1)
{
color2="red" ;
}
else if (cycle==1&&selection==2)
{
color2="blue" ;
}
else if (cycle==1&&selection==3)
{
color2="green" ;
}
else if (cycle==1&&selection==4)
{
color2="yellow" ;
}
else if (cycle==1&&selection==5)
{
color2="black" ;
}
else if (cycle==1&&selection==6)
{
color2="white" ;
}
else if (cycle==2&&selection==1)
{color3="red" ;}
else if (cycle==2&&selection==2)
{color3="blue" ;}
else if (cycle==2&&selection==3)
{color3="green" ;}
else if (cycle==2&&selection==4)
{color3="yellow" ;}
else if (cycle==2&&selection==5)
{color3="black" ;}
else if (cycle==2&&selection==6)
{color3="white" ;}
else if (cycle==3&&selection==1)
{color4="red" ;}
else if (cycle==3&&selection==2)
{color4="blue" ;}
else if (cycle==3&&selection==3)
{color4="green" ;}
else if (cycle==3&&selection==4)
{color4="yellow" ;}
else if (cycle==3&&selection==5)
{color4="black" ;}
else if (cycle==3&&selection==6)
{color4="white" ;}
cycle++;
int timewaster;
timewaster = 0;
while (timewaster < 888888887)
{
timewaster++;
}
}
std::cout << color1 << std::endl << color2 << std::endl << color3 << std::endl << color4 << std::endl;
std::cout << "Insert a random number for testing purposes." ;
int random_number;
std::cin >> random_number;
return 0;
}
Last edited on Aug 23, 2011 at 3:30am UTC
Aug 23, 2011 at 4:23am UTC
i figured out that if i removed the srand((unsigned)time(NULL)); from the for loop and put it at the beginning of the program, that it only seeds rand once, and actually helps to make it random. sorry if i wasted anybody else's time with this post.
Aug 23, 2011 at 4:39am UTC
I'm sure nobody's time was wasted.
Also, consider using arrays (this will remove the massive
if {...} else if {...} else if {...}
)
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
const int NUM_CYCLES = 4;
const int NUM_COLORS = 6;
const std::string all_colors[NUM_COLORS] = { "red" , "blue" , "green" , "yellow" , "black" , "white" };
int counter, cycle, selection;
std::string colors[NUM_CYCLES];
std::string guesses[NUM_CYCLES];
//...
if (cycle==0&&selection==1)
{
color1 = "red" ;
}
else if (cycle==0&&selection==2)
{
color1 = "blue" ;
}
else if (cycle==0&&selection==3)
{
color1="green" ;
}
else if (cycle==0&&selection==4)
{
color1="yellow" ;
}
else if (cycle==0&&selection==5)
{
color1="black" ;
}
else if (cycle==0&&selection==6)
{
color1="white" ;
}
colors[ cycle ] = all_colors[ selection - 1 ];
Last edited on Aug 23, 2011 at 4:42am UTC
Topic archived. No new replies allowed.