Click here to Skip to main content
15,889,773 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm currently making a scrabble game and am having trouble with grabbing tiles from the bag to make the player's hand.

Here is the code for the scrabble class that i have:
C++
#ifndef _SCRABBLE_H
#define _SCRABBLE_H

#include <string>
#include <stdlib.h>
#include <time.h>
#include "bagoftiles.h"
#include "topten.h"
#include "hand.h"
#include "randgen.h"

class scrabble
{
public:
	void grabtiles(hand h, bagoftiles b);
};

void scrabble::grabtiles(hand h, bagoftiles b)
{
	RandGen r;
	for (int i = 0;i < 7;i++) {
		h.sett(i, b.gett(r.RandInt(100)));
	}
}


But when i try to run it, the tiles end up showing as blanks with the letter as nothing and the value as 0.

and to clarify here are my classes:

Actual Tile:
C++
#ifndef _TILE_H
#define _TILE_H

#include <string>
#include <sstream>
#include <fstream>
using namespace std;

class tile 
{
public:
	tile();
	tile(string l, int v);

	string getl();
	int getv();
	void setl(string &l);
	void setv(int &v);
private:
	string myl;
	int myv;
};

tile::tile()
{
	myl = "";
	myv = 0;
}

tile::tile(string l, int v)
{
	myl = l;
	myv = v;
}

string tile::getl()
{
	return myl;
}

int tile::getv()
{
	return myv;
}

void tile::setl(string &l)
{
	myl = l;
}

void tile::setv(int &v)
{
	myv = v;
}
#endif

Class for Player's Hand:
C++
#ifndef _HAND_H
#define _HAND_H

#include <stdlib.h>
#include <time.h>
#include <string>
#include "tile.h"
using namespace std;

class hand
{
public:
	hand();
	hand(tile t);
	tile gett(int t);
	void sett(int n, tile t);
private:
	tile myt[7];
};

hand::hand()
{
	for (int i = 0;i < 7;i++) {
		myt[i].getl() = "";
	}
}

hand::hand(tile t)
{
	for (int i = 0;i < 7;i++) {
		myt[i] = t;
	}
}

tile hand::gett(int t)
{
	return myt[t];
}

void hand::sett(int n, tile t)
{
	myt[n] = t;
}
#endif

Bag Of Tiles Class:
C++
#ifndef _BAGOFTILES_H
#define _BAGOFTILES_H

#include <stdlib.h>
#include <time.h>
#include <string>
#include "tile.h"
using namespace std;

class bagoftiles
{
public:
	bagoftiles();
	bagoftiles(tile t[]);
	tile gett(int t);
	void gettiles();
	void shuffle();
private:
	tile myt[100];
};

bagoftiles::bagoftiles()
{
	for (int i = 0;i < 100;i++){
		myt[i].getl() = "";
	}
}

bagoftiles::bagoftiles(tile t[])
{
	for (int i = 0;i < 100;i++) {
		myt[i] = t[i];
	}
}

tile bagoftiles::gett(int t)
{
	return myt[t];
}

void bagoftiles::gettiles()
{
	string line, sscore;
	int i = 0, freq;
	ifstream myfile("tiles.txt");
	if (myfile.is_open())
	{
		int n = 0;
		while (getline(myfile, line))
		{
			istringstream buffer(line.substr(3, 2));
			buffer >> freq;
			while (i < freq) {
				myt[n].setl(line.substr(0, 1));
				int value;
				istringstream buffer(line.substr(1, 2));
				buffer >> value;
				myt[n].setv(value);
				n++;
				i++;
			}
			i = 0;
		}
		myfile.close();
	}
}

void bagoftiles::shuffle()
{
	srand((unsigned)time(NULL));
	int r;
	tile temp;
	for (int i = 0;i < 100;i++) {
		r = (rand() % 100);
		temp = myt[r];
		myt[r] = myt[i];
		myt[i] = temp;
	}
}
#endif


Main (actual tester):
C++
#include "scrabble.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	hand ht;
	bagoftiles bt;
	bt.gettiles();
	bt.shuffle();
	scrabble sc;
	sc.grabtiles(ht, bt);
	//for (int i = 0;i < 100;i++) {
	//	cout << bt.gett(i).getl() << '\t' << bt.gett(i).getv() << endl;
	//}
	for (int i = 0;i < 7;i++) {
		cout << ht.gett(i).getl() << '\t' << ht.gett(i).getv() << endl;
	}
	system("pause");
	return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 29-Nov-15 11:38am    
Sorry, but this is not a question.
—SA
Richard MacCutchan 29-Nov-15 11:41am    
One of the difficulties with code like this is all the meaningless single letter variable names make it almost impossible to understand. So most of us take one look and say "tl;dr"; maybe we should say 'short' instead.

1 solution

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.

[Update]
You know that the grabbed tiles are empty.
- Check that the bag tiles are not empty.
- Check what append when you grab a tile from the bag.
- Check what append until you display the empty tiles.
 
Share this answer
 
v2
Comments
CodeMaster9 29-Nov-15 11:11am    
Yes but I have tried to find the problem and it keeps on doing the same thing. I checked every line and didn't find an error.
Patrice T 29-Nov-15 11:21am    
Inspect the variables as you grab the tiles.
you will find where you fail to copy the tiles values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900