Startpunkten
This commit is contained in:
140
Mastermind/Lab_2.cpp
Normal file
140
Mastermind/Lab_2.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
/***************************************************************/
|
||||
/* Laboration 2 i C++ */
|
||||
/***************************************************************/
|
||||
/* Mastermind */
|
||||
/* Crille & Robin 971009 */
|
||||
/***************************************************************/
|
||||
|
||||
#include "lab_2.h"
|
||||
|
||||
typedef int secret[ARRAY];
|
||||
|
||||
/***************************************************************/
|
||||
/*SÄKER INMATNING */
|
||||
/***************************************************************/
|
||||
int mataIn()
|
||||
{
|
||||
char buffer[BUFFERSIZE];
|
||||
int f;
|
||||
|
||||
do {
|
||||
cin >> buffer;
|
||||
if (strcmp(buffer,"0") == 0)
|
||||
return 0;
|
||||
else {
|
||||
f = atoi(buffer);
|
||||
if (f!=0)
|
||||
return f;
|
||||
else
|
||||
cout <<"Inget tal, försök igen: ";
|
||||
}
|
||||
}while (!f);
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*GENERERAR EN hemligt_tal - VEKTOR */
|
||||
/***************************************************************/
|
||||
void hemligt(secret &tal)
|
||||
{
|
||||
int i,j,slumpat_tal,size=10;
|
||||
int slump[] = {0,1,2,3,4,5,6,7,8,9};
|
||||
|
||||
srand(time(NULL));
|
||||
for (i=0; i<ARRAY; i++) {
|
||||
slumpat_tal = rand()%size;
|
||||
tal[i]= slump[slumpat_tal];
|
||||
for(j=slumpat_tal ; j<size-1 ; j++)
|
||||
slump[j] = slump[j+1];
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
/*GISSNINGEN in_data TILLDELAS SIN PLATS I gissn - VEKTORN */
|
||||
/*****************************************************************/
|
||||
void gissning(secret gissn, int a)
|
||||
{
|
||||
int in_data;
|
||||
|
||||
cout << "\nVad gissar du på: ----";
|
||||
gotoxy(19,a+3);
|
||||
in_data = mataIn();
|
||||
gissn[0] = ((in_data % 10000)/1000);
|
||||
gissn[1] = ((in_data % 1000)/100);
|
||||
gissn[2] = ((in_data % 100)/10);
|
||||
gissn[3] = (in_data % 10);
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*SKRIVER UT S_R PÅ LÄMPLIGT STÄLLE */
|
||||
/***************************************************************/
|
||||
void kontroll(secret gissn, secret hemligt_tal, char help[])
|
||||
{
|
||||
int i,j;
|
||||
|
||||
for (j=0 ; j<ARRAY ; j++) {
|
||||
if (hemligt_tal[j] == gissn[j])
|
||||
help[j] = 'R';
|
||||
else {
|
||||
help[j] = '_';
|
||||
for (i=0 ; i<ARRAY ; i++) {
|
||||
if(gissn[j] == hemligt_tal[i])
|
||||
help[j] = 'S';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*SKRIVER UT HELPARRAYEN */
|
||||
/***************************************************************/
|
||||
void helparray(char help[],int a)
|
||||
{
|
||||
gotoxy(25,a+3);
|
||||
for (int i=0 ; i<ARRAY ; i++)
|
||||
cout << help[i];
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*AVBRYTER PGA FÖR MÅNGA GISSNINGAR */
|
||||
/***************************************************************/
|
||||
void fel(secret &tal)
|
||||
{
|
||||
clrscr();
|
||||
cout <<"Du får inte fler försök!\n"
|
||||
<<"Det rätta svaret var: ";
|
||||
for (int i=0 ; i<ARRAY ; i++)
|
||||
cout << tal[i];
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*MAIN */
|
||||
/***************************************************************/
|
||||
void main()
|
||||
{
|
||||
int omstart=1;
|
||||
while (omstart) {
|
||||
int hemligt_tal[ARRAY],gissn[ARRAY],a=0;
|
||||
char help[]={'_','_','_','_','\0'},replay;
|
||||
hemligt(hemligt_tal);
|
||||
clrscr();
|
||||
cout <<"MASTERMIND (AKA LAB_2) BY CRILLE & ROBIN"<< endl
|
||||
<<"UNREGISTRED VERSION. " << endl;
|
||||
do {
|
||||
a++;
|
||||
gissning(gissn,a);
|
||||
kontroll(gissn,hemligt_tal,help);
|
||||
helparray(help,a);
|
||||
}while (((help[0]!='R') || (help[1]!='R') ||
|
||||
(help[2]!='R') || (help[3]!='R'))&& a < TRIES);
|
||||
if (a >= TRIES)
|
||||
fel(hemligt_tal);
|
||||
else
|
||||
klar(a);
|
||||
cout <<"\nVill du spela en omgång till\ [J/N]:";
|
||||
cin >> replay;
|
||||
if (replay == 'n')
|
||||
omstart=0;
|
||||
}
|
||||
}
|
||||
|
||||
111
Mastermind/filhant.cpp
Normal file
111
Mastermind/filhant.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#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);
|
||||
}
|
||||
31
Mastermind/lab_2.h
Normal file
31
Mastermind/lab_2.h
Normal file
@@ -0,0 +1,31 @@
|
||||
//Headerfil för Lab 2
|
||||
|
||||
#ifndef _lab_2_h
|
||||
#define _lab_2_h
|
||||
|
||||
#include <iostream.h> //för in och utmatning
|
||||
#include <string.h> //för strcmp()
|
||||
#include <stdlib.h> //för atoi()
|
||||
#include <conio.h> //för clrscr() och getch()
|
||||
#include <fstream.h> //för filhantering
|
||||
#include <iomanip.h> //för printhigh
|
||||
#include <dos.h> //för datumet
|
||||
|
||||
#define ARRAY 4 //arraystorlek för hemligt_tal
|
||||
#define BUFFERSIZE 100 //arraystorlek för säker inmatning
|
||||
#define TRIES 15 //antal gissnigsförsök
|
||||
#define NAMESIZE 20 //antal tecken på namnet
|
||||
|
||||
|
||||
typedef struct spelare
|
||||
{
|
||||
char namn[NAMESIZE];
|
||||
int score;
|
||||
struct date datum;
|
||||
}spelare;
|
||||
|
||||
void klar (int a);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user