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/User.h"
#include "../inc/Computer.h"
#include "../inc/Screen.h"
#include "../inc/Common.h"
#ifdef WIN32
#include <windows.h>
#else
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#include <sys/mman.h>
#include <termios.h>
#endif
#ifndef WIN32
void playWave(const char *szFilename,int ibitsPerSample,int iChannels,int iSampleRate){
int fd_sound;
int fd_device;
struct stat fileinfo;
char *buf;
tcflow(STDOUT_FILENO,TCOOFF);
fd_sound=open(szFilename,O_RDONLY);
fstat(fd_sound,&fileinfo);
buf=static_cast<char*>(mmap(0,fileinfo.st_size,PROT_READ,MAP_SHARED,fd_sound,0));
fd_device=open("/dev/dsp",O_WRONLY);
ioctl(fd_device,SOUND_PCM_WRITE_BITS,&ibitsPerSample);
ioctl(fd_device,SOUND_PCM_WRITE_CHANNELS,&iChannels);
ioctl(fd_device,SOUND_PCM_WRITE_RATE,&iSampleRate);
write(fd_device,buf,fileinfo.st_size);
ioctl(fd_device,SOUND_PCM_SYNC,0);
close(fd_device);
munmap(buf,fileinfo.st_size);
close(fd_sound);
tcflow(STDOUT_FILENO,TCOON);
}
#endif
int main(int argc,char *argv[]){
User humanPlayer1;
Computer aiPlayer1;
Computer aiPlayer2;
Screen screen;
int iMainChoice=0;
int iCreditsChoice=0;
#ifndef WIN32
struct timespec timeout={0,200000000},remains={0,0};
#endif
do{
iMainChoice=screen.printMenu();
switch(iMainChoice){
case GAMETYPE_PLAYER_VS_COMPUTER:
screen.setMode(GAMETYPE_PLAYER_VS_COMPUTER);
aiPlayer2.setMode(GAMETYPE_PLAYER_VS_COMPUTER);
humanPlayer1.registerCallback(static_cast<void*>(&aiPlayer2),Computer::validateUserShotCallback);
aiPlayer2.registerCallback(static_cast<void*>(&humanPlayer1),User::validateCompShotCallback);
do{
screen.drawBoard();
humanPlayer1.deployFleet();
aiPlayer2.deployFleet();
screen.promptUserToShoot();
do{
if(!humanPlayer1.returnStatus())
humanPlayer1.shoot();
else
break;
if(!aiPlayer2.returnStatus())
aiPlayer2.shoot();
else
break;
}while(true);
iCreditsChoice=screen.printCredits(humanPlayer1.returnStatus(),aiPlayer2.returnStatus());
humanPlayer1.reset();
aiPlayer2.reset();
}while(iCreditsChoice==CREDITS_YES);
if(iCreditsChoice==CREDITS_NO)
return 0;
break;
case GAMETYPE_COMPUTER_VS_COMPUTER:
screen.setMode(GAMETYPE_COMPUTER_VS_COMPUTER);
aiPlayer1.setMode(GAMETYPE_COMPUTER_VS_COMPUTER);
aiPlayer2.setMode(GAMETYPE_COMPUTER_VS_COMPUTER);
aiPlayer1.registerCallback(static_cast<void*>(&aiPlayer2),Computer::validateCompShotCallback);
aiPlayer2.registerCallback(static_cast<void*>(&aiPlayer1),Computer::validateCompShotCallback);
do{
screen.drawBoard();
aiPlayer1.deployFleet();
aiPlayer2.deployFleet();
do{
if(!aiPlayer1.returnStatus())
aiPlayer1.shoot();
else
break;
#ifdef WIN32
Sleep(50);
#else
nanosleep(&timeout,&remains);
#endif
if(!aiPlayer2.returnStatus())
aiPlayer2.shoot();
else
break;
#ifdef WIN32
Sleep(50);
#else
nanosleep(&timeout,&remains);
#endif
}while(true);
iCreditsChoice=screen.printCredits(aiPlayer1.returnStatus(),aiPlayer2.returnStatus());
aiPlayer1.reset();
aiPlayer2.reset();
}while(iCreditsChoice==CREDITS_YES);
if(iCreditsChoice==CREDITS_NO)
return 0;
break;
case GAMETYPE_EXIT:
iCreditsChoice=screen.printCredits(false,false);
return 0;
break;
default:
return 0;
break;
}
}while(iMainChoice!=GAMETYPE_EXIT);
return 0;
}