Created
February 7, 2018 21:33
-
-
Save petterroea/f599cf967295d164269277db9a895c80 to your computer and use it in GitHub Desktop.
Oving 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Oppgave 1a | |
* v0: 5 | |
* increment: 2 | |
* iterations: 10 | |
* result: 25 | |
*/ | |
#include <iostream> | |
#include "tests.h" | |
#include "mastermind.h" | |
using namespace std; | |
int main() | |
{ | |
testCallByValue(); | |
testCallByPointer(); | |
testTablesSorting(); | |
testCStrings(); | |
playMastermind(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "mastermind.h" | |
#include "utilities.h" | |
//Shamelessly stolen from stackoverflow | |
// https://stackoverflow.com/questions/3437404/min-and-max-in-c | |
#define MIN(a,b) (((a)<(b))?(a):(b)) | |
#define MAX(a,b) (((a)>(b))?(a):(b)) | |
void playMastermind() { | |
const int SIZE = 4; | |
const int LETTERS = 6; | |
while(true) { | |
char code[SIZE+1] = {}; | |
char userInput[SIZE+1] = {}; | |
randomizeCString(code, SIZE, 'A', 'A'+LETTERS); | |
std::cout << "Du starter et nytt spill med en " << SIZE << "-tegns kode, med " << LETTERS << " forskjellige tegn-muligheter(A-" << static_cast<char>('A'+LETTERS) << "." << std::endl; | |
int tries = 10; | |
while(checkCharactersAndPosition(code, userInput, SIZE) < SIZE) { | |
if(tries == 0) { | |
std::cout << "Du har tapt!" << std::endl; | |
return; | |
} | |
std::cout << "Gjett koden. Du har " << tries << " forsøk." << std::endl; | |
readInputToCString(userInput, SIZE, 'A', 'A'+LETTERS); | |
std::cout << "Du har plassert " << checkCharacters(code, userInput, SIZE, 'A', 'A'+LETTERS) << " tegn riktig, hvorav " << checkCharactersAndPosition(code, userInput, SIZE) << " er ved riktig plass." << std::endl; | |
tries--; | |
} | |
std::cout << "Du vant! Du hadde " << (tries+1) << " forsøk igjen." << std::endl; | |
std::string choice; | |
while(choice != "y" && choice != "n") { | |
std::cout << "Spille igjen? (y/n): "; | |
std::cin >> choice; | |
if(choice != "y" && choice != "n") { | |
std::cout << "Ugyldig input." << std::endl; | |
} | |
} | |
if(choice == "n") { | |
break; | |
} | |
} | |
} | |
int checkCharactersAndPosition(char a[], char b[], int arrayLength) { | |
int counter = 0; | |
for(int i = 0; i < arrayLength; i++) { | |
if(a[i] == b[i]) { | |
counter++; | |
} | |
} | |
return counter; | |
} | |
int checkCharacters(char a[], char b[], int arrayLength, char startChar, char endChar) { | |
int correctGuessed = 0; | |
for(char c = startChar; c <= endChar; c++) { | |
correctGuessed += MIN(countOccurencesOfCharacter(a, arrayLength, c), countOccurencesOfCharacter(b, arrayLength, c)); | |
} | |
return correctGuessed; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
void playMastermind(); | |
int checkCharactersAndPosition(char a[], char b[], int arrayLength); | |
int checkCharacters(char a[], char b[], int arrayLength, char startChar, char endChar); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <cstdlib> | |
#include <ctime> | |
#include <iostream> | |
#include "utilities.h" | |
void testCallByValue() { | |
int v0 = 5; | |
int increment = 2; | |
int iterations = 10; | |
int result = incrementByValueNumTimes(v0, increment, iterations); | |
std::cout << "v0: " << v0 | |
<< " increment: " << increment | |
<< " iterations: " << iterations | |
<< " result: " << result << std::endl; | |
} | |
void testCallByPointer() { | |
int v0 = 5; | |
int increment = 2; | |
int iterations = 10; | |
incrementByValueNumTimes2(&v0, increment, iterations); | |
std::cout << "v0: " << v0 | |
<< " increment: " << increment | |
<< " iterations: " << iterations << std::endl; | |
} | |
void testTablesSorting() { | |
int percentages[20]; | |
std::srand(std::time(0)); | |
randomizeArray(percentages, 20); | |
printArray(percentages, 20); | |
swapNumbers(&percentages[0], &percentages[1]); | |
printArray(percentages, 20); | |
std::cout << medianOfArray(percentages, 20) << std::endl; | |
sortArray(percentages, 20); | |
printArray(percentages, 20); | |
std::cout << medianOfArray(percentages, 20) << std::endl; | |
} | |
void testCStrings() { | |
char grades[8*2+1] = {}; | |
randomizeCString(grades, 8, 'A', 'F'); | |
std::cout << grades << std::endl; | |
int gradeCount[6] = {countOccurencesOfCharacter(grades, 8*2, 'A'), | |
countOccurencesOfCharacter(grades, 8*2, 'B'), | |
countOccurencesOfCharacter(grades, 8*2, 'C'), | |
countOccurencesOfCharacter(grades, 8*2, 'D'), | |
countOccurencesOfCharacter(grades, 8*2, 'E'), | |
countOccurencesOfCharacter(grades, 8*2, 'F')}; | |
std::cout << "Average grade: " << static_cast<double>(gradeCount[0]*1+gradeCount[1]*2+gradeCount[2]*3+gradeCount[3]*4+gradeCount[4]*5+gradeCount[5]*6)/6.0 << std::endl; | |
char masterGrades[8*2*5+1] = {}; | |
int currentYear = 0; | |
std::cout << "Du skal skrive inn karakterer for hvert år:" << std::endl; | |
while(currentYear < 5) { | |
std::cout << "Skriv inn karakterer for år " << (currentYear+1) << ": " << std::endl; | |
readInputToCString(&masterGrades[8*2*currentYear], 8*2, 'A', 'E'); | |
currentYear++; | |
} | |
int masterGradeCount[5] = {countOccurencesOfCharacter(masterGrades, 8*2, 'A'), | |
countOccurencesOfCharacter(masterGrades, 8*2, 'B'), | |
countOccurencesOfCharacter(masterGrades, 8*2, 'C'), | |
countOccurencesOfCharacter(masterGrades, 8*2, 'D'), | |
countOccurencesOfCharacter(masterGrades, 8*2, 'E')}; | |
double averageNum = static_cast<double>(masterGradeCount[0] * 1 + masterGradeCount[1] * 2 + masterGradeCount[2] * 3 + masterGradeCount[3] * 4 + masterGradeCount[4] * 5) / 5.0; | |
std::cout << "Gjennomsnittskarakteren ble " << static_cast<char>('A' + round(averageNum) - 1) << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "utilities.h"; | |
void testCallByValue(); | |
void testCallByPointer(); | |
void testTablesSorting(); | |
void testCStrings(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include <string> | |
#include "utilities.h" | |
int randomWithLimits(int lower, int upper) { | |
return (std::rand())%(upper+1-lower)+lower; | |
} | |
int incrementByValueNumTimes(int startValue, int increment, int numTimes) { | |
for (int i = 0; i < numTimes; i++) { | |
startValue += increment; | |
} | |
return startValue; | |
} | |
void incrementByValueNumTimes2(int *startValue, int increment, int numTimes) { | |
for (int i = 0; i < numTimes; i++) | |
*startValue += increment; | |
} | |
//Pointere burde brukes fordi jeg bare kan returnere et tall om gangen | |
void swapNumbers(int* a, int* b) { | |
int temp = *a; | |
*a = *b; | |
*b = temp; | |
} | |
void printArray(int array[], int length) { | |
for(int i = 0; i < length; i++) { | |
std::cout << array[i] << ","; | |
} | |
std::cout << std::endl; | |
} | |
void randomizeArray(int array[], int length) { | |
for(int i = 0; i < length; i++) { | |
array[i] = randomWithLimits(0, 100); | |
} | |
} | |
void sortArray(int array[], int length) { | |
int i = 1; | |
while(i < length) { | |
int j = i; | |
while(j > 0 && array[j-1] > array[j]) { | |
int temp = array[j]; | |
array[j] = array[j-1]; | |
array[j-1] = temp; | |
j--; | |
} | |
i++; | |
} | |
} | |
double medianOfArray(int array[], int length) { | |
if(length%2==0) { | |
return (static_cast<double>(array[length/2])+static_cast<double>(array[length/2+1]))/2.0; | |
} else { | |
return static_cast<double>(array[length/2]); | |
} | |
} | |
void randomizeCString(char array[], int arrayLength, char lowerLimit, char upperLimit) { | |
for(int i = 0; i < arrayLength; i++) { | |
array[i] = static_cast<char>(randomWithLimits(static_cast<int>(lowerLimit), static_cast<int>(upperLimit))); | |
} | |
} | |
void readInputToCString(char array[], int arrayLength, char lowerLimit, char upperLimit) { | |
std::string input; | |
while (true) { | |
input = ""; | |
std::cout << "Skriv inn " << arrayLength << " tegn imellom " << lowerLimit << " og " << upperLimit << std::endl; | |
std::cin >> input; | |
if(input.length() != arrayLength) { | |
std::cout << "Du skrev ikke riktig antall tegn inn!" << std::endl; | |
continue; | |
} | |
bool containsUnwantedCharacters = false; | |
for(int i = 0; i < input.length(); i++) { | |
char c = toupper(input.c_str()[i]); | |
if(c < lowerLimit || c > upperLimit) { | |
std::cout << c << " er ikke et gyldig tegn..." << std::endl; | |
containsUnwantedCharacters = true; | |
break; | |
} | |
} | |
if(containsUnwantedCharacters) | |
continue; | |
break; | |
} | |
for(int i = 0; i < arrayLength; i++) { | |
array[i] = toupper(input.c_str()[i]); | |
} | |
} | |
int countOccurencesOfCharacter(char array[], int arrayLength, char character) { | |
int counter = 0; | |
for(int i = 0; i < arrayLength; i++) { | |
if(array[i] == character) | |
counter++; | |
} | |
return counter; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
int randomWithLimits(int lower, int upper); | |
int incrementByValueNumTimes(int startValue, int increment, int numTimes); | |
void incrementByValueNumTimes2(int *startValue, int increment, int numTimes); | |
void swapNumbers(int* a, int* b); | |
void printArray(int array[], int length); | |
void randomizeArray(int array[], int length); | |
void sortArray(int array[], int length); | |
double medianOfArray(int array[], int length); | |
void randomizeCString(char array[], int arrayLength, char lowerLimit, char upperLimit); | |
void readInputToCString(char array[], int arrayLength, char lowerLimit, char upperLimit); | |
int countOccurencesOfCharacter(char array[], int arrayLength, char character); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment