拼字游戏玩家的手 [英] Scrabble game player's hand
本文介绍了拼字游戏玩家的手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在制作一个拼字游戏,并且无法从包中抓取瓷砖以制作玩家的手。
这是拼字游戏的代码我有的课程:
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:
#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)));
}
}
但是当我尝试运行它时,瓷砖最终显示为空白字母为空,价值为0.
并在此澄清我的课程:
实际平铺:
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:
#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:
#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类:
Bag Of Tiles Class:
#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(实际测试人员):
Main (actual tester):
#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;
}
推荐答案
你的代码没有你想象的那样,你不明白为什么!
有一个几乎通用的解决方案:逐步在调试器上运行代码。
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
这个解决方案的缺点:
- 这是一个DIY,你是跟踪问题并找到根源的人,这导致了解决方案。
这个解决方案的好处:
- 你看到你的代码行为,你把它与你的期望相匹配。
次要效果
- 你会为自己找到虫子感到骄傲。
- 你的技能会提高。
你应该很快就会发现什么是错的。
[更新]
你知道抓起的瓷砖空的。
- 检查袋子瓷砖是不是空的。
- 检查从袋子里取出瓷砖时附加的东西。
- 检查附加内容,直到显示空的瓷砖。
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.
这篇关于拼字游戏玩家的手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文