Vcoderz Community
We create websites that have it all, beauty & brains
Lebanon Web Design & Development - Coddict
 

Go Back   Vcoderz Community > Computer Zone > Computers & Information Technologies

Notices

Computers & Information Technologies « Everything related to computers and internet. »

Reply
 
Share Thread Tools Search this Thread
Old 05-14-2011   #1
Sary
Registered Member
 
Sary's Avatar
 
Last Online: 11-08-2012
Join Date: Dec 2007
Posts: 113
Thanks: 77
Thanked 53 Times in 33 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default VB code request for a naval battle game !

1. Design a VB.NET windows application for a Naval Battles game.
Naval Battles is a turn-based; it is playable by 2 or more players, each commanding a fleet with the objective of sinking a certain amount of their opponents' ships.
Our project is to develop an nxn board, and the program will arrange its ships randomly.
The user should start firing weapons in order to destroy all ships.
The player has limited number of weapons.

Hint:
· Use 2D arrays to store the computer arrangement.
· Use picturebox objects in order to design the board, and split each weapon picture into parts in order to be loaded if the use hit a part of the ship.

Sample using circles instead of ships.




- Can anybody provide some help ?!



Last edited by Sary; 05-14-2011 at 09:09 PM.
Sary is offline   Reply With Quote
Old 05-14-2011   #2
Adam
Registered Member
 
Adam's Avatar
 
Last Online: 06-03-2013
Join Date: Feb 2008
Posts: 1,410
Thanks: 1,444
Thanked 1,235 Times in 689 Posts
Groans: 25
Groaned at 21 Times in 19 Posts
Default

I made this when a while ago, it's a text based version of the game battleship.
cons: It doesn't contain any images and it's written in main.

Code:
#include <iostream>
#include <string>

using namespace std;

void setNames(string names[]);
int  setNrOfShips();
void setPositions(int** board, string names[], int nrOfShips);
bool checkPositionVacancy(int** board, int pos, int nrOfShips, int i);
void printInfo(int** board, string names[], int nrOfShips, int i);
int  getNrOfShipsLeft(int** boards, int nrOfShips, int i);
bool checkForWinner(int** boards, int nrOfShips);
void play(int** boards, string names[], int nrOfShips);
void AnnounceWinner(int** boards, string names[], int nrOfShips);

int main()
{
    int nrOfShips = setNrOfShips();
    system("CLS");
    int** boards = new int*[2];
    for(int i = 0; i < 2; i++)
    {
        boards[i] = new int[nrOfShips];
    }
    string names[2] = {""};
    setNames(names);
    system("CLS");
    setPositions(boards, names, nrOfShips);

    do
    {
        system("CLS");
        play(boards, names, nrOfShips);
    }
    while(getNrOfShipsLeft(boards, nrOfShips, 0) != 0 && getNrOfShipsLeft(boards, nrOfShips, 1) != 0);

    system("CLS");

    AnnounceWinner(boards, names, nrOfShips);

    for(int i = 0; i < 2; i++)
    {
        delete [] boards[i];
    }
    delete [] boards;

    return 0;
}

void setNames(string names[])
{
    string name;
    cout << "Input Names!\n\n";
    cin.ignore();
    for(int i = 0; i < 2;i++)
    {
        cout << "Player #" << i+1 << ": ";
        getline(cin, name);
        names[i] = name;
    }
}

int setNrOfShips()
{
    int nrOfShips = 0;
    bool flag = false;

    do
    {
        system("CLS");
        cout << "Number of ships (max 14): ";
        cin  >> nrOfShips;
        if(nrOfShips > 0 && nrOfShips < 15)
        {
            flag = true;
        }
        else
        {
            system("CLS");
            cout << "Invalid input!\n";
            system("pause");
        }
    }
    while(!flag);
    return nrOfShips;
}

void setPositions(int** board, string names[], int nrOfShips)
{
    int pos = 0;
    bool flag = false;
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < nrOfShips; j++)
        {
            system("CLS");
            do
            {
                flag = false;
                cout << names[i] << endl;
                for(int k = 0; k < j; k++)
                {
                    cout << "Ship #" << k+1 << ": " << board[i][k] << endl;
                }
                cout << "Ship #" << j+1 << ": ";
                cin  >> pos;
                if(pos > 0 && pos < 16 && checkPositionVacancy(board, pos, nrOfShips, i))
                {
                    board[i][j] = pos;
                    flag = true;
                }
                else if(pos > 0 && pos < 16)
                {
                    cout << "Position is not vacant!\n";
                    system("pause");
                    system("CLS");
                }
                else
                {
                    cout << "Invalid position!\n";
                    system("pause");
                    system("CLS");
                }
            }
            while(!flag);
        }
    }
}

