Project1Yahtzee

advertisement
Project 1
Due Sunday, April 12 at midnight
For this project, you may choose to work with a partner, or you may choose to work alone. Choose
wisely. A partner’s failure to deliver is no excuse for an unfinished project.
If you choose to work with a partner, both names must be at the top of all files turned in, as well as the
names of both TAs. I will not require that you work with someone in your lab, but I strongly advise it
because next week’s lab will involve working on the project. All files should be zipped and turned in by
one partner. The other partner should submit only the name of her/his partner.
Project: Pseudo-Yahtzee.
This project involves both AVL trees and linked lists.
Overview: The game involves asking a player how many letters they want, then generating a list of that
many random letters. The user must then generate as many random words as s/he can using only those
letters. When the user is done, the user should enter a dummy value (I used “-1”). The list of words
generated is then checked with the dictionary. For every word generated that is in the dictionary, the
user gets 3 points. For every word generated that is not in the dictionary, the user loses 6 points. That’s
basically it.
A couple of notes:





When generating the list of x random letters, the game is pretty dull if no vowels are included.
So I generated a random number y between 1 and x, and then got that many random vowels. I
then got x-y consonants.
letters can be repeated.
To test to see whether you’ve loaded the file and created the AVL tree properly, you should
write an in-order printTree method as part of your AVL class and call it. If the words print out in
order alphabetically, your AVL tree is most likely being created correctly.
The dictionary I used was a scrabble dictionary, which means that most if not all of the words
are 8 characters or less. You may not want to pick a number much bigger than 8, or, as an
alternative, you may want to find your own dictionary.
You can use .size() to get the number of characters in a string. You must include <string>.
The text file I am giving you is called dict.txt, and has over 80000 words in it. Every time I ran the
program, it took my computer about a minute to load the entire dictionary into the AVL tree. Thus for
testing purposes, I created a much smaller dictionary file so I didn’t have to wait that whole minute
while I was debugging.
Outline:
I am including a general outline, which you may or may not choose to use for this project:
Files created include:











BSTY.hpp // the Binary Search Tree class declarations
BSTY.cpp // the Binary Search Tree class method definitions (using AVL balanced tree methods)
/* note: you can start writing this as a regular binary search tree, then modify it to be an AVL
tree later */
NodeTY.hpp // the NodeTree class declaration
NodeTY.cpp // the NodeTree class method definitions (this is for the tree)
Game.hpp // the class declarations for the Yahtzee game
Game.cpp // the class definitions for the Yahtzee game
LL.hpp // the class declaration for a linked list of words
LL.cpp // the class definitions for the linked list of words
NodeL.hpp // the Node class declaration for the linked list of words
NodeL.cpp // the Node class definition for the word linked list
Main.cpp // the main file, which creates a game object and then runs the game’s startGame
method
Note: LL.hpp, LL.cpp, NodeL.hpp and NodeL.cpp should simplified versions of the classes you wrote for
lab 4.
More Details:
My BSTY.hpp:
(not including include files)
class BSTY {
NodeTY *root;
public:
BSTY(); // constructor – sets root to NULL
~BSTY(); // destructor – deletes tree
bool insert(string x);
/* recursively inserts x into the tree with the current root (possibly of a
subtree) being n */
bool insert(string x, NodeTY *n);
/* Note the overloading of methods – this is needed if you choose to write
this method recursively */
void printTreeio();
/* prints out the data in the tree in order (this should print out the
dictionary alphabetically ) */
void printTreeio(NodeTY *n); /* again, needed if you choose recursion
bool search(string x);
/* searches tree for x – returns true if found, false otherwise */
bool search(NodeTY *n, string x);
};
My Game.hpp:
(not including include files)
class Game {
BSTY *dict; // the AVL tree
int numletters; // the number of letters the user wants
char *currletters; //the random set of letters
int numright; // the count of the number of words in the AVL tree
int totalwords; // the count of the total number of words generated
LL wordlist; // the linked list of words the user typed in.
public:
/*constructor, initializes AVL tree from “dict.txt” by calling ReadTreeFromFile
*/
Game();
/* constructor, initializes AVL tree by calling ReadTreeFromFile with infile
*/
Game(string infile);
/* this is the user interface part – it asks how many letters the user wants,
reads that number in, prints out the set of random letters (including at least
one vowel, and then tells the user to start typing in words. Each word typed
in is added to the wordlist (the linked list). When the user enters -1, the
function then calculates the user’s total score by calling a function that
first checks to make sure that each word only includes letters in the set of
random letters and then checks to see if each word in the list is in the AVL
tree. It then prints out the list of valid words and the user’s score. This
function loops until the user no longer wants to play again.
*/
void startGame();
void readTreeFromFile (string dictfile); /* see below for this method*/
/* this method (called by the startGame method) gets a set of x random letters
and returns it.
*/
char * getLetters(int x);
/* this method (called by the startGame method) loops while the user enters
potential words. Each word gets added to the linked list wordlist.
*/
void getWords();
/* checks to see of s only contains letters in currletters (the random set of
letters) and returns true if s only contains valid letters, false otherwise
*/
bool checkWLetters(string s);
/* Goes through the list of words, checks to see if each word contains only
letters in the random list (by calling checkWLetters), then checks to see if
the valid words are in the avl tree and, if it is, increase the user’s score,
and if it isn’t decreases the user’s score.
*/
void checkWordsForScore();
};
The readTreeFromFile method involves file input/output (file io). In this function I create an infile, and then read in
from it until I hit the end of file (eof). I included:
#include <fstream>
Here is my code for this function:
void Game::readTreeFromFile (string dictfile) {
dict = new BSTY();
ifstream file(dictfile.c_str()); // converts a string to a character array
string word;
while (!file.eof()) { // checks for end of file
file >> word;
if (!file.eof()) {
dict->insert(word);
}
}
return;
}
That’s it. When you’re done, zip up the 12 files (including dict.txt) and submit it.
/*********************************************************************************/
If I really wanted to make this game more challenging, there are a couple of things I could do:
1. I could generate every possible permutation of the letters, and check to see whether it’s in the
dictionary and not in the user-generated list of words. For every word in the dictionary but not
in the user’s list, the user would lose a point. (This can generate a lot of permutations,
depending on how many letters chosen)
2. I could add a timer, so the user would have a limited time in which to generate words
3. I could add weights to every word in the dictionary based on their “commonness” or how often
they are used. The rarer a word is, the higher the weight. Users would get extra points for rarer
words. (This would involve reading in a bunch of text files and keeping a list of words and the
count of how often each word occurred. There is something known as IDF, which is how we
calculate the weights of a word’s uniqueness. We use a (usually quite large) set of documents to
get a word list, and take the total count of the number of documents, and divide it by the
number of documents a particular word occurs in.)
Download