Browse Source

cleaned up comments. fixed an allocation bug in setWordArr

Joshua Bicking 8 years ago
parent
commit
69f1c5a290
6 changed files with 70 additions and 56 deletions
  1. 22 17
      intro.c
  2. 21 9
      main.c
  3. 10 10
      pass.c
  4. 7 11
      print.c
  5. 3 6
      print.h
  6. 7 3
      wordParse.c

+ 22 - 17
intro.c

@@ -1,13 +1,18 @@
-#define _BSD_SOURCE /* for unistd.h */
+// 
+// File: intro.c 
+// intro.c plays the introduction sequence
+// @author Josh Bicking <josh1147582>
+// // // // // // // // // // // // // // // // // // // // // // // 
 
+#define _BSD_SOURCE /* for unistd.h */
 #ifdef _WIN32
-#   include <Windows.h>
+// Windows implements sleep in Windows.h with Sleep(milliseconds)
 #   include <curses.h>
+#   include <Windows.h>
 #   define SLEEP(delay) Sleep(delay/1000)
-// Unix builds require ncurses.h for the Ncurses library.
-// Unix also requires unistd.h for usleep(microseconds).
-// usleep/1000 = Sleep
 #else
+// Unix requires unistd.h for usleep(microseconds).
+// usleep/1000 = Sleep
 #   include <ncurses.h>
 #   include <unistd.h>
 #   define SLEEP(delay) usleep(delay)
