-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility_func.cpp
More file actions
120 lines (102 loc) · 3.59 KB
/
utility_func.cpp
File metadata and controls
120 lines (102 loc) · 3.59 KB
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
// * ************************************************************************ //
// * * Utility Source * * //
// * * : defines utility function implementations * * //
// * * * * //
// * * Disclaimer: This document represents no significant intellectual * * //
// * * property and my be used freely. The author(s) takes no * * //
// * * responsibility for any loss or damage arising from the use of * * //
// * * the computer code contained herein. Where this document is * * //
// * * submitted as part of an assessment task this header must remain * * //
// * * intact and the student must make clear indication which parts * * //
// * * have been added by themselves. * * //
// * * * * //
// * ********************************************************************** * //
// * ********************************************************************** * //
// * * Utility.cpp * * //
// * * Author: Tim Wilkin * * //
// * * Created: 05/03/11 Last Modified: 12/07/11 * * //
// * ********************************************************************** * //
#include <stdlib.h>
#include <cmath>
#include "cd_game.h"
#include "utility_func.h"
COORD2 operator+(COORD2 const& lhs, COORD2 const& rhs)
{
return lhs += rhs;
}
COORD2 operator-(COORD2 const& lhs, COORD2 const& rhs)
{
return lhs -= rhs;
}
COORD2 operator+=(COORD2 const& lhs, COORD2 const& rhs)
{
COORD2 c = lhs;
c.col += rhs.col;
c.row += rhs.row;
return c;
}
COORD2 operator-=(COORD2 const& lhs, COORD2 const& rhs)
{
COORD2 c = lhs;
c.col -= rhs.col;
c.row -= rhs.row;
return c;
}
bool operator==(COORD2 const& lhs, COORD2 const& rhs)
{
return (lhs.col == rhs.col && lhs.row == rhs.row);
}
bool operator!=(COORD2 const& lhs, COORD2 const& rhs)
{
return !operator==(lhs, rhs);
}
COORD2 operator-(COORD2 const& c)
{
COORD2 nc;
nc.col = -c.col;
nc.row = -c.row;
return nc;
}
COORD2 RandomCoordinate(COORD2 const& lower, COORD2 const& upper)
{
short dx = upper.col - lower.col;
short dy = upper.row - lower.row;
COORD2 p;
p.col = lower.col + rand() % dx;
p.row = lower.row + rand() % dy;
return p;
}
float distance(COORD2 const &p1, COORD2 const &p2)
{
short dx = p2.col - p1.col;
short dy = p2.row - p1.row;
return sqrt(float(dx*dx + dy*dy));
}
#if defined (_WIN32)
void ClearConsole(HANDLE hStdOut)
{
CONSOLE_SCREEN_BUFFER_INFO csbiConsoleInfo;
GetConsoleScreenBufferInfo(hStdOut, &csbiConsoleInfo);
SMALL_RECT srcWindow = csbiConsoleInfo.srWindow;
DWORD dNumChars = (srcWindow.Right - srcWindow.Left + 1)*(srcWindow.Bottom - srcWindow.Top + 1);
COORD cStart;
cStart.X = srcWindow.Left;
cStart.Y = srcWindow.Top;
FillConsoleOutputCharacter(hStdOut, ' ', dNumChars, cStart, &dNumChars);
}
#endif // _WIN32
std::string substring(std::string& str, std::string& delim) {
size_t left = str.find_first_of(delim) + 1;
size_t count = str.find_last_of(delim) - left;
return str.substr(left, count);
}
void unquote(std::string& str) {
std::string q("\"");
str = substring(str, q);
}
size_t IndexOf(const char* str, const char *str_list[], size_t _siz) {
for (size_t i = 0; i<_siz; i++)
if (strcmp(str, str_list[i]) == 0)
return i;
return -1;
}