Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
// The main file for the puzzle program.
2
// Play the triangle puzzle without the impending check from the Cracker Barrel server!
3
// Revision: $Revision: 1.1 $
4
// Change Log
5
// $Log: puzzle.cpp,v $
6
// Revision 1.1 2016/03/17 15:57:00 pngwen
7
// Initial revision
8
//
9
10
#include <iostream>
11
#include <string>
12
#include <unistd.h>
13
#include <iomanip>
14
#include "termmanip.h"
15
#include "keystream.h"
16
#include "trianglePuzzle.h"
17
#include "tty_functions.h"
18
19
using namespace std;
20
21
// One static global variable, used sparingly but in good faith as there is but one tty
22
static ttySize sz;
23
24
//display elements
25
void displayHeader();
26
void displayFooter();
27
void displayMessage(int line, const string &msg);
28
void displayMoveResponse(TrianglePuzzle *p, bool valid);
29
30
int main()
31
{
32
TrianglePuzzle *p;
33
keycode kc;
34
35
//initialize things
36
sz = ttyGetSize(STDIN_FILENO);
37
p=new TrianglePuzzle();
38
39
//display the puzzle
40
cout << clearScreen << cursorOff;
41
p->display();
42
43
displayHeader();
44
displayFooter();
45
46
//get the input into the right state
47
kin.rawMode();
48
49
//do the puzzle
50
do {
51
kc = kin.getKey();
52
53
//moves begin valid
54
displayMoveResponse(p, true);
55
56
switch(kc) {
57
case UP: //up
58
case 'k': //with vim bindings!
59
case CTRL_P: //and emacs too
60
case 'w': //gamer!
61
p->up();
62
break;
63
64
case DOWN: //down
65
case 'j': //with vim bindings!
66
case CTRL_N: //and emacs too!
67
case 's': //gamer!
68
p->down();
69
break;
70
71
case LEFT: //left
72
case 'h': //with vim bindings!
73
case CTRL_B: //and emacs!
74
case 'a': //gamer!
75
p->left();
76
break;
77
78
case RIGHT: //right
79
case 'l': //vim!
80
case CTRL_F: //emacs!
81
case 'd': //gamer!
82
p->right();
83
break;
84
85
case 'r':
86
p->reset();
87
break;
88
89
case ENTER: //make yer move!
90
displayMoveResponse(p, p->move());
91
break;
92
93
case ESC:
94
kc = CTRL_C; //escape moves too
95
break;
96
}
97
} while(kin && kc != CTRL_C);
98
99
//clean up the screen
100
cout << cursorOn << clearScreen << cursorPosition(1,1);
101
cout.flush();
102
103
return 0;
104
}
105
106
107
void
108
displayHeader()
109
{
110
//put a header on the screen
111
cout << cursorPosition(1,1)
112
<< reverseVideo
113
<< clearLine << setw(sz.cols) << ' ';
114
displayMessage(1, "Triangle Peg Puzzle");
115
cout << cursorPosition(1,2) << normal;
116
cout.flush();
117
}
118
119
120
void
121
displayFooter()
122
{
123
displayMessage(sz.rows-1, "Arrows/jkhl/wasd - Move Cursor Enter: Select/Place Peg");
124
displayMessage(sz.rows, "r- Reset ESC - Exit");
125
}
126
127
128
void
129
displayMessage(int line, const string &msg)
130
{
131
//compute the x-coordinate to center the message
132
int x = sz.cols/2;
133
x -= msg.length()/2;
134
cout << cursorPosition(x, line) << msg;
135
cout.flush();
136
}
137
138
139
void
140
displayMoveResponse(TrianglePuzzle *p, bool valid)
141
{
142
int y;
143
144
//get the line to display the message on
145
y=p->y() - 1;
146
147
//clear the message line
148
cout << cursorPosition(1,y) << clearLine;
149
cout.flush();
150
151
//display the message (if there is one)
152
if(!valid) {
153
cout << red;
154
displayMessage(y, "Invalid Selection. Please Try Again");
155
cout << normal;
156
cout.flush();
157
}
158
}
159
160