You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
334 lines
7.7 KiB
334 lines
7.7 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "Modules.h"
|
|
|
|
|
|
// Methode, die den Eimer erstellt
|
|
void buildBin(int width, int heigth)
|
|
{
|
|
field[width][heigth] = 'V';
|
|
field[width + 1][heigth + 1] = '\\';
|
|
field[width + 2][heigth + 2] = '\\';
|
|
field[width - 1][heigth + 1] = '/';
|
|
field[width - 2][heigth + 2] = '/';
|
|
field[width][heigth + 1] = ' ';
|
|
}
|
|
|
|
void loadLevel(int lvlNum)
|
|
{
|
|
clearField();
|
|
ballX = -1;
|
|
ballY = 27;
|
|
|
|
int tmpX;
|
|
int tmpY;
|
|
|
|
switch (lvlNum)
|
|
{
|
|
// Level 1: Nur der Korb wird erstellt
|
|
case (1):
|
|
binX = 12;
|
|
binY = 10;
|
|
break;
|
|
|
|
// Level 2-6: Zufällige Positionen für den Korb hinzugefügt
|
|
case (2):
|
|
case (3):
|
|
case (4):
|
|
case (5):
|
|
case (6):
|
|
tmpX = rand() % 19;
|
|
tmpY = rand() % 12;
|
|
while (5 > tmpX)
|
|
{
|
|
tmpX = rand() % 19;
|
|
}
|
|
while (5 > tmpY)
|
|
{
|
|
tmpY = rand() % 12;
|
|
}
|
|
binX = tmpX;
|
|
binY = tmpY;
|
|
break;
|
|
|
|
// Wind wird dem Level 7 & 8 hinzugefügt
|
|
case (7):
|
|
binX = 2;
|
|
binY = 7;
|
|
windForce = -2;
|
|
printf("Ein Sturm zieht auf!\n");
|
|
sleep(3);
|
|
break;
|
|
|
|
case (8):
|
|
binX = 19;
|
|
binY = 7;
|
|
windForce = 1;
|
|
break;
|
|
|
|
// Wände erscheinen jetzt in den Leveln 9 & 10
|
|
case (9):
|
|
windForce = -1;
|
|
binX = 8;
|
|
binY = 9;
|
|
wallX = 7;
|
|
wallLength = 4;
|
|
wallY = 17;
|
|
break;
|
|
|
|
case (10):
|
|
windForce = 2;
|
|
binX = 16;
|
|
binY = 12;
|
|
wallX = 11;
|
|
wallLength = 4;
|
|
wallY = 19;
|
|
break;
|
|
// Abschluss aller Level
|
|
case (11):
|
|
system("clear");
|
|
generateField();
|
|
addScoreboard();
|
|
sortScoreboard();
|
|
exit(0);
|
|
break;
|
|
|
|
default:
|
|
exit(0);
|
|
}
|
|
|
|
getStartPosition();
|
|
printf("Der Ball wird geworfen!!!");
|
|
sleep(1);
|
|
}
|
|
|
|
int start()
|
|
{
|
|
loadLevel(lvlNumber);
|
|
while (1)
|
|
{
|
|
clearField();
|
|
field[ballX][ballY -= 1] = 'O';
|
|
generateField();
|
|
int ball_In_Bin = checkBallPosition();
|
|
|
|
switch (ball_In_Bin)
|
|
{
|
|
case (1):
|
|
scorePoints += 50;
|
|
loadLevel(++lvlNumber);
|
|
break;
|
|
|
|
case (2):
|
|
scorePoints -= 5;
|
|
lifeCount--;
|
|
if (lifeCount < 1)
|
|
{
|
|
if (lvlNumber == 1)
|
|
{
|
|
char choice = ' ';
|
|
printf("Scheint so, als wäre das Spiel eine Nummer zu groß für dich!\n");
|
|
printf("Willst du einen leichteren Modus starten?\n[J]a/[N]ein\n");
|
|
scanf(" %c", &choice);
|
|
system("start \"\" \"https://youtu.be/dQw4w9WgXcQ\"");
|
|
return 0;
|
|
}
|
|
addScoreboard();
|
|
sortScoreboard();
|
|
return 0;
|
|
}
|
|
loadLevel(lvlNumber);
|
|
break;
|
|
}
|
|
ball_In_Bin = 0;
|
|
sleep(1);
|
|
}
|
|
}
|
|
|
|
void getStartPosition()
|
|
{
|
|
while (ballX == -1)
|
|
{
|
|
for (int i = 2, j = 0; i <= 20; i += 2, j++)
|
|
{
|
|
field[i][ballY] = j + '0';
|
|
}
|
|
generateField();
|
|
|
|
printf("Wähle die Position des Balls (0-9):\n");
|
|
|
|
scanf("%d", &ballX);
|
|
|
|
if (ballX < 0 || 9 < ballX)
|
|
{
|
|
ballX = -1;
|
|
continue;
|
|
}
|
|
ballX = (ballX + 1) * 2;
|
|
}
|
|
}
|
|
|
|
void clearField()
|
|
{
|
|
for (int i = 0; i < fieldWidth; i++)
|
|
{
|
|
for (int j = 0; j < fieldHeigth; j++)
|
|
{
|
|
field[i][j] = ' ';
|
|
}
|
|
}
|
|
}
|
|
|
|
void generateField()
|
|
{
|
|
system("clear");
|
|
buildBin(binX, binY);
|
|
|
|
if (scorePoints < 0)
|
|
scorePoints = 0;
|
|
|
|
// Hier wird Stück für Stück die Zahl aufgeteilt, um alle
|
|
// einzelnen Stellen in das Feld einzutragen
|
|
int tmp = scorePoints;
|
|
field[12][1] = '0' + tmp % 10;
|
|
|
|
tmp /= 10;
|
|
field[11][1] = '0' + tmp % 10;
|
|
|
|
tmp /= 10;
|
|
field[10][1] = '0' + tmp % 10;
|
|
|
|
field[1][1] = lifeCount < 1 ? 'X' : 'O';
|
|
field[2][1] = lifeCount < 2 ? 'X' : 'O';
|
|
field[3][1] = lifeCount < 3 ? 'X' : 'O';
|
|
|
|
if (windForce != 0)
|
|
{
|
|
field[19][1] = windForce < 0 ? '<' : '>';
|
|
field[20][1] = windForce < 0 ? (windForce * -1 + '0') : (windForce + '0');
|
|
}
|
|
|
|
if (wallX > 0 && wallY > 0 && wallLength > 0)
|
|
{
|
|
for (int i = wallX; i < +wallX + wallLength; i++)
|
|
{
|
|
field[i][wallY] = '_';
|
|
}
|
|
}
|
|
|
|
if (lvlNumber > 6 && windIntervall % 2 == 0)
|
|
{
|
|
windForce < 0 ? (ballX -= windForce * -1) : (ballX += windForce);
|
|
}
|
|
windIntervall++;
|
|
|
|
for (int i = 0; i < fieldHeigth; i++)
|
|
{
|
|
printf("|");
|
|
for (int j = 0; j < fieldWidth; j++)
|
|
{
|
|
if (i == 0 || i == fieldHeigth - 1 || i == 2)
|
|
printf("=");
|
|
else
|
|
printf("%c", field[j][i]);
|
|
}
|
|
printf("|");
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void addScoreboard()
|
|
{
|
|
// Die .txt wird geöffnet
|
|
FILE *fp = fopen("ScoreBoard.txt", "a+");
|
|
char name[255];
|
|
|
|
// Liest den eingegebenen Namen ein
|
|
printf("Bitte gib deinen Namen ein:\n");
|
|
scanf("%s", name);
|
|
|
|
// Schreibt den Namen und die erreichten Punkte in die Textdatei
|
|
fprintf(fp, "%s %d\n", name, scorePoints);
|
|
fclose(fp);
|
|
}
|
|
// Checkt Position vom Ball
|
|
int checkBallPosition()
|
|
{
|
|
// Bei einem Tor gibt es eine 1 zurück
|
|
if (binY + 2 == ballY && (ballX == binX || ballX == binX + 1 || ballX == binX - 1))
|
|
{
|
|
return 1;
|
|
}
|
|
// Wenn der Ball daneben fliegt, gibt es eine 2 zurück
|
|
else if (binY + 1 >= ballY || field[ballX][ballY - 1] == '_' || ballX >= fieldWidth || ballX <= 0)
|
|
{
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void sortScoreboard()
|
|
{
|
|
// Öffnet die Textdatei oder erstellt sie, wenn keine existiert
|
|
FILE *fp;
|
|
fp = fopen("ScoreBoard.txt", "a+");
|
|
// Gibt eine Fehlermeldung, falls die Textdatei nicht existiert
|
|
if (fp == NULL)
|
|
{
|
|
printf("Datei konnte nicht geöffnet werden.\n");
|
|
}
|
|
else
|
|
{
|
|
system("clear");
|
|
// Öffnet die Textdatei
|
|
FILE *file = fopen("ScoreBoard.txt", "r");
|
|
char line[1000];
|
|
int numberOfLines = 0;
|
|
// Die Textdatei wird Zeile für Zeile eingelesen und in die Strukturliste eingefügt
|
|
for (int j = 0; fgets(line, sizeof line, file) != NULL && j <= 10; j++)
|
|
{
|
|
char *playerTmp = strtok(line, " ");
|
|
strcpy(playerList[j].name, playerTmp);
|
|
|
|
playerTmp = strtok(NULL, " ");
|
|
int playerPoints = atoi(playerTmp);
|
|
|
|
playerList[j].points = playerPoints;
|
|
|
|
numberOfLines = j;
|
|
sleep(0.1);
|
|
}
|
|
|
|
// Spielerliste mithilfe einer Hilfsveriable sortieren, um sie danach direkt ausgeben zu können
|
|
struct Player tmp;
|
|
for (int i = 0; i < numberOfLines; i++)
|
|
{
|
|
for (int j = 0; j < (numberOfLines - i); j++)
|
|
{
|
|
if (playerList[i].points < playerList[i + 1].points)
|
|
{
|
|
tmp = playerList[i];
|
|
playerList[i] = playerList[i + 1];
|
|
playerList[i + 1] = tmp;
|
|
}
|
|
}
|
|
}
|
|
|
|
fclose(fp);
|
|
// Listet die höchsten Scores in der Konsole auf
|
|
printf(" Scoreboard:\n\n");
|
|
for (int j = 0; j < numberOfLines; j++)
|
|
{
|
|
printf("%d. %s %s %d\n", j + 1, (j < 9) ? " " : " ", playerList[j].name, playerList[j].points);
|
|
sleep(0.5);
|
|
}
|
|
|
|
FILE *fp = fopen("ScoreBoard.txt", "w");
|
|
for (int i = 0; i < numberOfLines; i++)
|
|
{
|
|
fprintf(fp, "%s %d\n", playerList[i].name, playerList[i].points);
|
|
}
|
|
|
|
fclose(fp);
|
|
}
|
|
}
|