[TUTORIAL] [C++]ConsoleGame-Bermuda

Remis

Well-Known Member
Messages
663
This tutorial is about a simple Game, Bermuda.

First we define the includes:
Code:
#include <iostream>
using namespace std;
#include <stdlib.h>

const int X=9;
const int Y=7;
const int MaxShip=4;

struct tShip
{
    int x;
    int y;
    bool found;
};

Shows the game field template.
Code:
void ShowGameField(char SpielFeld[X][Y])
{
int x, y;

        cout << "     1  2  3  4  5  6  7  8  9" << endl;
        for (y=0; y<Y; y++)
        {
            cout << (char)('A'+y) << "  ";
            for (x=0; x<X; x++)
            {
                cout << "  " << SpielFeld[x][y] ;
            }
            cout << endl;
        }
}

Final function for the game design.
Code:
void initShow(char SpielFeld[X][Y])
{
int x, y;

    for (x=0; x<X; x++)
    {
        for (y=0; y<Y; y++)
        {
            SpielFeld[x][y] = '.';
        }
    }
}

Creates the ships
Code:
void initShips(tShip Ship[])
{
int i, j;
bool neueZahl;

    srand(0);
    for(i=0; i<MaxShip; i++)
    {
        Ship[i].found = false;
        do
        {
            Ship[i].x = rand() % X;
            Ship[i].y = rand() % Y;
            neueZahl = true;
            for (j=0; j<i; j++)
            {
                if (Ship[j].x==Ship[i].x
                   && Ship[j].y==Ship[i].y)
                {
                    neueZahl = false;
                }
            }
        } while (!neueZahl);
    }
    for (i=0; i<MaxShip; i++)
    {
        cout << Ship[i].x << "," << Ship[i].y << " ";
    }
    cout << endl;
}

initialize the game field.
Code:
void initGameField(char SpielFeld[X][Y], tShip Ship[])
{
    initShow(SpielFeld);
    initShips(Ship);
}

bool isHereAShip(tShip Ship[], int x, int y,
                      bool markieren=false)
// This function gets, whether a ship is at the poin. If paramter is true, Ship is found.
{
    for (int i=0; i<MaxShip; i++)
    {
        if (Ship[i].x==x && Ship[i].y==y)
        {
            if (markieren)
            {
                Ship[i].found = true;
            }
            return true;
        }
    }
    return false;
}

direction to search
Code:
int suche(tShip Ship[], int x, int y, int xdiff, int ydiff)
{
    x+=xdiff;
    y+=ydiff;
    while(x<X && x>=0 && y<Y && y>=0)
    {
        if (isHereAShip(Ship, x, y))
        {
            return 1;
        }
        x+=xdiff;
        y+=ydiff;
    }
    return 0;
}

Checks, if a ship is found.
Code:
int searchShip(tShip Ship[], int x, int y)
{
int Amount=0;

    if (isHereAShip(Ship, x, y, true))
    {
        return MaxShip;
    }
    else
    {
        for (int i=-1; i<=1; i++)
        {
            for (int j=-1; j<=1; j++)
            {
                if (!(i==0 && j==0))
                {
                    Amount += suche(Ship, x, y, i, j);
                }
            }
        }
    }
    return Amount;
}

checks the input.
Code:
void Input(int *x, int *y)
{
char cx, cy;

    cin >> cx >> cy;
    *x = cx - '1';
    *y = cy - 'A';
}

gameend, ends the game if a ship is found
Code:
bool GameEnd(tShip Ship[])
{
    for(int i=0; i<MaxShip; i++)
    {
        if (!Ship[i].found)
        {
            return false;
        }
    }
    return true;
}

Checks, if input is correct.
Code:
void PutIn(char SpielFeld[X][Y], int x, int y, int Amount)
{
    SpielFeld[x][y] = Amount>=MaxShip ? 'X' : '0'+Amount;
}

The main function. Here the functions are used.
Code:
int main()
{
tShip Ship[MaxShip];
char SpielFeld[X][Y];
int x, y;
int Amount;
    initGameField(SpielFeld, Ship);
    do
    {
        ShowGameField(SpielFeld);
        Input(&x, &y);
        Amount = searchShip(Ship, x, y);
        PutIn(SpielFeld, x, y, Amount);
		
    }
	
    while (!GameEnd(Ship));

}
Usage:
First the number (x)
Then the letter (y).
6B for example.

Download:
Bermuda.h
Bermuda.exe
 
Re: [C++]ConsoleGame-Bermuda

Nice post !

btw
I'm going to install c++ tomorrow :)
 
Re: [C++]ConsoleGame-Bermuda

PAWN is based C++,

I prefe these C++ programms:
Dev c++
http://bloodshed-dev-c.en.softonic.com/
Microsoft^^
http://www.microsoft.com/germany/express/product/visualcplusplusexpress.aspx
and Borland C++ (Borland C++ Builder costs money, Borland C++ Compiler is for free)
http://www.codegear.com/downloads/free/cppbuilder
 
Re: [C++]ConsoleGame-Bermuda

Zezombia said:
I already know that remis :tongue:.
I already know that you knew it :tongue: Only for some visitors who dont already know.

What c++ prog do you use? Do you know one to compile it to linux /mac programms?
 
Re: [C++]ConsoleGame-Bermuda

I don't use linux or mac, PC is the only things I'm developing for. Also I don't do c++, I just know the basics on it.
 
Re: [C++]ConsoleGame-Bermuda

i dont know what you guys are even talking about so ill just sit here quietly. :)
 
Re: [C++]ConsoleGame-Bermuda

omfg i needed this soo badly....
i searched all over the internet
thnx dude

<it happens to be one of the forums i use and i searchd the internet lol;...>
 
Re: [C++]ConsoleGame-Bermuda

SAkiller999 said:
omfg i needed this soo badly....
i searched all over the internet
thnx dude

<it happens to be one of the forums i use and i searchd the internet lol;...>
that happens to me all the time xD
 
Re: [C++]ConsoleGame-Bermuda

srry for the 2nd post but....

what did u use to compile??
what program...pls pm or just give the link thnx..
 
Back
Top Bottom