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 <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include "personType.h"
#include "boardType.h"
#include "doorType.h"
#include "keyType.h"
#include "lockType.h"
#include "locationType.h"
using namespace std;
const int N = 8;
int numberOfMoves = 0;
bool winGame(boardType board, personType mainPlayer, int tour[N][N], int x, int y);
bool isNextMoveValid(int tour[N][N], int x, int y);
int highestNumberOnBoard(int tour[N][N], int i, int j);
bool allSameNumber(int tour[N][N],int i, int j);
int main() {
int x;
int y;
srand(time(NULL));
int tour[N][N] = { {0} };
personType mainPlayer(N,N);
boardType board(N, N, N);
board.printBoard();
if (winGame(board,mainPlayer,tour, x, y) == true)
{
cout << "I opened all the doors!" << endl;
cout << "It took me " << numberOfMoves << " moves."<< endl;
} else {
cout << "Oops. Well, that didn't work." << endl;
}
return 0;
}
bool isNextMoveValid(int tour[N][N], int x, int y)
{
if (allSameNumber(tour, x, y))
return true;
else if (tour[x][y] == highestNumberOnBoard(tour, x, y))
return false;
else
return true;
}
int highestNumberOnBoard(int tour[N][N], int i, int j)
{
int max = tour[0][0];
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
if (tour[i][j] > max)
max = tour[i][j];
}
}
return max;
//what is the highest position ive traversed on board
}
bool allSameNumber(int tour[N][N], int i, int j)
{
//checks to see if been here already
//i could create a flag method
int test = tour[0][0];
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
if (tour[i][j] != test)
return false;
}
}
return true;
}
bool winGame(boardType board, personType mainPlayer, int tour[N][N], int x, int y)
{
numberOfMoves++;
if(board.victory())
return true;
{
locationType currentPlayerLocation = mainPlayer.getCurrentLocation();
tour[currentPlayerLocation.x][currentPlayerLocation.y]++;
if(board.isKeyAtThisLocation(currentPlayerLocation)){
mainPlayer.pickupKey(board.pickupKey(currentPlayerLocation));}
if(board.isDoorAtThisLocation(currentPlayerLocation)){
mainPlayer.openDoor(board.getDoor(currentPlayerLocation));}
personType movePlayerRight(mainPlayer);
personType movePlayerLeft(mainPlayer);
personType movePlayerUp(mainPlayer);
personType movePlayerDown(mainPlayer);
if(movePlayerRight.moveRight() && isNextMoveValid (tour, currentPlayerLocation.x+1, currentPlayerLocation.y)){
return winGame(board, movePlayerRight, tour, x ,y);}
else if(movePlayerLeft.moveLeft() && isNextMoveValid (tour, currentPlayerLocation.x-1, currentPlayerLocation.y)){
return winGame(board, movePlayerLeft, tour, x ,y);}
else if(movePlayerUp.moveUp()&& isNextMoveValid (tour, currentPlayerLocation.x, currentPlayerLocation.y+1)){
return winGame(board, movePlayerUp, tour,x ,y);}
else if(movePlayerDown.moveDown()&& isNextMoveValid (tour, currentPlayerLocation.x, currentPlayerLocation.y-1)){
return winGame(board, movePlayerDown, tour,x,y);}
{
return (board.victory());
}
}
}
// locationType doorLocations[N]
// under 250 moves
|