bool checkPositionVacancy(int** board, int pos, int nrOfShips, int i)
{
    int counter = -1;
    bool vacant = true;

    do
    {
        counter++;
        if(board[i][counter] == pos)
        {
            vacant = false;
        }
    }
    while(vacant && counter < nrOfShips);

    return vacant;
}

void printInfo(int** board, string names[], int nrOfShips, int i)
{
    cout << names[i] << "\t";
    cout << "Friendly Ships left: " << getNrOfShipsLeft(board, nrOfShips, i) << "/" << nrOfShips << "\t";
    cout << "Enemy Ships Left: " << getNrOfShipsLeft(board, nrOfShips, !i) << "/" << nrOfShips << endl;
}

int getNrOfShipsLeft(int** boards, int nrOfShips, int i)
{
    int shipsLeft = nrOfShips;
    for(int k = 0; k < nrOfShips; k++)
    {
        if(boards[i][k] == 0)
        {
            shipsLeft--;
        }
    }

    return shipsLeft;
}

void play(int** boards, string names[], int nrOfShips)
{
    int target = 0,
        counter = -1;
    bool flag = false,
         hit = false;

    for(int i = 0; i < 2; i++)
    {
        do
        {
            flag = false;
            system("CLS");
            printInfo(boards, names, nrOfShips, i);
            cout << "\nTarget (1-15): ";
            cin  >> target;
            if(target < 1 || target > 15)
            {
                cout << "Invalid target!\n";
                system("pause");
            }
            else
            {
                flag = true;
            }
        }
        while(!flag);

        counter = -1;

        do
        {
            hit = false;
            counter++;
            if(boards[!i][counter] == target)
            {
                boards[!i][counter] = 0;
                hit = true;
            }
        }
        while(!hit && counter < nrOfShips);

        if(hit)
        {
            cout << "Target Hit!\n";
            system("pause");
        }
        else
        {
            cout << "Miss!\n";
            system("pause");
        }
    }
}

void AnnounceWinner(int** boards, string names[], int nrOfShips)
{
    if(getNrOfShipsLeft(boards, nrOfShips, 0) == 0 && getNrOfShipsLeft(boards, nrOfShips, 1) == 0)
    {
        cout << "It's a tie!\n\n";
    }
    else if(getNrOfShipsLeft(boards, nrOfShips, 0) == 0)
    {
        cout << names[1] <<  " is the Winner!\n\n";
    }
    else if(getNrOfShipsLeft(boards, nrOfShips, 1) == 0)
    {
        cout << names[0] <<  " is the Winner!\n\n";
    }
}

Last edited by Adam; 05-14-2011 at 11:09 PM.
Adam is offline   Reply With Quote
Old 05-15-2011   #3
Sary
Registered Member
 
Sary's Avatar
 
Last Online: 11-08-2012
Join Date: Dec 2007
Posts: 113
Thanks: 77
Thanked 53 Times in 33 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

Adam

dude, your code is in C++
I want it as VB !
Sary is offline   Reply With Quote
Old 05-17-2011   #4
-t-o-n-y-
Registered Member
 
-t-o-n-y-'s Avatar
 
Last Online: 03-13-2012
Join Date: Nov 2006
Posts: 974
Thanks: 2,087
Thanked 482 Times in 317 Posts
Groans: 15
Groaned at 3 Times in 3 Posts
Default

^^ if you did it can you post it in here ?
-t-o-n-y- is offline   Reply With Quote
Old 05-17-2011   #5
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

Quote:
Originally Posted by sary View Post
adam

dude, your code is in c++
i want it as vb !
شو الفرق يعني؟
الحياة أصعب من هيك
__________________

Google is offline   Reply With Quote
Reply

  Vcoderz Community > Computer Zone > Computers & Information Technologies

Tags
battle, code, game, naval, request



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:31 AM.


Lebanon web design and development
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Share