Pārlūkot izejas kodu

centering looks bad. maybe some spacing?

Josh Bicking 8 gadi atpakaļ
vecāks
revīzija
27f838828e
4 mainītis faili ar 106 papildinājumiem un 83 dzēšanām
  1. 24 12
      intro.c
  2. 65 54
      pass.c
  3. 13 13
      print.c
  4. 4 4
      print.h

+ 24 - 12
intro.c

@@ -23,13 +23,25 @@
 #include "print.h"
 #include "intro.h"
 
+
 /// Plays the introduction sequence. 
 ///
 void intro(){
     clear();
     SLEEP(250000);
     
-    slowPrint("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK", 0);
+    // Get the size of the terminal window. 
+    // The size of the game window is 53x22.
+    int BUFF_Y = 0, BUFF_X = 0;
+    int y, x;
+    getmaxyx(stdscr, y, x);
+
+    if(y > 22)
+        BUFF_Y = y/2 - 22/2;
+    if(x > 53)
+        BUFF_X = x/2 - 53/2;
+
+    slowPrint("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK", 0, BUFF_Y, BUFF_X);
 
     move(1, 0);
     refresh();
@@ -39,36 +51,36 @@ void intro(){
     refresh();
     SLEEP(1500000);
 
-    slowType("SET TERMINAL/INQUIRE", 2);
+    slowType("SET TERMINAL/INQUIRE", 2, BUFF_Y, BUFF_X);
 
-    slowPrint("RIT-V300", 4);
+    slowPrint("RIT-V300", 4, BUFF_Y, BUFF_X);
 
     mvprintw(6,0,"%c", '>');
     refresh();
     SLEEP(1500000);
-    slowType("SET FILE/PROTECTION=OWNER:RWED ACCOUNTS.F",6);
+    slowType("SET FILE/PROTECTION=OWNER:RWED ACCOUNTS.F",6, BUFF_Y, BUFF_X);
     
     mvprintw(7,0,"%c", '>');
     refresh();
     SLEEP(1500000);
-    slowType("SET HALT RESTART/MAINT",7);
+    slowType("SET HALT RESTART/MAINT",7, BUFF_Y, BUFF_X);
     
-    slowPrint("Initializing Robco Industries(TM) Boot Agent v2.3.0",9);
+    slowPrint("Initializing Robco Industries(TM) Boot Agent v2.3.0",9, BUFF_Y, BUFF_X);
 
-    slowPrint("RBIOS-4.02.08.00 53EE5.E7.E8",10);
+    slowPrint("RBIOS-4.02.08.00 53EE5.E7.E8",10, BUFF_Y, BUFF_X);
 
-    slowPrint("Copyright 2201-22-3 Robco Ind.",11);
+    slowPrint("Copyright 2201-22-3 Robco Ind.",11, BUFF_Y, BUFF_X);
 
-    slowPrint( "Uppermem: 64 KB",12);
+    slowPrint( "Uppermem: 64 KB",12, BUFF_Y, BUFF_X);
 
-    slowPrint("Root (5A8)",13);
+    slowPrint("Root (5A8)",13, BUFF_Y, BUFF_X);
 
-    slowPrint("Maintenance Mode",14);
+    slowPrint("Maintenance Mode",14, BUFF_Y, BUFF_X);
 
     mvprintw(16,0,"%c",'>');
     refresh();
     SLEEP(1500000);
-    slowType("RUN DEBUG/ACCOUNTS.F",16);
+    slowType("RUN DEBUG/ACCOUNTS.F",16, BUFF_Y, BUFF_X);
     move(16,0);
     refresh();
     SLEEP(50000);

+ 65 - 54
pass.c

@@ -47,15 +47,26 @@ static int getCharLoc(int y, int x){
 
 void pass(){
     
+    // Get the size of the terminal window. 
+    // The size of the game window is 53x22.
+    int BUFF_Y = 0, BUFF_X = 0;
+    int maxy, maxx;
+
+    getmaxyx(stdscr, maxy, maxx);
+
+    if(maxy > 22)
+        BUFF_Y = maxy/2 - 22/2;
+    if(maxx > 53)
+        BUFF_X = maxx/2 - 53/2;
     // Clear the screen
     erase();
 
     // Intro text
-    passPrint("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL",0);
+    passPrint("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL", 0, BUFF_Y, BUFF_X);
     
-    passPrint("ENTER PASSWORD NOW", 1);
+    passPrint("ENTER PASSWORD NOW", 1, BUFF_Y, BUFF_X);
 
-    passPrint("4 ATTEMPT(S) LEFT: * * * *", 3);
+    passPrint("4 ATTEMPT(S) LEFT: * * * *", 3, BUFF_Y, BUFF_X);
     
     // Generate the hex values on the left sides
     int arbHex;
@@ -144,7 +155,7 @@ void pass(){
         for(int j=0; j<12; j++){
             temp[j] = bigString[j+current];
         }
-        printChoices(arbHex,temp,i, OFFSET_LEFT);
+        printChoices(arbHex,temp,i, OFFSET_LEFT, BUFF_Y, BUFF_X);
         current = current + 12;
         arbHex = arbHex + 12;
     }
@@ -154,13 +165,13 @@ void pass(){
         for(int j=0; j<12; j++){
             temp[j] = bigString[j+current];
         }
-        printChoices(arbHex,temp,i, OFFSET_RIGHT);
+        printChoices(arbHex,temp,i, OFFSET_RIGHT, BUFF_Y, BUFF_X);
         current = current + 12;
         arbHex = arbHex + 12;
     }
     
     
-    mvprintw(21,40,"%c",'>');
+    mvprintw(BUFF_Y+21,BUFF_X+40,"%c",'>');
     move(5,7);
     char currentChar[12]; // Max length currentChar could be (total possible length of a bracket trick)
     currentChar[0] = (char)mvinch(5,7);
@@ -213,27 +224,27 @@ void pass(){
         getyx(stdscr,y,x);
         
         // Get allowances left
-        mvprintw(1,0,"                                 ");
-        mvprintw(3,0,"                              ");
+        mvprintw(BUFF_Y+1,BUFF_X+0,"                                 ");
+        mvprintw(BUFF_Y+3,BUFF_X+0,"                              ");
         switch(allowances){
-            case 1: mvprintw(3,0,"1 ATTEMPT(S) LEFT: *");
+            case 1: mvprintw(BUFF_Y+3,BUFF_X+0,"1 ATTEMPT(S) LEFT: *");
                     attron(A_BLINK);
-                    mvprintw(1,0,"!!! WARNING: LOCKOUT IMNINENT !!!");
+                    mvprintw(BUFF_Y+1,BUFF_X+0,"!!! WARNING: LOCKOUT IMNINENT !!!");
                     attroff(A_BLINK);
                     attron(A_BOLD);
                     break;
-            case 2: mvprintw(3,0,"2 ATTEMPT(S) LEFT: * *");
-                    mvprintw(1,0,"ENTER PASSWORD NOW");
+            case 2: mvprintw(BUFF_Y+3,BUFF_X+0,"2 ATTEMPT(S) LEFT: * *");
+                    mvprintw(BUFF_Y+1,BUFF_X+0,"ENTER PASSWORD NOW");
                     break;
-            case 3: mvprintw(3,0,"3 ATTEMPT(S) LEFT: * * *");
-                    mvprintw(1,0,"ENTER PASSWORD NOW");
+            case 3: mvprintw(BUFF_Y+3,BUFF_X+0,"3 ATTEMPT(S) LEFT: * * *");
+                    mvprintw(BUFF_Y+1,BUFF_X+0,"ENTER PASSWORD NOW");
                     break;
-            case 4: mvprintw(3,0,"4 ATTEMPT(S) LEFT: * * * *");
-                    mvprintw(1,0,"ENTER PASSWORD NOW");
+            case 4: mvprintw(BUFF_Y+3,BUFF_X+0,"4 ATTEMPT(S) LEFT: * * * *");
+                    mvprintw(BUFF_Y+1,BUFF_X+0,"ENTER PASSWORD NOW");
                     break;
             case 0: clear();
-                    mvprintw(10,20,"TERMINAL LOCKED");
-                    mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
+                    mvprintw(BUFF_Y+10,BUFF_X+20,"TERMINAL LOCKED");
+                    mvprintw(BUFF_Y+12,BUFF_X+12,"PLEASE CONTACT AN ADMINISTRATOR");
                     refresh();
                     SLEEP(3000000);
                     endwin();
@@ -249,10 +260,10 @@ void pass(){
             charCounter = 0;
             while(charCounter!=bracketLength+1){
                 currentChar[charCounter] = (char)mvinch(origy,charStart+charCounter);
-                mvprintw(origy,charStart+charCounter,"%c",(int)currentChar[charCounter]);
+                mvprintw(BUFF_Y+origy,BUFF_X+charStart+charCounter,"%c",(int)currentChar[charCounter]);
                 charCounter++;
             }
-            mvprintw(21,41,"            ",currentChar[0]);
+            mvprintw(BUFF_Y+21,BUFF_X+41,"            ",currentChar[0]);
             needsClearing = 0;
             move(y,origx);
         }
@@ -260,7 +271,7 @@ void pass(){
             charCounter = 0;
             while(charCounter!=wordLength){
                 currentChar[charCounter] = (char)mvinch(starty,startx);
-                mvprintw(starty,startx,"%c",currentChar[charCounter]);
+                mvprintw(BUFF_Y+starty,BUFF_X+startx,"%c",currentChar[charCounter]);
                 charCounter++;
                 startx++;
                 if(startx==19 || startx==39){
@@ -272,7 +283,7 @@ void pass(){
                     }
                 }
             }
-            mvprintw(21,41,"            ",currentChar[0]);
+            mvprintw(BUFF_Y+21,BUFF_X+41,"            ",currentChar[0]);
             needsClearingMultiLine = 0;
             move(y,x);
         }
@@ -300,7 +311,7 @@ void pass(){
                     charCounter = 0;
                     while(1){
                         currentChar[charCounter] = (char)mvinch(y,charStart+charCounter);
-                        mvprintw(y,charStart+charCounter,"%c",currentChar[charCounter]);
+                        mvprintw(BUFF_Y+y,BUFF_X+charStart+charCounter,"%c",currentChar[charCounter]);
                         if(currentChar[charCounter] == endBracket)
                             break;
                         charCounter++;
@@ -309,7 +320,7 @@ void pass(){
                     // Print the bracket trick to output
                     attron(A_BOLD);
                     for(i=0;i<=charCounter;i++)
-                        mvprintw(21,41+i,"%c",(int)currentChar[i]);
+                        mvprintw(BUFF_Y+21,BUFF_X+41+i,"%c",(int)currentChar[i]);
                     // Notify that highlighting will need to be cleared next move
                     needsClearing = 1;
                 }
@@ -318,7 +329,7 @@ void pass(){
                 (endBracket == '>' && currentChar[0]=='<') || 
                 (endBracket == ']' && currentChar[0]=='[') || 
                 (endBracket == '}' && currentChar[0]=='{'))){
-                mvprintw(21,41,"%c",currentChar[0]);
+                mvprintw(BUFF_Y+21,BUFF_X+41,"%c",currentChar[0]);
             }
         }
         // Check for letters
@@ -364,7 +375,7 @@ void pass(){
             charCounter = 0;
             while(charCounter!=wordLength){
                 currentChar[charCounter] = (char)mvinch(tempy,tempx);
-                mvprintw(tempy,tempx,"%c",currentChar[charCounter]);
+                mvprintw(BUFF_Y+tempy,BUFF_X+tempx,"%c",currentChar[charCounter]);
                 charCounter++;
                 tempx++;
                 if(tempx==19 || tempx==39){
@@ -380,13 +391,13 @@ void pass(){
                     // Print the word to output
                     attron(A_BOLD);
                     for(i=0;i<charCounter;i++)
-                        mvprintw(21,41+i,"%c",(int)currentChar[i]);
+                        mvprintw(BUFF_Y+21,BUFF_X+41+i,"%c",(int)currentChar[i]);
                     // Notify that highlighting will need to be cleared next move
                     needsClearingMultiLine = 1;
         }
         // Nothing was found, print current char
         else
-            mvprintw(21,41,"%c",currentChar[0]);
+            mvprintw(BUFF_Y+21,BUFF_X+41,"%c",currentChar[0]);
         
         move(origy,origx);
         refresh();
@@ -421,9 +432,9 @@ void pass(){
         if(keyPress==3)     // Ctrl-C
             exit(0);
         if(keyPress=='\n'){ // Enter
-            mvprintw(17,40,"              ");
-            mvprintw(18,40,"              ");
-            mvprintw(19,40,"              ");
+            mvprintw(BUFF_Y+17,BUFF_X+40,"              ");
+            mvprintw(BUFF_Y+18,BUFF_X+40,"              ");
+            mvprintw(BUFF_Y+19,BUFF_X+40,"              ");
             // If the char is a left bracket
             if(((currentChar[0]=='(') && currentCharContains(currentChar,')')) || 
                (currentChar[0]=='<' && currentCharContains(currentChar,'>')) || 
@@ -436,14 +447,14 @@ void pass(){
                 if(rand()%5==0){
                     // 20% chance of allowance replenish
                     sprintf(output,"Allowance   ");
-                    mvprintw(18,40,">");
+                    mvprintw(BUFF_Y+18,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(18,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+18,BUFF_X+41+i,"%c",output[i]);
                     }
                     sprintf(output,"replenished.");
-                    mvprintw(19,40,">");
+                    mvprintw(BUFF_Y+19,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(19,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+19,BUFF_X+41+i,"%c",output[i]);
                     }
                     allowances = 4;
                 }
@@ -490,7 +501,7 @@ void pass(){
                     tempx = startx;
                     tempy = starty;
                     while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
-                        mvprintw(tempy,tempx,"%c",'.');
+                        mvprintw(BUFF_Y+tempy,BUFF_X+tempx,"%c",'.');
                         bigString[getCharLoc(tempy,tempx)] = '.';
                         tempx++;
                         if(tempx==19 || tempx==39){
@@ -500,9 +511,9 @@ void pass(){
                     }
                     
                     sprintf(output,"Dud removed.");
-                    mvprintw(19,40,">");
+                    mvprintw(BUFF_Y+19,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(19,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+19,BUFF_X+41+i,"%c",output[i]);
                     }
             
                 }
@@ -515,29 +526,29 @@ void pass(){
                         rightLetters--;
                 }
                 if(rightLetters==WORD_SIZE){
-                    mvprintw(15,40,">");
+                    mvprintw(BUFF_Y+15,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(15,41+i,"%c",currentChar[i]);
+                        mvprintw(BUFF_Y+15,BUFF_X+41+i,"%c",currentChar[i]);
                     }
                     sprintf(output,"Exact match!");
-                    mvprintw(16,40,">");
+                    mvprintw(BUFF_Y+16,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(16,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+16,BUFF_X+41+i,"%c",output[i]);
                     }
                     sprintf(output,"Please wait ");
-                    mvprintw(17,40,">");
+                    mvprintw(BUFF_Y+17,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(17,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+17,BUFF_X+41+i,"%c",output[i]);
                     }
                     sprintf(output,"while system");
-                    mvprintw(18,40,">");
+                    mvprintw(BUFF_Y+18,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(18,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+18,BUFF_X+41+i,"%c",output[i]);
                     }
                     sprintf(output,"is accessed.");
-                    mvprintw(19,40,">");
+                    mvprintw(BUFF_Y+19,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(19,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+19,BUFF_X+41+i,"%c",output[i]);
                     }
                     refresh();
                     SLEEP(3000000);
@@ -552,19 +563,19 @@ void pass(){
                     
                 }
                 else{
-                    mvprintw(17,40,">");
+                    mvprintw(BUFF_Y+17,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(17,41+i,"%c",currentChar[i]);
+                        mvprintw(BUFF_Y+17,BUFF_X+41+i,"%c",currentChar[i]);
                     }
                     sprintf(output,"Entry denied");
-                    mvprintw(18,40,">");
+                    mvprintw(BUFF_Y+18,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(18,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+18,BUFF_X+41+i,"%c",output[i]);
                     }
                     sprintf(output,"%d/%d correct.",rightLetters,WORD_SIZE);
-                    mvprintw(19,40,">");
+                    mvprintw(BUFF_Y+19,BUFF_X+40,">");
                     for(i=0;i<12;i++){
-                        mvprintw(19,41+i,"%c",output[i]);
+                        mvprintw(BUFF_Y+19,BUFF_X+41+i,"%c",output[i]);
                     }
                     allowances--;
                 }

+ 13 - 13
print.c

@@ -21,12 +21,12 @@
 #include "print.h"
 #include "pass.h"
 
-void slowPrint(char arr[], int line){
+void slowPrint(char arr[], int line, int ybuf, int xbuf){
     for(int i=0; (unsigned long)i<strlen(arr); i++){  
         /* Print the current character in the current position. */
-        mvprintw(line,i,"%c",arr[i]);
+        mvprintw(ybuf+line, xbuf+i, "%c", arr[i]);
         /* Move the cursor to the next position */
-        move(line, i+1);
+        move(ybuf+line, xbuf+i+1);
         refresh();
         /* If any keyboard input was recieved, go directly to pass(), otherwise continue */
         if(kbhit()){
@@ -37,10 +37,10 @@ void slowPrint(char arr[], int line){
     return;
 }
 
-void slowType(char arr[], int line){
+void slowType(char arr[], int line, int ybuf, int xbuf){
     for(int i=0; (unsigned long)i<strlen(arr); i++){  
-        mvprintw(line,i+1,"%c",arr[i]);
-        move(line, i+2);
+        mvprintw(ybuf+line, xbuf+i, "%c", arr[i]);
+        move(ybuf+line, xbuf+i+2);
         refresh();
         if(kbhit()){
             pass();
@@ -50,10 +50,10 @@ void slowType(char arr[], int line){
     return;
 }
 
-void passPrint(char arr[], int line){
+void passPrint(char arr[], int line, int ybuf, int xbuf){
     for(int i=0; (unsigned long)i<strlen(arr); i++){  
-        mvprintw(line,i,"%c",arr[i]);
-        move(line, i+1);
+        mvprintw(ybuf+line, xbuf+i, "%c", arr[i]);
+        move(ybuf+line, xbuf+i+1);
         refresh();
         SLEEP(20000);
     }
@@ -71,11 +71,11 @@ int kbhit(){
     }
 }
 
-void printChoices(int hex, char arr[], int line, int offset){
-    mvprintw(line,offset,"0x%X", hex);
+void printChoices(int hex, char arr[], int line, int offset, int ybuf, int xbuf){
+    mvprintw(ybuf+line, xbuf+offset, "0x%X", hex);
     for(int i=0; i<12; i++)
-        mvprintw(line,7+offset+i,"%c",arr[i]);
-    move(line, 20+offset);
+        mvprintw(ybuf+line, xbuf+7+offset+i, "%c", arr[i]);
+    move(ybuf+line, xbuf+20+offset);
     refresh();
     SLEEP(30000);
 }

+ 4 - 4
print.h

@@ -5,14 +5,14 @@
 /// @param arr  string to print
 /// @param line  line on which to print the string
 ///
-void slowPrint(char arr[], int line);
+void slowPrint(char arr[], int line, int ybuf, int xbuf);
 
 
 /// Print characters onto the screen (player input).
 /// @param arr  string to print
 /// @param line  line on which to print the string
 ///
-void slowType(char arr[], int line);
+void slowType(char arr[], int line, int ybuf, int xbuf);
 
 
 /// Operates the same way as slowPrint, but cannot be interrupted 
@@ -20,7 +20,7 @@ void slowType(char arr[], int line);
 /// @param arr  string to print
 /// @param line  line on which to print the string
 /// 
-void passPrint(char arr[], int line);
+void passPrint(char arr[], int line, int ybuf, int xbuf);
 
 
 /// Checks if a key has been pressed. Used to skip the opening sequence and
@@ -37,7 +37,7 @@ int kbhit();
 /// @param line  line on which to print the string
 /// @param offset  used to determine which side of the screen to print on
 ///
-void printChoices(int hex, char arr[], int line, int offset);
+void printChoices(int hex, char arr[], int line, int offset, int ybuf, int xbuf);
 
 #endif // PRINT_H