#include <iostream>
#include <cctype>
#include <unistd.h>
#include <iomanip>
#include "termmanip.h"
#include "keystream.h"
#include "disk.h"
#include "diskStack.h"
#include "tower.h"
#include "tty_functions.h"
using namespace std;
static ttySize sz;
void clear();
int echo(char e);
void displayHeader();
void displayMessage(int line, const string >itle, const string &mtitle);
void displayMove(int &sc);
void Game();
void displayInstructions(int line, const string ins);
int main()
{
int dn;
Tower *t;
char src, dest;
int i=0;
sz = ttyGetSize(STDIN_FILENO);
clear();
cout << "How many disks? ";
cin >> dn;
t = new Tower(dn);
displayHeader();
t -> display();
kin.cbreakMode();
do {
displayMove(i);
cout << cursorPosition(10,dn+7) << clearLine;
displayInstructions(dn,"Move (Q-Quit, S-Solve, ABC-Move):");
cout << clearLine;
cout.flush();
src = kin.getKey();
echo(src);
switch(src) {
case 'a':
case 'b':
case 'c':
displayInstructions(dn+1, "To (Q-Quit, S-Solve, ABC-makove):");
cout << clearLine;
cout.flush();
dest = kin.getKey();
echo(dest);
switch(dest) {
case 'a':
case 'b':
case 'c':
t -> move(src, dest);
i++;
break;
};
break;
case 's':
t -> solve();
break;
case 'q':
src = CTRL_C;
clear();
cout << "Thanks for playing" << endl;
break;
default:
break;
}
} while((t -> solved() != true) or (src == CTRL_C));
cout << clear << "You Win! Press any key to continue" ;
return 0;
}
int echo(char e){
if (e == 'a' or 'b' or 'c' or 's' or 'q') {
cout << e;
}
cout.flush();
return e;
};
void clear() {
cout << clearScreen << cursorPosition(1,1);
};
void displayHeader() {
clear();
cout << blackBackground << setw(sz.cols) << ' ';
displayMessage(1, "The Tower of Hanoi", "Moves:");
cout << cursorPosition(1,2) << normal;
cout.flush();
};
void displayMessage(int line, const string >itle, const string &mtitle)
{
int x = sz.cols/2;
int x2 = sz.cols/1.08;
x -= gtitle.length()/2;
cout << cursorPosition(x, line) << white << gtitle << cursorPosition(x2, line) << mtitle;
cout << normal;
}
void displayMove(int &score)
{
int x = sz.cols/1.03;
cout << cursorPosition(x,1) << blackBackground << white << score << normal;
};
void displayInstructions(int line, const string ins) {
int x = sz.rows/2;
cout << cursorPosition(10, line+6) << ins;
};