CSC112 Spring 2016 Examples
// The main file for the puzzle program.1// Play the triangle puzzle without the impending check from the Cracker Barrel server!2// Revision: $Revision: 1.1 $3// Change Log4// $Log: puzzle.cpp,v $5// Revision 1.1 2016/03/17 15:57:00 pngwen6// Initial revision7//89#include <iostream>10#include <string>11#include <unistd.h>12#include <iomanip>13#include "termmanip.h"14#include "keystream.h"15#include "trianglePuzzle.h"16#include "tty_functions.h"1718using namespace std;1920// One static global variable, used sparingly but in good faith as there is but one tty21static ttySize sz;2223//display elements24void displayHeader();25void displayFooter();26void displayMessage(int line, const string &msg);27void displayMoveResponse(TrianglePuzzle *p, bool valid);2829int main()30{31TrianglePuzzle *p;32keycode kc;3334//initialize things35sz = ttyGetSize(STDIN_FILENO);36p=new TrianglePuzzle();3738//display the puzzle39cout << clearScreen << cursorOff;40p->display();4142displayHeader();43displayFooter();4445//get the input into the right state46kin.rawMode();4748//do the puzzle49do {50kc = kin.getKey();5152//moves begin valid53displayMoveResponse(p, true);5455switch(kc) {56case UP: //up57case 'k': //with vim bindings!58case CTRL_P: //and emacs too59case 'w': //gamer!60p->up();61break;6263case DOWN: //down64case 'j': //with vim bindings!65case CTRL_N: //and emacs too!66case 's': //gamer!67p->down();68break;6970case LEFT: //left71case 'h': //with vim bindings!72case CTRL_B: //and emacs!73case 'a': //gamer!74p->left();75break;7677case RIGHT: //right78case 'l': //vim!79case CTRL_F: //emacs!80case 'd': //gamer!81p->right();82break;8384case 'r':85p->reset();86break;8788case ENTER: //make yer move!89displayMoveResponse(p, p->move());90break;9192case ESC:93kc = CTRL_C; //escape moves too94break;95}96} while(kin && kc != CTRL_C);9798//clean up the screen99cout << cursorOn << clearScreen << cursorPosition(1,1);100cout.flush();101102return 0;103}104105106void107displayHeader()108{109//put a header on the screen110cout << cursorPosition(1,1)111<< reverseVideo112<< clearLine << setw(sz.cols) << ' ';113displayMessage(1, "Triangle Peg Puzzle");114cout << cursorPosition(1,2) << normal;115cout.flush();116}117118119void120displayFooter()121{122displayMessage(sz.rows-1, "Arrows/jkhl/wasd - Move Cursor Enter: Select/Place Peg");123displayMessage(sz.rows, "r- Reset ESC - Exit");124}125126127void128displayMessage(int line, const string &msg)129{130//compute the x-coordinate to center the message131int x = sz.cols/2;132x -= msg.length()/2;133cout << cursorPosition(x, line) << msg;134cout.flush();135}136137138void139displayMoveResponse(TrianglePuzzle *p, bool valid)140{141int y;142143//get the line to display the message on144y=p->y() - 1;145146//clear the message line147cout << cursorPosition(1,y) << clearLine;148cout.flush();149150//display the message (if there is one)151if(!valid) {152cout << red;153displayMessage(y, "Invalid Selection. Please Try Again");154cout << normal;155cout.flush();156}157}158159160