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 11-07-2010   #1
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 [Help]C++

I got an assignment to make a simplified version of the game battleship.
I know that this code is pretty stupid because I haven's used classes but we got this assignment in the beginning of the course and therefore there are some instructions in the assignment that make using classes a lot dumber.

The problem I'm having is that the second player's hits aren't being detected.
I've been looking for about 30 minutes at the code and I'm not finding the problem.

So if someone could take a look at it and give an answer:
Code:
#include <iostream>
#include <string>
#include <crtdbg.h>

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);

        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";
    }
}


Adam is offline   Reply With Quote
Old 11-11-2010   #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

Nevermind, fixed it
Adam is offline   Reply With Quote
Old 11-15-2010   #3
ZC
Registered Member
 
ZC's Avatar
 
Last Online: 07-15-2013
Join Date: Aug 2006
Posts: 100
Thanks: 13
Thanked 24 Times in 24 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

hehehehe man:P ana 3am ekhod a C++ intro course w dayi3:P i wanted to ask someone to fix MY assignment and then i saw ur assignment battal baddeh:P... ana 3indeh assignment.. ask the user to input a string.. and then input another string.. the program should check if the second is a substring of teh first:P i wrote most of the code but its not working..do you mind taking a look at it? ill post it bass irja3 3la bet
__________________
Books all say different things while people flap their yellow wings trying
to soar by being a whore of life!!!!




ZC is offline   Reply With Quote
Old 11-16-2010   #4
ZC
Registered Member
 
ZC's Avatar
 
Last Online: 07-15-2013
Join Date: Aug 2006
Posts: 100
Thanks: 13
Thanked 24 Times in 24 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

A program that checks if a string is a substring of another....!

#include<iostream>
using namespace std;
#include<cstring>
int main()
{

char a[100];
char b[50];
int i=0;
int j=0;
int lb=strlen(b);
int la=strlen(a);
bool is=false;
cout<<"please input a string"<<endl;
cin.get(a,100);
char garbage;
cin.get(garbage);
cout<<"now enter a string to check if it is a substring"<<endl;
cin.get(b,50);
while(la>=lb&&a[i]!='\0')
{
if(a[i]==b[j])
{i++;
j++;
is=true;}
if(a[i]!=b[j])
{i++;
is=false;
j=0;}

}


if(is=true)
cout<<"yes"<<endl;
if(is=false)
cout<<"no"<<endl;
}
__________________
Books all say different things while people flap their yellow wings trying
to soar by being a whore of life!!!!




ZC is offline   Reply With Quote
Old 11-16-2010   #5
Tawa
 
Tawa's Avatar
 
Last Online: 06-12-2019
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,120
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
Default

Quote:
Originally Posted by ZC View Post
A program that checks if a string is a substring of another....!

#include<iostream>
using namespace std;
#include<cstring>
int main()
{

char a[100];
char b[50];
int i=0;
int j=0;
int lb=strlen(b);
int la=strlen(a);
bool is=false;
cout<<"please input a string"<<endl;
cin.get(a,100);
char garbage;
cin.get(garbage);
cout<<"now enter a string to check if it is a substring"<<endl;
cin.get(b,50);
while(la>=lb&&a[i]!='\0')
{
if(a[i]==b[j])
{i++;
j++;
is=true;}
if(a[i]!=b[j])
{i++;
is=false;
j=0;}

}


if(is=true)
cout<<"yes"<<endl;
if(is=false)
cout<<"no"<<endl;
}

For a start, those two lines should be written after you input a and b:
int lb=strlen(b);
int la=strlen(a);

lb and la do not change when b and a changes, you have to write lb = strlen(b) and la = strlen(a) everytime you change one of them.

However, both variables are not needed, because there's a function in the string class called find that does all that for you.

