7 Replies - 1084 Views - Last Post: 22 January 2009 - 07:05 AM Rate Topic: -----

#1 mlh640  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-January 09

guessing game with a database?

Posted 20 January 2009 - 06:34 PM

I'm working on a guessing game where you compete against the computer you guess it's number and then it guesses your number. It then sees who guessed in the fewest number of tries. Right now the computer guesses randomly and the random range is narrowed with each guess. sometimes it is good and others not so code - kindof random.

I want to create a method that will store the choices made by the computer and the human to then analyze. I'm not sure if a database is the best choice for this or not. I'm using C++ and am a little daunted by seemingly lack of info on databases for c++. My current coding works, just need some pointers to move forward in databasing this project or another method to accomplish the same goals. Thanks ahead of time.

Mike

Is This A Good Question/Topic? 0
  • +

Replies To: guessing game with a database?

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: guessing game with a database?

Posted 20 January 2009 - 09:31 PM

Provide what you have, people might be more willing to help.
Was This Post Helpful? 0
  • +
  • -

#3 mlh640  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-January 09

Re: guessing game with a database?

Posted 20 January 2009 - 10:35 PM

View PostHyper, on 20 Jan, 2009 - 08:31 PM, said:

Provide what you have, people might be more willing to help.

#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
	srand(time(NULL));
	int comp_tries;
	int tries;
	int guess;
	int answer;
	int Rand_Num;
	int low = 1;
	int high = 100;
	char again = 'Y';
	guess = rand() % high + low;
	
	cout << "Hi! Let's play a game that I can learn from.\n";
	cout << "I'll pick a number between 1 and 100 and you"
		 << " will try to guess and then you'll pick the number"
		 << " and I'll guess it.\n\n";
	cin.get();
	while (again == 'Y') {
		cout << "Please pick a number between 1 and 100 and I will try to guess it.\n";
		cout << "Please hit the enter key when you are ready to begin.";
		cin.get();
		cout << "Is your number: " << guess << "?\n";
		comp_tries = 1;
		cout << "Type 1 if yes, 2 if your number is lower or 3 if it is higher: ";
		cin >> answer;
	
		for (; answer !=1; comp_tries++) {
			if (answer == 2) {
				high = guess;
				guess = rand() % ((high--)-low) + low;
				cout << "Then is your number: " << guess << "?\n";
				cout << "Type 1 if yes, 2 if your number is lower or 3 if it is higher: ";
				cin >> answer;
				cin.ignore();
			}   
			else if (answer == 3) {
				low = guess;
				guess = rand() % (high-(low++)) + (low++); 
				cout << "Then is your number: " << guess << "?\n";
				cout << "Type 1 if yes, 2 if your number is lower or 3 if it is higher: ";
				cin >> answer;
				cin.ignore();
		   }
		}  
		cout << "I guessed your number in " << comp_tries << " tries.\n";
		cin.get();
		
		
		Rand_Num = rand() % 100 + 1;
		cout << "Try to guess my number between 1 and 100: ";
		cin >> guess;
		tries = 1;
		while (guess != Rand_Num) {
			if (guess > Rand_Num) {
				cout << "Sorry! Please guess a lower number: ";
				cin >> guess;
				tries = tries + 1;
			}
			if (guess < Rand_Num) {
				cout << "Sorry! Please guess a higher number: ";
				cin >> guess;
				tries = tries + 1;
			}
		}
		cin.ignore();	
		cout << "You guessed my number in " << tries << " tries. Good Job\n";
		if (comp_tries > tries) {
			cout << "You beat me at my own game!\n";
		}
		else if (comp_tries < tries) {
			cout << "You humans aren't very logical are you? I won!\n";
		}
		else if (comp_tries == tries) {
			cout << "We tied! We better have a rematch.\n";
		}
		cout << "Would you like to challenge me again? Y/N:\n";
		cin >> again;
	
	}
	cin.ignore();
	cout << "Thanks for playing with me! Come back soon";
	cin.get();
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#4 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: guessing game with a database?

Posted 21 January 2009 - 07:54 AM

If you want to store them, try File I/O and store them in .txt files. I think you shouldn't be using databases for this small project :), but hey, you can do whatever you want :D.
Was This Post Helpful? 0
  • +
  • -

#5 corliss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 189
  • Joined: 25-October 06

Re: guessing game with a database?

Posted 21 January 2009 - 10:04 AM

View Postmanzoor, on 21 Jan, 2009 - 06:54 AM, said:

If you want to store them, try File I/O and store them in .txt files. I think you shouldn't be using databases for this small project :), but hey, you can do whatever you want :D.

Agreed.
I would use encrypted Files just to make it fun.....and secure.
Was This Post Helpful? 0
  • +
  • -

#6 mlh640  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-January 09

Re: guessing game with a database?

Posted 21 January 2009 - 09:36 PM

thanks for the information guys.
Was This Post Helpful? 0
  • +
  • -

#7 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: guessing game with a database?

Posted 21 January 2009 - 09:58 PM

View Postmlh640, on 20 Jan, 2009 - 05:34 PM, said:

I'm using C++ and am a little daunted by seemingly lack of info on databases for c++.


You might be looking at it from the wrong perspective. C++, and most programming languages, don't *have* databases. They *use* databases via a API provided by the database vendor, usually in the form of a static or dynamic library. C/C++ are pretty much universally supported by every database I know, probably because the database itself is written in C/C++ and that is the first binding (API library) they create.

Here is a short list:

PostgreSQL
MySQL
Berkeley DB (formally sleepycat)
SQLite
Oracle
Raima
Codebase
Xbase++

Do a search, there are *many* options. Depending on how much data you intend to collect, and what you mean by "analyze", your choice of a database vs. text files (as suggested by other posters) may vary. An SQL based solution would be good if you indend to do a lot of analysis on a lot of data. It all depends on your requirements and how far you are going to go with this.

Matthew

This post has been edited by matthew180: 21 January 2009 - 09:59 PM

Was This Post Helpful? 1
  • +
  • -

#8 mlh640  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-January 09

Re: guessing game with a database?

Posted 22 January 2009 - 07:05 AM

View Postmatthew180, on 21 Jan, 2009 - 08:58 PM, said:

You might be looking at it from the wrong perspective. C++, and most programming languages, don't *have* databases. They *use* databases via a API provided by the database vendor, usually in the form of a static or dynamic library. C/C++ are pretty much universally supported by every database I know, probably because the database itself is written in C/C++ and that is the first binding (API library) they create.



So let me get this straight. If I want to access data from a database using MySQL I go to the website, get their API get a book on programming mysql and then I use the coding in the mysql in my c++ program? Just making sure so I don't tackle this problem from the wrong angle. Thanks for your comments.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1