CSC112 Spring 2016 Examples
// Functions for the TrianglePuzzle Class1// Revision: $Revision: 1.1 $2// Change Log3// $Log: trianglePuzzle.cpp,v $4// Revision 1.1 2016/03/17 15:59:57 pngwen5// Initial revision6//78#include <iostream>9#include <unistd.h>10#include "tty_functions.h"11#include "trianglePuzzle.h"12#include "keystream.h"13#include "termmanip.h"1415using namespace std;161718TrianglePuzzle::TrianglePuzzle(int _x, int _y) : Widget(_x, _y, 27, 9), pegs(5)19{20int coff = 12;21int roff = 0;2223//build the puzzle24for(int row=1; row<=5; row++) {25for(int col=0; col < row; col++) {26pegs[row-1].push_back(new Peg(_x + coff + (col * 6), _y+roff));27if(row != 1) {28pegs[row-1][col]->place();29}30}3132//update the offsets33roff+=2;34coff-=3;35}3637//set up the cursor stuff38this->cx = this->cy = 0;39this->mx = this->my = -1;4041//select the default peg42this->pegs[this->cy][this->cx]->select();43}444546TrianglePuzzle::TrianglePuzzle() : TrianglePuzzle(1, 1)47{48//put ourselves in the middle of the screen49ttySize size = ttyGetSize(STDIN_FILENO);5051//compute the midpoint52int mx = size.cols / 2 - 13;53int my = size.rows / 2 - 4;5455//set ourselves to the midpoint and then adjust for the offset56x(mx);57y(my);58for(auto ritr=pegs.begin(); ritr!=pegs.end(); ritr++) {59for(auto citr=ritr->begin(); citr != ritr->end(); citr++) {60(*citr)->x((*citr)->x() + mx);61(*citr)->y((*citr)->y() + my);62}63}64}656667TrianglePuzzle::~TrianglePuzzle()68{69//delete all the pegs!70for(auto ritr=pegs.begin(); ritr!=pegs.end(); ritr++) {71for(auto citr=ritr->begin(); citr != ritr->end(); citr++) {72delete *citr;73}74}75}7677void78TrianglePuzzle::display()79{80//display all the pegs81for(auto ritr=pegs.begin(); ritr!=pegs.end(); ritr++) {82for(auto citr=ritr->begin(); citr != ritr->end(); citr++) {83(*citr)->display();84}85}86}8788//select a given peg89void90TrianglePuzzle::selectPeg(int px, int py)91{92//unselect the current one93pegs[cy][cx]->deselect();9495//correct potential issues in y first96if(py < 0) {97//torus up!98py = pegs.size() - 1;99} else if (py >= pegs.size()) {100//torus down!101py = 0;102}103104//correct x105if(px < 0) {106//torus left!107px = pegs[py].size() -1;108} else if(px >= pegs[py].size()) {109//torus right!110px = 0;111}112113//select the chosen one and redisplay114this->cx = px;115this->cy = py;116pegs[cy][cx]->select();117display();118}119120121//move the selection up a row122void123TrianglePuzzle::up()124{125//kick it up a notch126selectPeg(cx, cy-1);127}128129130//move the selection down a row131void132TrianglePuzzle::down()133{134//bring it on down135selectPeg(cx, cy+1);136}137138139//move the selection right a column140void141TrianglePuzzle::right()142{143//to the right, the right....144selectPeg(cx+1, cy);145}146147148//move the selection left a column149void150TrianglePuzzle::left()151{152//to the left, the left...153selectPeg(cx-1, cy);154}155156157//perform a part of a move. Returns true if valid, false if invalid158bool159TrianglePuzzle::move()160{161int dx, dy;162int midx, midy;163int sx, sy;164165//handle the first selection166if(mx == -1) {167//can't select holes, only pegs168if(!pegs[cy][cx]->isOccupied()) return false;169170//select the peg!171mx = cx;172my = cy;173pegs[my][mx]->color(red);174pegs[my][mx]->display();175return true;176}177178//ok, so it's the second selection! First, let's turn off the highlighting179pegs[my][mx]->color(normal);180pegs[my][mx]->display();181182//get the starting point183sx = mx;184sy = my;185186//reset move state187my=mx=-1;188189190//'now, let's check to see if the user is playing fair191if(pegs[cy][cx]->isOccupied()) return false; //this means that it's not a blank square192193//midpoints and distances194midx = (cx+sx)/2;195midy = (cy+sy)/2;196dx = cx - sx;197dy = cy - sy;198if(dx<0) dx *= -1;199if(dy<0) dy *= -1;200201202//validate same row moves203if(dy == 0 && dx != 2) return false;204205//validate different row moves206if(dy != 0 && dy != 2) return false;207if(dx != 2 && dx != 0) return false;208209//validate that we are skipping a peg210if(!pegs[midy][midx]->isOccupied()) return false;211212213214//HUZZAH! make the move!215pegs[sy][sx]->take();216pegs[cy][cx]->place();217pegs[midy][midx]->take();218219return true;220}221222223void224TrianglePuzzle::reset()225{226for(int y=0; y<pegs.size(); y++) {227for(int x=0; x<pegs[y].size(); x++) {228//the first row is empty, all others are taken229if(y==0) {230pegs[y][x]->take();231} else {232pegs[y][x]->place();233}234}235}236}237238239