So your code should look something like this:

Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
    string a;
    string b;
    int i;
    
    cout << "Enter first String: ";
    getline(cin, a);
       
    cout << "Enter second String: ";
    cin.ignore();
    getline(cin, b);

    i = a.find(b);

    if(i < 0)
        cout << "The second String is not a substring." << endl;
    else
        cout << "The second String is a substring." << endl;

    return 0;
}
__________________
What we do in life, echoes in eternity.
Tawa is offline   Reply With Quote
Old 11-16-2010   #6
ZC
Registered Member
 
ZC's Avatar
 
Last Online: 07-15-2013
Join Date: Aug 2006
Posts: 100
Thanks: 13
Thanked 24 Times in 24 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

mish ekhid il .find ....
__________________
Books all say different things while people flap their yellow wings trying
to soar by being a whore of life!!!!




ZC is offline   Reply With Quote
Old 11-16-2010   #7
ZC
Registered Member
 
ZC's Avatar
 
Last Online: 07-15-2013
Join Date: Aug 2006
Posts: 100
Thanks: 13
Thanked 24 Times in 24 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

man lek ana my algorithm.. baddeh illo inno isa le2a ya3mol compare la a[1] ma3 b[1] isa different icrement a[1] only isa a[i]==a[j] start incrementing both. isa kholis il b[j] w il boolean inno is substring is true ktir mni7... isa le2a a[i] different than b[j] bya3teh boolean false..hek...badda nested while loops 2 i think...
__________________
Books all say different things while people flap their yellow wings trying
to soar by being a whore of life!!!!




ZC is offline   Reply With Quote
Old 11-16-2010   #8
ZC
Registered Member
 
ZC's Avatar
 
Last Online: 07-15-2013
Join Date: Aug 2006
Posts: 100
Thanks: 13
Thanked 24 Times in 24 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

btw wow ur program works... i wish 3allamna il 2istez il .find
__________________
Books all say different things while people flap their yellow wings trying
to soar by being a whore of life!!!!




ZC is offline   Reply With Quote
Old 11-16-2010   #9
Tawa
 
Tawa's Avatar
 
Last Online: 06-12-2019
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,120
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
Default

Quote:
Originally Posted by ZC View Post
man lek ana my algorithm.. baddeh illo inno isa le2a ya3mol compare la a[1] ma3 b[1] isa different icrement a[1] only isa a[i]==a[j] start incrementing both. isa kholis il b[j] w il boolean inno is substring is true ktir mni7... isa le2a a[i] different than b[j] bya3teh boolean false..hek...badda nested while loops 2 i think...

Did you learn how to write Functions?
__________________
What we do in life, echoes in eternity.
Tawa is offline   Reply With Quote
Old 11-16-2010   #10
ZC
Registered Member
 
ZC's Avatar
 
Last Online: 07-15-2013
Join Date: Aug 2006
Posts: 100
Thanks: 13
Thanked 24 Times in 24 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

Problem 3. Substrings.
Write a computer program which given two C-strings as input, checks if the first is a substring of the
second.
You are supposed to allow whitespaces in the input strings. You program should interpret a new line
as the end of first input string and similarly for the second input string.
Note that if s1[0 . . . l1 −1] and s2[0 . . . l2 −1] are character strings of lengths l1 and l2 respectively, we
say that s1 is a substring of s2 if there exists an integer i, 0  i  l2 −(l1 −1), such that s1[j] = s2[i+j]
for j = 0, . . . , l1 − 1
For example, “obl” is a substring of “Problem 2”, but “oblm” is not a substring of “Problem 2”.
Your program is supposed to give a YES/NO answer only.
Hint: You need two nested loops and boolean variable.
You are supposed to use the break statement to break the outer loop when the first occurrence of the
s1 in s2 is found (if any).
Note: To read two C-strings using cin.get, use an auxiliary variable to absorb the newline after
reading the first string (as otherwise the second string will be set to the empty string due the the newline
remaining in the input stream .... something annoying in C++
__________________
Books all say different things while people flap their yellow wings trying
to soar by being a whore of life!!!!




ZC is offline   Reply With Quote
Reply

  Vcoderz Community > Computer Zone > Computers & Information Technologies

Tags
helpc



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

Advanced Search

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 02:46 AM.


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