Jump to content

number guessing game

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
ligerz785

ligerz785

    Newbie

  • Members
  • Pip
  • 5 posts
ok, ive been learning C++ on my own, and i now have a number guessing game compiled and everything. The only thing that im not sure of is the quality of the program and whether or not it is coded properly.

here is my code:


#include <iostream>

#include <string>

#include <time.h>

#include <cstdlib>

#include <windows.h>

using namespace std;

void setcolor(unsigned short color)                 //The function that you'll use to

{                                                   //set the colour

    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hcon,color);

}


int main()


{ 



	int a,b,c,d,e,f,g;

	string str1, str2, str3;

	

	e = 1;

	while (e == 1)

	{

		g = 11;

		a = 0;

		f = 0;

	string str2 = "yes";

	string str3 = "no";

	

	for(c = 4; c <=1000; c++)

	{

		srand((unsigned)time(NULL));

		a = (rand() % 1000) +1;


	}


	

	d = 11;

	while (d > 0)

	{

	g = g-1;

	d = d-1;

	if (d == 10)

	{ setcolor (9);

		system("CLS");

		cout << "Number of tries left: " << g << endl << endl;

		cout << "Try to guess a number between 1 and 1000 in under 10 tries: ";

	cin >> b;

	}

	

	else if ( (d == 9) && (b == a))

	{ setcolor (4);

		system("CLS");

		cout << "Number of tries left: " << g << endl << endl;

		cout << "WOW!!! .__. You managed to get it on the first try!! That's AMAZING!!!!!!!" << endl;

	d = 0;

	}

	else if ( (d < 10) && (b < a))

	{ setcolor (5);

		system("CLS");

		cout << "Number of tries left: " << g << endl << endl;

		cout << "Your guess was too low, why don't you guess again?  "; 

		cin >> b;

	}

	else if ( (d < 10) && (b > a))

	{ setcolor (15);

		system("CLS");

		cout << "Number of tries left: " << g << endl << endl;

		cout << "Your guess was too high, why don't you guess again?  ";

		cin >> b;

	}

	else if ( (d < 10) && (b == a))

	{ setcolor (11);

		system("CLS");

		cout << "Number of tries left: " << g << endl << endl;

		cout << "CONGRATULATIONS!!! You finally got it! ^_^" << endl;

	d = 0;

	} 

	}

if ( (d == 0) && (b != a))

{ setcolor (12);

	system("CLS");

	cout << "The number was: " << a << endl << endl;

	cout <<  "Well, I'm sorry. You didn't manage to guess it.\nWas this your first time playing?";

}

else if ( (d == 0) && (b == a))

		{ setcolor (3);

			system("CLS");

			cout << "Number of tries left: " << g << endl << endl;

		cout << "CONGRATULATIONS!!! You finally got it! ^_^" << endl;

	d = 0;

	}


while (f != 1)

{ setcolor (13);

cout << endl << endl << "Would you like to play again? (yes/no) ";

cin >> str1;


if (str1.compare(str2) == 0)

{

	e = 1;

	f = 1;

}

else if (str1.compare(str3) == 0)

{

	e = 2;

	f = 1;

}

else 

{ setcolor (14);

	system("CLS");

	cout << "WOW! You really are stupid. -_- You can't even type yes or no....\nGo fall in a hole you waste of space!!\n";

}

	}

}

	return 0;

}






would anybody be able to comment on it and tell me what i could do to improve my overall programming and what i should do now to improve my knowledge of C++?

^_^ thanks in advance for your time and comments

#2
ligerz785

ligerz785

    Newbie

  • Members
  • Pip
  • 5 posts
i forgot to ask this earlier, but how would i go about incorporating the above code in to this


// GT_HelloWorldWin32.cpp

// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c


#include <windows.h>

#include <stdlib.h>

#include <string.h>

#include <tchar.h>

#include <iostream>

#include <time.h>

using namespace std;


void setcolor(unsigned short color)                 //The function that you'll use to

{                                                   //set the colour

    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hcon,color);

}


// Global variables


// The main window class name.

static TCHAR szWindowClass[] = _T("win32app");


