| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi I just learnt how to do c++ a few days ago so im pretty rubbish but I did know a bit of jscript so it's not completely new. Im trying to make the game mastermind, the one with the pegs. Anyway, I still don't even know how to make an application to run the game on so this prototype version will run entirely on command prompt. However there is one function that won't work, here is the function:
int compare_position_and_color (string x, string z) { int r; r = 0; if (x == z) { r = 1; } return (r); } Basicly the computers peg and the man's peg are sent to this function as x and z. If they are the same then it returns the value 1, otherwise it returns the value 0, reeaallyy simple. Yet for some reason it doesn't work. It comes up with this: error C2440 : "return" : cannot convert from "std :: string" to "int" I think it means something about a string variable trying to turn into a int variable but I don't really know. Could anyone please help me and I would be really gratefull. Here is the rest of the code just in case you need it but i doubt you will: Code:
//My attempt at building a mastermind game
#include <ctime>
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
//Declaring the variables:
string compcolor1,compcolor2,compcolor3,compcolor4;
string mancolor1,mancolor2,mancolor3,mancolor4;
int number_of_red,number_of_green,number_of_blue,numbe r_of_yellow,number_of_black,number_of_white;
// The function that generates four random colours out of red,green,blue,yellow,black, and white:
int random (int a)
{
string r;
if (a<5461)
{
r="red";
goto end;
}
if (a<10922)
{
r="green";
goto end;
}
if (a<16383)
{
r="blue";
goto end;
}
if (a<21844)
{
r="yellow";
goto end;
}
if (a<27305)
{
r="black";
goto end;
}
r="white";
end:
return (r);
}
// The function that compares the computer's colours to the human's colours to see if any of the human's guesses was bang on:
int compare_position_and_color (string x, string z)
{
int r;
r = 0;
if (x == z)
{
r = 1;
}
return (r);
}
// The function that compares one of the humans reds to one of the computer's reds and then takes the computer's red of the list so it cannot be counted twice:
int compare_red ()
{
int r = 0;
if (number_of_red > 0)
{
r = 1;
--number_of_red;
}
return (r);
}
//Same as above function but for green instead of red:
int compare_green ()
{
int r = 0;
if (number_of_green > 0)
{
r = 1;
--number_of_green;
}
return (r);
}
//Same as above but for blue:
int compare_blue ()
{
int r = 0;
if (number_of_blue > 0)
{
r = 1;
--number_of_blue;
}
return (r);
}
//Same as above but for yellow:
int compare_yellow ()
{
int r = 0;
if (number_of_yellow > 0)
{
r = 1;
--number_of_yellow;
}
return (r);
}
//Same as above but for black:
int compare_black ()
{
int r = 0;
if (number_of_black > 0)
{
r = 1;
--number_of_black;
}
return (r);
}
//Same as above but for white
int compare_white ()
{
int r = 0;
if (number_of_white > 0)
{
r = 1;
--number_of_white;
}
return (r);
}
//This function determines what colour the human's choice is so it can be sent to one of the appropriate above functions:
int compare_color (string y)
{
int r = 0;
{
if (y == "red")
{
r = compare_red ();
}
if (y == "green")
{
r = compare_green ();
}
if (y == "blue")
{
r = compare_blue ();
}
if (y == "yellow")
{
r = compare_yellow ();
}
if (y == "white")
{
r = compare_white ();
}
if (y == "black")
{
r = compare_black ();
}
}
return (r);
}
//This function is called to calculate how many of each colour are in the computer's guess:
int number_of (string g)
{
int r = 0;
if (compcolor1 == g) r++;
if (compcolor2 == g) r++;
if (compcolor3 == g) r++;
if (compcolor4 == g) r++;
return (r);
}
//This is the main function where all the input and output take place:
int main ()
{
srand((unsigned)time(0));
compcolor1 = random (rand());
compcolor2 = random (rand());
compcolor3 = random (rand());
compcolor4 = random (rand());
number_of_red = number_of ("red");
number_of_green = number_of ("green");
number_of_blue = number_of ("blue");
number_of_yellow = number_of ("yellow");
number_of_black = number_of ("black");
number_of_white = number_of ("white");
cout << "Welcome to MasterMind by Toby Crisford - the test version. Please note that the final version will be far better graphics and quality, this is just a sneak preview. The 6 possible comlours that the computer can pick are red,yellow,green,blue,black, and white\n\n";
int i;
for (i=1;i<11;i++)
{
cout << "\n\nYou are on guess number " << i << " Enter you guesses below:\n\n";
cout << "guess the first colour here: ";
cin >> mancolor1;
cout << "guess the second colour here: ";
cin >> mancolor2;
cout << "guess the third colour here: ";
cin >> mancolor3;
cout << "guess the fourth colour here: ";
cin >> mancolor4;
int result1a = compare_position_and_color (mancolor1,compcolor1);
if (result1a == 1)
{
int result1b = 0;
goto result2;
}
int result1b = compare_color (mancolor1);
result2:
int result2a = compare_position_and_color (mancolor2,compcolor2);
if (result2a == 1)
{
int result2b = 0;
goto result3;
}
int result2b = compare_color (mancolor2);
result3:
int result3a = compare_position_and_color (mancolor3,compcolor3);
if (result3a == 1)
{
int result3b = 0;
goto result4;
}
int result3b = compare_color (mancolor3);
result4:
int result4a = compare_position_and_color (mancolor4,compcolor4);
if (result4a == 1)
{
int result4b = 0;
goto endofresults;
}
int result4b = compare_color (mancolor4);
endofresults:
int correct_color;
int correct_position_and_color = result1a + result2a + result3a + result4a;
correct_color = result1b + result2b + result3b + result4b;
if (correct_position_and_color == 4) goto victory;
cout << "\n You got " << correct_position_and_color << " in the right position and " << correct_color << " in the wrong place but the right colour!";
}
cout << "\n\n You are now out of guesses I am afraid that you have lost! better luck next time!";
cin >> i;
goto end;
victory:
cout << "\n\n Congratulations you have guessed correctly and have thus won! I hope you enjoyed the game of mastermind - made by Toby Crisford!";
end:
return 0;
}
Last edited by B-Con : February 10th, 2006 at 06:39 PM. Reason: added code tags |
|
#2
|
|||
|
|||
|
Please someone has to help me I want this prototype game finished by thursday evening.
![]() |
|
#3
|
||||
|
||||
WOWSA is right lol, well let me take a peek at it. I gotta keep looking over your code but your error is because you are trying to make a string an int which is a no no. I gotta look over your game a little further because I have never played it so I got to try an understand it before I can give you some more advice. Your doing pretty good for a beginner ![]()
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. Last edited by Geo.Garnett : February 8th, 2006 at 06:27 PM. |
|
#4
|
|||
|
|||
|
Code:
std::string random (int a)
{
string r;
if (a<5461)
{
r="red";
goto end;
}
if (a<10922)
{
r="green";
goto end;
}
if (a<16383)
{
r="blue";
goto end;
}
if (a<21844)
{
r="yellow";
goto end;
}
if (a<27305)
{
r="black";
goto end;
}
r="white";
end:
return (r);
}
well first off, your random function is the one causing the problem...your function returns an integer and you are trying to return string r. When you post code, enclose it in the code tags so it keeps all your spacing and formatting. It is very difficult to understand and follow your app the way it's posted. Your numbers that you're using are very confusing...and you should not have to use goto's at ALL!!! (very big no-no). You'll also want to try and remove those global variables out of your application and try declairing them where they're used. It's a good start for a beginner though. You should probably rewrite this function as the following: Code:
std::string random (int a)
{
string r;
if (a<5461)
r="red";
else if (a<10922)
r="green";
else if (a<16383)
r="blue";
else if (a<21844)
r="yellow";
else if (a<27305)
r="black";
else
r="white";
return (r);
}
|
|
#5
|
|||
|
|||
|
Thanks
Thanks so much I was looking at the wrong function all along to try and work out the problem! You have been a great help and I am making the appropriate changes now
|
|
#6
|
|||
|
|||
|
Quote:
Looking at your code as above, I am not talking about the long code, there is an error. x == z will give you wrong results even if x and z appear equal. To eresolve this use strcmp(x,z) funciton. |
|
#7
|
|||
|
|||
|
Ok
Yes I did get loads of wrong results and I have been through rewriting almost all of the code, if anyone is interested here is the finished version:
Code:
#include <ctime>
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
std::string random (int a)
{
string r;
if (a<5461)
{
r="red";
goto end;
}
if (a<10922)
{
r="green";
goto end;
}
if (a<16383)
{
r="blue";
goto end;
}
if (a<21844)
{
r="yellow";
goto end;
}
if (a<27305)
{
r="black";
goto end;
}
r="white";
end:
return (r);
}
int main()
{
string compcolor[4];
string mancolor[4];
srand((unsigned)time(0));
for (int o=0;o<4;o++)
{
compcolor[o] = random (rand());
}
cout << "Welcome to MasterMind by Toby Crisford - the test version. Please note that the final version will be far better graphics and quality, this is just a sneak preview. The 6 possible colours that the computer can pick are red,yellow,green,blue,black, and white\n\n";
for (int i=1;i<11;i++)
{
cout << "\n\nYou are on guess number " << i << " Enter you guesses below:\n\n";
for (int n=0;n<4;n++)
{
cout << "guess colour " << n+1 << " here: ";
cin >> mancolor[n];
}
string comp[4];
string man[4];
int correct_position = 0;
int correct_color = 0;
for (int h=0;h<4;h++)
{
man[h] = mancolor[h];
}
for (int j=0;j<4;j++)
{
comp[j] = compcolor[j];
}
for (int k=0;k<4;k++)
{
if (man[k] == comp[k])
{
correct_position++;
comp[k] = " ";
man[k] = "x";
}
}
for (int l=0;l<4;l++)
{
for (int m=0;m<4;m++)
{
if (man[l] == comp[m])
{
correct_color++;
comp[m] = " ";
break;
}
}
}
if (correct_position == 4)
{
cout << "\n\n Congratulations you have guessed correctly and have thus won! I hope you enjoyed the game of mastermind - made by Toby Crisford!";
break;
}
else
{
cout << "\n\n You got " << correct_position << " in the right place and " << correct_color << " In the wrong place but the right colour";
if (i == 10)
{
cout << "\n\n You are out of guesses now and I'm afraid you have lost";
}
}
for (int z=0;z<4;z++)
{
comp[z] = compcolor[z];
}
}
cout << "\n\n\nThe correct answer was " << compcolor[0] << " " << compcolor[1] << " " << compcolor[2] << " " << compcolor[3];
int z;
cin >> z;
return 0;
}
It works fine now! |
|
#8
|
|||
|
|||
|
It workd by comparing all the computer's colours to the human's ones and when it finds a match it sets the colour to null so that it can't be counted twice. At the end of the turn the computer's colours are set back to what they were originally.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Please help im a noob at c++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|