This tutorial is about a simple Game, Bermuda.
First we define the includes:
Shows the game field template.
Final function for the game design.
Creates the ships
initialize the game field.
direction to search
Checks, if a ship is found.
checks the input.
gameend, ends the game if a ship is found
Checks, if input is correct.
The main function. Here the functions are used.
Usage:
First the number (x)
Then the letter (y).
6B for example.
Download:
Bermuda.h
Bermuda.exe
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));
}
First the number (x)
Then the letter (y).
6B for example.
Download:
Bermuda.h
Bermuda.exe