// The string that appears in the application's title bar.

static TCHAR szTitle[] = _T("Win32 Guided Tour Application");


HINSTANCE hInst;


// Forward declarations of functions included in this code module:

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hInstance,

                   HINSTANCE hPrevInstance,

                   LPSTR lpCmdLine,

                   int nCmdShow)

{

    WNDCLASSEX wcex;


    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;

    wcex.lpfnWndProc    = WndProc;

    wcex.cbClsExtra     = 0;

    wcex.cbWndExtra     = 0;

    wcex.hInstance      = hInstance;

    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);

    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);

    wcex.lpszMenuName   = NULL;

    wcex.lpszClassName  = szWindowClass;

    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));


    if (!RegisterClassEx(&wcex))

    {

        MessageBox(NULL,

            _T("Call to RegisterClassEx failed!"),

            _T("Win32 Guided Tour"),

            NULL);


        return 1;

    }


    hInst = hInstance; // Store instance handle in our global variable


    // The parameters to CreateWindow explained:

    // szWindowClass: the name of the application

    // szTitle: the text that appears in the title bar

    // WS_OVERLAPPEDWINDOW: the type of window to create

    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)

    // 500, 100: initial size (width, length)

    // NULL: the parent of this window

    // NULL: this application dows not have a menu bar

    // hInstance: the first parameter from WinMain

    // NULL: not used in this application

    HWND hWnd = CreateWindow(

        szWindowClass,

        szTitle,

        WS_OVERLAPPEDWINDOW,

        CW_USEDEFAULT, CW_USEDEFAULT,

        500, 100,

        NULL,

        NULL,

        hInstance,

        NULL

    );


    if (!hWnd)

    {

        MessageBox(NULL,

            _T("Call to CreateWindow failed!"),

            _T("Win32 Guided Tour"),

            NULL);


        return 1;

    }


    // The parameters to ShowWindow explained:

    // hWnd: the value returned from CreateWindow

    // nCmdShow: the fourth parameter from WinMain

    ShowWindow(hWnd,

        nCmdShow);

    UpdateWindow(hWnd);


    // Main message loop:

    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))

    {

        TranslateMessage(&msg);

        DispatchMessage(&msg);

    }


    return (int) msg.wParam;

}


//

//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)

//

//  PURPOSE:  Processes messages for the main window.

//

//  WM_PAINT    - Paint the main window

//  WM_DESTROY  - post a quit message and return

//

//

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    PAINTSTRUCT ps;

    HDC hdc;

    TCHAR greeting[] = _T("Hello, how are you?");

	

    switch (message)

    {

    case WM_PAINT:

        hdc = BeginPaint(hWnd, &ps);

		setcolor (14);


        // Here your application is laid out.

        // For this introduction, we just print out "Hello, World!"

        // in the top left corner.

        TextOut(hdc,

            5, 5,

            greeting, _tcslen(greeting));

		


        // End application-specific layout section.


        EndPaint(hWnd, &ps);

        break;

    case WM_DESTROY:

        PostQuitMessage(0);

        break;

    default:

        return DefWindowProc(hWnd, message, wParam, lParam);

        break;

    }


	return 0;

}


so that the program opens in its own window instead of opening in command prompt.

#3
Zed

Zed

    Newbie

  • Members
  • Pip
  • 4 posts
Hi,
first of all, can you please change you locals names. a, b, c, d .... non understandable. It is vary hard to understand such code. Just use human readable variables names.

regards,
Aram.

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts

"ligerz785" said:

i forgot to ask this earlier, but how would i go about incorporating the above code in to this
It's not something you just do. It's a bigger project than you think, I think. You'll have to find a tutorial, guide or book on Windows development using the Windows API. Another option would be to use a toolkit (like wxWidgets, JUCE, ...), which makes it slightly easier for you.

#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Using a toolkit is cheating.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#6
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
And now you tell me, why is it cheating?

#7
ligerz785

ligerz785

    Newbie

  • Members
  • Pip
  • 5 posts
ok, thanks for the help. ill look at changing the variable names, and i'll keep in mind in future variables to make them understandable.

Quote


