Files
Datateknik_Programmering/Mastermind/filhant.cpp
2026-03-05 13:40:46 +01:00

112 lines
3.3 KiB
C++

#include "lab_2.h"
/***************************************************************/
/*SKRIVER UT HIGHSCORE-LISTAN */
/***************************************************************/
void printhigh(spelare listarray[],int antal)
{
int i;
clrscr();
cout <<"HIGHSCORES\n"
<<"----------\n";
cout << setw(6) << "Plats:"
<< setw(NAMESIZE) << "Namn:"
<< setw(15) << "Gissningar:"
<< setw(18) << "Datum:"
<< endl;
for (i=0 ; i<antal ; i++)
if (listarray[i].score != 0)
cout << setw(6) << (i+1)
<< setw(NAMESIZE) << listarray[i].namn
<< setw(15) << listarray[i].score
<< setw(12)
<< listarray[i].datum.da_year << "-"
<< (listarray[i].datum.da_mon+0) << "-"
<< (listarray[i].datum.da_day+0)
<< endl;
getch();
}
/***************************************************************/
/*BUBBLESORT */
/***************************************************************/
void swap(spelare &element1, spelare &element2)
{
spelare temp;
temp = element1;
element1 = element2;
element2 = temp;
}
void bubbleSort(spelare listarray[], const int storlek)
{
for (int i=0 ; i<storlek ; i++)
for (int j=0 ; j<storlek-1 ; j++)
if ((listarray[j].score > listarray[j+1].score) && (listarray[j+1].score !=0))
swap (listarray[j], listarray[j+1]);
else
if (listarray[j].score == 0)
swap (listarray[j], listarray[j+1]);
}
/***************************************************************/
/*LÄSER FRÅN FILEN */
/***************************************************************/
int las(spelare listarray[])
{
int x=0;
fstream load("highscr.dat",ios::in| ios::binary);
if(!load)
return 0;
while ((load.peek() != EOF) && (x < NAMESIZE))
load.read((char *)&listarray[x++],sizeof(listarray[x]));
load.close();
return x;
}
/***************************************************************/
/*SKRIVER TILL FILEN */
/***************************************************************/
void skriv(spelare listarray[])
{
fstream save("highscr.dat",ios::out| ios::binary);
if (!save)
cout <<"Error!";
else
for (int i=0 ; i<=NAMESIZE ; i++)
save.write((char *)&listarray[i], sizeof(listarray[i]));
save.close();
}
/***************************************************************/
/*SPELAREN ÄR KLAR MED OMGÅNGEN, SKRIVER EV. IN SITT NAMN OSV. */
/***************************************************************/
void klar(int a)
{
int x;
char spana;
spelare p, listarray[NAMESIZE];
cout << "\n\nRätt svar efter " << a << " gissningar" << endl;
cout << "Tryck på en tangent..." << endl;
getch();
for (int i=0 ; i<NAMESIZE ; i++)
listarray[i].score=0;
x = las(listarray);
if ((listarray[NAMESIZE-1].score > a) || (listarray[NAMESIZE-1].score==0))
{
cout <<"Du platsar på highscore-listan!"
<<"\nSkriv in ditt namn: ";
cin >> p.namn;
getdate(&p.datum);
p.score=a;
listarray[NAMESIZE-1] = p;
bubbleSort(listarray,NAMESIZE);
skriv(listarray);
}
printhigh(listarray,5);
cout <<"\n Vill du se hela listan? (J/N): ";
cin >> spana;
if (spana == 'j')
printhigh(listarray,NAMESIZE);
}