Browse Source

task 1 done

Iman Anwarzai 1 year ago
parent
commit
57ff7e265e
11 changed files with 114 additions and 17 deletions
  1. 3 3
      Makefile
  2. 30 10
      board.c
  3. BIN
      board.o
  4. BIN
      boggle
  5. 5 0
      boggle.h
  6. BIN
      dictionaryChecker.o
  7. BIN
      scoreboard.o
  8. 71 0
      signals.c
  9. BIN
      signals.o
  10. BIN
      user.o
  11. 5 4
      users.txt

+ 3 - 3
Makefile

@@ -1,12 +1,12 @@
 CC = gcc
-CFLAGS = -g  -Wall -std=c99
+CFLAGS = -g  -Wall -std=gnu99
 DEPS = boggle.h
 
 %.o: %.c $(DEPS)
 	$(CC) -c -o $@ $< $(CFLAGS)
 
-boggle: board.o user.o scoreboard.o dictionaryChecker.o 
-	$(CC) $(CFLAGS)  -o boggle board.o user.o scoreboard.o dictionaryChecker.o
+boggle: board.o user.o scoreboard.o dictionaryChecker.o signals.o 
+	$(CC) $(CFLAGS)  -o boggle board.o user.o scoreboard.o dictionaryChecker.o signals.o
 
 clean:
 	rm boggle

+ 30 - 10
board.c

@@ -3,9 +3,16 @@
 #include <stdlib.h>
 #include <time.h>
 #include <ctype.h>
+#include <unistd.h>
 #include "boggle.h"
+//not working
+
+extern int signaled;
+extern int rounds;
 
 char dice[16][6]; 