Quote:
Originally Posted by ligerz785
i forgot to ask this earlier, but how would i go about incorporating the above code in to this
It's not something you just do. It's a bigger project than you think, I think. You'll have to find a tutorial, guide or book on Windows development using the Windows API. Another option would be to use a toolkit (like wxWidgets, JUCE, ...), which makes it slightly easier for you.

ok, i had noticed that it was rather challenging, and since there is no easy way to do it, i'll just hold off on making app programs like that until i am more solid with C++.

I've started a text-based RPG, and seeing as single letter variables are hard to understand, would it be better to keep the non-named variables since im going to need a lot of them, or should i name them, and make different variants of the same variables? my main problem that i have with that is actually coming up with enough variable names.

again, thanks in advance ^_^

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

v0id said:

And now you tell me, why is it cheating?

Er, no idea. Sorry.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
ligerz785

ligerz785

    Newbie

  • Members
  • Pip
  • 5 posts
ok, this is the code for the RPG game that i was talking about. I've been trying to get it to save a file that the user gives the name for. Once it gets to the end, nothing happens at all, does anybody have any thoughts as to why? and i apologize for the hard to read variables, this program is in its early stages and needs refining.

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <time.h>
#include <cstdlib>
using namespace std;

void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}


int main()
{

	int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as,at,au,av,aw,ax,ay,az,strength, dexterity, constitution, intelligence, wisdom, charisma;

	string charclass, race, alignment, gender, str1, str2, str3, str4, str5, str6, str7, str8, str9;

	char name[10], alias[10], charname[10];

	str8 = "yes";
	str9 = "no";
	b = 1;
	c = 0;

	cout << "Welcome to Crimson Moon." << endl << endl;
	system ("pause");
	system ("CLS");

	cout << "Greetings adventurer, it isn't worth my time to\n remember who has visited here before, so I'm going to ask if\n you already have a name and an alias.\n" << endl;
	cout << "So, do you have a name and an alias? (yes/no)";
	cin >> str7;

	if (str7.compare(str8) == 0)
	{
		cout << "Ok, then please tell me your name.\n\n And do be quick about it, I dont have all day.\n";
		cin >> charname;
		
		

		ifstream character (charname);
		if (character.is_open())
		{
		cout << "The file is working";
		system ("pause");
		}
		else 
		{cout << "The file is not working or does not exist.";
		system ("pause");
		}


	}

	

	else if (str7.compare(str9) == 0)
	{
		system ("CLS");
		cout << " Ok, please tell me your name. Oh, please put a '.cpp' at the end of your name." << endl;
		cin >> name;
		b = 2;
		c += 1;
		ofstream character (name);
		if (character.is_open())
		{
		cout << "File opened";
		while (c != 7)
		{
		
		if (b == 2)
		{
			system("CLS");
		cout << "Now your alias, please." << endl;
		cin >> alias;
		b = 3;
		c += 1;
		}
		else if (b == 3)
		{
			system("CLS");
		cout << "If you dont mind me asking, what race are you..." << endl;
		cin >> race;
		b = 4;
		c += 1;
		}
		else if (b == 4)
		{
			system("CLS");
		cout << "...and what is your gender?" << endl;
		cin >> gender;
		b = 5;
		c += 1;
		}
		else if (b == 5)
		{
		system ("pause");
		system ("CLS");
		cout << "Thank you for your patience, we are almost done." << endl << "Now, what is your alignment?" << endl;
		cout << "1 - Good\n2 - Evil\n3 - Lawful\n4 - Chaotic\n5 - Lawful Good\n6 - Lawful Evil\n7 - Chaotic Good\n8 - Chaotic Evil\n9 - Help\n";
		cin >> a;
		if (a == 1)
		{alignment = "Good";
		b = 6;
		c += 1;}
		else if (a == 2)
		{alignment = "Evil";
		b = 6;
		c += 1;}
		else if (a == 3)
		{alignment = "Lawful";
		b = 6;
		c += 1;}
		else if (a == 4)
		{alignment = "Chaotic";
		b = 6;
		c += 1;}
		else if (a == 5)
		{alignment = "Lawful Good";
		b = 6;
		c += 1;}
		else if (a == 6)
		{alignment = "Lawful Evil";
		b = 6;
		c += 1;}
		else if (a == 7)
		{alignment = "Chaotic Good";
		b = 6;
		c += 1;}
		else if (a == 8)
		{alignment = "Chaotic Evil";
		b = 6;
		c += 1;}
		else if (a == 9)
		{}
		else 
		{system ("CLS");
		cout << "Come on, get with the program! Dx";}
		}
		else if (b == 6)
		{
			system("CLS");
			cout << "Now that all that is out of the way we can get to the exciting part...\n well sorta, you dont get to fight anything yet." << endl;
			system ("pause");
			system ("CLS");
			cout << "Ok, what would you like your class to be?\n1 - Barbarian\n2 - Bard\n3 - Cleric\n4 - Druid\n5 - Fighter\n6 - Monk\n7 - Paladin\n8 - Ranger\n9 - Rogue\n10 - Sorceror\n11 - Wizard\n12 - Help\n";
			cin >> d;
			if (d == 1)
			{
			charclass = "Barbarian";
			strength = 16;
			dexterity = 14;
			constitution = 18;
			intelligence = 10;
			wisdom = 14;
			charisma = 12;
			b += 1;
			c += 1;
			}
			else if (d == 2)
			{
			charclass = "Bard";
			strength = 14;
			dexterity = 18;
			constitution = 14;
			intelligence = 12;
			wisdom = 10;
			charisma = 16;
			b += 1;
			c += 1;
			}
			else if (d == 3)
			{
			charclass = "Cleric";
			strength = 14;
			dexterity = 14;
			constitution = 12;
			intelligence = 10;
			wisdom = 18;
			charisma = 16;
			b += 1;
			c += 1;
			}
			else if (d == 4)
			{
			charclass = "Druid";
			strength = 12;
			dexterity = 14;
			constitution = 14;
			intelligence = 10;
			wisdom = 18;
			charisma = 16;
			b += 1;
			c += 1;
			}
			else if (d == 5)
			{
			charclass = "Fighter";
			strength = 18;
			dexterity = 14;
			constitution = 16;
			intelligence = 14;
			wisdom = 10;
			charisma = 12;
			b += 1;
			c += 1;
			}
			else if (d == 6)
			{
			charclass = "Monk";
			strength = 14;
			dexterity = 16;
			constitution = 12;
			intelligence = 14;
			wisdom = 18;
			charisma = 10;
			b += 1;
			c += 1;
			}
			else if (d == 7)
			{
			charclass = "Paladin";
			strength = 14;
			dexterity = 12;
			constitution = 14;
			intelligence = 10;
			wisdom = 16;
			charisma = 18;
			b += 1;
			c += 1;
			}
			else if (d == 8)
			{
			charclass = "Ranger";
			strength = 14;
			dexterity = 16;
			constitution = 10;
			intelligence = 12;
			wisdom = 18;
			charisma = 14;
			b += 1;
			c += 1;
			}
			else if (d == 9)
			{
			charclass = "Rogue";
			strength = 12;
			dexterity = 18;
			constitution = 14;
			intelligence = 16;
			wisdom = 10;
			charisma = 14;
			b += 1;
			c += 1;
			}
			else if (d == 10)
			{
			charclass = "Sorceror";
			strength = 12;
			dexterity = 14;
			constitution = 10;
			intelligence = 14;
			wisdom = 16;
			charisma = 18;
			b += 1;
			c += 1;
			}
			else if (d == 11)
			{
			charclass = "Wizard";
			strength = 10;
			dexterity = 14;
			constitution = 14;
			intelligence = 18;
			wisdom = 16;
			charisma = 12;
			b += 1;
			c += 1;
			}
			else if (d == 12)
			{}
			
			else 
			{
			system ("CLS");
			cout << "Are you an idiot or something? Enter a number between 1 and 12 you moron! Dx";
			}
			}
			}
			
			
			
			
			
			}
			
			}

			cout << "Hello";


return 0;

}

this has been frustrating me, and i have tried a lot to get it to work, i just have no idea where to go from here.