c++ battleship game

This is a discussion on c++ battleship game within the Game Programming forums, part of the General Programming Boards category; Ok, I'm working on a battleship game for my programming class. The user ship placement work nicely (I dont think ...

  1. #1
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44

    help me with a c++ battleship game

    Ok, I'm working on a battleship game for my programming class.
    The user ship placement work nicely (I dont think collision detection works on this version but it does on the one I have at school... Regardless thats not a problem...) Also the computer ship placement works mighty fine 99% of the time, except the submarine placement... For some reason it doesn't work... Can anyone explain why it only places the single letter?
    (btw excuse the lack of comments... The code is pretty simple and I'll give you guys enough credit to figure out what its doing...)
    Code:
    //jt foster
    //2
    //BATTLESHIP
    //(expect some neat ascii art when Im further along)
    #include <iostream.h>
    #include <string.h>
    #include <windows.h>
    #include <stdlib.h> //for rand
    #include <stdio.h> //for printf
    #include <time.h>   //for time
    void showplayerscreen();
    void shipplace();
    void computerplace();
    void computerchoice(int size1, int left1, int up1, int char11);
    void compscreen();
    void placechoice(int size, int left, int up, int char1);
    char shipname[10], compname[10];
    int x,intcolumn, introw,intup,intleft,compcolumn, comprow,compup,compleft;
    char playerboard [10][10]=
    {
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    };
    char hiddencomp [10][10]=
    {
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    };
    char showncomp [10][10]=
    {
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    	{46,46,46,46,46,46,46,46,46,46},
    };
    /*
    this is just random random number stuff
    
        srand(time(NULL));
        printf("A random number from 0 to 99: %d", rand() % 100);
        return 0;
    
    int rand_mid(int low, int high)
    {
       return low+rand()%(high-low+1);
    }
    
    */
    main()
    {
    	HANDLE hStdout;
    	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    	cout<<"Loading"<<endl;
    	Sleep(1000);
    	system("CLS");
    	cout<<"Loading."<<endl;
    	Sleep(1000);
    	system("CLS");
    	cout<<"Loading.."<<endl;
    	Sleep(1000);
    	system("CLS");
    	cout<<"Loading..."<<endl;
    	Sleep(1000);
    	system("CLS");
    	//this neat loading screen 2001 JTech Enterprises.
    	SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE|BACKGROUND_RED);
    	cout<<"Welcome to BATTLESHIP..."<<endl;
    	SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    //	showplayerscreen();
    //	compscreen();
    //	shipplace();
    	computerplace();
    	compscreen();
    	return 0;
    }
    
    void compscreen()
    {
    	
    	comprow=0;
    	compcolumn=0;
    	cout << "" << endl;
    	cout << "    Computer's Board (debug)" << endl;
    	cout << "======================" << endl;
    	cout << " |0|1|2|3|4|5|6|7|8|9|" ;
    	cout<<'\n';
    	cout << comprow << "|";
    	do
    	{
    		if(comprow == 9 && compcolumn == 10)
    		{break;}
    		if(compcolumn > 9 && comprow < 9)
    		{
    			cout << "|"<< endl;
    			comprow = comprow + 1;
    			cout << (comprow) << "|";
    			compcolumn = 0;
    		}
    		if(comprow < 10)
    		{
    			if(compcolumn < 9)
    			{
    				cout << hiddencomp[comprow] [compcolumn] << " ";
    			}
    			if(compcolumn > 8)
    			{
    				cout << hiddencomp[comprow] [compcolumn];
    			}
    		}
    		compcolumn = compcolumn + 1;
    	}
    	while(comprow < 10);
    	comprow = 0;
    	compcolumn = 0;
    	cout << "|" << endl;
    	cout << "======================" << endl;
    	cout << "" << endl;
    }
    
    void computerplace()
    {
    	srand(time(NULL));
    	for (x=5;x>=2;x--)
    	{
    		switch(x)
    		{
    			case 5:
    				strcpy(compname,"Carrier");
    			break;
    			case 4:
    				strcpy(compname,"Battleship");
    			break;
    			case 3:
    				strcpy(compname,"Kruiser");
    			break;
    			case 2:
    				strcpy(compname,"Destroyer");
    			break;
    		}
    		do
    		{
    			compleft=rand()%(10);
    			compup=rand()%(10);
    		}
    		while((hiddencomp[compleft][compup]!=46));
    		hiddencomp[compleft][compup]=int(compname[0]);
    		computerchoice(x,compleft,compup, (int(compname[0])));
    	}
    	do
    	{
    		compleft=rand()%(10);
    		compup=rand()%(10);
    	}
    	while((hiddencomp[compleft][compup]!=46));
    	hiddencomp[compleft][compup]=83;
    	computerchoice(3,compleft,compup, 83);
    }
    
    void computerchoice(int size1, int left1, int up1, int char11)
    {
    	srand(time(NULL));
    	int choice,loop;
    	choice=1+rand()%(4);
    	switch (choice)
    	{
    		case 1:
    			for(loop=1;loop<size1;loop++)
    			{
    				if(hiddencomp[left1-loop][up1]!=46 || int(left1-(size1-1))<(0))
    				{
    					hiddencomp[left1][up1]=46;
    					x=x+1;
    					break;
    				}
    				if(int(left1-(size1-1)) >= (0) && x==size1)
    				{
    					for(loop=1;loop<size1;loop++)
    					{hiddencomp[left1-loop][up1]=char11;}
    					compscreen();
    				}
    			}
    		break;
    		case 2:
    			for(loop=1;loop<size1;loop++)
    			{
    				if(hiddencomp[left1+loop][up1]!=46 || int(left1-(size1-1))>(9))
    				{
    					hiddencomp[left1][up1]=46;
    					x=x+1;
    					break;
    				}
    				if(int(left1-(size1-1)) <= (9) && x==size1)
    				{
    					for(loop=1;loop<size1;loop++)
    					{hiddencomp[left1+loop][up1]=char11;}
    					compscreen();
    				}
    			}
    		break;
    		case 3:
    			for(loop=1;loop<size1;loop++)
    			{
    				if(hiddencomp[left1-loop][up1]!=46 || int(left1-(size1-1))>(9))
    				{
    					hiddencomp[left1][up1]=46;
    					x=x+1;
    					break;
    				}
    				if(int(up1+(size1-1)) <= (9) && x==size1)
    				{
    					for(loop=1;loop<size1;loop++)
    					{hiddencomp[left1][up1+ loop]=char11;}
    					compscreen();
    				}
    			}
    		break;
    		case 4:
    			for(loop=1;loop<size1;loop++)
    			{
    				if(hiddencomp[left1-loop][up1]!=46 || int(up1-(size1-1))<(0))
    				{
    					hiddencomp[left1][up1]=46;
    					x=x+1;
    					break;
    				}
    				if(int(up1-(size1-1)) >= (0) && x==size1)
    				{
    					for(loop=1;loop<size1;loop++)
    					{hiddencomp[left1][up1-loop]=char11;}
    					compscreen();
    				}
    			}
    		break;
    	}
    }
    
    void showplayerscreen()
    {
    	introw=0;
    	intcolumn=0;
    	cout << "" << endl;
    	cout << "    Player's Board" << endl;
    	cout << "======================" << endl;
    	cout << " |0|1|2|3|4|5|6|7|8|9|" ;
    	cout<<'\n';
    	cout << introw << "|";
    	do
    	{
    		if(introw == 9 && intcolumn == 10)
    		{break;}
    		if(intcolumn > 9 && introw < 9)
    		{
    			cout << "|"<< endl;
    			introw = introw + 1;
    			cout << (introw) << "|";
    			intcolumn = 0;
    		}
    		if(introw < 10)
    		{
    			if(intcolumn < 9)
    			{
    				cout << playerboard[introw] [intcolumn] << " ";
    			}
    			if(intcolumn > 8)
    			{
    				cout << playerboard[introw] [intcolumn]<<flush;
    			}
    		}
    		intcolumn = intcolumn + 1;
    	}
    	while(introw < 10);
    	introw = 0;
    	intcolumn = 0;
    	cout << "|" << endl;
    	cout << "======================" << endl;
    	cout << "" << endl;
    }
    
    void placechoice(int size,int left,int up,int char1)
    {
    	int choice,loop;
    	cout<<"Do you want your ship to face: "<< '\n' << "1. UP   2.DOWN   3.RIGHT   4.LEFT"<<'\n';
    	cin>>choice;
    	switch (choice)
    	{
    		case 1:
    			for(loop=1;loop<size;loop++)
    			{
    				if(playerboard[left-loop][up]!=46 || int(left-(size-1))<(0))
    				{
    					playerboard[left][up]=46;
    					cout<<"Don't place ships illegally..."<<endl;
    					x=x+1;
    					break;
    				}
    				if(int(left-(size-1)) >= (0) && x==size)
    				{
    					for(loop=1;loop<size;loop++)
    					{playerboard[left-loop][up]=char1;}
    					showplayerscreen();
    				}
    			}
    		break;
    		case 2:
    			for(loop=1;loop<size;loop++)
    			{
    				if(playerboard[left+loop][up]!=46 || int(left-(size-1))>(9))
    				{
    					playerboard[left][up]=46;
    					cout<<"Don't place ships illegally..."<<endl;
    					x=x+1;
    					break;
    				}
    				if(int(left-(size-1)) <= (9) && x==size)
    				{
    					for(loop=1;loop<size;loop++)
    					{playerboard[left+loop][up]=char1;}
    					showplayerscreen();
    				}
    			}
    		break;
    		case 3:
    			for(loop=1;loop<size;loop++)
    			{
    				if(playerboard[left-loop][up]!=46 || int(left-(size-1))>(9))
    				{
    					playerboard[left][up]=46;
    					cout<<"Don't place ships illegally..."<<endl;
    					x=x+1;
    					break;
    				}
    				if(int(up+(size-1)) <= (9) && x==size)
    				{
    					for(loop=1;loop<size;loop++)
    					{playerboard[left][up+ loop]=char1;}
    					showplayerscreen();
    				}
    			}
    		break;
    		case 4:
    			for(loop=1;loop<size;loop++)
    			{
    				if(playerboard[left-loop][up]!=46 || int(up-(size-1))<(0))
    				{
    					playerboard[left][up]=46;
    					cout<<"Don't place ships illegally..."<<endl;
    					x=x+1;
    					break;
    				}
    				if(int(up-(size-1)) >= (0) && x==size)
    				{
    					for(loop=1;loop<size;loop++)
    					{playerboard[left][up-loop]=char1;}
    					showplayerscreen();
    				}
    			}
    		break;
    	}
    }
    
    void shipplace()
    {
    	for (x=5;x>=2;x--)
    	{
    		switch(x)
    		{
    			case 5:
    				strcpy(shipname,"Carrier");
    			break;
    			case 4:
    				strcpy(shipname,"Battleship");
    			break;
    			case 3:
    				strcpy(shipname,"Kruiser");
    			break;
    			case 2:
    				strcpy(shipname,"Destroyer");
    			break;
    		}
    		do
    		{
    			cout<<"Plot one end of the "<<shipname<<"."<<'\n';
    			cout<<"Please put the left side coordinate, then the top..."<< '\n';
    			cin>>intleft;
    			cin>>intup;
    		}
    		while(intleft<0||intleft>9||intup<0||intup>9||(playerboard[intleft][intup]!=46));
    		playerboard[intleft][intup]=int(shipname[0]);
    		showplayerscreen();
    		placechoice(x,intleft,intup, (int(shipname[0])));
    	}
    	do
    	{
    		cout<<"Plot one end of the Submarine."<<'\n';
    		cout<<"Please put the left side coordinate, then the top..."<< '\n';
    		cin>>intleft;
    		cin>>intup;
    	}
    	while(intleft<0||intleft>9||intup<0||intup>9||(playerboard[intleft][intup]!=46));
    	playerboard[intleft][intup]=83;
    	showplayerscreen();
    	placechoice(3,intleft,intup, 83);
    }
    Now the calls for the majority of this code (specifically the user placement) are commented out so I dont have to deal with them when debugging the cpu placement...
    Thanks to anyone who actually looks through this or compiles it to figger out my problem...
    Last edited by JTtheCPPgod; 01-01-2002 at 04:47 PM.

  2. #2
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    Oh and btw, Im using MS Visual C++ as my compiler and my OS is windows 98 (i think its also 98 on my school cpu, I dunno) yea, just incase that helps for some reason

  3. #3
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    Argh, I figured it out on my own. I still have no idea why it was doing what it was doing (it was exploiting a loophole in the code, but oftentimes it would screw up even when the conditions for the loophole weren't met)

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Hey, could I have the working code for that? It looks pretty cool.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    Yea, once I finish, I'll post the code.
    "No! I must have my delicious cupcakes, my sweet cakey treasures, piping hot from their 40 watt WOMB!!!"
    --Captain Murphy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  2. C# - Building a SCUMM-like game.. questions
    By Iyouboushi in forum Game Programming
    Replies: 0
    Last Post: 05-24-2008, 10:54 PM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM
  5. here is my battleship game so far...come try it out
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2002, 12:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21