I need better code examples

closed account (G30oGNh0)
I give you my current source on a simple Rogue style game.

It's a very basic design but I want further input on the systems used before I begin and get's too hard coded to change.

So far a simple movement system, character system and a openJournal function.

Quote code and suggest better/easier/efficient perspectives, thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 #include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

using std::string;
using std::getline;

char keyPressed;
bool inGame = true;

void retrieveAction();
void retrieveAction(char menu);
void processAction();
void openJournal();

const int wmx = 10;
const int wmy = 10;

string worldMap[wmy][wmx] = {
	{ "Yesteryear", "Graveyard", "Golden Palace", "Yarven's Lake", "Helms Peak", "Wasteland", "Dragonstone", "Troll Cave", "Murky Cavern", "Blackwater" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" },
	{ "", "", "", "", "", "", "", "", "", "" }
};

struct playerOne
{
	string heroName;
	string heroClass;
	int heroX;
	int heroY;
	string heroLocation()
	{
		return worldMap[heroY][heroX];
	}
	bool canMove(int nHeroX, int nHeroY)
	{
		if((nHeroX < 0 || nHeroX >= wmx) && (nHeroY < 0 || nHeroY >= wmy))
		{
			return false;
		}
		return true;
	}
}plr;

void createPlayer();

int main()
{
	createPlayer();

	while(inGame)
	{
		retrieveAction();
		processAction();
	}
	return 0;
}

void retrieveAction()
{
	cout << "$> ";
	cin >> keyPressed;
}

void retrieveAction(char menu)
{
	switch(menu)
	{
	case 'j':
		cout << "$>Journal> ";
		cin >> keyPressed;
		cin.ignore();
		// processJournalAction()
		return;
	default: cout << "\nNo action found\n" << endl;
	}
}

void processAction()
{
	char *action;
	action = &keyPressed;
	switch(*action)
	{
	case 'w':
		if(plr.canMove(plr.heroX, plr.heroY + 1))
		{
			plr.heroY++;
		}
		else
		{
			cout << "\nIt would be unwise to travel into the wilderness." << endl;
		}
		return;// move north
	case 's': 
		if(plr.canMove(plr.heroX, plr.heroY - 1))
		{
			plr.heroY--;
		}
		else
		{
			cout << "\nIt would be unwise to travel into the wilderness." << endl;
		}
		return;// move south
	case 'a': 
		if(plr.canMove(plr.heroX - 1, plr.heroY))
		{
			plr.heroX--;
		}
		else
		{
			cout << "\nIt would be unwise to travel into the wilderness." << endl;
		}
		return;// move west
	case 'd': 
		if(plr.canMove(plr.heroX + 1, plr.heroY))
		{
			plr.heroX++;
		}
		else
		{
			cout << "\nIt would be unwise to travel into the wilderness." << endl;
		}
		return;// move east
	case 'j': 
		openJournal();
		while(*action != 'x')
		{
			retrieveAction(keyPressed);
		}
		return;
	default: cout << "\nNo action found\n" << endl;
	}
}

void openJournal()
{
	cout << "\n[Player] " << plr.heroName << "\t[Class] " << plr.heroClass << "\t[Location] " << plr.heroLocation() << endl;	
}

void createPlayer()
{
	cout << "Welcome to Rogue." << endl;
	cout << "Great adventures await..." << endl;
	cout << "\nWhat is your name? ";
	getline(cin, plr.heroName);
	plr.heroX = 0;
	plr.heroY = 0;
}
Last edited on
I would start with making a world and position class / struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct position
{
  int x;
  int y;
};

struct world
{
  // being as close to your code as possible
  int width;
  int height;

  bool has_position ( const position& pos )
  {
    return pos.x < width && // ...
  }
};

struct player
{
  position p;

  void move( const position& pos ) { p = pos; }

  // others
};
closed account (G30oGNh0)
Hi LowestOne, thanks for the reply.

Late last night I was converting the player to a class, but I always run into problems, so far it's going well. I have also separated the movement from the processAction function and starting to call plr.movePlayer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void processAction()
{
	switch(keyPressed)
	{
	case 'w':
		plr.movePlayer();
		return;// move north
	case 's': 
		plr.movePlayer();
		return;// move south
	case 'a': 
		plr.movePlayer();
		return;// move west
	case 'd': 
		plr.movePlayer();
		return;// move east
	case 'j': 
		plr.openJournal();
		return;
	default: cout << "\nNo action found\n" << endl;
	}
}


Out of interest, why have you wrote 3 separate structures? Would it be easier to contain player specific variables ( such as it's position ) to a single class? Could you explain how these structs would be used?

Thanks in advance.
Topic archived. No new replies allowed.