void movePlayer(char A, short d) { if (A == 'H') //H for horizontal { short newY = player_1.getPlayerPosY() + d; if (maze[player_1.getPlayerPosX()][newY] == 1) { cout << "Sorry, wall! \n"; } else { player_1.setPlayerPosY(player_1.getPlayerPosY() + d); } } if (A == 'V') //V for vertical { short newX = player_1.getPlayerPosX() + d; if (maze[newX][player_1.getPlayerPosY()] == 1) { cout << "Sorry, wall! \n"; } else { player_1.setPlayerPosX(player_1.getPlayerPosX() + d); } } }
void checkCatch() { if ((player_1.getPlayerPosX() == aRabbit[0]->getRabbitPosX()) && (player_1.getPlayerPosY() == aRabbit[0]->getRabbitPosY())) { cout << "\nRabbit Caught!!!\n"; aRabbit[0]->setisCaught(true); } }
int main() { spawnRabbit(); spawnRabbit(); do{ cout << "Player position: "; cout << player_1.getPlayerPosX(); cout << ","; cout << player_1.getPlayerPosY(); cout << ";\n"; rabbitAI(); //move the rabbits checkCatch(); //draw the maze walls / player / rabbits for (int h = 0; h < 23; h++) //draw row { for (int i = 0; i < 31; i++) //draw colum { if (h == player_1.getPlayerPosX() && i == player_1.getPlayerPosY()) // put the player down { if (aRabbit[0]->getisCaught()) { cout << "R"; //with rabbit } else { cout << "X"; //with no rabbit } } else if (h == aRabbit[0]->getRabbitPosX() && i == aRabbit[0]->getRabbitPosY()) //draw 1st rabbit { cout << "O"; } else if (h == aRabbit[1]->getRabbitPosX() && i == aRabbit[1]->getRabbitPosY()) //draw 2nd rabbit { cout << "o"; } else if (maze[h][i] == true) //put # where the wall is { cout << "#"; } else //put space where the tunnel is { cout << " "; } } cout << "\n"; } cin >> cKeypressed; cout << "\n\n"; if (aRabbit[0]->getisCaught()) { PlayerDirection = 1; } if (cKeypressed == playerDirections[PlayerDirection][0]) { movePlayer('H', -1); } if (cKeypressed == playerDirections[PlayerDirection][1]) { movePlayer('H', +1); } if (cKeypressed == playerDirections[PlayerDirection][2]) { movePlayer('V', -1); } if (cKeypressed == playerDirections[PlayerDirection][3]) { movePlayer('V', +1); } } while(cKeypressed != 'e' || cKeypressed != 'E'); return 0; }
void rabbitAI() { for (short r = 0; r != Rabbitcount; r++) //do it for all the rabbits { if (!aRabbit[r]->getisCaught()) { //does the rabbit move, or not bool isMoving = false; bool moved = false; short i = RollDice(RabbitMoveChance); if (i == RabbitMoveChance) { isMoving = true; cout << " \n rabbit moving! \n"; do //this bit makes sure that the rabbit always moves to a valid position (=does not want to go against the wall) { //get the direction ( 1==up, 2==down, 3==left, 4==right) i = RollDice(4); if (i == 1) { short newX = aRabbit[r]->getRabbitPosX() - 1; //get the "future location" if (maze[newX][aRabbit[r]->getRabbitPosY()] == 0) //not that's hitting the wall { aRabbit[r]->setRabbitPosX(aRabbit[r]->getRabbitPosX() - 1); //move the object there moved = true; //exit the loop } } if (i == 2) { short newX = aRabbit[r]->getRabbitPosX() + 1; if (maze[newX][aRabbit[r]->getRabbitPosY()] == 0) { aRabbit[r]->setRabbitPosX(aRabbit[r]->getRabbitPosX() + 1); moved = true; } } if (i == 3) { short newY = aRabbit[r]->getRabbitPosY() - 1; if (maze[aRabbit[r]->getRabbitPosX()][newY] == 0) { aRabbit[r]->setRabbitPosY(aRabbit[r]->getRabbitPosY() - 1); moved = true; } } if (i == 4) { short newY = aRabbit[r]->getRabbitPosY() + 1; if (maze[aRabbit[r]->getRabbitPosX()][newY] == 0) { aRabbit[r]->setRabbitPosY(aRabbit[r]->getRabbitPosY() + 1); moved = true; } } } while (moved != true); } } else //if rabbit is caught { aRabbit[r]->setRabbitPosX(player_1.getPlayerPosX()); aRabbit[r]->setRabbitPosY(player_1.getPlayerPosY()); } } }