GPL'ed Battleship clone Win32/Linux 1.2.1
Submitted By:
i_like_cpp
Rating:
(Not rated) (
Rate It)
/*
Free Battleship clone for Win32/Linux Console.
Copyright (C) 2005 Dennis Gertitschke
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "../inc/Player.h"
#include "../inc/Ship.h"
#include "../inc/Screen.h"
#include <stdlib.h>
Player::Player(){
m_pFrigate=new Frigate();
m_pDestroyer=new Destroyer();
m_pCruiser=new Cruiser();
m_pBattleship=new Battleship();
m_pScreen=new Screen();
m_bDefeat=false;
m_iShipCount=4;
for(int i=0;i<100;i++){
m_arrBoard[i]=NULL;
m_barrValidStart[i]=true;
m_barrValidShot[i]=true;
}
m_iIndex=0;
}
Player::~Player(){
delete m_pFrigate;
delete m_pDestroyer;
delete m_pCruiser;
delete m_pBattleship;
delete m_pScreen;
}
bool Player::returnStatus(){
return m_bDefeat;
}
bool Player::validateCoordinate(char cX,int iY){
if(cX<'A'||cX>'J'||iY<1||iY>10)
return false;
else
return true;
}
int Player::transformToBoardIndex(char cX,int iY){
return (cX-65)+(iY-1)*10;
}
void Player::markNeighboursInvalid(char cStartX,int iStartY){
if(validateCoordinate(static_cast<char>(cStartX+1),iStartY)){
m_iIndex=transformToBoardIndex(static_cast<char>(cStartX+1),iStartY);
m_barrValidStart[m_iIndex]=false;
}
if(validateCoordinate(static_cast<char>(cStartX-1),iStartY)){
m_iIndex=transformToBoardIndex(static_cast<char>(cStartX-1),iStartY);
m_barrValidStart[m_iIndex]=false;
}
if(validateCoordinate(cStartX,iStartY+1)){
m_iIndex=transformToBoardIndex(cStartX,iStartY+1);
m_barrValidStart[m_iIndex]=false;
}
if(validateCoordinate(cStartX,iStartY-1)){
m_iIndex=transformToBoardIndex(cStartX,iStartY-1);
m_barrValidStart[m_iIndex]=false;
}
if(validateCoordinate(static_cast<char>(cStartX+1),iStartY-1)){
m_iIndex=transformToBoardIndex(static_cast<char>(cStartX+1),iStartY-1);
m_barrValidStart[m_iIndex]=false;
}
if(validateCoordinate(static_cast<char>(cStartX+1),iStartY+1)){
m_iIndex=transformToBoardIndex(static_cast<char>(cStartX+1),iStartY+1);
m_barrValidStart[m_iIndex]=false;
}
if(validateCoordinate(static_cast<char>(cStartX-1),iStartY-1)){
m_iIndex=transformToBoardIndex(static_cast<char>(cStartX-1),iStartY-1);
m_barrValidStart[m_iIndex]=false;
}
if(validateCoordinate(static_cast<char>(cStartX-1),iStartY+1)){
m_iIndex=transformToBoardIndex(static_cast<char>(cStartX-1),iStartY+1);
m_barrValidStart[m_iIndex]=false;
}
}