CSC112 Spring 2016 Examples
#include "crossFour.h"1#include "termmanip.h"2#include "keystream.h"3#include <unistd.h>4#include <iostream>56using namespace std;78//constructors and destructors9CrossFour::CrossFour() : CrossFour(1,1)10{1112}1314CrossFour::CrossFour(int cx, int cy) : Widget(cx, cy, 35, 1), pegs(9)15{16//build the pegses17for(int i=0; i<pegs.size(); i++) {18pegs[i] = new Peg(x()+i*4, y());19}2021reset();22}2324CrossFour::~CrossFour()25{26for(int i=0; i<pegs.size(); i++) {27delete pegs[i];28}29}3031//display our puzzle32void33CrossFour::display()34{35for(int i=0; i<pegs.size(); i++) {36pegs[i]->display();37}38}3940//cursor movements41void42CrossFour::left()43{44pegs[cx]->deselect();45cx--;46if(cx < 0) cx = pegs.size()-1;47pegs[cx]->select();48}495051void52CrossFour::right()53{54pegs[cx]->deselect();55cx++;56cx %= pegs.size();57pegs[cx]->select();58}596061//perform a move62bool63CrossFour::move()64{65//selecting source?66if(sx == -1) {67//validate our selection68if(!pegs[cx]->isOccupied()) return false;6970//source is selected!71sx = cx;72return true;73}7475//select destination and complete the move7677//validate direction78if(pegs[sx]->color() == blue and sx >= cx){79sx = -1;80return false;81}82if(pegs[sx]->color() == red and sx <= cx) {83sx = -1;84return false;85}8687int d = sx - cx;88if(d<0) d*=-1;8990//validate the distance91if(d < 1 or d > 2) {92sx = -1;93return false;94}9596if(pegs[cx]->isOccupied()) {97sx = -1;98return false;99}100101//it's valid! Move it!102pegs[cx]->color(pegs[sx]->color());103pegs[sx]->take();104pegs[cx]->place();105sx =-1;106display();107return true;108}109110//autosolver111void112CrossFour::solve()113{114Solver *solution = new Solver(this);115cout << cursorPosition(1,2) << "Thinking...";116cout.flush();117backtrack(solution);118cout << cursorPosition(1,2) << clearLine;119cout.flush();120121/* Solver *move = solution;122while(move) {123displayState(move->state);124move = (Solver*) move->after();125}126*/127delete solution;128}129130131//reset the puzzle to starting position132void133CrossFour::reset()134{135//blue pegs136for(int i=0; i<pegs.size()/2; i++) {137pegs[i]->place();138pegs[i]->color(blue);139}140141//blank peg142pegs[pegs.size()/2]->take();143144//red pegs145for(int i=pegs.size()/2+1; i<pegs.size(); i++) {146pegs[i]->place();147pegs[i]->color(red);148}149150//select the default peg151sx=-1;152cx = pegs.size()/2;153pegs[cx]->select();154display();155}156157158CrossFour::Solver::Solver(CrossFour *puzzle)159{160//we have made no move!161movedPeg=-1;162nextTrial = 0;163state[0]='B';164state[1]='B';165state[2]='B';166state[3]='B';167state[4]=' ';168state[5]='R';169state[6]='R';170state[7]='R';171state[8]='R';172_before = _after = NULL;173this->puzzle = puzzle;174}175176177CrossFour::Solver::Solver(CrossFour *puzzle, char state[], int movedPeg)178{179this->puzzle = puzzle;180this->movedPeg = movedPeg;181nextTrial=0;182//copy the pegs183for(int i=0; i<9; i++) {184this->state[i] = state[i];185}186_before = _after = NULL;187}188189190bool191CrossFour::Solver::reject()192{193if(accept()) return false;194195//iterate196for(int i = 0; i < 9; i++) {197if(state[i] == 'B' && i != 6) {198if(state[i+2] == ' ') {199return false;200}201}202if(state[i] == 'B' && i != 7) {203if(state[i+1] == ' ') {204return false;205}206}207if(state[i] =='R' && i != 0) {208if(state[i-1] == ' ') {209return false;210}211}212if(state[i] =='R' && i > 1) {213if(state[i-2] == ' ') {214return false;215}216}217}218return true;219}220221222223bool224CrossFour::Solver::accept()225{226puzzle->displayState(state);227//iterate through the state228for(int i = 0; i <9; i++) {229if(i >= 0 and i <= 3) {230if (state[i] != 'R') return false;231}232if(i == 4) {233if (state[i] != ' ') return false;234}235if(i >= 5 and i <= 8) {236if (state[i] != 'B') return false;237}238}239//if we made it this far, everything is correct vibes240241return true;242}243244245MoveState*246CrossFour::Solver::next()247{248Solver *moved = NULL;249250for(nextTrial; !moved and nextTrial <= 8; nextTrial++)251{252switch(state[nextTrial]){253case 'B':254if(nextTrial < 8 and state[nextTrial+1] == ' ')255{256moved = new Solver(puzzle, state, movedPeg);257_after = moved;258moved->_before = this;259moved->state[nextTrial] = ' ';260moved->state[nextTrial+1] = 'B';261} else if(nextTrial < 7 and state[nextTrial+2] == ' ') {262moved = new Solver(puzzle, state, movedPeg);263_after = moved;264moved->_before = this;265moved->state[nextTrial] = ' ';266moved->state[nextTrial+2] = 'B';267}268break;269case 'R':270if(nextTrial > 0 and state[nextTrial-1] == ' ')271{272moved = new Solver(puzzle, state, movedPeg);273_after = moved;274moved->_before = this;275moved->state[nextTrial] = ' ';276moved->state[nextTrial-1] = 'R';277} else if(nextTrial > 1 and state[nextTrial-2] == ' ') {278moved = new Solver(puzzle, state, movedPeg);279_after = moved;280moved->_before = this;281moved->state[nextTrial] = ' ';282moved->state[nextTrial-2] = 'R';283}284break;285286default:287break;288}289290}291292return moved;293}294295//move "memory"296MoveState*297CrossFour::Solver::before()298{299return _before;300}301302303MoveState*304CrossFour::Solver::after()305{306return _after;307}308309//display the current state310void CrossFour::displayState(char state[])311{312for(int i=0; i<9; i++) {313pegs[i]->take();314switch(state[i]) {315case 'B':316pegs[i]->color(blue);317pegs[i]->place();318break;319case 'R':320pegs[i]->color(red);321pegs[i]->place();322break;323}324}325display();326cout.flush();327usleep(250000);328}329330