-
Notifications
You must be signed in to change notification settings - Fork 1
/
director.cpp
146 lines (137 loc) · 3.69 KB
/
director.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <curses.h>
#include <Windows.h>
#include <ctime>
#include <fstream>
#include "director.h"
#include "screen.h"
using namespace std;
void Director::updateFood(int y, int x) {
// check if ant collides with food. If no, do not move food. If yes, move food and add 1 to score
vector<int> coords = food.getCoords();
if (y == coords[0] && x == coords[1] + 1) {
score++;
food.newFood();
}
mvaddch(coords[0], coords[1], food.getFood());
move(y, x);
refresh();
}
void Director::updateMove(char key, Input inputs) {
// updates the ant's position based on input
const char antBody = ant.getBody();
int x;
int y;
int wait = 0;
while (!inputs.keyPress()) {
y = getcury(curscr);
x = getcurx(curscr);
// if ant hits wall, turn it the opposite direction
if (y == 0) {
key = 's';
}
else if (y == 29) {
key = 'w';
}
else if (x - 1 == 0) {
key = 'd';
}
else if (x - 1 == 118){
key = 'a';
}
// do something based on which key was pressed
switch (key) {
case 'w':
mvaddch(y - 1, x - 1, antBody);
wait = 50;
break;
case 'a':
mvaddch(y, x - 2, antBody);
wait = 20;
break;
case 's':
mvaddch(y + 1, x - 1, antBody);
wait = 50;
break;
case 'd':
mvaddch(y, x, antBody);
wait = 20;
break;
}
refresh();
// food check function
updateFood(getcury(curscr), getcurx(curscr));
Sleep(wait);
clear();
waited += wait;
}
}
void Director::doUpdate(char key, Input inputs) {
// calls update functions
updateMove(key, inputs);
}
char Director::getInput(Input inputs) {
// function to get and return user input
char key = inputs.getInput();
return key;
}
void Director::updateFile() {
// function to update the file with new highscore
ofstream file;
file.open("highscore.txt");
file << score;
file.close();
}
void Director::gameLoop() {
// initiate input class
Input input_service;
// create a new food
food.newFood();
// game timer (not perfect, but not terrible)
while (waited < 20000) {
// get user input
char key = getInput(input_service);
// update screen according to user input
doUpdate(key, input_service);
}
// open highscore file and write the new highscore to it, if it is higher than the previous highscore
string line;
ifstream file("highscore.txt");
while (getline (file, line)) {
if (stoi(line) < score) {
file.close();
updateFile();
}
}
mvprintw(13, 52, "Time is up!");
refresh();
Sleep(2000);
clear();
}
int Director::execute() {
// initiate screen
Screen screen;
string line;
// read file and display highscore
ifstream file("highscore.txt");
while (getline (file, line)) {
for (int i = 0; i < line.size(); i++) {
char highscore = line[i];
mvaddch(0, 12 + i, highscore);
}
}
file.close();
// print at coordinates y, x, string
curs_set(0);
mvprintw(0, 1, "Highscore: ");
mvprintw(11, 50, "Welcome to Ant");
mvprintw(13, 29, "You have about 30 seconds to eat as much fruit as possible");
mvprintw(15, 45, "Press any key to begin");
// refresh the window to include the new print
refresh();
// start game
gameLoop();
// return the score to display to the user and store in a file.
return score;
}