CoCalc Public Fileshanoi.cppOpen with one click!
Author: Gevaun Grant
Views : 42
Description: [email protected]
Compute Environment: Ubuntu 18.04 (Deprecated)
1
#include <iostream>
2
#include <cctype>
3
#include <unistd.h>
4
#include <iomanip>
5
#include "termmanip.h"
6
#include "keystream.h"
7
#include "disk.h"
8
#include "diskStack.h"
9
#include "tower.h"
10
#include "tty_functions.h"
11
12
using namespace std;
13
14
//the size of the window
15
static ttySize sz;
16
17
void clear(); //clears screen
18
//display elements
19
int echo(char e); //echos only if a, b or c
20
void displayHeader(); //displays the title and scores
21
void displayMessage(int line, const string &gtitle, const string &mtitle); //headersubs
22
void displayMove(int &sc); //tracks the moves
23
void Game(); //the actual game part thing
24
void displayInstructions(int line, const string ins);
25
26
int main()
27
{
28
int dn; //number of disks to play with
29
Tower *t; //towers display
30
char src, dest;
31
int i=0;
32
33
//get window size
34
sz = ttyGetSize(STDIN_FILENO);
35
36
//display the tower of hanoi
37
clear();
38
cout << "How many disks? ";
39
cin >> dn;
40
41
//creates the towers
42
t = new Tower(dn);
43
44
//draw the header
45
displayHeader();
46
t -> display();
47
48
//get the input into the right state
49
kin.cbreakMode();
50
do {
51
displayMove(i);
52
cout << cursorPosition(10,dn+7) << clearLine;
53
displayInstructions(dn,"Move (Q-Quit, S-Solve, ABC-Move):");
54
cout << clearLine;
55
cout.flush();
56
src = kin.getKey();
57
echo(src);
58
switch(src) {
59
case 'a':
60
case 'b':
61
case 'c':
62
displayInstructions(dn+1, "To (Q-Quit, S-Solve, ABC-makove):");
63
cout << clearLine;
64
cout.flush();
65
dest = kin.getKey();
66
echo(dest);
67
switch(dest) {
68
case 'a':
69
case 'b':
70
case 'c':
71
t -> move(src, dest);
72
i++;
73
break;
74
};
75
break;
76
77
case 's': //solve
78
t -> solve();
79
break;
80
81
case 'q':
82
src = CTRL_C; //escape moves too
83
clear();
84
cout << "Thanks for playing" << endl;
85
break;
86
87
default:
88
break;
89
}
90
} while((t -> solved() != true) or (src == CTRL_C));
91
cout << clear << "You Win! Press any key to continue" ;
92
return 0;
93
94
}
95
//cbreak echoing
96
int echo(char e){
97
if (e == 'a' or 'b' or 'c' or 's' or 'q') {
98
cout << e;
99
}
100
cout.flush();
101
return e;
102
};
103
104
//clear screen and put cursor 1,1
105
void clear() {
106
cout << clearScreen << cursorPosition(1,1);
107
};
108
109
//draw the header of the game
110
void displayHeader() {
111
//blackbackfround for header
112
clear(); //clear screen
113
cout << blackBackground << setw(sz.cols) << ' ';
114
displayMessage(1, "The Tower of Hanoi", "Moves:");
115
cout << cursorPosition(1,2) << normal;
116
cout.flush();
117
};
118
119
//creates message in the header
120
void displayMessage(int line, const string &gtitle, const string &mtitle)
121
{
122
//find the center of the screen and display the message header
123
int x = sz.cols/2; //gets x coordinate for game title
124
int x2 = sz.cols/1.08; //gets x coordinate for the move title
125
x -= gtitle.length()/2; //gets center of characters for game name
126
cout << cursorPosition(x, line) << white << gtitle << cursorPosition(x2, line) << mtitle;
127
cout << normal;
128
}
129
130
//move tracker
131
void displayMove(int &score)
132
{
133
int x = sz.cols/1.03; //get x coordinate for the move number
134
//x -= score.length()/2; //get center of characters for moves
135
cout << cursorPosition(x,1) << blackBackground << white << score << normal;
136
};
137
138
//instructions
139
void displayInstructions(int line, const string ins) {
140
int x = sz.rows/2;
141
cout << cursorPosition(10, line+6) << ins;
142
};
143