2 Design and write your own space-invaders game. Control a gun si mouse over the
ID: 3708364 • Letter: 2
Question
2 Design and write your own space-invaders game. Control a gun si mouse over the enemy space-craft to zap the invaders. Invader space craft should be generated at random intervals and at random positions on the screen. Display on the screen the number of seconds remaining before the game finishes and the number of invader space-craft zapped. You might also allow different levels of difficulty. This problem gives you plenty of opportunity to experiment with sounds and graphics. Remember to use threads in this problem. ght by moving the 7M 4./ 6M A linked list is a sequence of nodes in which each node is linked or connected to the node following it. A named reference variable points to the first node in the linked list. A null reference is used to indicate the end of the linked list. Write a program to create a linked list of nonzero integer random numbers stored in disorder. Build a second linked list that contains the integers from the first linked list sorted into key order. Find the largest number in the first linked list and copy this to the second linked list. As each integer is used from the first linked list, delete it from the first linked list. Repeat the process until the first linked list is empty. Display the contents of the second linked list. (Note: You need to enter the integers in the linked list) 5. key 6M II nnlication for generating a Captcha. The captcha is a combination ofExplanation / Answer
Please find the code.
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// Structure for node
typedef struct node {
int key;// Integer data in node.
struct node *next;
// Next pointer in list of node.
}Node;
// Inserting at the begining of list in node.
void insert(Node **head, int data) {
Node *p = NULL;
// Check if head is null
if(*head == NULL) {
// Allocate head space and initialize it.
*head = (Node *) malloc(sizeof (Node));
(*head)->key = data;
(*head)->next = NULL;
}
else {
// head is not null.
// create new node.
p = (Node *) malloc(sizeof (Node));
// Assign data to key of new node.
p->key = data;
// make new node's next point to head.
p->next = *head;
// initialize head to new node.
*head = p;
}
}
// function to display nodes in link list per line
void display(Node *head) {
Node *p = head;
while(p != NULL) {
printf("%d ",p->key);
p = p->next;
}
}
// Function to get the sorted List according to the logic given in question.
void getSortedList(Node **head,Node **sortList) {
Node *p = *head, *q = NULL; //Initialization.
Node *toDelete = NULL; /*Pointer to delete node*/
// Pointer for prev node of deleting node. null if the deleted node is head node.
Node *prevDelete = NULL;
int max1; //Assuming positive integers
while(p!=NULL) { // iterate till all the elements in list are deleted.
q = NULL;
max1 = -1;
toDelete = prevDelete = NULL;
while(p != NULL) {
if(p->key > max1){ // Find the max number from list
max1 = p->key;
// mark it has to be deleted.
toDelete = p;
// get the previous node of to be deleted node.
prevDelete = q;
}
q = p;// keep prev node.
p = p->next;// increment current node.
}
insert(sortList, max1); // insert the max node in list.
if(prevDelete == NULL) { // if first node is deleted update the head
*head = toDelete->next;
}
else {// else update the prev node's next to point at to delete node's next node.
prevDelete->next = toDelete->next;
}
//printf("Deleting Node %d ", toDelete->key);
//Delete the node.
toDelete->next = NULL;
free(toDelete);
// Move back p to head pointer.
p = *head;
}
}
int main() {
srand(time(NULL)); // required for random number generation.
Node *head = NULL; // head of first list
Node *sortedList = NULL; // head of sorted list.
int i;
for(i = 0; i<10; i++) {
insert(&head, rand()%100); // inserting into list 10 random numbers.
}
// displaying the original list.
printf("Original list ");
display(head);
getSortedList(&head, &sortedList);
printf("Sorted list ");
display(sortedList); // Displaying sorted list.
return 0;
}
Sample output.
Original list
1
35
2
77
72
28
82
74
46
24
Sorted list
1
2
24
28
35
46
72
74
77
82
Thanks,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.