Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Please help!!! Basic C++

Name: Anonymous 2009-03-25 20:32

I really need you guys!

I have an assignment due tommorrow (out of 4 others) that I just haven't been able to grasp.
The aim is to write a C++ program to play a simplified version of battleships. This involves using a single dimensional array length of 30 for the playing board of 0s and 1s. And two sizes of ship, 1 (6of) and 2 (3of), placed randomly on the board. Then just a 'Hit' and 'Miss' system. Once the games over it should tell the player how many guesses it took them.

If there is anyone who could lend a hand I would be extreamly greatful!

Name: Anonymous 2009-03-26 8:46

#include "stdafx.h"
#include <iostream>

using namespace std;

int main(int argc, char ** argv)
{
    bool board[30];
    int count = 0;
    int x;
    bool gameEnd = 0;
    int guess;

    for (int i=0; i<30; i++) { board[i]=0; }

    for(int i=0;i<=6;i++)
    {
        board[rand()%30+1]=1;
    }
    for(int i=0;i<=2;i++)
    {
        x=rand()%29+1;board[x]=1; board[x+1]=1;
    }

    do
    {
        cout<<"pick a number betwe%£%£%en 1 and 30: ";
        cin >> guess;
        if(board[guess]==1)
        {
            cout<<"HIT!\n";
            board[guess] = 0;
            cout << "You have been trolled " << count << " times.";
        }
        else
        {
            cout<<"MISS!\n";
            cout << "You have been trolled " << count << " times.";
        }
        count ++;
   
        int checker;
        for (int count =0; count < 30; count++)
        {
           if (board[count]==1)
              checker++;
           if (checker == 30)
              gameEnd = 1;
        }
    }
    while(count<100 && gameEnd == 0);

    cout << " It took you "<< count << " tries.";
    return 0;
}

Why does it endgame on the first turn?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List