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
// This plays the cross-4 puzzle.
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 "crossFour.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(CrossFour *p, bool valid);
29
30
int main()
31
{
32
CrossFour *p;
33
keycode kc;
34
35
//initialize things
36
sz = ttyGetSize(STDIN_FILENO);
37
p=new CrossFour(sz.cols / 2 - 35/2, 6);
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.cbreakMode();
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
58
case LEFT: //left
59
case 'h': //with vim bindings!
60
case CTRL_B: //and emacs!
61
case 'a': //gamer!
62
p->left();
63
break;
64
65
case RIGHT: //right
66
case 'l': //vim!
67
case CTRL_F: //emacs!
68
case 'd': //gamer!
69
p->right();
70
break;
71
72
case 'r':
73
p->reset();
74
break;
75
76
case 's':
77
p->solve();
78
break;
79
80
case ENTER: //make yer move!
81
displayMoveResponse(p, p->move());
82
break;
83
84
case ESC:
85
kc = CTRL_C; //escape moves too
86
break;
87
}
88
} while(kin && kc != CTRL_C);
89
90
//clean up the screen
91
cout << cursorOn << clearScreen << cursorPosition(1,1);
92
cout.flush();
93
94
return 0;
95
}
96
97
98
void
99
displayHeader()
100
{
101
//put a header on the screen
102
cout << cursorPosition(1,1)
103
<< reverseVideo
104
<< clearLine << setw(sz.cols) << ' ';
105
displayMessage(1, "Cross-4 Peg Puzzle");
106
cout << cursorPosition(1,2) << normal;
107
cout.flush();
108
}
109
110
111
void
112
displayFooter()
113
{
114
displayMessage(sz.rows-2, "s- Solve")
115
displayMessage(sz.rows-1, "Arrows/jkhl/wasd - Move Cursor Enter: Select/Place Peg");
116
displayMessage(sz.rows, "r- Reset ESC - Exit");
117
}
118
119
120
void
121
displayMessage(int line, const string &msg)
122
{
123
//compute the x-coordinate to center the message
124
int x = sz.cols/2;
125
x -= msg.length()/2;
126
cout << cursorPosition(x, line) << msg;
127
cout.flush();
128
}
129
130
131
void
132
displayMoveResponse(CrossFour *p, bool valid)
133
{
134
int y;
135
136
//get the line to display the message on
137
y=p->y() - 1;
138
139
//clear the message line
140
cout << cursorPosition(1,y) << clearLine;
141
cout.flush();
142
143
//display the message (if there is one)
144
if(!valid) {
145
cout << red;
146
displayMessage(y, "Invalid Selection. Please Try Again");
147
cout << normal;
148
cout.flush();
149
}
150
}
151
152