#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "ASMProcedures.h"
using namespace std;
// PROTO types
void doGame();
// it will uses blocks every time
const int BLOCK = 219; // ascii block char
const int max_y = 20;
const int max_x = 20;
const int BLACK = 14; // color black
const int BLUE = 1; // blue
const int RED = 4; // red
const int GREEN = 2; // green
const int
EMPTY = 9; // light blue
int screen[max_x][max_y];
int maze[max_x][max_y] =
{
EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
,
BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK,
EMPTY, EMPTY, EMPTY, BLACK
, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, BLACK
, BLUE
, BLUE
, BLUE
, EMPTY, EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, BLACK
, EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, BLUE
,EMPTY,
EMPTY, EMPTY, EMPTY, BLACK
, EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
,
EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, BLUE
, BLUE
,BLUE
, BLUE
, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY,
BLUE
, BLUE
, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY,
EMPTY, BLUE
, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY,
EMPTY, BLUE
, BLUE
, BLUE
, EMPTY, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY, EMPTY, EMPTY, BLACK
, BLACK
, BLACK
,
EMPTY, EMPTY, EMPTY, BLUE
, BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY, BLACK
, EMPTY, EMPTY, EMPTY, BLUE
, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY, BLACK
, EMPTY, EMPTY, EMPTY, BLUE
, BLUE
, BLUE
,
EMPTY, EMPTY, EMPTY, EMPTY, BLACK
, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY, BLACK
, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
BLACK
, BLACK
, BLACK
, BLACK
, BLACK
, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
};
struct avatarType
{
int x;
int y;
} avatar, endPT, startPT, dest;
// game score and such
int score = 0;
char input;
int timer = 0;
int temp;
int roll;
int elapTicks;
double elapMilli, elapSeconds, elapMinutes;
bool done = false;
bool validInput = false;
int topScore = 0;
// string fileName = "topscore.txt";
string playerName = "anonymous";
string topName = playerName;
int main()
{
// READ FROM FILE (if exists)
ifstream OpenFile("topscore.txt");
if (! OpenFile.eof() )
{
cout << "High scored read in from data file " << endl;
OpenFile >> topName;
OpenFile >> topScore;
cout << "High score was " << topScore << " by " << topName << endl << endl;
}
else
cout << "Error opening high score file, possibly doesnt exist" << endl;
OpenFile.close();
doWait();
// MENU LOOP
do
{
// GOAL :: Input player name, selection of options, help screen, play game, exit
game
// draw menu
clearscreen();
cout << "MENU:" << endl
<< "1. enter player name" << endl
<< "2. options" << endl
<< "3. help" << endl
<< "4. PLAY game" << endl
<< "5. exit" << endl << endl;
cin >> input;
switch(input)
{
case '1': {
cout << endl
<< "Please enter your name:";
cin >> playerName;
cout << endl << endl;
cout << "New player name is now: " << playerName;
doWait();
}; break;
case '2':{
cout << endl
<< "sorry, there currently are no options to set"
<< endl
<< "but I'll show you the top score:" << endl
<< topName << " has a top score of " << topScore << endl
<< endl;
doWait();
}; break;
case '3':{
cout << endl
<< endl
<< "HELP!" << endl
<< "Movement keys" << endl
<< "A/a move left" << endl
<< "D/d move right" << endl
<< "W/w move up" << endl
<< "S/s move down" << endl
<< endl
<< " RED squares +20 points" << endl
<< "GREEN squares +10 points" << endl
<< endl
<< "The timer is subtracted from your end score, so be
quick!"
<< endl << endl;
doWait();
}; break;
case '4':{
// play game
doGame();
}; break;
case '5':{
cout << "exiting" << endl;
// write high score to file, including users name
// topScore and topName --> to file fileName
ofstream saveFile("topscore.txt");
saveFile << topName << endl;
saveFile << topScore << endl;
saveFile.close();
doWait();
}; break;
}
} while (1==1);
// DONE
}
void doGame()
{
// set starting location of avatar and end point of maze
startPT.x = 0;
startPT.y = 19;
avatar = startPT;
endPT.x = 19;
endPT.y = 0;
// starting values
score = 0;
done = false;
validInput = false;
// setup the maze
for (int x=0; x < max_x; x++)
for (int y=0; y < max_y; y++)
{
temp = maze[x][y];
if ( (temp
!= EMPTY) && (x
!= startPT
.x
) && (y
!= startPT
.y
) )
{
if (temp == BLUE)
else
if (roll == 2)
temp = GREEN;
if (roll == 1)
temp = RED;
}
screen[x][y] = temp;
}
clock_t Begin
, End; //initialize Begin and End for the timer
Begin = clock() * CLK_TCK; //start the timer
// clear the screen
clearscreen();
// DO
while (! done)
{
// show the timer
End = clock
() * CLK_TCK
; // stop the timer
elapMilli
= (End - Begin
)/1000; // milliseconds from Begin to End
elapSeconds = elapMilli/1000; // seconds from Begin to End
showTimer(elapSeconds);
// draw the maze
for (int x=0; x < max_x; x++)
for (int y=0; y < max_y; y++)
drawBlock(x,y,screen[x][y]);
// print current score
showScore(score - elapSeconds);
// show avatar
drawAvatar(avatar.x,avatar.y);
// get user movement keys
validInput = false;
dest = avatar;
input = getInput();
switch(input)
{
case 97:
case 65:{ // a or A (LEFT)
dest.x--;
validInput = true;
}; break;
case 119:
case 87:{ // w or W (UP)
dest.y--;
validInput = true;
}; break;
case 100:
case 68:{ // d or D (RIGHT)
dest.x++;
validInput = true;
}; break;
case 115:
case 83:{ // s or S (DOWN)
dest.y++;
validInput = true;
}; break;
case 120:
case 88:{ // x or X
done = true;
}; break;
}
// check for valid values
if (( dest.x < 0) || (dest.x >= max_x))
validInput = false;
if (( dest.y < 0) || (dest.y >= max_y))
validInput = false;
if (validInput)
{
// check the destination square to see if its valid
if (screen
[dest
.x
][dest
.y
] != EMPTY)
{
avatar = dest;
// check if on red/green square - increase score
if (screen[avatar.x][avatar.y] == RED)
{
score += 10;
screen[avatar.x][avatar.y] = maze[avatar.x][avatar.y];
}
if (screen[avatar.x][avatar.y] == GREEN)
{
score += 20;
screen[avatar.x][avatar.y] = maze[avatar.x][avatar.y];
}
// check if at end of maze - then EXIT
if ((avatar.x == endPT.x) && ((avatar.y == endPT.y)))
done = true;
}
}
}
// REPEAT
// show the timer
End = clock
() * CLK_TCK
; // stop the timer
elapMilli
= (End - Begin
)/1000; // milliseconds from Begin to End
elapSeconds = elapMilli/1000; // seconds from Begin to End
showTimer(elapSeconds);
// print current score
score -= elapSeconds; // lower the score by the time it took
showScore(score);
// show avatar
drawAvatar(avatar.x,avatar.y);
if ((avatar.x == endPT.x) && ((avatar.y == endPT.y)))
{
doWin();
// only save top score if you won
if (score > topScore)
{
topScore = score;
topName = playerName;
}
}
else
doWait();
return;
}