diff options
-rw-r--r-- | 2048 Game/Game | bin | 12544 -> 15792 bytes | |||
-rw-r--r-- | 2048 Game/Game.cpp | 210 | ||||
-rw-r--r-- | 2048 Game/Random (1) | bin | 0 -> 8704 bytes | |||
-rw-r--r-- | 2048 Game/configFile.txt | 8 | ||||
-rw-r--r-- | 2048 Game/random | bin | 0 -> 9824 bytes | |||
-rw-r--r-- | 2048 Game/random.cpp | 24 | ||||
-rw-r--r-- | Algorithms/BubbleSort.cpp | 15 | ||||
-rw-r--r-- | Algorithms/LinkedLists | bin | 0 -> 11320 bytes | |||
-rw-r--r-- | Algorithms/LinkedLists.cpp | 188 | ||||
-rw-r--r-- | LinkedLists.cpp | 4 |
10 files changed, 426 insertions, 23 deletions
diff --git a/2048 Game/Game b/2048 Game/Game Binary files differindex 9aef631..a00ba24 100644 --- a/2048 Game/Game +++ b/2048 Game/Game diff --git a/2048 Game/Game.cpp b/2048 Game/Game.cpp index 2969aad..3b5232f 100644 --- a/2048 Game/Game.cpp +++ b/2048 Game/Game.cpp @@ -1,32 +1,59 @@ +/* + * includes necessary libraries + */ #include <iostream> #include <fstream> #include <string> #include <sstream> +#include <cstdlib> -#define GRIDSIZE 4 -#define UP 'w' -#define DOWN 's' -#define RIGHT 'd' -#define LEFT 'a' +/* + * defines names for the constants we want to use + * this is to make the code more readable + */ +#define GRIDSIZE 4 +#define BASE 2 +#define UP 'w' +#define DOWN 's' +#define RIGHT 'd' +#define LEFT 'a' using namespace std; -// defines the functions -void printGrid(int (&)[GRIDSIZE][GRIDSIZE]); -bool checkGameOver(int (&)[GRIDSIZE][GRIDSIZE]); +// declares the functions +void printGrid(int(&) [GRIDSIZE][GRIDSIZE]); +void moveGrid(int(&) [GRIDSIZE][GRIDSIZE], char&); +void mergeArray(int*&, int&); +void placeBase(int (&) [GRIDSIZE][GRIDSIZE]); +bool checkGridChange(int (&) [GRIDSIZE][GRIDSIZE], int (&) [GRIDSIZE][GRIDSIZE]); +bool checkGameOver(int(&) [GRIDSIZE][GRIDSIZE]); + +/* + * main function that handles all the i/o except for printing the grid + * as this is easier done by a seperate function + */ int main(int argc, char* argv[]) { // defines the 2D and tmp array int grid[GRIDSIZE][GRIDSIZE]; + int before[GRIDSIZE][GRIDSIZE]; + // defines tmp string file name to check if file exists string fileName; + char usrInput; + // defines input filestream for configFile ifstream configFile; - cout << "enter initial configuration file name:" << endl; + // set random seed to be the current time + srand(time(NULL)); + + // gets the input from the user for the input file + cout << "Enter initial configuration file name:" << endl; cin >> fileName; // opens the user defined file configFile.open(fileName.c_str()); + // enters the integers from the file into the array if file is correct if(configFile.is_open()) { for(int i = 0; i < GRIDSIZE; ++i) { for(int j = 0; j < GRIDSIZE; ++j) { @@ -35,25 +62,53 @@ int main(int argc, char* argv[]) { } configFile.close(); } else { - cout << "the file doesn't exist, using default start" << endl; + // if the file is not valid it will enter the default values of 0 and 2 in the + // bottom right + cout << "The file doesn't exist, using default start" << endl; // creates default grid for(int i = 0; i < GRIDSIZE; ++i) { for(int j = 0; j < GRIDSIZE; ++j) { grid[i][j] = 0; } } - grid[GRIDSIZE-1][GRIDSIZE-1] = 2; + grid[GRIDSIZE-1][GRIDSIZE-1] = BASE; } - // prints the grid printGrid(grid); + /* + * executes this loop while the game isn't over, which means that the user can + * still have turns and there are zero's left, as well as adjacent equivalent + * numbers that can be added + */ while(!checkGameOver(grid)) { - + // gets user input + cout << "Select move [w,a,s,d]: "; + cin >> usrInput; + cout << endl; + // copies the grid into the before grid + for(int i = 0; i < GRIDSIZE; ++i) { + for(int j = 0; j < GRIDSIZE; ++j) { + before[i][j] = grid[i][j]; + } + } + // moves grid in correct direction + moveGrid(grid, usrInput); + // check if grid has changed which means that the movement was valid + if(checkGridChange(grid, before)){ + // places the random number in the correct base defined above + placeBase(grid); + // prints the grid + printGrid(grid); + } } - + cout << "game over" << endl; return 0; } +/* + * prints the contents a 2D array with a constant size + * so that the user can see the result of their input + */ void printGrid(int (&gridArray)[GRIDSIZE][GRIDSIZE]) { for(int i = 0; i < GRIDSIZE; ++i) { for(int j = 0; j < GRIDSIZE; ++j) { @@ -64,24 +119,140 @@ void printGrid(int (&gridArray)[GRIDSIZE][GRIDSIZE]) { cout << endl; } -void moveVertical() { +/* + * handles the inputs of UP, DOWN, LEFT and RIGHT by the user and + * sends them to the merge algorithm in the correct order + */ +void moveGrid(int (&gridArray)[GRIDSIZE][GRIDSIZE], char &movDir) { + // pointer that will point towards first element in array + int* elementPtr; + // defines the separation to the next element which + // says if it moves in rows or columns + int separation; + // if statements to find what direction the user wants to move the grid + if(movDir == UP) { + // separation set so that it goes up in rows + separation = GRIDSIZE; + for(int i = 0; i < GRIDSIZE; ++i) { + // set elementpointer to be the first element in the rows + elementPtr = &gridArray[0][i]; + // calls merge function + mergeArray(elementPtr, separation); + } + } else if(movDir == DOWN) { + separation = -GRIDSIZE; + for(int i = 0; i < GRIDSIZE; ++i) { + elementPtr = &gridArray[GRIDSIZE-1][i]; + mergeArray(elementPtr, separation); + } + } else if(movDir == LEFT) { + // set separation to increment in columns + separation = 1; + for(int i = 0; i < GRIDSIZE; ++i) { + elementPtr = gridArray[i]; + mergeArray(elementPtr, separation); + } + } else if(movDir == RIGHT) { + separation = -1; + for(int i = 0; i < GRIDSIZE; ++i) { + elementPtr = &gridArray[i][GRIDSIZE-1]; + mergeArray(elementPtr, separation); + } + } } -void moveHorizontal() { - +/* + * merges arrays together depending on the input + */ +void mergeArray(int* &arrayPointer, int &addrSep) { + // defines array of one size larger so that all elements are moved correctly + int nonZeroRow[GRIDSIZE+1]; + // sets all elements to 0 + for(int i = 0; i < GRIDSIZE+1; ++i) { + nonZeroRow[i] = 0; + } + // sets the counter to 0 + int nonZeroCounter = 0; + // sets all the elements in the array to be the nonzero elements + // pointed to by the pointer + for(int i = 0; i < GRIDSIZE; ++i) { + if(*(arrayPointer+addrSep*i) != 0) { + nonZeroRow[nonZeroCounter++] = *(arrayPointer+addrSep*i); + } + } + for(int i = 0; i < nonZeroCounter; ++i) { + // if two adjacent cells are equal multiply by the base + if(nonZeroRow[i] == nonZeroRow[i+1]) { + nonZeroRow[i] *= BASE; + // then move all the other elements up + for(int j = i+1; j < GRIDSIZE; ++j) { + nonZeroRow[j] = nonZeroRow[j+1]; + } + } + } + // set all the pointer locations to be equal to the elements in the array + for(int i = 0; i < GRIDSIZE; ++i) { + *(arrayPointer+addrSep*i) = nonZeroRow[i]; + } } -void merge() { +/* + * adds a base num at a random location where a zero was + */ +void placeBase(int (&gridArray)[GRIDSIZE][GRIDSIZE]) { + // 2D array that will hold locations of the zeros + int zeroArray[GRIDSIZE*GRIDSIZE][2]; + // sets the 0 counter + int zeroCount = 0; + for(int i = 0; i < GRIDSIZE; ++i) { + for(int j = 0; j < GRIDSIZE; ++j) { + if(gridArray[i][j] == 0) { + // sets the x and y values in the zero array + zeroArray[zeroCount][0] = i; + zeroArray[zeroCount++][1] = j; + } + } + } + // gets a random number + int randomLoc = rand() % zeroCount; + // sets the random location in the grid to equal that value + gridArray[zeroArray[randomLoc][0]][zeroArray[randomLoc][1]] = BASE; +} +/* + * checks if the two grids are the same + * if they are this means it will not add the random 2 + * and not print the grid + */ +bool checkGridChange(int (&gridArray)[GRIDSIZE][GRIDSIZE], int (&beforeGrid)[GRIDSIZE][GRIDSIZE]) { + for(int i = 0; i < GRIDSIZE; ++i) { + for(int j = 0; j < GRIDSIZE; ++j) { + if(gridArray[i][j] != beforeGrid[i][j]) { + // if it finds a value that doesn't match, grid must have changed hence return true + return true; + } + } + } + return false; } +/* + * checks if the game is over by checking if 0's are present + * or if there are equal numbers side by side + */ bool checkGameOver(int (&gridArray)[GRIDSIZE][GRIDSIZE]) { + // uses two for loops to go through all boxes in the grid for(int i = 0; i < GRIDSIZE; ++i) { for(int j = 0; j < GRIDSIZE; ++j) { + // checks if the current box is a 0 if(gridArray[i][j] == 0) { + // returns false as this means the game isn't over return false; - } else if(i > 0 && gridArray[i][j] == gridArray[i-1][j]) { + } + // if not it checks if there are any repeating numbers around + // it as this means they can add + else if(i > 0 && gridArray[i][j] == gridArray[i-1][j]) { return false; } else if(i < GRIDSIZE-1 && gridArray[i][j] == gridArray[i+1][j]) { return false; @@ -92,5 +263,6 @@ bool checkGameOver(int (&gridArray)[GRIDSIZE][GRIDSIZE]) { } } } + // if none of the if statements is true it will end the games return true; }
\ No newline at end of file diff --git a/2048 Game/Random (1) b/2048 Game/Random (1) Binary files differnew file mode 100644 index 0000000..4907ab1 --- /dev/null +++ b/2048 Game/Random (1) diff --git a/2048 Game/configFile.txt b/2048 Game/configFile.txt index b4f6ca6..0a28df6 100644 --- a/2048 Game/configFile.txt +++ b/2048 Game/configFile.txt @@ -1,4 +1,4 @@ -128 64 2 0 -0 0 32 0 -0 0 0 16 -0 8 4 0
\ No newline at end of file +4 2 2 0 +0 0 0 0 +0 0 0 0 +0 0 0 0
\ No newline at end of file diff --git a/2048 Game/random b/2048 Game/random Binary files differnew file mode 100644 index 0000000..5350541 --- /dev/null +++ b/2048 Game/random diff --git a/2048 Game/random.cpp b/2048 Game/random.cpp new file mode 100644 index 0000000..1587cb0 --- /dev/null +++ b/2048 Game/random.cpp @@ -0,0 +1,24 @@ +/* +* @Author: yannherklotz +* @Date: 2016-02-01 17:47:47 +* @Last Modified by: yannherklotz +* @Last Modified time: 2016-02-04 15:08:05 +*/ + +#include <iostream> +#include <cstdlib> +#include <ctime> + +using namespace std; + +int main(){ + srand(time(NULL)); + cout << rand() % 1000 << endl; + cout << rand() % 1000 << endl; + cout << rand() % 1000 << endl; + cout << rand() % 1000 << endl; + cout << rand() % 1000 << endl; + cout << rand() % 1000 << endl; + cout << rand() % 1000 << endl; + return 0; +}
\ No newline at end of file diff --git a/Algorithms/BubbleSort.cpp b/Algorithms/BubbleSort.cpp new file mode 100644 index 0000000..587b507 --- /dev/null +++ b/Algorithms/BubbleSort.cpp @@ -0,0 +1,15 @@ +/* +* @Author: yannherklotz +* @Date: 2016-02-08 23:26:59 +* @Last Modified by: yannherklotz +* @Last Modified time: 2016-02-08 23:26:59 +*/ + +#include <iostream> + +using namespace std; + +int main(){ + + return 0; +}
\ No newline at end of file diff --git a/Algorithms/LinkedLists b/Algorithms/LinkedLists Binary files differnew file mode 100644 index 0000000..21e8c09 --- /dev/null +++ b/Algorithms/LinkedLists diff --git a/Algorithms/LinkedLists.cpp b/Algorithms/LinkedLists.cpp new file mode 100644 index 0000000..360714e --- /dev/null +++ b/Algorithms/LinkedLists.cpp @@ -0,0 +1,188 @@ +/* +* @Author: yannherklotz +* @Date: 2016-02-08 22:20:52 +* @Last Modified by: yannherklotz +* @Last Modified time: 2016-02-09 12:43:15 +*/ + +#include <iostream> + +using namespace std; + +typedef int list_t; + +struct linkNode { + list_t val; + linkNode* next; +}; + +void addel(list_t, linkNode*&); +void deallocate(linkNode*&); +void printListOpposite(linkNode*); +void printListCorrect(linkNode*); +int listLength(linkNode*); +int deleteItem(linkNode*&, list_t); +linkNode* concatenateList(linkNode*, linkNode*); + +int main(){ + linkNode* listA = NULL; + linkNode* listB = NULL; + linkNode* listC = NULL; + + int n; + list_t el; + + cout << "How many elements: "; + cin >> n; + + for(int i = 0; i < n; ++i) { + cout << "Enter element: "; + cin >> el; + + addel(el, listA); + } + printListOpposite(listA); + cout << endl; + printListCorrect(listA); + cout << "List Count: " << listLength(listA) << endl; + cout << "which element to remove? "; + cin >> n; + deleteItem(listA, n); + printListCorrect(listA); + + cout << "How many elements in list B: "; + cin >> n; + + for(int i = 0; i < n; ++i) { + cout << "Enter element: "; + cin >> el; + + addel(el, listB); + } + + printListCorrect(listB); + listC = concatenateList(listA, listB); + cout << "Concatenated list:" << endl; + printListCorrect(listC); + cout << "delete from a: "; + cin >> n; + deleteItem(listA, n); + cout << "delete from b: "; + cin >> n; + deleteItem(listB, n); + cout << endl << "A" << endl; + printListCorrect(listA); + cout << "B" << endl; + printListCorrect(listB); + cout << "A+B" << endl; + printListCorrect(listC); + + deallocate(listA); + deallocate(listB); + deallocate(listC); + return 0; +} + +void addel(list_t el, linkNode* &ln) { + linkNode* tmp = new linkNode; + tmp->val = el; + tmp->next = ln; + ln = tmp; +} + +void deallocate(linkNode* &ln) { + while(ln != NULL) { + linkNode* nextln = ln->next; + delete ln; + ln = nextln; + } +} + +void printListOpposite(linkNode* ln) { + while(ln != NULL) { + cout << ln->val << endl; + ln = ln->next; + } +} + +void printListCorrect(linkNode* ln) { + if(ln != NULL) { + printListCorrect(ln->next); + cout << ln->val << endl; + } +} + +int listLength(linkNode* ln) { + int count = 0; + while(ln != NULL) { + ++count; + ln = ln->next; + } + return count; +} + +int findItem(linkNode* ln, list_t el) { + int count = 0; + while(ln != NULL) { + if(ln->val == el) { + return count; + } + ++count; + } + return(-1); +} + +int deleteItem(linkNode* &ln, list_t el) { + linkNode* sp = ln; + linkNode* fp = ln->next; + + if(sp->val == el) { + ln = ln->next; + delete sp; + } else { + while(fp != NULL) { + if(fp->val == el) { + sp->next = fp->next; + delete fp; + return 1; + } + fp = fp->next; + sp = sp->next; + } + } + return 0; +} + +linkNode* concatenateList(linkNode* fl, linkNode* sl) { + linkNode* tmpsl = NULL; + linkNode* tmpfl = NULL; + + while(fl != NULL) { + addel(fl->val, tmpfl); + fl = fl->next; + } + while(sl != NULL) { + addel(sl->val, tmpsl); + sl = sl->next; + } + + linkNode* tmp = tmpsl; + while(tmp->next != NULL) { + tmp = tmp->next; + } + tmp->next = tmpfl; + return tmpsl; +} + +void reverseOrder(listNode* &ln) { + linkNode* sp = ln; + linkNode* tmp = ln; + ln = ln->next; + tmp->next = NULL; + while(ln->next != NULL) { + tmp = ln->next; + ln->next = sp; + sp = ln; + ln = tmp; + } +}
\ No newline at end of file diff --git a/LinkedLists.cpp b/LinkedLists.cpp index 2966f76..55dc090 100644 --- a/LinkedLists.cpp +++ b/LinkedLists.cpp @@ -7,6 +7,10 @@ struct intList { intList* next_el; }; +// add functions to add elements to a list +// create a list and be able to order them +// make ordered linked list + int main() { intList* ilist = NULL; int el, n; |