Kino-Kartenspiel in C: Quellcode und Programmlogik

Informatik9,07 KB

Kino-Kartenspiel: Quellcode in C

Kino-Kartengenerator und -verwaltung in C: Dieses Programm implementiert die Erstellung, Sortierung, Speicherung, Suche und Auswertung von Spielkarten (Cartones) für ein Kino- bzw. Bingospiel.

Includes, Konstanten und Datenstrukturen

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define FIL 5
#define COL 3

struct Vorlage {
int number;
char name[30];
int board[FIL][COL];
};

Funktionsprototypen

Deklaration der Schnittstellen für das Hauptmenü und die Kartenoperationen:

  • int menu();
  • void muestraCartones(struct Vorlage regCarton);
  • void generaCarton(int kino[FIL][COL]);
  • void generaSorteo(int preis[FIL][COL]);
  • void revisaCarton(int stromaufnahme[FIL][COL], struct Vorlage regCarton);
  • void muestraCarton(int board[FIL][COL]);
  • void ordenaCarton(int board[FIL][COL]);
  • void buscaCarton(struct Vorlage regCarton);
  • void grabaCarton(int board[FIL][COL]);

Hauptprogramm: main()

Die main-Funktion initialisiert den Zufallsgenerator und steuert die Interaktion über ein Menü:

int main() {
int kino[FIL][COL], stromaufnahme[FIL][COL];
struct Vorlage struktur;
int option;

srand(time(0));

do {
option = menu();
switch (option) {
case 1:
generaCarton(kino);
ordenaCarton(kino);
grabaCarton(kino);
muestraCarton(kino);
break;
case 2:
muestraCartones(struktur);
break;
case 3:
buscaCarton(struktur);
break;
case 4:
generaCarton(stromaufnahme);
ordenaCarton(stromaufnahme);
muestraCarton(stromaufnahme);
break;
case 5:
revisaCarton(stromaufnahme, struktur);
break;
}
} while (option != 0);

return 0;
}

Implementierung der Funktionen

Kartenerstellung: generaCarton()

void generaCarton(int board[FIL][COL]) {
int i, j, random, c, k, l;
printf("\nKarton wird generiert:\n");
for (i = 0; i < FIL; i++) {
for (j = 0; j < COL; j++) {
board[i][j] = 0;
}
}
for (i = 0; i < FIL; i++) {
for (j = 0; j < COL; j++) {
do {
random = rand() % 25 + 1;
c = 0;
for (k = 0; k < FIL; k++) {
for (l = 0; l < COL; l++) {
if (random == board[k][l]) {
c++;
}
}
}
} while (c != 0);
board[i][j] = random;
}
}
}

Kartenanzeige: muestraCarton()

void muestraCarton(int board[FIL][COL]) {
int i, j;
printf("\nKarton sortiert:\n");
for (i = 0; i < FIL; i++) {
printf("\n");
for (j = 0; j < COL; j++) {
printf("\t%d", board[i][j]);
}
}
}

Kartensortierung: ordenaCarton()

void ordenaCarton(int board[FIL][COL]) {
int i, j, k, aux, vector[15];
k = 0;
for (i = 0; i < FIL; i++) {
for (j = 0; j < COL; j++) {
vector[k] = board[i][j];
k++;
}
}
for (i = 0; i < 14; i++) {
for (j = i + 1; j < 15; j++) {
if (vector[i] > vector[j]) {
aux = vector[i];
vector[i] = vector[j];
vector[j] = aux;
}
}
}
printf("\n");
k = 0;
for (i = 0; i < FIL; i++) {
for (j = 0; j < COL; j++) {
board[i][j] = vector[k];
k++;
}
}
}

Kartenspeicherung: grabaCarton()

void grabaCarton(int board[FIL][COL]) {
struct Vorlage regCarton;
int i, j;
for (i = 0; i < FIL; i++) {
for (j = 0; j < COL; j++) {
regCarton.board[i][j] = board[i][j];
}
}
FILE *file;
printf("Anzahl Kartons / Nummer: ");
scanf("%d", &regCarton.number);
printf("Name: ");
scanf("%s", regCarton.name);
file = fopen("cartones.txt", "a");
if (file != NULL) {
fwrite(&regCarton, sizeof(regCarton), 1, file);
fclose(file);
}
}

Konsolenmenü: menu()

int menu() {
int opt;
do {
printf("\n--- Menue ---");
printf("\n1: Generieren Kino");
printf("\n2: Alle Kartons anzeigen");
printf("\n3: Karton suchen");
printf("\n4: Ziehung / Sweepstakes erstellen");
printf("\n5: Karton pruefen (Review Carton)");
printf("\n0: Beenden (Exit)");
printf("\nGeben Sie eine Option ein: ");
scanf("%d", &opt);
} while (opt < 0 || opt > 5);
return opt;
}

Alle Kartons ausgeben: muestraCartones()

void muestraCartones(struct Vorlage regCarton) {
int i, j;
FILE *file = fopen("cartones.txt", "r");
if (file == NULL) return;
while (fread(&regCarton, sizeof(regCarton), 1, file)) {
printf("\nKarton-Nr.: %d", regCarton.number);
printf("\nBenutzername: %s", regCarton.name);
for (i = 0; i < FIL; i++) {
printf("\n");
for (j = 0; j < COL; j++) {
printf("\t%d", regCarton.board[i][j]);
}
}
}
fclose(file);
}

Karton suchen: buscaCarton()

void buscaCarton(struct Vorlage regCarton) {
int i, j, buscarNum, found = 0;
FILE *file = fopen("cartones.txt", "r");
if (file == NULL) return;
printf("Karton-Nummer zur Suche: ");
scanf("%d", &buscarNum);
while (fread(&regCarton, sizeof(regCarton), 1, file)) {
if (regCarton.number == buscarNum) {
found = 1;
printf("\nBenutzername: %s", regCarton.name);
for (i = 0; i < FIL; i++) {
printf("\n");
for (j = 0; j < COL; j++) {
printf("\t%d", regCarton.board[i][j]);
}
}
break;
}
}
fclose(file);
if (found == 0) {
printf("\nNummer der Karte wurde nicht gefunden.");
}
}

Karton mit Ziehung abgleichen: revisaCarton()

void revisaCarton(int stromaufnahme[FIL][COL], struct Vorlage regCarton) {
int i, j, buscarNum, found = 0, matches = 0, k, l;
FILE *file = fopen("cartones.txt", "r");
if (file == NULL) return;
printf("Karton-Nummer zum Pruefen: ");
scanf("%d", &buscarNum);
while (fread(&regCarton, sizeof(regCarton), 1, file)) {
if (regCarton.number == buscarNum) {
found = 1;
printf("\nBenutzername: %s", regCarton.name);
for (i = 0; i < FIL; i++) {
printf("\n");
for (j = 0; j < COL; j++) {
printf("\t%d", regCarton.board[i][j]);
}
}
muestraCarton(stromaufnahme);
printf("\nTreffer: ");
for (i = 0; i < FIL; i++) {
for (j = 0; j < COL; j++) {
for (k = 0; k < FIL; k++) {
for (l = 0; l < COL; l++) {
if (regCarton.board[i][j] == stromaufnahme[k][l]) {
matches++;
printf("%d ", stromaufnahme[k][l]);
}
}
}
}
}
break;
}
}
fclose(file);
if (found == 0) {
printf("\nNummer der Karte wurde nicht gefunden.");
} else {
printf("\nUebereinstimmungen gefunden (matches): %d\n", matches);
}
}

Herunterladen

PDF Word HTML