4 Replies - 641 Views - Last Post: 13 January 2008 - 08:18 AM Rate Topic: -----

#1 Trake  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 29-June 07

Is there a better way of doing this?

Post icon  Posted 10 January 2008 - 06:43 PM

Hi all, I have just started creating a small game in C++ and below is the code I have so far (not much I know). But before I go further I want to know if what I am doing is using best practices, especially the error checking.

Thanks for any help

Trake

#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <time.h>
#include <windows.h>

//Global variables
int difficulty = 0;

void playerCreate();

int main()
{
	srand ( time(NULL) ); //start srand

	char newGame; //local variable for getting start or exit

	while (newGame != 'q' && newGame != 'Q')
	{
		std::cout << "Hello, and welcome to Adventure Land.\nWould you like to start a new game? (q to quit)\n";
		std::cin >> newGame;			
		if (newGame == 'y' || newGame == 'Y')
		{
			playerCreate();
		}
		else
		{
			std::cout << "\nThat is not a valid choice please choose again.\n";
		}

	}
	std::cout << "\nThank you for playing!\n";
	return 0;
}

void playerCreate()
{
	system("cls"); //clear screen

	std::cout << "\nPlease select a difficulty from 1(easiest) to 3(hardest).\n";
	while (!(difficulty >=1 && difficulty <= 3))
	{
	std::cin >> difficulty;
	if (!(difficulty >=1 && difficulty <= 3))
	{
		std::cout << "\nThat is not a valid choice please choose again.\n";
	}
	}


} 


Is This A Good Question/Topic? 0
  • +

Replies To: Is there a better way of doing this?

#2 VernonDozier  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 06-January 08

Re: Is there a better way of doing this?

Posted 11 January 2008 - 12:39 AM

I ran it. It worked. As far as "best practices" go, a lot of people discourage global variables like how you used "difficulty" in your solution. Me, I use 'em, but I think I'm in the minority as far as that best practice goes. Also, you're comparing newGame to a value before it is initialized. It's unlikely, but what if the last value in that memory address was the ascii value of 'q' or 'Q'? You should initialize it to something before comparing just to be sure. Same thing goes with "difficulty". Though the odds are way against the chance that it was 1, 2, or 3 before you declared it, since you didn't initialize it, it could have been and the user would never be prompted. You could initialize both to 0 off the bat just to be safe.
Was This Post Helpful? 0
  • +
  • -

#3 mmakrzem  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 56
  • Joined: 11-January 08

Re: Is there a better way of doing this?

Posted 11 January 2008 - 10:26 AM

"best" is very relative. Depends on what you plan on doing with it I guess.

I avoid using global variables, and put everything inside of a class. Each class has its own .h and .cpp file.

That way if I need to reuse code in the future, I can just grab the pieces I need without have to go into code, and cut and paste.
Was This Post Helpful? 0
  • +
  • -

#4 Trake  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 29-June 07

Re: Is there a better way of doing this?

Posted 13 January 2008 - 05:06 AM

Thanks for the advice both, when it comes to classes, is it better to use classes over structs for something like this? Or what are the advatages/disadvantages of both?
I have tried to use classes before, but I end up setting things as public which makes me think that a struct with public as default is better?

Thanks in advance

Trake
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Is there a better way of doing this?

Posted 13 January 2008 - 08:18 AM

In C++, classes are generally preferred, with structs being consider just little above primitives for usage cases. In reality, structs and classes can be near identical, so it's more an expression of intent.

You could do something like this:
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

class Player {
private:
	int difficulty;
public:
	Player(int difficulty);
	int getDifficulty();
	void display(ostream& out);
};
Player::Player(int difficulty) { this->difficulty = difficulty; }
int Player::getDifficulty() { return this->difficulty; }
void Player::display(ostream& out) {
	out << "Player: " << this->difficulty << endl;
}


Player * playerCreate() {
	cout << endl;
	cout << "Please select a difficulty from 1(easiest) to 3(hardest)." << endl;
	
	int difficulty = -1;
	while (true) {
		cin >> difficulty;
		if (difficulty >=1 && difficulty <= 3) {
			return new Player(difficulty);
		}
		cout << "\nThat is not a valid choice please choose again.\n";
	}
} 


char getResponseChar() {
	char ch;
	cin >> ch;
	return tolower(ch);
}


int main() {
	Player *player = NULL;
	srand ( time(NULL) ); //start srand

	while (true) {
		cout << "Hello, and welcome to Adventure Land." << endl;
		cout << "Would you like to start a new game? (q to quit)" << endl;
		char newGame = getResponseChar();
		if (newGame == 'y') {
			player = playerCreate();
			player->display(cout);
			delete(player);
		} else if (newGame =='q') {
			break;
		} else {
			cout << endl << "That is not a valid choice please choose again." << endl;
		}
	}
	cout << endl << "Thank you for playing!" << endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1