Casey Cornaire fall2013 Opus
Its a Dick for!
Last semester! JOE FREEEEEEE!!!!!!
8-28-13
in class today we discussed reviewed memory and data types in class. we also reviewed how to allocate memory. we went over the opus and then discussed the first project for the class which is to either do free cell, forty thieves or eight off.
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 7 int a, *b; 8 a=12; 9 b=&a; 10 fprintf(stdout, "[a] address is: 0x%X\n", &a); 11 fprintf(stdout, "[a] contains: %d\n", a); 12 // fprintf(stdout, "[a] dereferences to: %d\n", a); 13 14 fprintf(stdout, "[b] address is: 0x%X\n", &b); 15 fprintf(stdout, "[b] contains: 0x%X\n", b); 16 fprintf(stdout, "[b] dereferences to: %d\n",*b); 17 18 a=137; 19 20 fprintf(stdout, "[a] address is: 0x%X\n", &a); 21 fprintf(stdout, "[a] contains: %d\n", a); 22 // fprintf(stdout, "[a] dereferences to: %d\n", a); 23 24 fprintf(stdout, "[b] address is: 0x%X\n", &b); 25 fprintf(stdout, "[b] contains: 0x%X\n", b); 26 fprintf(stdout, "[b] dereferences to: %d\n",*b); 27 28 *b=2600; 29 30 fprintf(stdout, "[a] address is: 0x%X\n", &a); 31 fprintf(stdout, "[a] contains: %d\n", a); 32 // fprintf(stdout, "[a] dereferences to: %d\n", a); 33 34 fprintf(stdout, "[b] address is: 0x%X\n", &b); 35 fprintf(stdout, "[b] contains: 0x%X\n", b); 36 fprintf(stdout, "[b] dereferences to: %d\n",*b); 37 return(0); 38 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 signed char sc=0; 7 unsigned char uc=0; 8 9 printf("a signed char is %hhu bytes\n", sizeof(sc)); 10 printf("lower bound is: %hhd\n", ((unsigned char)(sc-1)/2)+1); 11 printf("Upper bound is: %hhd\n", ((unsigned char)(sc-1)/2)); 12 printf("--------\n"); 13 printf("an unsigned char is %hhu bytes \n", sizeof(uc)); 14 printf("lower bound is: %hhu\n", uc); 15 printf("Upper bound is: %hhu\n", uc-1); 16 printf("--------\n"); 17 return(0); 18 }
on this day we continued with data tpyes
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 signed char sc=0; 7 unsigned char uc=0; 8 signed short ssi=0; 9 unsigned short usi=0; 10 signed int si=0; 11 unsigned int ui=0; 12 signed long sl=0; 13 unsigned long ul=0; 14 signed long long sll=0; 15 unsigned long long ull=0; 16 float f=0; 17 18 19 printf(" "); 20 printf("a signed char is %hhd bytes\n", sizeof(sc)); 21 printf("lower bound is: %hhd\n", ((unsigned char)(sc-1)/2)+1); 22 printf("Upper bound is: %hhd\n", ((unsigned char)(sc-1)/2)); 23 printf("--------\n"); 24 printf("an unsigned char is %hhu bytes \n", sizeof(uc)); 25 printf("lower bound is: %hhu\n", uc); 26 printf("Upper bound is: %hhu\n", uc-1); 27 printf("--------\n"); 28 printf("a signed short int is %hd bytes\n", sizeof(ssi)); 29 printf("lower bound is: %hd\n", ((unsigned short)(ssi-1)/2)+1); 30 printf("Upper bound is: %hd\n", ((unsigned short)(ssi-1)/2)); 31 printf("--------\n"); 32 printf("an unsigned short int is %hu bytes \n", sizeof(usi)); 33 printf("lower bound is: %hu\n", usi); 34 printf("Upper bound is: %hu\n", usi-1); 35 printf("--------\n"); 36 printf("a signed int is %d bytes\n", sizeof(si)); 37 printf("lower bound is: %d\n", ((unsigned int)(si-1)/2)+1); 38 printf("Upper bound is: %d\n", ((unsigned int)(si-1)/2)); 39 printf("--------\n"); 40 printf("an unsigned int is %u bytes \n", sizeof(ui)); 41 printf("lower bound is: %u\n", ui); 42 printf("Upper bound is: %u\n", ui-1); 43 printf("--------\n"); 44 printf("a signed long int is %ld bytes\n", sizeof(sl)); 45 printf("lower bound is: %ld\n", ((unsigned long int)(sl-1)/2)+1); 46 printf("Upper bound is: %ld\n", ((unsigned long int)(sl-1)/2)); 47 printf("--------\n"); 48 printf("an unsigned long int is %lu bytes \n", sizeof(ul)); 49 printf("lower bound is: %lu\n", ul); 50 printf("Upper bound is: %lu\n", ul-1); 51 printf("--------\n"); 52 printf("a signed float is %f bytes\n", sizeof(f)); 53 printf("lower bound is: %f\n", (( float)(f-1)/2)+1); 54 printf("Upper bound is: %f\n", ((float)(f-1)/2)); 55 printf("--------\n"); 56 printf("an unsigned float is %f bytes \n", sizeof(f)); 57 printf("lower bound is: %f\n", f); 58 printf("Upper bound is: %f\n", f-1); 59 printf("--------\n"); 60 61 return(0); 62 }
today we built a program that dealt with arrays, structs, and loops as a review
#include <stdio.h> #include <stdlib.h> int main() { struct person{ char *name; int age; long int id; }; typedef struct person Person; Person People[8]; int i=0; for(i=0; i<8; i++) { fprintf(stdout, "Enter person #%d's name: ", (i+1)); People[i].name=(char*)malloc(sizeof(char)*20); fscanf(stdin, "%s", People[i].name); fprintf(stdout, "Enter %'s age: ", People[i].name); fscanf(stdin, "%d", &People[i].age); fprintf(stdout, "Enter %s's id number: ", People[i].name); fscanf(stdin, "%ld", &People[i].id); } i=-1; while(i==-1) { fprintf(stdout, "Look up date for person #: "); fscanf(stdin, "%d", &i); if(!((i>=0)&&(i<=7))) { fprintf(stdout, "Invalid person #, Try Again!\n"); i=-1; } } i--; fprintf(stdout, "Name: %s\n", People[i].name); fprintf(stdout, "Age: %d\n", People[i].age); fprintf(stdout, "ID: %ld\n", People[i].id); return(0); }
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node 5 { 6 int value; 7 struct node *next; 8 }; 9 10 typedef struct node Node; 11 12 int main() 13 { 14 int input; 15 Node *list, *tmp; 16 list=tmp=NULL; 17 while(input!=-1) 18 { 19 printf("Enter a value(-1 to end): "); 20 scanf("%d", &input); 21 if(input!=-1) 22 { 23 if(list==NULL) 24 { 25 list=tmp=(Node*)malloc(sizeof(Node)); 26 tmp->next=NULL; 27 list->value=input; 28 } 29 else 30 { 31 tmp->next=(Node*)malloc(sizeof(Node)); 32 tmp->next->next=NULL; 33 tmp->next->value=input; 34 tmp=tmp->next; 35 } 36 } 37 } 38 tmp=list; 39 input=0; 40 while(tmp!=NULL) 41 { 42 printf("[%d] %d ->", input,tmp->value); 43 tmp=tmp->next; 44 input=input+1; 45 } 46 printf("NULL\n"); 47 //insert into list 48 printf("Which node would you like to insert before?"); 49 scanf("%d",&input); 50 int seeker; 51 tmp=list; 52 Node *tmp2=NULL; 53 for(seeker=0;<=input;seeker++) 54 { 55 tmp=tmp->next; 56 } 57 printf("Enter a value to insert: "); 58 scanf("%d", &input); 59 tmp2=(node*)malloc(sizeof(Node)); 60 tmp2->value=input; 61 tmp2->next=tmp->next; 62 tmp->next=tmp2; 63 return(0); 64 }
today we worked on insert function, we had noticed that if you insert before node 0, it actually puts it after, the following code prevents
if(input==0) { fprintf(stdout, "Enter value for new node: "): fscanf(stdin, "%d", &input); tmp2=(Node*malloc(sizeof(Node)); tmp2->value=input; tmp2->next=NULL; tmp2->next=tmp; list=tmp2; }
today we worked on developing a singly liked list, building a list of nodes and manipulating the list with insert, apeend and remove
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node { 5 int value; 6 struct node *next; 7 }; 8 9 typedef struct node Node; 10 11 int main() 12 { 13 int input; 14 Node *list, *tmp; 15 16 list = tmp = NULL; 17 while (input != -1) 18 { 19 printf("Enter a value(-1 to end): "); 20 scanf("%d", &input); 21 if (input != -1) 22 { 23 if (list == NULL) 24 { 25 list = tmp = (Node *) malloc(sizeof(Node)); 26 tmp->next = NULL; 27 list->value = input; 28 } 29 else 30 { 31 tmp->next = (Node *) malloc(sizeof(Node)); 32 tmp->next->next = NULL; 33 tmp->next->value = input; 34 tmp = tmp->next; 35 } 36 } 37 } 38 tmp = list; 39 input = 0; 40 while (tmp != NULL) 41 { 42 printf("[%d] %d ->", input, tmp->value); 43 tmp = tmp->next; 44 input = input + 1; 45 } 46 printf("NULL\n"); 47 //insert into list 48 printf("Which node would you like to insert before?"); 49 scanf("%d", &input); 50 tmp = list; 51 int seeker; 52 53 tmp = list; 54 Node *tmp2 = NULL; 55 56 for (seeker = 0; seeker < input - 1; seeker++) 57 { 58 tmp = tmp->next; 59 } 60 61 if (input == 0) 62 { 63 printf(" enter value for new node "); 64 scanf("%d", &input); 65 tmp2 = (Node *) malloc(sizeof(Node)); 66 tmp2->value = input; 67 tmp2->next = NULL; 68 tmp2->next = tmp; 69 list = tmp2; 70 } 71 else 72 { 73 printf("Enter a value to insert: "); 74 scanf("%d", &input); 75 tmp2 = (Node *) malloc(sizeof(Node)); 76 tmp2->value = input; 77 tmp2->next = tmp->next; 78 tmp->next = tmp2; 79 } 80 tmp = list; 81 input = 0; 82 83 while (tmp != NULL) 84 { 85 printf("[%d] %d ->", input, tmp->value); 86 tmp = tmp->next; 87 input = input + 1; 88 } 89 printf("NULL\n"); 90 return (0); 91 }
#include<stdio.h> #include<stdlib.h> struct node { int value; struct node *next; }; typedef struct node Node; int main() { int input; Node *list, *tmp; list = tmp = NULL; while (input != -1) { printf("Enter a value(-1 to end): "); scanf("%d", &input); if (input != -1) { if (list == NULL) { list = tmp = (Node *) malloc(sizeof(Node)); tmp->next = NULL; list->value = input; } else { tmp->next = (Node *) malloc(sizeof(Node)); tmp->next->next = NULL; tmp->next->value = input; tmp = tmp->next; } } } tmp = list; input = 0; while (tmp != NULL) { printf("[%d] %d ->", input, tmp->value); tmp = tmp->next; input = input + 1; } printf("NULL\n"); //insert into list printf("Which node would you like to insert before?"); scanf("%d", &input); tmp = list; int seeker; tmp = list; Node *tmp2 = NULL; for (seeker = 0; seeker < input - 1; seeker++) { tmp = tmp->next; } if (input == 0) { printf(" enter value for new node "); scanf("%d", &input); tmp2 = (Node *) malloc(sizeof(Node)); tmp2->value = input; tmp2->next = NULL; tmp2->next = tmp; list = tmp2; } else { printf("Enter a value to insert: "); scanf("%d", &input); tmp2 = (Node *) malloc(sizeof(Node)); tmp2->value = input; tmp2->next = tmp->next; tmp->next = tmp2; } tmp = list; input = 0; tmp = list; printf("Which node would you like to insert after?"); scanf("%d", &input); tmp = list; seeker; tmp = list; Node *tmp3 = NULL; for (seeker = 0; seeker < input; seeker++) { tmp = tmp->next; } if (input == 0) { printf(" enter value for new node "); scanf("%d", &input); tmp3 = (Node *) malloc(sizeof(Node)); tmp3->value = input; tmp3->next = NULL; tmp3->next = tmp; list = tmp3; } else { printf("Enter a value to insert: "); scanf("%d", &input); tmp3 = (Node *) malloc(sizeof(Node)); tmp3->value = input; tmp3->next = tmp->next; tmp->next = tmp3; } tmp = list; input = 0; while (tmp != NULL) { printf("[%d] %d ->", input, tmp->value); tmp = tmp->next; input = input + 1; } printf("NULL\n"); return (0); }
Today we started the doubly linked list and apparently we have to make it print forward and backward. we did accomplish the display forward
1#include<stdio.h> 2 #include <stdlib.h>\ 3 4 struct node { 5 6 int value; 7 struct node *next; 8 struct node *prev; 9 10 }; 11 12 typedef stuct node Node; 13 14 struct list { 15 struct node *start; 16 struct node *end; 17 }; 18 19 typedef struct list List; 20 21 List *build(); 22 void displayf( list * ); 23 void displayf( list * ); 24 int main() 25 { 26 27 myList = build() 28 displayf( myList); 29 displayb( myList); 30 return 0; 31 } 32 33 List *build() 34 35 { 36 Node *tmp = NULL; 37 List *myList = (List *)malloc(sizeof(list)); 38 myList -> start = myList -> end = NULL; 39 int input = 0; 40 printf( "enter a value ( -1 to quit ): "); 41 scanf( "%d", &input); 42 while( input != -1 ) 43 { 44 if( myList -> start == NULL) 45 { 46 myList -> start = myList -> end = (Node *)malloc(sizeof(Node)); 47 mylist -> start -> value = input; 48 myList -> end -> prev -> myList -> start -> next = NULL; 49 } else 51 } 52 53 myList -> end -> next = (Node*)malloc(sizeof(Node); 54 myList -> end -> next -> next = NULL; 55 myList -> end -> next -> prev = mylist -> end; 56 myList -> end -> myList -> end -> next; 57 printf( "enter another value ( -1 to quit ): "); 58 scanf( "%d", &input);
today we continued on the doubly linked list and built the display back function
1 #include <stdio.h> 2 #include <stdlib.h>\ 3 4 struct node { 5 6 int value; 7 struct node *next; 8 struct node *prev; 9 10 }; 11 12 typedef stuct node Node; 13 14 struct list { 15 struct node *start; 16 struct node *end; 17 }; 18 19 typedef struct list List; 20 21 void displayf( List * ) 22 { 23 Node *tmp = NULL; 24 tmp = List -> start; 25 int input = 0; 26 27 while (tmp != NULL) 28 { 29 printf("[%d] %d ->", input, tmp->value); 30 tmp = tmp->next; 31 input = input + 1; 32 } 33 printf("NULL\n"); 34 } 35 36 void displayb ( List * ) 37 { 38 Node *tmp = NULL; 39 tmp = List -> end; 40 int input = 0; 41 42 while( tmp != NULL ) 43 44 { 45 46 47 while (tmp != NULL) 48 { 49 printf("[%d] %d ->", input, tmp->value); 50 tmp = tmp <- prev; 51 input = input - 1; 52 } 53 printf("NULL\n"); 54 }
today we continued on the doubly linked list and built the display back function
1 #include <stdio.h> 2 #include <stdlib.h>\ 3 4 struct node { 5 6 int value; 7 struct node *next; 8 struct node *prev; 9 10 }; 11 12 typedef stuct node Node; 13 14 struct list { 15 struct node *start; 16 struct node *end; 17 }; 18 19 typedef struct list List; 20 21 void displayf( List * ) 22 { 23 Node *tmp = NULL; 24 tmp = List -> start; 25 int input = 0; 26 27 while (tmp != NULL) 28 { 29 printf("[%d] %d ->", input, tmp->value); 30 tmp = tmp->next; 31 input = input + 1; 32 } 33 printf("NULL\n"); 34 } 35 36 void displayb ( List * ) 37 { 38 Node *tmp = NULL; 39 tmp = List -> end; 40 int input = 0; 41 42 while( tmp != NULL ) 43 44 { 45 46 47 while (tmp != NULL) 48 { 49 printf("[%d] %d ->", input, tmp->value); 50 tmp = tmp <- prev; 51 input = input - 1; 52 } 53 printf("NULL\n"); 54 }
today we accomplished completing the insert function
List *insert(List *myList, Node *place, Node *newNode) { if(place==myList->start) { newNode->next=place; place->prev=newNode; newNode->prev=NULL; myList->start=newNode; } else { newNode->next=place; place->prev->next=newNode; newNode->prev=place->prev; place->prev=newNode; } return(myList); }
today we worked on the getNode function or remove
List *getNode(List *myList, Node **place) { if(myList->start->next==NULL) { myList->start=NULL; } else if(*place==myList->start) { myList->start=myList->start->next; myList->start->prev=NULL; (*place)->next=NULL; } else if(*place==myList->end) { myList->end=myList->end->prev; myList->end->next=NULL; (*place)->prev=NULL; } else { (*place)->prev->next=(*place)->next; (*place)->next->prev=(*place)->prev; (*place)->prev=(*place)->next=NULL; } return(myList); }
today we made a header file for the doubly linked list
#ifndef _DLL_H #define _DLL_H #include <stdio.h> #include <stdlib.h> struct node { int value; struct node *next; struct node *prev; }; typedef struct node Node; struct list { struct node *start; struct node *end; }; typedef struct list List; List *build(); void displayf(List*); void displayb(List*); List *insert(List *, Node *, Node *); List *append(List *, Node *, Node *); List *getNode(List *, Node **); #endif
today I started to work on the menu for the singly linked list will paste code when finished with it all
today we did the knowledge assessment, ouch did that hurt, I need to get a clue!!!
on this day we continued to work on the knowledge assessment, still not good
on this day we took the second part of the knowledge assessment, I really am not good at this stuff
back to the grind, today we talked about stacks,
pop() - takes the item off the stack push() - puts the item on the top of the stack peek() - shows us the top of the stack top - pointer always located at the top of the stack mkstack() - creates new stack
today we worked on creating the push and pop functions
#include "stack.h" Stack * push(Stack *myStack, Node *newNode) { if((*myStack->size <= 0) || (myStack->data->qty < myStack->size)) { (*myStack)->data = append((*myStack)->data, (*myStack)->data->e$ (*myStack)->top = (*myStack)->data->end; } return(myStack);
#include "stack.h" Node *pop(Stack **myStack) { Node *tmp = NULL; if(myStack != NULL) { tmp = getNode(&(*myStack)->data,(*myStack)->data->data->end); (*myStack)->top = (*myStack)->data->end; } return(tmp); }
today we did a stack test program in class
nclude <stdio.h> #include "stack.h" int main() { Node *tmp; Stack *myStack; myStack = mkstack(0); tmp = create(); tmp->value = fgetc(stdin); fgetc(stdin); while(tmp->value != '\n') { myStack = push(myStack, tmp); tmp = create(); tmp->value = fgetc(stdin); fgetc(stdin); } fprintf(stdout, "linked list has %d nodes\n", myStack->data->qty); fprintf(stdout, "String is: "); do { tmp = pop(&myStack); if(tmp != NULL) { fprintf(stdout, "%c", tmp->value); } } while(tmp != NULL); fprintf(stdout, "\n"); return(0); }
today I defeated the clear command for the singly linked list
Node *clear(Node *list) {
Node *tmp = list; Node *tmp2; int count;
while(tmp!=NULL) { tmp=tmp->next; count++; }
tmp=list;
for(; count>0 ; count--) { tmp2=tmp->next; tmp->next=NULL; free(tmp); tmp=tmp2; }
return(NULL);
}
today I worked on the having trouble with the parameters for thmenu for the singly linked list, it is coming along pretty good, but I am trouble with the parameters for the function calls in the case statements
completed my singly link list menu today!!! well minus the sort command though
#include <stdio.h> #include <stdlib.h> struct node { int value; struct node *next; }; typedef struct node Node; Node *build(Node *); Node *display(Node *); Node *clear(Node *); Node *insertNode(Node *); Node *appendNode(Node *); Node *removeNode(Node *); Node *sort(Node *); int main() { Node *list = NULL; int input = 0; while(input != 8) { printf("This is a Singley Linked List.\n"); printf("What would you like to do?\n"); printf("1. Build List.\n"); printf("2. Display List.\n"); printf("3. Clear List.\n"); printf("4. Insert Value.\n"); printf("5. Append Value.\n"); printf("6. Remove Value.\n"); printf("7. Sort List\n"); printf("8. Quit.\n"); printf("Selection: "); scanf("%d", &input); switch(input) { case 1: list = build(list); break; case 2: list = display(list); break; case 3: list = clear(list); break; case 4: list = insertNode(list); break; case 5: list = appendNode(list); break; case 6: list = removeNode(list); break; case 7: list = sort(list); break; default: printf("Invalid choice. Please choose again."); break; } } return(0); } Node *build(Node *list) { int input = 0; Node *tmp = NULL; while (input != -1) { printf("Enter a value (-1 to end): "); scanf("%d", &input); if (input != -1) { if (list == NULL) { list = tmp = (Node *) malloc(sizeof(Node)); tmp->next = NULL; list->value = input; } else { tmp->next = (Node *)malloc(sizeof(Node)); tmp->next->next = NULL; tmp->next->value = input; tmp = tmp->next; } } } display(list); return(list); } Node *display(Node *list) { int input = 0; Node *tmp = NULL; tmp = list; while (tmp != NULL) { printf("[%d] %d -> ", input, tmp->value); tmp = tmp->next; input = input + 1; } printf("NULL\n"); return(list); } Node *clear(Node *list) { Node *tmp = list; Node *tmp2; int count; while(tmp!=NULL) { tmp=tmp->next; count++; } tmp=list; for(; count>0 ; count--) { printf( "%d/n", &count ); tmp2=tmp->next; tmp->next=NULL; free(tmp); tmp=tmp2; printf( "%d/n", &count ); } return(NULL); } // Insert into List. Node *insertNode(Node *list) { int input = 0; // Allows the "seeking" of nodes. Also need to reset tmp. int seeker; Node *tmp = NULL; // Placeholder for Node number. Node *tmp2 = NULL; tmp = list; printf("Which node would you like to insert before?: "); scanf("%d", &input); tmp = list; // So seeker is at correct node. for (seeker = 0; seeker < (input - 1); seeker++) { tmp = tmp->next; } // Fix for n-1 case. if (input == 0) { printf("Enter a value to insert: "); scanf("%d", &input); tmp2 = (Node *) malloc(sizeof(Node)); tmp2->value = input; tmp2->next = NULL; tmp2->next = tmp; list = tmp2; } else { printf("Enter a value to insert: "); scanf("%d", &input); // Works for n-1 cases. tmp2 = (Node *) malloc(sizeof(Node)); tmp2->value = input; tmp2->next = NULL; tmp2->next = tmp->next; tmp->next = tmp2; display(list); } return(list); } // Append to List. Same as insert; but inserts before not after. Node *appendNode(Node *list) { int input = 0; int seeker = 0; // Need to reset to NULL. Node *tmp = NULL; Node *tmp2 = NULL; tmp = list; printf("Which node would you like to append after?: "); scanf("%d", &input); tmp = list; // So seeker is at correct node. for (seeker = 0; seeker < (input); seeker++) { tmp = tmp->next; } printf("Enter a value to append: "); scanf("%d", &input); tmp2 = (Node *) malloc(sizeof(Node)); // Insert the input into tmp2. tmp2 -> value = input; // tmp2's next becomes tmp's next. tmp2 -> next = tmp -> next; // tmp's next becomes tmp2. tmp -> next = tmp2; display(list); return(list); } // Remove from List. Node *removeNode(Node *list) { int input = 0; int seeker = 0; // Need to reset to NULL. Node *tmp = NULL; Node *tmp2 = NULL; tmp = list; // Input is the node number to remove. printf("Which node would you like to remove?: "); scanf("%d", &input); for(seeker=0; seeker<(input-1);seeker++) { tmp = tmp -> next; } // tmp2 becomes tmp's next. tmp2 = tmp -> next; // tmp's next becomes tmp2's next. tmp -> next = tmp2 -> next; // tmp2's next becomes null. tmp2 -> next = NULL; display(list); return(list); } Node *sort(Node *list) { Node*tmp; Node*tmp2; tmp=list; Node *max; int back; int count = 0; while(tmp!=NULL) { tmp=tmp->next; count++; } int first=count; for(; first>1; first--) { tmp=list; tmp2=list; max=tmp; count=first; for(; count>0; count--) { if(max -> value < tmp2 -> value) { max=tmp2; } tmp2 = tmp2 -> next; } list=removeNode(list); tmp = list; for(back=0; back<(first-2); back++) { tmp=tmp->next; } list=appendNode(list); } display(list); return(list); }
today I started working on my doubly liked list menu, I wanted to try something a little different with the case statements, will paste code when done.
on this day I worked on my DLL menu and its coming along good, need to start my sort and clear functions soon
on this day I worked on my sort function for my doubly linked list, its a pain in the ass!! but I am slowly getting it done
today I continued on the sort function for the DLL, I keep getting seg faults and I cannot figure out why
continued the sort function today and making some headway, fixed one seg fault but now getting another later down the road, tried the fix for the one before but it didn't work…
today I finished the sort command for my DLL!!
List *sort(List *myList) { Node *tmp; Node *tmp2; tmp = myList->start; Node *max; int back; int count = 0; while(tmp != NULL) { tmp = tmp->next; count++; } int first = count; for(; first>1; first--) { tmp = myList->start; tmp2 = myList->start; max = tmp; for(; count>0; count--) { if(max->value < tmp2->value) { max = tmp2; } tmp2 = tmp2->next; } myList = getNode(myList, &max); tmp = myList->start; for(back = 0; back<(first-2); back++) { tmp = tmp->next; } myList = append(myList, tmp, max); } return(myList); }
Now time to work on the clear function, I was able to get a good portion of it done, will continue tommmorow
continued on the clear function and completed the code just trying to clean up some errors
finished the clear function,
List *clear(List *mylist) 60 { 61 Node *tmp = mylist->start; 62 Node *tmp2; 63 int count; 64 65 while(tmp != NULL) 66 { 67 tmp = tmp->next; 68 } 69 70 tmp = mylist->start; 71 72 for(; count>0; count--) 73 { 74 if(tmp == mylist->end) 75 { 76 mylist->end = NULL; 77 mylist->start = NULL; 78 free(tmp); 79 } 80 else 81 { 82 tmp2 = tmp->next; 83 tmp->next = NULL; 84 tmp->prev = NULL; 85 tmp2->prev = NULL; 86 mylist->start = tmp2; 87 free(tmp); 88 tmp = tmp2; 89 } 90 } 91 mylist->start = 0; 92 mylist->end = 0; 93 94 return(mylist); 95 }
on this day I put all my functions into a listops file for the DLL
#include "DLL.h" 2 3 4 List *insert(List * mylist, Node * place, Node * newNode) 5 { 6 if (place == mylist->start) 7 { 8 newNode->next = place; 9 if( place != NULL ) 10 { 11 place->prev = newNode; 12 } 13 newNode->prev = NULL; 14 mylist->start = newNode; 15 } 16 else 17 { 18 if( newNode == NULL) 19 { 20 newNode->next = place; 21 22 if( place != NULL ) 23 { 24 place->prev->next = newNode; 25 } 26 } 27 newNode->prev = place->prev; 28 place->prev = newNode; 29 } 30 31 32 return (mylist); 33 } 34 35 List * getNode( List * mylist, Node **place ) 36 { 37 if( *place == mylist -> start ) 38 { 39 mylist -> start = mylist -> start -> next; 40 mylist -> start -> prev = NULL; 41 ( *place ) -> next = NULL; 42 } 43 else if( *place == mylist -> end ) 44 { 45 mylist -> end = mylist -> end -> prev; 46 mylist -> end -> next = NULL; 47 ( *place ) -> prev = NULL; 48 49 } 50 else 51 { 52 ( *place ) -> prev -> next = ( *place ) -> next; 53 ( *place ) -> next -> prev = ( *place ) -> prev; 54 55 } 56 return(mylist); 57 } 58 59 List *clear(List *mylist) 60 { 61 Node *tmp = mylist->start; 62 Node *tmp2; 63 int count; 64 65 while(tmp != NULL) 66 { 67 tmp = tmp->next; 68 } 69 70 tmp = mylist->start; 71 72 for(; count>0; count--) 73 { 74 if(tmp == mylist->end) 75 { 76 mylist->end = NULL; 77 mylist->start = NULL; 78 free(tmp); 79 } 80 else 81 { 82 tmp2 = tmp->next; 83 tmp->next = NULL; 84 tmp->prev = NULL; 85 tmp2->prev = NULL; 86 mylist->start = tmp2; 87 free(tmp); 88 tmp = tmp2; 89 } 90 } 91 mylist->start = 0; 92 mylist->end = 0; 93 94 return(mylist); 95 } 96 97 98 List *sort(List *mylist) 99 { 100 Node *tmp; 101 Node *tmp2; 102 tmp = mylist->start; 103 Node *max; 104 int t; 105 int count = 0; 106 while(tmp != NULL) 107 { 108 tmp = tmp->next; 109 count++; 110 } 111 int i; 112 int a; 113 int b; 114 for(a=count; a>1; a--) 115 { 116 for(i=count; i>1; i--) 117 { 118 tmp = mylist->start; 119 tmp2 = mylist->start; 120 max = tmp; 121 for(b=count; b>0; b--) 122 { 123 if(max->value < tmp2->value) 124 { 125 max = tmp2; 126 } 127 tmp2 = tmp2->next; 128 } 129 mylist = getNode(mylist, &max); 130 131 tmp = mylist->start; 132 for(t = 0; t<(i-2); t++) 133 { 134 tmp = tmp->next; 135 } 136 mylist = append(mylist, tmp, max); 137 } 138 return(mylist); 139 } 140 } 141 142 143 List *build() 144 { 145 Node *tmp=NULL; 146 List *mylist=(List*)malloc(sizeof(List)); 147 mylist->start=mylist->end=NULL; 148 int input = 0; 149 printf("Enter a value (-1 to quit): "); 150 scanf( "%d", &input); 151 152 while(input !=-1) 153 { 154 if(mylist->start==NULL) 155 { 156 mylist->start=mylist->end=(Node*)malloc(sizeof(Node)); 157 mylist->start->value=input; 158 mylist->end->prev=mylist->start->next=NULL; 159 } 160 else 161 { 162 mylist->end->next=(Node*)malloc(sizeof(Node)); 163 mylist->end->next->value=input; 164 mylist->end->next->next=NULL; 165 mylist->end->next->prev=mylist->end; 166 mylist->end=mylist->end->next; 167 } 168 printf("Enter another value(-1 to quit): "); 169 scanf( "%d", &input); 170 } 171 displayf(mylist); 172 displayb(mylist); 173 return(mylist); 174 } 175 176 List *append(List *mylist, Node *place, Node *newNode) 177 { 178 if( place == NULL ) 179 { 180 place = mylist->start; 181 mylist->start = newNode; 182 mylist->end = newNode; 183 184 } 185 else if(place==mylist->end) 186 { 187 newNode->prev=place; 188 place->next=newNode; 189 newNode->next=NULL; 190 mylist->end=newNode; 191 } 192 else 193 { 194 newNode->prev=place; 195 place->next->prev=newNode; 196 newNode->next=place->next; 197 place->next=newNode; 198 } 199 200 return(mylist); 201 } 202 203
on this day I made a quantity function and put it in my listops
204 void qty(List * mylist) 205 { 206 int i = 1; 207 Node *tmp = NULL; 208 tmp = mylist->start; 209 210 while (tmp->next != NULL) 211 { 212 tmp = tmp->next; 213 i++; 214 } 215 216 printf( "%d\n", i ); 217 218 }
on this day I worked more on my listops option menu. I have been getting seg faults and was unsure why. apparently it has been the parameters that I have been passing into the insert function. matt helped me with setting it up, upon him leaving, I was getting another segfault that I have not seen before fscanf.c: No such file or directory
I had never seen this before, so I googled the message and found out that it was an ampersand that I was forgetting, but through this I learned about the backtrace and list commands within gdb so that was very helpful!!!
I was getting seg faults for one program bc my build function was not returning anything and was causing my list pointer to be NULL, but that only fixed one.
on this day I made a display file that houses my display function
1 #include "DLL.h" 2 3 void displayf( List *mylist) 4 { 5 Node *tmp=mylist->start; 6 int input=0; 7 8 while(tmp !=NULL) 9 { 10 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 11 tmp=tmp->next; 12 input=input++; 13 } 14 fprintf(stdout, "NULL\n"); 15 } 16 17 18 void displayb( List *mylist) 19 { 20 Node *tmp=mylist->end; 21 int input=0; 22 23 while(tmp !=NULL) 24 { 25 fprintf(stdout, "[%d] %d -> ", input, tmp->value); 26 tmp=tmp->prev; 27 input=input++; 28 } 29 fprintf(stdout, "NULL\n"); 30 }
finished the menu program for my DLL
#include "DLL.h" 4 int main() 5 { 6 7 Node * Value = ( Node *)malloc(sizeof(Node)); 8 Node * position; 9 int choice, input, i; 10 11 12 printf( "1: insert:\n") ; 13 printf( "2: append:\n") ; 14 printf( "3: remove:\n") ; 15 printf( "4: sort\n") ; 16 printf( "5: clear\n") ; 17 printf( "6: displayf\n") ; 18 printf( "7: displayb\n") ; 19 printf( "8: qty\n") ; 20 List *mylist = build(); 21 22 printf( " Pick your Poison: "); 23 scanf( "%d", &choice ); 24 25 while(choice != '#') 26 { 27 28 if( choice == 1 ) 29 { 30 printf( " where would you like to insert before?: "); 31 scanf("%d", &input); 32 position = mylist->start; 33 for( i = 0; i < input; i++ ) 34 { 35 position = position -> next; 36 } 37 printf( "enter a Value: "); 38 scanf( "%d", &Value->value ); 39 insert( mylist, position, Value ); 40 } 41 42 else if( choice == 2 ) 43 { 44 printf( " where would you like to insert after?: "); 45 scanf("%d", &input); 46 position = mylist->start; 47 for( i = 0; i < input; i++ ) 48 { 49 position = position -> next; 50 } 51 printf( "enter a Value: "); 52 scanf( "%d", &Value->value ); 53 append( mylist, position, Value); 54 } 55 56 else if( choice == 3 ) { 58 59 position = mylist -> start; 60 printf("Which node would you like to remove: "); 61 scanf( "%d", &input); 62 63 for(i = 0; i < input; i++) 64 { 65 position = position->next; 66 } 67 getNode( mylist, &position ); 68 } 69 70 else if( choice == 4 ) 71 { 72 sort( mylist); 73 } 74 75 else if( choice == 5 ) 76 { 77 78 clear( mylist); 79 } 80 81 else if( choice == 6 ) 82 { 83 displayf( mylist); 84 } 85 86 else if( choice == 7 ) 87 { 88 displayb( mylist); 89 } 90 91 else if( choice == 8 ) 92 { 93 qty(mylist); 94 } 95 96 97 printf( " Pick your Poison: "); 98 scanf( "%d", &choice ); 99 100 } 101 102 return 0; 103 } 104
ON this day I started to get my stack all together and I am getting errors, rtying to work those out
on this day I continued to work on the stack but am getting nowhere, I think im going to start working on my EoCE and implement in C++ and see if I can make it work in there
Worked on my EoCE for C++ inplementation all week!!!!!!!!!!!!!!!!!!!
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
During this week we set up the raspberry pi's and waited on resistors and LED's
Today I set up the raspberry pi to turn an led on and off, verified by matt
today I did some research on the link set by matt and started the second project
on this day I was able to get the second project completed verified by Matt
on this day I started working on the bit counter, I am starting with a generic version that just lights up the lights in sequence with no bit counter
on this day I continued to work on the generic counter and it is almost finished
on this day I finished my generic counter
#include “rpi.h”
void clear() { GPIO_CLR = 1 << 7; GPIO_CLR = 1 << 8; GPIO_CLR = 1 << 9; GPIO_CLR = 1 << 10; sleep(1); } int main() { if(map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } int count = 0; // Define pin 7 as output INP_GPIO(7); OUT_GPIO(7); INP_GPIO(8); OUT_GPIO(8); INP_GPIO(9); OUT_GPIO(9); INP_GPIO(10); OUT_GPIO(10); // Toggle pin 7 (blink a led!) //15 GPIO_SET = 1 << 7; GPIO_SET = 1 << 8; GPIO_SET = 1 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); //14 GPIO_SET = 1 << 7; GPIO_SET = 1 << 8; GPIO_SET = 1 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); //13 GPIO_SET = 1 << 7; GPIO_SET = 1 << 8; GPIO_SET = 0 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); GPIO_SET = 1 << 7; GPIO_SET = 1 << 8; GPIO_SET = 0 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); GPIO_SET = 1 << 7; GPIO_SET = 0 << 8; GPIO_SET = 1 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); GPIO_SET = 1 << 7; GPIO_SET = 0 << 8; GPIO_SET = 1 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); GPIO_SET = 1 << 7; GPIO_SET = 0 << 8; GPIO_SET = 0 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); GPIO_SET = 1 << 7; GPIO_SET = 0 << 8; GPIO_SET = 0 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 1 << 8; GPIO_SET = 1 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 1 << 8; GPIO_SET = 1 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 1 << 8; GPIO_SET = 0 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 1 << 8; GPIO_SET = 0 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 0 << 8; GPIO_SET = 1 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 0 << 8; GPIO_SET = 1 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 0 << 8; GPIO_SET = 0 << 9; GPIO_SET = 1 << 10; sleep(2); clear(); GPIO_SET = 0 << 7; GPIO_SET = 0 << 8; GPIO_SET = 0 << 9; GPIO_SET = 0 << 10; sleep(2); clear(); return 0; }
On this day I started to work on the counter using arrays, its coming along pretty good but I am having trouble grasping the concept of the counter
on this day I continued the work on my binary counter and it seems to be coming along pretty good, had matt help me with some syntax issues and it got me going in the right direction
on this day I thought I had finished the project and it compiles cleanly but it just wont count correctly, maybe it has something to do with the bit shift but not sure, will resume
I am completely f'n stumped, my code was looked at by a couple people and they agree that it should work, WTF
today we started pn the P2p with connecting 2 pis together and having one be a sender and the other a receiver.
on this day we continued the p2pproject, almost done witht his one
finished the p2p project
#include "rpi.h" #include<stdlib.h> int i,j,k,l; int main() { if(map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } /* INP_GPIO(17); OUT_GPIO(7); */ INP_GPIO(22); OUT_GPIO(10); /* INP_GPIO(9); OUT_GPIO(11); INP_GPIO(8); OUT_GPIO(23); */ GPIO_CLR = 1 << 8; GPIO_CLR = 1 << 9; GPIO_CLR = 1 << 22; GPIO_CLR = 1 << 17; while(1) { i = GPIO_READ(8); printf("%d \n", i); if(i !=0) { GPIO_SET = 1 << 23; } else { GPIO_CLR = 1 << 23; } j = GPIO_READ(9); printf("%d \n", j); if(j !=0) { GPIO_SET = 1 <<11; } else { GPIO_CLR = 1 << 11; } k = GPIO_READ(22); printf("%d \n", k); if(k !=0) { GPIO_SET = 1 << 10; } else { GPIO_CLR = 1 << 10; } l = GPIO_READ(17); printf("%d \n", l); if(l !=0) { GPIO_SET = 1 << 7; } else { GPIO_CLR = 1 << 7; } } }
#include "rpi.h" #include <stdlib.h> #include <stdio.h> void clear() { GPIO_CLR = 1 << 23; GPIO_CLR = 1 << 11; GPIO_CLR = 1 << 10; GPIO_CLR = 1 << 7; sleep(1); } int main() { int i; char place = 0, counter = 0, *bin; bin = (char *)malloc(sizeof(char) +4); if(map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } // Define pin 7 as output INP_GPIO(25); OUT_GPIO(23); INP_GPIO(8);//red OUT_GPIO(11); INP_GPIO(24);//blue second in OUT_GPIO(10); INP_GPIO(22);//yellow far right OUT_GPIO(7); int pins[4]; pins[0] = 23; pins[1] = 11; pins[2] = 10; pins[3] = 7; while(1) { place = counter; if((place - 8) >= 0) { *(bin + 0) = 1; place = place - 8; } else *(bin + 0) = 0; if((place - 4) >= 0) { *(bin + 1) = 1; place = place - 4; } else *(bin + 1) = 0; if((place - 2) >= 0) { *(bin + 2) = 1; place = place - 2; } else *(bin + 2) = 0; if(place == 1) { *(bin + 3) = 1; } else *(bin + 3) = 0; for(i = 0; i <= 3; i++) { if( *(bin + i) == 1) { GPIO_SET = 1 << pins[i]; } else GPIO_CLR = 1 << pins[i]; } printf( "%d", *(bin + 0)); printf( "%d", *(bin + 1)); printf( "%d", *(bin + 2)); printf( "%d\n", *(bin + 3)); counter++; if(counter > 15) { counter = 0; } usleep(1000000); } clear(); return 0; }
started on my morse code library today
#include <stdio.h> #include <stdlib.h> #include "rpi.h" int main() { if(map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } INP_GPIO(23); OUT_GPIO(23); char morse; printf("type a string: \n"); scanf( "%c", &morse ); while( morse != '#' ) { if( morse == 'A' ) { GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(375000); GPIO_CLR = 1 << 23; usleep(600000); printf( ".- " ) ; } else if( morse == 'B' ) { GPIO_SET = 1 << 23; usleep(375000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(600000); printf( "-... " ) ; } else if( morse == 'C' ) { GPIO_SET = 1 << 23; usleep(375000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(375000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(600000); printf( "-.-. " ) ; } else if( morse == 'D' ) { GPIO_SET = 1 << 23; usleep(375000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(600000); printf( "-.. " ) ; } else if( morse == 'E' ) { GPIO_SET = 1 << 23; usleep(125000); GPIO_CLR = 1 << 23; usleep(600000); printf( ". " ) ; } } printf( "\n" ); return 0; }
worked more on my morse code library
#include <stdio.h> #include <stdlib.h> #include "rpi.h" int main() { if(map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } INP_GPIO(23); OUT_GPIO(23); char morse; printf("type a string: \n"); scanf( "%c", &morse ); while( morse != '#' ) { if( morse == 'A' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(1480000); printf( ".- " ) ; } else if( morse == 'B' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(760000); printf( "-... " ) ; } else if( morse == 'C' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(760000); printf( "-.-. " ) ; } else if( morse == 'D' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(1200000); printf( "-.. " ) ; } else if( morse == 'E' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(2080000); printf( ". " ) ; } else if( morse == 'F' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(760000); printf( "..-. " ) ; } else if( morse == 'G' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(1200000); printf( "--. " ) ; } else if( morse == 'H' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(760000); printf( ".... " ) ; } else if( morse == 'I' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(1640000); printf( ".. " ) ; } else if( morse == 'J' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(600000); printf( ".--- " ) ; } else if( morse == 'K' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(1040000); printf( "-.- " ) ; } else if( morse == 'L' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(760000); printf( ".-.. " ) ; } else if( morse == 'M' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(600000); printf( "-- " ) ; } else if( morse == 'N' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(1640000); printf( "-. " ) ; } else if( morse == 'P' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(760000); printf( ".--. " ) ; } else if( morse == 'Q' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(600000); printf( "--.- " ) ; } else if( morse == 'R' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(1200000); printf( ".-. " ) ; } else if( morse == 'S' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(1200000); printf( "... " ) ; } else if( morse == 'T' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(1920000); printf( "- " ) ; } else if( morse == 'U' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(1040000); printf( "..- " ) ; } else if( morse == 'V' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(600000); printf( "...- " ) ; } else if( morse == 'W' ) { GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(3600000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(1040000); printf( ".-- " ) ; } else if( morse == 'X' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(600000); printf( "-..- " ) ; } else if( morse == 'Y' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(600000); printf( "-.-- " ) ; } else if( morse == 'Z' ) { GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(240000); GPIO_CLR = 1 << 23; usleep(200000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(360000); GPIO_SET = 1 << 23; usleep(80000); GPIO_CLR = 1 << 23; usleep(760000); printf( "--.. " ) ; } scanf( "%c", &morse ); } printf( "\n" ); return 0; }
On this day we tried to work on our project but went nowhere with the latency issue
It is seeming that we will never get this fully working
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
1 - 230
on this day I worked on inventorying the lair computer equipment, checking to see if the compters had ram, HD, and whether they worked or not
1 - 230
on this day I continued working on the inventory project
5 to 7
on this day I started to install virtual box and ubuntu so I could have a linux virtual machine on my laptop, took a little while to set up the virtualbox specs
1 to 3
on this day I helped charlotte with arranging equipment in the lair
1 to 230
on this day I continued to assist charlotte with the equipment in the lair
2 - 4
on this day I started to work on inventory in the cage, wow what a mess
1 - 330
On this day I continued the lair cage inventory, slpwly but surely getting it done
1 to 3
on this day I continued in the cage, its very overwhelming but am making some progress
2 - 4
on this day I went back into the cage, I really have no idea what I am going to do with all this stuff…
4 -5
on this day I started to work on my tetris game tutorial.
first lesson: Defines.h
this is where we define all of the aspects of the game like the window, game speed, levels, points, pieces, etc.
on this day I continued with the tutorial, its alot of information to take in but Im starting to get the grasp
5 to 7
on this day I finished the defines.h file
#pragma once // Window related defines // #define WINDOW_WIDTH 300 #define WINDOW_HEIGHT 400 #define WINDOW_CAPTION "Falling Blocks" // Game related defines // #define FRAMES_PER_SECOND 30 #define FRAME_RATE 1000/FRAMES_PER_SECOND #define GAME_AREA_LEFT 53 #define GAME_AREA_RIGHT 251 #define GAME_AREA_BOTTOM 298 #define NUM_LEVELS 5 #define POINTS_PER_LINE 525 #define POINTS_PER_LEVEL 6300 #define INITIAL_SPEED 60 #define SPEED_CHANGE 10 #define SLIDE_TIME 15 #define SQUARES_PER_ROW 10 #define SQUARE_MEDIAN 10 // Starting position of the focus block // #define BLOCK_START_X 151 #define BLOCK_START_Y 59 // Location on game screen for displaying... // #define LEVEL_RECT_X 42 // current level #define LEVEL_RECT_Y 320 #define SCORE_RECT_X 42 // current score #define SCORE_RECT_Y 340 #define NEEDED_SCORE_RECT_X 42 // score needed for next level #define NEEDED_SCORE_RECT_Y 360 #define NEXT_BLOCK_CIRCLE_X 214 // next block in line to be focus block #define NEXT_BLOCK_CIRCLE_Y 347 // Locations within bitmap of background screens // #define LEVEL_ONE_X 0 #define LEVEL_ONE_Y 0 #define LEVEL_TWO_X 300 #define LEVEL_TWO_Y 0 #define LEVEL_THREE_X 300 #define LEVEL_THREE_Y 0 #define LEVEL_FOUR_X 0 #define LEVEL_FOUR_Y 396 #define LEVEL_FIVE_X 300 #define LEVEL_FIVE_Y 396 // Location within bitmap of colored squares // #define RED_SQUARE_X 600 #define RED_SQUARE_Y 400 #define PURPLE_SQUARE_X 620 #define PURPLE_SQUARE_Y 400 #define GREY_SQUARE_X 640 #define GREY_SQUARE_Y 400 #define BLUE_SQUARE_X 660 #define BLUE_SQUARE_Y 400 #define GREEN_SQUARE_X 680 #define GREEN_SQUARE_Y 400 #define BLACK_SQUARE_X 700 #define BLACK_SQUARE_Y 400 #define YELLOW_SQUARE_X 720 #define YELLOW_SQUARE_Y 400
3 to 4
on this day I started on the enums.h file
this initializes the block types and the dirctions of the blocks
#pragma once enum BlockType { SQUARE_BLOCK, T_BLOCK, L_BLOCK, BACKWARDS_L_BLOCK, STRAIGHT_BLOCK, S_BLOCK, BACKWARDS_S_BLOCK }; enum Direction { LEFT, RIGHT, DOWN };
5 to 6
on this day I worked on the cSquare.h file, this is to individualize the blocks within the shapes so its easier for the collision detection
5 to 7
on this day I continued with the cSquare.h file, getting pretty close to done with it, I am getting a good understanding of the information
530 to 7
on this day I finished cBlock.h file
#pragma once #include "cSquare.h" class cBlock { private: int m_CenterX; int m_CenterY; BlockType m_Type; cSquare* m_Squares[4]; public: cBlock(int x, int y, SDL_Surface* bitmap, BlockType type) : m_CenterX(x), m_CenterY(y), m_Type(type) { for (int i=0; i<4; i++) { m_Squares[i] = NULL; } SetupSquares(x, y, bitmap); } void SetupSquares(int x, int y, SDL_Surface* bitmap) { m_CenterX = x; m_CenterY = y; // Make sure that any current squares are deleted // for (int i=0; i<4; i++) { if (m_Squares[i]) delete m_Squares[i]; } switch (m_Type) { case SQUARE_BLOCK: { m_Squares[0] = new cSquare(x - SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); m_Squares[1] = new cSquare(x - SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); m_Squares[2] = new cSquare(x + SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); // Lower right // m_Squares[3] = new cSquare(x + SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); } break; case T_BLOCK: { // Top // m_Squares[0] = new cSquare(x + SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); // Middle // m_Squares[1] = new cSquare(x + SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); // Left // m_Squares[2] = new cSquare(x - SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); // Right // m_Squares[3] = new cSquare(x + (SQUARE_MEDIAN * 3), y + SQUARE_MEDIAN, bitmap, m_Type); } break; case L_BLOCK: { m_Squares[0] = new cSquare(x - SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); m_Squares[1] = new cSquare(x - SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); m_Squares[2] = new cSquare(x - SQUARE_MEDIAN, y + (SQUARE_MEDIAN * 3), bitmap, m_Type); m_Squares[3] = new cSquare(x + SQUARE_MEDIAN, y + (SQUARE_MEDIAN * 3), bitmap, m_Type); } break; case BACKWARDS_L_BLOCK: { // | // m_Squares[0] = new cSquare(x + SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); // | // m_Squares[1] = new cSquare(x + SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); // _| // m_Squares[2] = new cSquare(x + SQUARE_MEDIAN, y + (SQUARE_MEDIAN * 3), bitmap, m_Type); // __ // m_Squares[3] = new cSquare(x - SQUARE_MEDIAN, y + (SQUARE_MEDIAN * 3), bitmap, m_Type); } break; case STRAIGHT_BLOCK: { // Top // m_Squares[0] = new cSquare(x + SQUARE_MEDIAN, y - (SQUARE_MEDIAN * 3), bitmap, m_Type); m_Squares[1] = new cSquare(x + SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); m_Squares[2] = new cSquare(x + SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); m_Squares[3] = new cSquare(x + SQUARE_MEDIAN, y + (SQUARE_MEDIAN * 3), bitmap, m_Type); // Bottom // } break; case S_BLOCK: { // Top right // m_Squares[0] = new cSquare(x + (SQUARE_MEDIAN * 3), y - SQUARE_MEDIAN, bitmap, m_Type); // Top middle // m_Squares[1] = new cSquare(x + SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); // Bottom middle // m_Squares[2] = new cSquare(x + SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); // Bottom left // m_Squares[3] = new cSquare(x - SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); } break; case BACKWARDS_S_BLOCK: { // Top left // m_Squares[0] = new cSquare(x - SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); // Top middle // m_Squares[1] = new cSquare(x + SQUARE_MEDIAN, y - SQUARE_MEDIAN, bitmap, m_Type); // Bottom middle // m_Squares[2] = new cSquare(x + SQUARE_MEDIAN, y + SQUARE_MEDIAN, bitmap, m_Type); // Bottom right // m_Squares[3] = new cSquare(x + (SQUARE_MEDIAN * 3), y + SQUARE_MEDIAN, bitmap, m_Type); } break; } } void Draw(SDL_Surface* Window) { for (int i=0; i<4; i++) { m_Squares[i]->Draw(Window); } } void Move(Direction dir) { switch (dir) { case LEFT: { m_CenterX -= SQUARE_MEDIAN * 2; } break; case RIGHT: { m_CenterX += SQUARE_MEDIAN * 2; } break; case DOWN: { m_CenterY += SQUARE_MEDIAN*2; } break; } for (int i=0; i<4; i++) { m_Squares[i]->Move(dir); } } void Rotate() { int x1, y1, x2, y2; for (int i=0; i<4; i++) { x1 = m_Squares[i]->GetCenterX(); y1 = m_Squares[i]->GetCenterY(); x1 -= m_CenterX; y1 -= m_CenterY; x2 = - y1; y2 = x1; x2 += m_CenterX; y2 += m_CenterY; m_Squares[i]->SetCenterX(x2); m_Squares[i]->SetCenterY(y2); } } int* GetRotatedSquares() { int* temp_array = new int[8]; int x1, y1, x2, y2; for (int i=0; i<4; i++) { x1 = m_Squares[i]->GetCenterX(); y1 = m_Squares[i]->GetCenterY(); x1 -= m_CenterX; y1 -= m_CenterY; x2 = - y1; y2 = x1; x2 += m_CenterX; y2 += m_CenterY; temp_array[i*2] = x2; temp_array[i*2+1] = y2; } return temp_array; } cSquare** GetSquares() { return m_Squares; } };
2 - 4
cBock.h cBlock class will store its center, type, and the four squares that it's built from.
SetupSquares() initializes the locations of the block's squares with respect to its center.
cBlock's Draw() method simply calls the Draw() methods of its squares. Its Move() method just changes its center variables and then calls its squares' Move() methods.
5 - 7
on this day I continued the cBlocks.h file
10pm to 12am on this day I finished the cSquare.h file
#pragma once #include "Enums.h" #include "Defines.h" class cSquare { private: int m_CenterX; int m_CenterY; BlockType m_BlockType; SDL_Surface* m_Bitmap; public: cSquare() { } cSquare(int x, int y, SDL_Surface* bitmap, BlockType type) : m_CenterX(x), m_CenterY(y), m_Bitmap(bitmap), m_BlockType(type) { } void Draw(SDL_Surface* window) { SDL_Rect source; switch (m_BlockType) { case SQUARE_BLOCK: { SDL_Rect temp = { RED_SQUARE_X, RED_SQUARE_Y, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; source = temp; } break; case T_BLOCK: { SDL_Rect temp = { PURPLE_SQUARE_X, PURPLE_SQUARE_Y, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; source = temp; } break; case L_BLOCK: { SDL_Rect temp = { GREY_SQUARE_X, GREY_SQUARE_Y, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; source = temp; } break; case BACKWARDS_L_BLOCK: { SDL_Rect temp = { BLUE_SQUARE_X, BLUE_SQUARE_Y, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; source = temp; } break; case STRAIGHT_BLOCK: { SDL_Rect temp = { GREEN_SQUARE_X, GREEN_SQUARE_Y, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; source = temp; } break; case S_BLOCK: { SDL_Rect temp = { BLACK_SQUARE_X, BLACK_SQUARE_Y, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; source = temp; } break; case BACKWARDS_S_BLOCK: { SDL_Rect temp = { YELLOW_SQUARE_X, YELLOW_SQUARE_Y, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; source = temp; } break; } SDL_Rect destination = { m_CenterX - SQUARE_MEDIAN, m_CenterY - SQUARE_MEDIAN, SQUARE_MEDIAN * 2, SQUARE_MEDIAN * 2 }; SDL_BlitSurface(m_Bitmap, &source, window, &destination); } void Move(Direction dir) { switch (dir) { case LEFT: { m_CenterX -= SQUARE_MEDIAN * 2; } break; case RIGHT: { m_CenterX += SQUARE_MEDIAN * 2; } break; case DOWN: { m_CenterY += SQUARE_MEDIAN*2; } break; } } // Accessors // int GetCenterX() { return m_CenterX; } int GetCenterY() { return m_CenterY; } // Mutators // void SetCenterX(int x) { m_CenterX = x; } void SetCenterY(int y) { m_CenterY = y; } };
5 to 630
on this day I started the main function, this includes: collision detection, initialization of the screen, background, shape movement, etc
about 18 hours all togther
I have been working on and off on the main function and it is confusing as hell, so I am researching alot and finding alot of good things about the functions and why they are used but its coming along and will be done soon and of course the god damn collision detection is kicking my ass!! I am typing the code in manually.
5pm to 8pm
Glorious day, I have completed the main file and am ready to fly
#pragma comment(lib, "SDL.lib") #pragma comment(lib, "SDLmain.lib") #pragma comment(lib, "SDL_TTF.lib") #include <stack> // We'll use the STL stack to store our function pointers #include <vector> // An STL vector will store the squares that are not part of the focus block #include "time.h" // We use time(), located in "time.h", to seed our random generator #include "math.h" // We'll be using the abs() function located in "math.h" #include "SDL.h" // Main SDL header #include "SDL_TTF.h" // True Type Font header #include "Defines.h" // Our defines header #include "Enums.h" // Our enums header #include "cBlock.h" // Contains the class that represents a game block using namespace std; struct StateStruct { void (*StatePointer)(); }; // Global data // stack<StateStruct> g_StateStack; SDL_Surface* g_Bitmap = NULL; SDL_Surface* g_Window = NULL; SDL_Event g_Event; int g_Timer; integer cBlock* g_FocusBlock = NULL; cBlock* g_NextBlock = NULL; block vector<cSquare*> g_OldSquares; block int g_Score = 0; int g_Level = 1; int g_FocusBlockSpeed = INITIAL_SPEED; // Init and Shutdown functions // void Init(); void Shutdown(); // Functions to handle the states of the game // void Menu(); void Game(); void Exit(); void GameWon(); void GameLost(); void DrawBackground(); void ClearScreen(); void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB); void HandleMenuInput(); void HandleGameInput(); void HandleExitInput(); void HandleWinLoseInput(); bool CheckEntityCollisions(cSquare* square, Direction dir); bool CheckWallCollisions(cSquare* square, Direction dir); bool CheckEntityCollisions(cBlock* block, Direction dir); bool CheckWallCollisions(cBlock* block, Direction dir); bool CheckRotationCollisions(cBlock* block); void CheckWin(); void CheckLoss(); void HandleBottomCollision(); void ChangeFocusBlock(); int CheckCompletedLines(); int main(int argc, char **argv) { Init(); while (!g_StateStack.empty()) { g_StateStack.top().StatePointer(); } Shutdown(); return 0; } void Init() { SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER); g_Window = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_ANYFORMAT); SDL_WM_SetCaption(WINDOW_CAPTION, 0); g_Timer = SDL_GetTicks(); g_Bitmap = SDL_LoadBMP("data/FallingBlocks.bmp"); // Seed our random number generator // srand( time(0) ); // Initialize blocks and set them to their proper locations. // g_FocusBlock = new cBlock(BLOCK_START_X, BLOCK_START_Y, g_Bitmap, (BlockType)(rand()%7)); g_NextBlock = new cBlock(NEXT_BLOCK_CIRCLE_X, NEXT_BLOCK_CIRCLE_Y, g_Bitmap, (BlockType)(rand()%7)); StateStruct state; state.StatePointer = Exit; g_StateStack.push(state); state.StatePointer = Menu; g_StateStack.push(state); // Initialize the true type font library // TTF_Init(); } void Shutdown() { // Shutdown the true type font library // TTF_Quit(); // Free our surfaces // SDL_FreeSurface(g_Bitmap); SDL_FreeSurface(g_Window); cSquare** temp_array_1 = g_FocusBlock->GetSquares(); cSquare** temp_array_2 = g_NextBlock->GetSquares(); // Delete our blocks // delete g_FocusBlock; delete g_NextBlock; // Delete the temporary arrays of squares // for (int i=0; i<4; i++) { delete temp_array_1[i]; delete temp_array_2[i]; } // Delete the squares that are in the game area // for (int i=0; i<g_OldSquares.size(); i++) { delete g_OldSquares[i]; } // Tell SDL to shutdown and free any resources it was using // SDL_Quit(); } void Menu() { if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE ) { HandleMenuInput(); ClearScreen(); DisplayText("Start (G)ame", 120, 120, 12, 255, 255, 255, 0, 0, 0); DisplayText("(Q)uit Game", 120, 150, 12, 255, 255, 255, 0, 0, 0); SDL_UpdateRect(g_Window, 0, 0, 0, 0); g_Timer = SDL_GetTicks(); } } void Game() { static int force_down_counter = 0; static int slide_counter = SLIDE_TIME; if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE ) { HandleGameInput(); force_down_counter++; if (force_down_counter >= g_FocusBlockSpeed) { // Always check for collisions before moving anything // if ( !CheckWallCollisions(g_FocusBlock, DOWN) && !CheckEntityCollisions(g_FocusBlock, DOWN) ) { g_FocusBlock->Move(DOWN); // move the focus block force_down_counter = 0; // reset our counter } } if ( CheckWallCollisions(g_FocusBlock, DOWN) || CheckEntityCollisions(g_FocusBlock, DOWN) ) { slide_counter--; } else { slide_counter = SLIDE_TIME; } if (slide_counter == 0) { slide_counter = SLIDE_TIME; HandleBottomCollision(); } // Make sure nothing from the last frame is still drawn. // ClearScreen(); // Draw the background // DrawBackground(); // Draw the focus block and next block. // g_FocusBlock->Draw(g_Window); g_NextBlock->Draw(g_Window); // Draw the old squares. // for (int i=0; i < g_OldSquares.size(); i++) { g_OldSquares[i]->Draw(g_Window); } // Draw the text for the current level, score, and needed score. // char temp[256]; string score = "Score: "; itoa(g_Score, temp, 10); score.append( temp ); string nextscore = "Needed Score: "; itoa(g_Level*POINTS_PER_LEVEL, temp, 10); nextscore.append(temp); string level = "Level: "; itoa(g_Level, temp, 10); level.append(temp); DisplayText(score, SCORE_RECT_X, SCORE_RECT_Y, 8, 0, 0, 0, 255, 255, 255); DisplayText(nextscore, NEEDED_SCORE_RECT_X, NEEDED_SCORE_RECT_Y, 8, 0, 0, 0, 255, 255, 255); DisplayText(level, LEVEL_RECT_X, LEVEL_RECT_Y, 8, 0, 0, 0, 255, 255, 255); SDL_UpdateRect(g_Window, 0, 0, 0, 0); g_Timer = SDL_GetTicks(); } } void Exit() { if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE ) { HandleExitInput(); // Make sure nothing from the last frame is still drawn. // ClearScreen(); DisplayText("Quit Game (Y or N)?", 100, 150, 12, 255, 255, 255, 0, 0, 0); SDL_UpdateRect(g_Window, 0, 0, 0, 0); g_Timer = SDL_GetTicks(); } } // Display a victory message. // void GameWon() { if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE ) { HandleWinLoseInput(); ClearScreen(); DisplayText("You Win!!!", 100, 120, 12, 255, 255, 255, 0, 0, 0); DisplayText("Quit Game (Y or N)?", 100, 140, 12, 255, 255, 255, 0, 0, 0); SDL_UpdateRect(g_Window, 0, 0, 0, 0); g_Timer = SDL_GetTicks(); } } // Display a game over message. // void GameLost() { if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE ) { HandleWinLoseInput(); ClearScreen(); DisplayText("You Lose.", 100, 120, 12, 255, 255, 255, 0, 0, 0); DisplayText("Quit Game (Y or N)?", 100, 140, 12, 255, 255, 255, 0, 0, 0); SDL_UpdateRect(g_Window, 0, 0, 0, 0); g_Timer = SDL_GetTicks(); } } // This function draws the background // void DrawBackground() { SDL_Rect source; switch (g_Level) { case 1: { SDL_Rect temp = { LEVEL_ONE_X, LEVEL_ONE_Y, WINDOW_WIDTH, WINDOW_HEIGHT }; source = temp; } break; case 2: { SDL_Rect temp = { LEVEL_TWO_X, LEVEL_TWO_Y, WINDOW_WIDTH, WINDOW_HEIGHT }; source = temp; } break; case 3: { SDL_Rect temp = { LEVEL_THREE_X, LEVEL_THREE_Y, WINDOW_WIDTH, WINDOW_HEIGHT }; source = temp; } break; case 4: { SDL_Rect temp = { LEVEL_FOUR_X, LEVEL_FOUR_Y, WINDOW_WIDTH, WINDOW_HEIGHT }; source = temp; } break; case 5: { SDL_Rect temp = { LEVEL_FIVE_X, LEVEL_FIVE_Y, WINDOW_WIDTH, WINDOW_HEIGHT }; source = temp; } break; } SDL_Rect destination = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT }; SDL_BlitSurface(g_Bitmap, &source, g_Window, &destination); } void ClearScreen() { SDL_FillRect(g_Window, 0, 0); } void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB) { TTF_Font* font = TTF_OpenFont("arial.ttf", size); SDL_Color foreground = { fR, fG, fB}; // text color SDL_Color background = { bR, bG, bB }; // color of what's behind the text SDL_Surface* temp = TTF_RenderText_Shaded(font, text.c_str(), foreground, background); SDL_Rect destination = { x, y, 0, 0 }; SDL_BlitSurface(temp, NULL, g_Window, &destination); SDL_FreeSurface(temp); TTF_CloseFont(font); } void HandleMenuInput() { if ( SDL_PollEvent(&g_Event) ) { if (g_Event.type == SDL_QUIT) { while (!g_StateStack.empty()) { g_StateStack.pop(); } return; } // Handle keyboard input here // if (g_Event.type == SDL_KEYDOWN) { if (g_Event.key.keysym.sym == SDLK_ESCAPE) { g_StateStack.pop(); return; // this state is done, exit the function } // Quit // if (g_Event.key.keysym.sym == SDLK_q) { g_StateStack.pop(); return; // game is over, exit the function } // Start Game // if (g_Event.key.keysym.sym == SDLK_g) { StateStruct temp; temp.StatePointer = Game; g_StateStack.push(temp); return; // this state is done, exit the function } } } } void HandleGameInput() { static bool down_pressed = false; static bool left_pressed = false; static bool right_pressed = false; if ( SDL_PollEvent(&g_Event) ) { if (g_Event.type == SDL_QUIT) { while (!g_StateStack.empty()) { g_StateStack.pop(); } return; // game is over, exit the function } // Handle keyboard input here // if (g_Event.type == SDL_KEYDOWN) { if (g_Event.key.keysym.sym == SDLK_ESCAPE) { g_StateStack.pop(); return; // this state is done, exit the function } if (g_Event.key.keysym.sym == SDLK_UP) { // Check collisions before rotating // if (!CheckRotationCollisions(g_FocusBlock)) { g_FocusBlock->Rotate(); } } if (g_Event.key.keysym.sym == SDLK_LEFT) { left_pressed = true; } if (g_Event.key.keysym.sym == SDLK_RIGHT) { right_pressed = true; } if (g_Event.key.keysym.sym == SDLK_DOWN) { down_pressed = true; } } if (g_Event.type == SDL_KEYUP) { if (g_Event.key.keysym.sym == SDLK_LEFT) { left_pressed = false; } if (g_Event.key.keysym.sym == SDLK_RIGHT) { right_pressed = false; } if (g_Event.key.keysym.sym == SDLK_DOWN) { down_pressed = false; } } } if (down_pressed) { if ( !CheckWallCollisions(g_FocusBlock, DOWN) && !CheckEntityCollisions(g_FocusBlock, DOWN) ) { g_FocusBlock->Move(DOWN); } } if (left_pressed) { if ( !CheckWallCollisions(g_FocusBlock, LEFT) && !CheckEntityCollisions(g_FocusBlock, LEFT) ) { g_FocusBlock->Move(LEFT); } } if (right_pressed) { if ( !CheckWallCollisions(g_FocusBlock, RIGHT) && !CheckEntityCollisions(g_FocusBlock, RIGHT) ) { g_FocusBlock->Move(RIGHT); } } } void HandleExitInput() { if ( SDL_PollEvent(&g_Event) ) { if (g_Event.type == SDL_QUIT) { while (!g_StateStack.empty()) { g_StateStack.pop(); } return; } // Handle keyboard input here // if (g_Event.type == SDL_KEYDOWN) { if (g_Event.key.keysym.sym == SDLK_ESCAPE) { g_StateStack.pop(); return; // this state is done, exit the function } if (g_Event.key.keysym.sym == SDLK_y) { g_StateStack.pop(); return; // game is over, exit the function } if (g_Event.key.keysym.sym == SDLK_n) { StateStruct temp; temp.StatePointer = Menu; g_StateStack.push(temp); return; // this state is done, exit the function } } } } void HandleWinLoseInput() { if ( SDL_PollEvent(&g_Event) ) { // Handle user manually closing game window // if (g_Event.type == SDL_QUIT) { // While state stack isn't empty, pop // while (!g_StateStack.empty()) { g_StateStack.pop(); } return; } // Handle keyboard input here // if (g_Event.type == SDL_KEYDOWN) { if (g_Event.key.keysym.sym == SDLK_ESCAPE) { g_StateStack.pop(); return; } if (g_Event.key.keysym.sym == SDLK_y) { g_StateStack.pop(); return; } if (g_Event.key.keysym.sym == SDLK_n) { g_StateStack.pop(); StateStruct temp; temp.StatePointer = Exit; g_StateStack.push(temp); temp.StatePointer = Menu; g_StateStack.push(temp); return; } } } } bool CheckEntityCollisions(cSquare* square, Direction dir) { int distance = SQUARE_MEDIAN * 2; // Center of the given square // int centerX = square->GetCenterX(); int centerY = square->GetCenterY(); // Determine the location of the square after moving // switch (dir) { case DOWN: { centerY += distance; } break; case LEFT: { centerX -= distance; } break; case RIGHT: { centerX += distance; } break; } for (int i=0; i<g_OldSquares.size(); i++) { if ( ( abs(centerX - g_OldSquares[i]->GetCenterX() ) < distance ) && ( abs(centerY - g_OldSquares[i]->GetCenterY() ) < distance ) ) { return true; } } return false; } bool CheckEntityCollisions(cBlock* block, Direction dir) { cSquare** temp_array = block->GetSquares(); for (int i=0; i<4; i++) { if ( CheckEntityCollisions(temp_array[i], dir) ) return true; } return false; } bool CheckWallCollisions(cSquare* square, Direction dir) { // Get the center of the square // int x = square->GetCenterX(); int y = square->GetCenterY(); switch (dir) { case DOWN: { if ( (y + (SQUARE_MEDIAN*2)) > GAME_AREA_BOTTOM ) { return true; } else { return false; } } break; case LEFT: { if ( (x - (SQUARE_MEDIAN*2)) < GAME_AREA_LEFT ) { return true; } else { return false; } } break; case RIGHT: { if ( (x + (SQUARE_MEDIAN*2)) > GAME_AREA_RIGHT ) { return true; } else { return false; } } break; } return false; } bool CheckWallCollisions(cBlock* block, Direction dir) { cSquare** temp_array = block->GetSquares(); for (int i=0; i<4; i++) { if ( CheckWallCollisions(temp_array[i], dir) ) return true; } return false; } // Check for collisions when a block is rotated // bool CheckRotationCollisions(cBlock* block) { int* temp_array = block->GetRotatedSquares(); int distance = SQUARE_MEDIAN * 2; for (int i=0; i<4; i++) { // Check to see if the block will go out of bounds // if ( (temp_array[i*2] < GAME_AREA_LEFT) || (temp_array[i*2] > GAME_AREA_RIGHT) ) { delete temp_array; return true; } if ( temp_array[i*2+1] > GAME_AREA_BOTTOM ) { delete temp_array; return true; } for (int index=0; index<g_OldSquares.size(); index++) { if ( ( abs(temp_array[i*2] - g_OldSquares[index]->GetCenterX()) < distance ) && ( abs(temp_array[i*2+1] - g_OldSquares[index]->GetCenterY()) < distance ) ) { delete temp_array; return true; } } } delete temp_array; return false; } void HandleBottomCollision() { ChangeFocusBlock(); int num_lines = CheckCompletedLines(); if ( num_lines > 0 ) { g_Score += POINTS_PER_LINE * num_lines; if (g_Score >= g_Level * POINTS_PER_LEVEL) { g_Level++; CheckWin(); // check for a win after increasing the level g_FocusBlockSpeed -= SPEED_CHANGE; // shorten the focus blocks movement interval } } CheckLoss(); } void ChangeFocusBlock() { cSquare** square_array = g_FocusBlock->GetSquares(); // Add focus block squares to g_OldSquares // for (int i=0; i<4; i++) { g_OldSquares.push_back(square_array[i]); } delete g_FocusBlock; // delete the current focus block g_FocusBlock = g_NextBlock; // set the focus block to the next block g_FocusBlock->SetupSquares(BLOCK_START_X, BLOCK_START_Y, g_Bitmap); g_NextBlock = new cBlock(NEXT_BLOCK_CIRCLE_X, NEXT_BLOCK_CIRCLE_Y, g_Bitmap, (BlockType)(rand()%7)); } int CheckCompletedLines() { int squares_per_row[13]; for (int index=0; index<13; index++) squares_per_row[index] = 0; int row_size = SQUARE_MEDIAN * 2; int bottom = GAME_AREA_BOTTOM - SQUARE_MEDIAN; int top = bottom - 12 * row_size; int num_lines = 0; int row; for (int i=0; i<g_OldSquares.size(); i++) { row = (g_OldSquares[i]->GetCenterY() - top) / row_size; squares_per_row[row]++; } // Erase any full lines // for (int line=0; line<13; line++) { // Check for completed lines // if (squares_per_row[line] == SQUARES_PER_ROW) { num_lines++; for (int index=0; index<g_OldSquares.size(); index++) { if ( ( (g_OldSquares[index]->GetCenterY() - top) / row_size ) == line ) { delete g_OldSquares[index]; // delete the square g_OldSquares.erase(g_OldSquares.begin() + index); // remove it from the vector index--; // make sure we don't skip anything } } } } for (int index=0; index<g_OldSquares.size(); index++) { for (int line=0; line<13; line++) { // Determine if this row was filled // if (squares_per_row[line] == SQUARES_PER_ROW) { // If it was, get the location of it within the game area // row = (g_OldSquares[index]->GetCenterY() - top) / row_size; // Now move any squares above that row down one // if ( row < line ) { g_OldSquares[index]->Move(DOWN); } } } } return num_lines; } void CheckWin() { if (g_Level > NUM_LEVELS) { // Clear the old squares vector // for (int i=0; i<g_OldSquares.size(); i++) { delete g_OldSquares[i]; } g_OldSquares.clear(); // Reset score and level // g_Score = 0; g_Level = 1; // Pop all states // while (!g_StateStack.empty()) { g_StateStack.pop(); } // Push the victory state onto the stack // StateStruct win; win.StatePointer = GameWon; g_StateStack.push(win); } } void CheckLoss() { if ( CheckEntityCollisions(g_FocusBlock, DOWN) ) { for (int i=0; i<g_OldSquares.size(); i++) { delete g_OldSquares[i]; } g_OldSquares.clear(); // Reset score and level // g_Score = 0; g_Level = 1; // Pop all states // while (!g_StateStack.empty()) { g_StateStack.pop(); } StateStruct lose; lose.StatePointer = GameLost; g_StateStack.push(lose); } }
5 - 8
on this day I worked on setting up debian on my home desktop that I had gotten from matt, setup was a nightmare and updates were slow as hell. was having problems with the network and couldnt figure it out and then reazlized that it was the original installation that was giving me the problem
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.
As an aid, feel free to use the following questions to help you generate content for your entries:
I don't understand why it wouldn't paste correctly but here it is