@@ -24,7 +29,7 @@ void intro(){
     clear();
     SLEEP(250000);
     
-    slowPrint("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK",strlen("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK"), 0);
+    slowPrint("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK", 0);
 
     move(1, 0);
     refresh();
@@ -34,36 +39,36 @@ void intro(){
     refresh();
     SLEEP(1500000);
 
-    slowType("SET TERMINAL/INQUIRE",strlen("SET TERMINAL/INQUIRE"), 2);
+    slowType("SET TERMINAL/INQUIRE", 2);
 
-    slowPrint("RIT-V300",strlen("RIT-V300"), 4);
+    slowPrint("RIT-V300", 4);
 
     mvprintw(6,0,"%c", '>');
     refresh();
     SLEEP(1500000);
-    slowType("SET FILE/PROTECTION=OWNER:RWED ACCOUNTS.F",strlen("SET FILE/PROTECTION=OWNER:RWED ACCOUNTS.F"),6);
+    slowType("SET FILE/PROTECTION=OWNER:RWED ACCOUNTS.F",6);
     
     mvprintw(7,0,"%c", '>');
     refresh();
     SLEEP(1500000);
-    slowType("SET HALT RESTART/MAINT",strlen("SET HALT RESTART/MAINT"),7);
+    slowType("SET HALT RESTART/MAINT",7);
     
-    slowPrint("Initializing Robco Industries(TM) Boot Agent v2.3.0",strlen("Initializing Robco Industries(TM) Boot Agent v2.3.0"),9);
+    slowPrint("Initializing Robco Industries(TM) Boot Agent v2.3.0",9);
 
-    slowPrint("RBIOS-4.02.08.00 53EE5.E7.E8",strlen("RBIOS-4.02.08.00 53EE5.E7.E8"),10);
+    slowPrint("RBIOS-4.02.08.00 53EE5.E7.E8",10);
 
-    slowPrint("Copyright 2201-22-3 Robco Ind.",strlen("Copyright 2201-22-3 Robco Ind."),11);
+    slowPrint("Copyright 2201-22-3 Robco Ind.",11);
 
-    slowPrint( "Uppermem: 64 KB",strlen("Uppermem: 64 KB"),12);
+    slowPrint( "Uppermem: 64 KB",12);
 
-    slowPrint("Root (5A8)",strlen("Root (5A8)"),13);
+    slowPrint("Root (5A8)",13);
 
-    slowPrint("Maintenance Mode",strlen("Maintenance Mode"),14);
+    slowPrint("Maintenance Mode",14);
 
     mvprintw(16,0,"%c",'>');
     refresh();
     SLEEP(1500000);
-    slowType("RUN DEBUG/ACCOUNTS.F",strlen("RUN DEBUG/ACCOUNTS.F"),16);
+    slowType("RUN DEBUG/ACCOUNTS.F",16);
     move(16,0);
     refresh();
     SLEEP(50000);

+ 21 - 9
main.c

@@ -1,15 +1,17 @@
 #ifdef _WIN32
+// Windows builds require curses.h for the PDcurses library.
 #   include <curses.h>
 #else
+// Unix builds require ncurses.h for the Ncurses library.
 #   include <ncurses.h>
 #endif
 
 #include <stdlib.h>
-#include <time.h>
-#include <string.h>
-#include "intro.h"
-#include "pass.h"
-#include "wordParse.h"
+#include <time.h> /* For making a random seed */
+#include <string.h> /* For strcmp, to identify argv[1] */
+#include "intro.h" /* To launch intro */
+#include "pass.h" /* To launch pass */
+#include "wordParse.h" /* To read the file */
 
 int main(int argc, char * argv[]){
 
@@ -29,10 +31,12 @@ int main(int argc, char * argv[]){
         exit(0);
     }
 
+    // Open the config file
     FILE *fp = NULL;
 
     fp = fopen("FalloutTerminal.cfg", "r");
 
+    // Check if a difficulty arg was given
     if(argc > 1){
         if(!strcmp(argv[1], "--veryEasy")) {
             setVeryEasy();
@@ -54,32 +58,40 @@ int main(int argc, char * argv[]){
             exit(EXIT_FAILURE);
         }
     }
+    // Otherwise, read the file for words
     else {    
         readWordsFromFile(fp);
     }
 
+    // Read what should be launch on completion/victory
     readLaunches(fp);
 
+    // Gen a random seed
     srand ( (unsigned)time(NULL) );
+
+    // Begin curses
     initscr();
     noecho();
     refresh();
     attron(A_BOLD);
     nodelay(stdscr, 1);
+
+    // Check for color support. Start color if it exists.
     if(has_colors() == 1){
-        /* Colors */
         start_color();
         init_pair(1,COLOR_GREEN,COLOR_BLACK);
         attron(COLOR_PAIR(1));
     }
 
-
-
+    // Run intro
     intro();
+
+    // Run pass
     pass();
 
+    // Close the config file
     fclose(fp);
 
-    return 0;
+    return EXIT_SUCCESS;
 }
 

+ 10 - 10
pass.c

@@ -1,12 +1,14 @@
-#define _BSD_SOURCE /* for unistd.h */
+// 
+// File: pass.c 
+// pass.c plays the password guessing game
+// @author Josh Bicking <josh1147582>
+// // // // // // // // // // // // // // // // // // // // // // // 
 
+#define _BSD_SOURCE /* for unistd.h */
 #ifdef _WIN32
 #   include <Windows.h>
 #   include <curses.h>
 #   define SLEEP(delay) Sleep(delay/1000)
-// Unix builds require ncurses.h for the Ncurses library.
-// Unix also requires unistd.h for usleep(microseconds).
-// usleep/1000 = Sleep
 #else
 #   include <ncurses.h>
 #   include <unistd.h>
@@ -61,13 +63,11 @@ void pass(){
     }
     
     /* Intro text */
-    char prompt[] = "ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL";
-    passPrint(prompt,strlen(prompt),0);
+    passPrint("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL",0);
     
-    char prompt2[] = "ENTER PASSWORD NOW";
-    passPrint(prompt2, strlen(prompt2), 1);
-    char prompt3[] = "4 ATTEMPT(S) LEFT: * * * *";
-    passPrint(prompt3, strlen(prompt3), 3);
+    passPrint("ENTER PASSWORD NOW", 1);
+
+    passPrint("4 ATTEMPT(S) LEFT: * * * *", 3);
     
     /* Generate the hex values on the left sides */
     int arbHex;

+ 7 - 11
print.c

@@ -5,14 +5,10 @@
 // // // // // // // // // // // // // // // // // // // // // // // 
 
 #define _BSD_SOURCE /* for unistd.h */
-
 #ifdef _WIN32
 #   include <Windows.h>
 #   include <curses.h>
 #   define SLEEP(delay) Sleep(delay/1000)
-// Unix builds require ncurses.h for the Ncurses library.
-// Unix also requires unistd.h for usleep(microseconds).
-// usleep/1000 = Sleep
 #else
 #   include <ncurses.h>
 #   include <unistd.h>
@@ -21,11 +17,12 @@
 
 #include <stdlib.h>
 #include <time.h>
+#include <string.h>
 #include "print.h"
 #include "pass.h"
 
-void slowPrint(char arr[], int size, int line){
-    for(int i=0; i<size; i++){  
+void slowPrint(char arr[], int line){
+    for(int i=0; (unsigned long)i<strlen(arr); i++){  
         /* Print the current character in the current position. */
         mvprintw(line,i,"%c",arr[i]);
         /* Move the cursor to the next position */
@@ -40,8 +37,8 @@ void slowPrint(char arr[], int size, int line){
     return;
 }
 
-void slowType(char arr[], int size, int line){
-    for(int i=0; i<size; i++){  
+void slowType(char arr[], int line){
+    for(int i=0; (unsigned long)i<strlen(arr); i++){  
         mvprintw(line,i+1,"%c",arr[i]);
         move(line, i+2);
         refresh();
@@ -53,9 +50,8 @@ void slowType(char arr[], int size, int line){
     return;
 }
 
-void passPrint(char arr[], int size, int line){
-    int i;
-    for(i=0; i<size; i++){  
+void passPrint(char arr[], int line){
+    for(int i=0; (unsigned long)i<strlen(arr); i++){  
         mvprintw(line,i,"%c",arr[i]);
         move(line, i+1);
         refresh();

+ 3 - 6
print.h

@@ -3,27 +3,24 @@
 
 /// Print characters onto the screen (not player input).
 /// @param arr  string to print
-/// @param size  size of the string
 /// @param line  line on which to print the string
 ///
-void slowPrint(char arr[], int size, int line);
+void slowPrint(char arr[], int line);
 
 
 /// Print characters onto the screen (player input).
 /// @param arr  string to print
-/// @param size  size of the string
 /// @param line  line on which to print the string
 ///
-void slowType(char arr[], int size, int line);
+void slowType(char arr[], int line);
 
 
 /// Operates the same way as slowPrint, but cannot be interrupted 
 /// by a key press.
 /// @param arr  string to print
-/// @param size  size of the string
 /// @param line  line on which to print the string
 /// 
-void passPrint(char arr[], int size, int line);
+void passPrint(char arr[], int line);
 
 
 /// Checks if a key has been pressed. Used to skip the opening sequence and

+ 7 - 3
wordParse.c

@@ -23,12 +23,13 @@ char * completeProg;
 
 void readWordsFromFile(FILE* fp){
 
+    // If there's no config file, default to very easy
     if(fp == NULL) {
         setVeryEasy();
         return;
     }
 
-    // Check if FalloutTerminal.cfg is invalid
+    // Check each line for valid words. Stop once launching is reached.
     char * buf;
     size_t n = 0;
     
@@ -92,6 +93,7 @@ void readWordsFromFile(FILE* fp){
         numWords++;
     }
 
+    // If words to choose wasn't specified or found, default to 7
     if(wordsToChoose == 0)
         wordsToChoose = 7; 
 
@@ -105,8 +107,10 @@ void readWordsFromFile(FILE* fp){
 }
 
 void readLaunches(FILE* fp){
+    // Rewind the file
     rewind(fp);
 
+    // If the file doesn't exist, stop
     if(fp == NULL){
         return;
     }
@@ -114,6 +118,7 @@ void readLaunches(FILE* fp){
     char * buf;
     size_t n = 0;
 
+    // Look or the parameters. Stop once LAUNCH_ON_COMPLETE is read.
     while(getline(&buf, &n, fp)){
 
         // Remove the \n at the end of buff
@@ -141,8 +146,7 @@ void setWordArr(char *words[]){
     wordArr = malloc(numWords * sizeof(char*));
 
     for(int i=0; i<numWords; i++) {
-            // TODO replace numWords with wordsize
-            *(wordArr+i) = malloc(sizeof(char) * numWords);
+            *(wordArr+i) = malloc(sizeof(char) * (strlen(words[0])+1));
             strcpy(*(wordArr+i), *(words+i));
     }