Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
// The Peg Class Functions
2
// Revision: $Revision: 1.1 $
3
// Change Log
4
// $Log: peg.cpp,v $
5
// Revision 1.1 2016/03/17 15:53:21 pngwen
6
// Initial revision
7
//
8
9
#include <iostream>
10
#include "termmanip.h"
11
#include "peg.h"
12
13
using namespace std;
14
15
16
//constructors
17
Peg::Peg(int _x, int _y) : Widget(_x, _y, 3, 1)
18
{
19
_selected = _occupied = false;
20
_color = normal;
21
}
22
23
24
Peg::Peg() : Peg(1,1)
25
{
26
//pass!
27
}
28
29
//display the peg
30
void
31
Peg::display()
32
{
33
cout << cursorPosition(_x, _y);
34
cout << "(";
35
cout << color(); //the color!
36
//selected means reverse video!
37
if(isSelected()) {
38
cout << reverseVideo;
39
}
40
41
//filled or not
42
if(isOccupied()) {
43
cout << "*";
44
} else {
45
cout << " ";
46
}
47
48
//back to normal video
49
cout << normal;
50
cout << ")";
51
52
cout.flush();
53
}
54
55
56
//select the peg
57
void
58
Peg::select()
59
{
60
_selected = true;
61
display();
62
}
63
64
//deselect the peg
65
void
66
Peg::deselect()
67
{
68
_selected = false;
69
display();
70
}
71
72
//fill the peg hole
73
void
74
Peg::place()
75
{
76
_occupied = true;
77
display();
78
}
79
80
81
//take the peg from the hole
82
void
83
Peg::take()
84
{
85
_occupied = false;
86
display();
87
}
88
89
90
//get the status of the peg hole
91
bool
92
Peg::isSelected()
93
{
94
return _selected;
95
}
96
97
98
bool
99
Peg::isOccupied()
100
{
101
return _occupied;
102
}
103
104
105
//get and set the color
106
colorType
107
Peg::color()
108
{
109
return _color;
110
}
111
112
113
void
114
Peg::color(colorType c)
115
{
116
_color = c;
117
}
118
119