Browse Source

player and oard fixed

Unknown 1 year ago
parent
commit
dc15c3ba2b
8 changed files with 76 additions and 22 deletions
  1. BIN
      boggle
  2. 5 5
      game.c
  3. BIN
      game.o
  4. 62 14
      game_server.c
  5. 2 2
      game_server.h
  6. BIN
      game_server.o
  7. 7 1
      players.c
  8. BIN
      players.o

BIN
boggle


+ 5 - 5
game.c

@@ -13,9 +13,9 @@ int score;
 int word_score;
 int old_score;
 int skip;
-extern char board[4][4];
+//char board[4][4];
 
-char dice[16][6] = {
+/*char dice[16][6] = {
   {'R', 'I', 'F', 'O', 'B', 'X'},
   {'I', 'F', 'E', 'H', 'E', 'Y'},
   {'D', 'E', 'N', 'O', 'W', 'S'},
@@ -32,7 +32,7 @@ char dice[16][6] = {
   {'R', 'A', 'L', 'E', 'S', 'C'},
   {'U', 'W', 'I', 'L', 'R', 'G'},
   {'P', 'A', 'C', 'E', 'M', 'D'}
-};
+};*/
 char* actions[2] = {"quit", "logout"};
 char* messages[7] = {"Too Short", "Already Found", "Not on Board", "Not a Word", "Welcome to Boggle", "Hello Again", "New Round"};
 
@@ -42,7 +42,7 @@ void swap(int* i, int* j) {
   *j = temp;
 }
 
-void generate_board(){
+/*void generate_board(){
 	srand(time(0));
 	int filled[16];
 	for(int i = 0; i < 16; i++)
@@ -56,7 +56,7 @@ void generate_board(){
                         board[i][j] = dice[dicenumber][rand() % 6];
 			filled[dicenumber] = 1;
 		}
-}
+}*/
 
 /*void refresh_screen(int score, int bonus, char* prev) {
   system("clear");

BIN
game.o


+ 62 - 14
game_server.c

@@ -8,10 +8,45 @@ Client *client_list = NULL;
 
 short port = -1;
 
-extern char board[4][4];
+char board[4][4];
+
+char dice[16][6] = {
+  {'R', 'I', 'F', 'O', 'B', 'X'},
+  {'I', 'F', 'E', 'H', 'E', 'Y'},
+  {'D', 'E', 'N', 'O', 'W', 'S'},
+  {'U', 'T', 'O', 'K', 'N', 'D'},
+  {'H', 'M', 'S', 'R', 'A', 'O'},
+  {'L', 'U', 'P', 'E', 'T', 'S'},
+  {'A', 'C', 'I', 'T', 'O', 'A'},
+  {'Y', 'L', 'G', 'K', 'U', 'E'},
+  {'Q', 'B', 'M', 'J', 'O', 'A'},
+  {'E', 'H', 'I', 'S', 'P', 'N'},
+  {'V', 'E', 'T', 'I', 'G', 'N'},
+  {'B', 'A', 'L', 'I', 'Y', 'T'},
+  {'E', 'Z', 'A', 'V', 'N', 'D'},
+  {'R', 'A', 'L', 'E', 'S', 'C'},
+  {'U', 'W', 'I', 'L', 'R', 'G'},
+  {'P', 'A', 'C', 'E', 'M', 'D'}
+};
+
+void generate_board(){
+	srand(time(0));
+	int filled[16];
+	for(int i = 0; i < 16; i++)
+		filled[i] = 0;
+
+        for(int i = 0; i < 4; i++)
+                for(int j = 0; j < 4; j++){
+			int dicenumber = rand() % 16;
+			while(filled[dicenumber] == 1)
+				dicenumber = rand() % 16;
+                        board[i][j] = dice[dicenumber][rand() % 6];
+			filled[dicenumber] = 1;
+		}
+}
 
 int main(int argc, char* argv[]) {
-   // struct client *p;
+   	struct client *p;
 	if (argc > 1)
 		port = (short)(atoi(argv[1]));
 	else
@@ -34,37 +69,50 @@ int main(int argc, char* argv[]) {
 	tv.tv_usec = 0;
 	   
 	//start timer
-    alarm(TIMER_TICK);
-
+    	alarm(TIMER_TICK);
+	int maxfd;
+ 	Client * curr;
+	fd_set fdlist;
+	generate_board();
+	int rv;
+	
 	//TODO: implement select()
     while (1) {
-        fd_set fdlist; 
+        //fd_set fdlist; 
  	//maxfd = serv_fd;
-	int maxfd = listenfd+ 1;
+	maxfd = listenfd+ 1;
 	FD_ZERO(&fdlist);
 	FD_SET(listenfd, &fdlist);
 
-	Client * curr = client_list;
+	curr = client_list;
 	while (curr != NULL){
 		FD_SET(curr->fd, &fdlist);
 		if (curr->fd + 1 > maxfd)
 			maxfd = curr->fd;
+		curr = curr->next;
 	}
 
-	if (select(maxfd + 1, &fdlist, NULL, NULL, &tv) < 0) {
+	if ((rv =select(maxfd + 1, &fdlist, NULL, NULL, &tv)) < 0) {
 		perror("select");
-	} 
+	}
+	else if (rv == 0) { //timeout
+			fprintf(stderr, "Select() Timed Out\n");
+		} 
 	else {
 		curr = client_list;
-		while(curr)
-			if (FD_ISSET(curr->fd, &fdlist))
-				break;
+		while(curr){
+			if (FD_ISSET(curr->fd, &fdlist)){
+				p = curr;
+				receiveclient(p);		
+			}
+			curr= curr->next;
+		}
 		//if (p)
 		//	handle(p->fd); 
 		if (FD_ISSET(listenfd, &fdlist))
 			new_connection(maxfd);
 	}
-	tv.tv_sec = TIMER_TICK - 1;
+	tv.tv_sec = TIMER_TICK;
         //struct timeval t
         tv.tv_usec = 0;
     }
@@ -335,7 +383,7 @@ int do_command(struct client * p, int cmd_argc, char **cmd_argv) {
 	sendclient(p, "\r\n");
 	for (int i = 0; i < 4; i++) {
     		for (int j = 0; j < 4; j++) {
-			char str[2];
+			char str[3];
       			sprintf(str, "%c ", board[i][j]);
 			sendclient(p, str);
     		}

+ 2 - 2
game_server.h

@@ -26,7 +26,7 @@
 
 #define BUFFER_SIZE 1024
 
-extern char board[4][4];
+//extern char board[4][4];
 
 //Node for the linked list which stores currently connected clients
 typedef struct client {
@@ -106,6 +106,6 @@ int parse_line(Client *p);
 int do_command(struct client * p, int cmd_argc, char **cmd_argv);
 
 int find_network_newline(char *buf, int inbuf);
-void generate_board();
+//void generate_board();
 
 #endif

BIN
game_server.o


+ 7 - 1
players.c

@@ -9,13 +9,19 @@
  * of players whose head is pointed to by *player_ptr_add.
  */
 int create_player(const char *name, Player **player_ptr_add) {
-   	Player * p = malloc(sizeof(Player *));
+	if(find_player(name, *player_ptr_add))
+		return 1;   	
+	Player * p = calloc(1, sizeof(Player));
 	strcpy(p->name, name);
 	p->total_score = 0;
 	p->max_score = 0;
 	p->total_games = 0;
 	p->next = NULL;
 	Player * curr = *player_ptr_add;
+	if(curr == NULL){
+		*player_ptr_add = p;
+		return 0;
+	}
 	while(curr->next)
 		curr = curr->next;
 	curr->next = p;

BIN
players.o