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
|
#include <fstream>
#include <string>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <iomanip>
using namespace std;
char horzLocation ='A';
int vertLocation = 2;
int invalidMoves = 0;
string direction;
int numOfSpaces;
int gamePieceMover
(char &horzL, int &vertL, int &invalidMoves, string direction, int numOfSpaces);
int main()
{
int moveAttempt = 0;
string line;
ifstream intData;
string startPoint;
intData.open("C:/Users/T_Dub/Desktop/data.txt");
if(!intData)
{
cout<<"Error opening file, the program will close.\n\n";
getch();
return 0;
}
while( !intData.eof() )
{
getline(intData, line);
moveAttempt++;
}
intData.close();
intData.open("C:/Users/T_Dub/Desktop/data.txt");
while ( !( intData.eof() ) )
{
intData >> direction >> numOfSpaces;
cout << direction << " " << numOfSpaces << endl;
cout << gamePieceMover (horzLocation, vertLocation, invalidMoves, direction,
numOfSpaces) << endl << endl;
}
getchar();
intData.close();
return 0;
}
int gamePieceMover(char &horzL, int &vertL, int &invalidMoves,
string direction, int numOfSpaces)
{
cout << "Starting Point: " << horzL << vertL << endl;
cout << "Direction Length: " << direction.length() << endl;
if ( !( (direction.at(0) == 'L' && horzL - numOfSpaces < 'A')
|| (direction.at(0) == 'R' && horzL - numOfSpaces > 'H')
|| (direction.at(0) == 'U' && vertL + numOfSpaces > 8)
|| (direction.at(0) == 'D' && vertL - numOfSpaces < 1) ) )
{
cout << "Passed Valid 1st Direction Loop" << endl; //Test
if (direction.length() == 2)
{
cout << "Passed Length Loop" << endl; //Test
if ( (direction.at(1) == 'L' && horzL - numOfSpaces > 'A')
|| (direction.at(1) == 'R' && horzL - numOfSpaces < 'H' ) )
{
cout << "Passed Valid 2nd Direction Loop" << endl;
switch (direction.at(1)) //Second
{
case 'L':
horzL -= numOfSpaces;
break;
case 'R':
cout << "Passed R Switch" << endl;
horzL += numOfSpaces;
break;
default:
cout << "critical error.";
}
} else {
cout << "Didn't Pass Valid 2nd Direction Loop" << endl;
}
} else {
cout << "Didn't Pass Length Direction Loop" << endl;
}
switch (direction.at(0)) //First
{
case 'U':
cout << "Passed U Switch" << endl;
vertL += numOfSpaces;
break;
case 'D':
vertL -= numOfSpaces;
break;
case 'L':
horzL -= numOfSpaces;
break;
case 'R':
horzL += numOfSpaces;
break;
default:
cout << "critical error.";
}
} else {
cout << "Didn't Pass Valid 1st Direction Loop" << endl;
invalidMoves++;
}
cout << invalidMoves << endl;
cout << horzL << vertL;
getch();
return;
}
|