|
|
|||||||
| Computers & Information Technologies « Everything related to computers and internet. » |
![]() |
|
|
Share | Thread Tools | Search this Thread |
|
|
#1 |
|
Registered Member
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
|
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. |
|
|
|
|
|
#2 |
|
Registered Member
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
|
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. |
|
|
|
|
|
#3 |
|
Registered Member
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
|
Adam
dude, your code is in C++ I want it as VB ! |
|
|
|
|
|
#4 |
|
Registered Member
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
|
^^ if you did it can you post it in here ?
|
|
|
|
|
|
#5 |
|
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
|
شو الفرق يعني؟
الحياة أصعب من هيك
__________________
|
|
|
|
![]() |
|
| Tags |
| battle, code, game, naval, request |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|