// WumpusGame.cpp implementation for the calss WumpusGame.cpp #include "WumpusGame.h" // sets the game to alive WumpusGame::WumpusGame () { gameAlive=false; } // deallocated the dynamic memory WumpusGame::~WumpusGame() { DeleteOldBoard(); } // deallocated the dynamic memory void WumpusGame::DeleteOldBoard() { gameAlive = false; for(int i=0; iGetVisit()) gameGrid[x][y]->Draw(display,x,y); } } player.Draw(display); } // destroys the game board // and makes a new board void WumpusGame::NewGame(GP142Display& display) { if(gameAlive) { DeleteOldBoard(); display.clear(White); } NewBoard(display); } // moves the player around by the key board void WumpusGame::Move(char direction, GP142Display& display){ if(hunter == false) { NewGame(display); return; } if(direction == 'w') { player.DrawOVER(display); player.MoveUp(); player.Draw(display); //set to visit gameGrid[player.GetX()][player.GetY()]->SetVisit(); gameGrid[player.GetX()][player.GetY()]->Draw(display,player.GetX(),player.GetY()); //checks if player position is adjacent to wumpus or pits TypeQuerry(player.GetX(), player.GetY(), display); } if(direction == 's') { player.DrawOVER(display); player.MoveDown(); player.Draw(display); gameGrid[player.GetX()][player.GetY()]->SetVisit(); gameGrid[player.GetX()][player.GetY()]->Draw(display,player.GetX(),player.GetY()); TypeQuerry(player.GetX(), player.GetY(), display); } if(direction == 'a') { player.DrawOVER(display); player.MoveLeft(); player.Draw(display); gameGrid[player.GetX()][player.GetY()]->SetVisit(); gameGrid[player.GetX()][player.GetY()]->Draw(display,player.GetX(),player.GetY()); TypeQuerry(player.GetX(), player.GetY(), display); } if(direction == 'd') { player.DrawOVER(display); player.MoveRight(); player.Draw(display); gameGrid[player.GetX()][player.GetY()]->SetVisit(); gameGrid[player.GetX()][player.GetY()]->Draw(display,player.GetX(),player.GetY()); TypeQuerry(player.GetX(), player.GetY(), display); } } // check is the caves around is the wumpus // and if it is then it will either kill the // wumpus or kill the player void WumpusGame::Shoot(char direction, GP142Display& display){ int x,y ;// to hold the position of the player x=player.GetX(); y=player.GetY(); // if the player is death // new game if(hunter == false) { NewGame(display); return; } bool hit; hit = false; hunter = false; // if the player hits the wumpus then player wins the game // however if the player does not hit the wumpus player has // no choice to die!! if(direction == 'i') { if(y<6) { if(gameGrid[x][(y+1)]->ShootWumpus()) { gameGrid[x][(y+1)]->Draw(display,x, (y+1) ); display.write(CaveX, CaveY -65, "You have captured the Wumpus. You Win", Red,20); hit=true; } } if(!hit) display.write(CaveX, CaveY -65, "You shot at the wrong direction. Geme Over", Red,12); } if(direction == 'k') { if(y>0) { if(gameGrid[x][(y-1)]->ShootWumpus()) { gameGrid[x][(y-1)]->Draw(display,x, (y-1) ); display.write(CaveX, CaveY -65, "You have captured the Wumpus. You Win", Red,20); hit=true; } } if(!hit) display.write(CaveX, CaveY -65, "You shot at the wrong direction. Geme Over", Red,12); } if(direction == 'j') { if(x>0) { if(gameGrid[(x-1)][y]->ShootWumpus()) { gameGrid[(x-1)][y]->Draw(display,(x-1), y ); display.write(CaveX, CaveY -65, "You have captured the Wumpus. You Win", Red,20); hit=true; } } if(!hit) display.write(CaveX, CaveY -65,"You shot at the wrong direction. Geme Over", Red,12); } if(direction == 'l') { if(x<6) { if(gameGrid[(x+1)][y]->ShootWumpus()) { gameGrid[(x+1)][y]->Draw(display,(x+1), y); display.write(CaveX, CaveY -65, "You have captured the Wumpus. You Win", Red,20); hit=true; } } if(!hit) display.write(CaveX, CaveY -65, "You shot at the wrong direction. Geme Over", Red,12); } if(!hit) display.write(CaveX, CaveY -65, "You shot at the wrong direction. Geme Over", Red,12); } // finds the adjancent cave is either a pit or a wumpus void WumpusGame::TypeQuerry(int x, int y,GP142Display& display){ // if the player is death if(hunter == false) { NewGame(display); gameAlive = false; return; } // check if the caves around it is // wumpus of pits if(gameGrid[x][y]->GetWhatsNear()) { gameGrid[x][y]->Action(display); hunter = false; return; } if(x>0) { if(gameGrid[x-1][y]->GetWhatsNear()) { gameGrid[x-1][y]->FeelWhatsNear(display); } } if(x<6) { if(gameGrid[x+1][y]->GetWhatsNear()) { gameGrid[x+1][y]->FeelWhatsNear(display); } } if(y>0) { if(gameGrid[x][y-1]->GetWhatsNear()) { gameGrid[x][y-1]->FeelWhatsNear(display); } } if(y<6) { if(gameGrid[x][y+1]->GetWhatsNear()) { gameGrid[x][y+1]->FeelWhatsNear(display); } } } // new board the sets the pits, wumpus and player // position radomly void WumpusGame::NewBoard(GP142Display& display){ gameAlive = true; hunter = true; // make all the caves empty for now for (int l=0; l< MaxCave; l++) { for (int m=0; m< MaxCave; m++) { gameGrid[l][m] = new Empty(); } } // random function to set postion for the playa!! player.SetX(rand()% MaxCave); player.SetY(rand()% MaxCave); // draw the character on to the graphics player.Draw(display); // and set the positon the visited gameGrid[player.GetX()][player.GetY()]->SetVisit(); // also empty gameGrid[player.GetX()][player.GetY()] = new Empty(); gameGrid[player.GetX()][player.GetY()]->Draw(display,player.GetX(),player.GetY()); int xPt,yPt; // find the positions for wumpus // do until wumpus doent equal to the player position do{ xPt = (rand()% MaxCave); yPt = (rand()% MaxCave); }while(xPt==player.GetX() && yPt== player.GetY()); // delete the one of the empty delete gameGrid[xPt][yPt]; // and set it to wumpus gameGrid[xPt][yPt] = new Wumpus(); // positions for pits int xPits, yPits; // make 5 or less pits for(int i=0; i<5; i++) { // do until pits position does not match with the // wumpus or the character do{ xPits = (rand()% MaxCave); yPits = (rand()% MaxCave); }while((xPits == xPt && yPits == yPt) || (xPits==player.GetX() && yPits== player.GetY())); // deallocted the position delete gameGrid[xPits][yPits]; // and set it to one of the pits gameGrid[xPits][yPits] = new Pits(); } // check if the character is standing adjacent to // pits or wumps TypeQuerry(player.GetX(), player.GetY(), display); // set the player position to visited gameGrid[player.GetX()][player.GetY()]->SetVisit(); }//end createnewboard