/* memory.d Program to help keep memory good Will use memorization of numbers May add different techniques later August 3rd, 2008 */ /* To-do list: Have more than one number change - In different memory test Difficulties - more numbers Make sure integer is big enough Visual memory by displaying different images, make with multi-dimensional arrays of characters, put in a module. Check for custom sleep function from cplusplus reference. Consider putting variables in a struct - done partly Make a program that includes this and the addition warm-up, calls the executables */ //Imports import std.stdio; import std.random; //needed for choosing random number in an array of numbers import std.c.time; //for sleep function /****** *Types* ******/ struct Data { int correctAnswers; int incorrectAnswers; int totalQuestions; int listLength; double percentageOfCorrectAnswers; } /***** *Main* *****/ void main() { Data memoryInformation; introduction(); int length = getNumberLength(); /* assign the length of the list of numbers that the user wants because it is used in various places */ int amountOfQuestions = getNumberOfQuestions(); memoryTest( length, amountOfQuestions, memoryInformation ); displayResults( memoryInformation ); } /********** *Functions* **********/ void introduction() { writefln( "Ready for some memorization?\n" ); writefln( "With this program, you will get a series of numbers to memorize." ); writefln( "Eventually, there will be some shapes to memorize too." ); writefln( "The length of the list and number of questions is up to you." ); writefln( "So, with that, let's get memorizing!\n" ); } int getNumberLength() { writefln( "How long would you like the list to be?" ); int length; scanf( "%d", &length ); return length; } int getNumberOfQuestions() { writefln( "How many memory questions would you like to be asked?" ); int numberOfQuestions; scanf( "%d", &numberOfQuestions ); return numberOfQuestions; } void memoryTest( int lengthOfList, int numberOfQuestions, inout Data questions ) { int[] list; /* Need to create a variable for the list of numbers because we need to check for in the checkArray function for an argument */ int arrayAsAnInteger; int userInput; for ( int i = 0; i < numberOfQuestions; i++ ) { writefln( "List #%d:", i ); list = createArray( lengthOfList ); arrayAsAnInteger = convertArrayToInteger( list ); writefln( "%d", arrayAsAnInteger ); wait( 5 ); //custom sleep function clearScreen(); writefln( "Please enter the number you saw." ); writef( ">> " ); scanf( "%d", &userInput ); if ( userInput != arrayAsAnInteger ) { questions.incorrectAnswers++; writefln( "Incorrect!" ); writefln( "Next list..." ); } else { questions.correctAnswers++; writefln( "Correct!" ); writefln( "Next list..." ); } } } int[] createArray( int lengthOfList ) { //create the array to return int[] memoryList; for ( int i = 0; i < lengthOfList; i++ ) { memoryList[i] = rand() % 10; /* we can't have a single-digit number be greater than 10 :) */ } return memoryList; } int convertArrayToInteger( int[] arrayToConvert ) { int convertedArray; int currentNumber; int j = 0; for ( int i = arrayToConvert.length; i > 0; i-- ) { currentNumber = arrayToConvert[ i ] * ( pow( 10, j ) ); convertedArray += currentNumber; j++; } return convertedArray; } /* void displayArray( int[] arrayToDisplay ) { foreach( int array; arrayT0Display ) { writef( "%d", array ); } } //Check to see if what the user entered was right void checkUserArray( int[] userArray, int[] correctArray ) { } */ void clearScreen() { int numberOfLinesInWindow = 25 + 5; //^^I counted how many lines there were. there was 25, but add 5 for re-assurance for ( int i = 0; i < numberOfLinesInWindow; i++ ) { writefln( "" ); } } void wait( int seconds ) { clock_t waitTime = clock() + seconds * CLOCKS_PER_SEC; while ( clock() > waitTime ) { } } void displayResults( Data questions ) { writefln( "You're done!" ); writefln( "You got %d out of %d questions right.", questions.correctAnswers, questions.totalQuestions ); questions.percentageOfCorrectAnswers = cast( double ) questions.correctAnswers / cast( double ) questions.totalQuestions * 100; writefln( "That means you got %0.1f percent of the questions correct!", questions.percentageOfCorrectAnswers ); writefln( "Good job!\n" ); writefln( "Press any key to quit..." ); getch(); } int pow( int base, int exponent ) { int poweredUpNumber; switch( exponent ) { case 0: return 0; case 1: return base; default: for ( int i = 2; i <= exponent; i++ ) { poweredUpNumber = base * base; } return poweredUpNumber; } }