+int rounds = 1;
+int signaled = 0;
 
 void freeall(int ** arr){
         for(int i = 0; i < 4; i++)
@@ -126,7 +133,7 @@ int main () {
         arr[1] = malloc(sizeof(char) * 4);
         arr[2] = malloc(sizeof(char) * 4);
         arr[3] = malloc(sizeof(char) * 4);
-
+	create_sigaction();
         createboard(arr);
 
         int score = 0;
@@ -150,12 +157,16 @@ int main () {
 	char ** words = malloc(sizeof(char*) * 50);
 	for(int i =0; i < 50;i++)
         	words[i] = malloc(17);
-	int wc = 0;	
+	int wc = 0;
+	alarm(TIME_INTERVAL);	
 	while(1){
                 display(arr);
-                printf("enter a word from the board, or n for the next board, or u to logout, or q to quit: ");
-                char * word = malloc(sizeof(char) * 16);
+                printf("ROUND %d\n", rounds);
+		printf("enter a word from the board, or u to logout, or q to quit:");
+                //alarm(60);
+		char * word = malloc(sizeof(char) * 16);
                 scanf("%s", word);
+		//printf("THE WORD IS %s\n", word);
 		if(strcmp(word, "q") == 0){
 			char * entered = malloc(2);
 			printf("are you sure you want to quit? press y or n\n");
@@ -169,25 +180,31 @@ int main () {
 			}
 			
 		}
-		else if(strcmp(word, "n") == 0){
-                  	char * entered = malloc(2);
+		else if(signaled == 1){ //IF TIMES UP
+                  	/*char * entered = malloc(2);
 			printf("are you sure you want to start a new session? press y or n\n");
 			scanf("%s", entered);	
 			if(strcmp(entered, "y") == 0){
 				system("clear");
 				printf("your current score: %d \n", score);
                         	displayScoreboard(head_s);
-	//			printf("scoreboard displayed properly\n");
 				createboard(arr);
-	//			printf("new board created\n");
 			}
-			free(entered);
+			free(entered);*/
+			system("clear");
+			printf("TIMES UP!\n");
+			rounds++;
+			displayScoreboard(head_s);
+			createboard(arr);
+			signaled = 0;
+			alarm(TIME_INTERVAL);
 		}
 		else if(strcmp(word, "u") == 0){
 			char * entered = malloc(2);
 			printf("are you sure you want to logout? press y or n\n");
 			scanf("%s", entered);
 			if(strcmp(entered, "y") == 0){
+				rounds = 1;
 				system("clear");
 				userUpdate(head, user, score);
 			//User * p = head;
@@ -208,7 +225,8 @@ int main () {
 				words = malloc(sizeof(char * ) * 50);
 				for(int i =0; i < 50;i++)
 					words[i] = malloc(17);
-	//			createboard(arr);
+				createboard(arr);
+				alarm(TIME_INTERVAL);
 			}
 			free(entered);
 		}
@@ -246,6 +264,7 @@ int main () {
 			else if(!isInDictionary(word)){
 				system("clear");
 				printf("that word is not in the dictionary\n");
+			//	printf("%s\n", word);
 			}
 			else{
 				system("clear");
@@ -255,6 +274,7 @@ int main () {
 		else{
 			system("clear");
 			printf("that word is too short\n");
+		//	printf("%s\n", word);
 		}
 	free(word);
 	}

BIN
board.o


BIN
boggle


+ 5 - 0
boggle.h

@@ -4,6 +4,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#define TIME_INTERVAL 10
+extern int rounds;
+extern int signaled;
 
 typedef struct user {
         char * name;
@@ -33,4 +36,6 @@ void freeLL_s(User_s * head);
 void displayScoreboard(User_s * head);
 int isInDictionary(char * word);
 int hasBeenUsed(char ** words, char * word);
+int create_sigaction();
+void createboard(char ** arr);
 #endif

BIN
dictionaryChecker.o


BIN
scoreboard.o


+ 71 - 0
signals.c

@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <ctype.h>
+#include <signal.h>
+#include <unistd.h>
+#include "boggle.h"
+
+extern int rounds;
+extern int signaled;
+
+void new_round (int sig) {
+        //system("clear");
+	//puts("...");
+	//system("clear");
+	//rounds++;
+	//puts("here\n");
+	signaled = 1;
+	alarm(TIME_INTERVAL);
+}
+
+
+int create_sigaction() {
+        //create sigaction variable
+	struct sigaction action;
+        action.sa_handler = new_round;
+        sigemptyset(&action.sa_mask);
+        action.sa_flags = 0;
+        //register new handling behaviour
+        sigaction(SIGALRM, &action, NULL);
+        //int i = 0;
+        return 0;
+} 
+
+/*char *name;
+
+void sing(int code) {
+    printf("Happy birthday to you,\n");
+    printf("Happy birthday to you,\n");
+    sleep(4);
+    printf("Happy birthday dear %s,\n", name);
+    printf("Happy birthday to you!\n");
+}
+
+int catch_signal(int sig, void (*handler)(int)) {
+    struct sigaction action;
+    action.sa_handler = handler;
+    sigemptyset(&action.sa_mask);
+    sigaddset (&action.sa_mask, SIGINT);
+    action.sa_flags = 0;
+    return sigaction (sig, &action, NULL);
+}
+
+int task3_blocking(int argc, char **argv) {
+    catch_signal(SIGUSR1, sing);
+    sigset_t block_set;
+    sigemptyset(&block_set);
+    sigaddset(&block_set, SIGUSR1);
+    for (;;) {
+        printf("Busy studying! Go away.\n");
+        // Don't be interrupted by SIGUSR1.
+        //         sigprocmask(SIG_BLOCK, &block_set, NULL);
+        //                 sleep(30);
+        //
+        //                         printf("Okay I can party now.\n");
+        //                                 sigprocmask(SIG_UNBLOCK, &block_set, NULL);
+        //                                         sleep(20);
+        //                                             }
+        //                                                 return 0;
+        //                                                 }*/

BIN
signals.o


BIN
user.o


+ 5 - 4
users.txt

@@ -1,7 +1,7 @@
-iman 9 145 96
+iman 9 169 101
 alena 4 8 7
 ella 7 15 10
-ale 2 11 4
+ale 2 12 5
 aya 0 3 0
 cayleigh 1 2 2
 athena 0 0 0
@@ -11,10 +11,11 @@ newuser 0 0 0
 anu 3 8 10
 ismael 0 1 0
 anushka 0 0 0
-marina 3 4 6
+marina 3 6 8
 stoney 2 1 2
 ben 0 7 0
 akarsh 1 1 1
-aaa 0 0 0
+aaa 0 1 0
 bbb 0 0 0
 aaaa 0 0 0
+ima 0 1 0