/* iching.c MS-DOS version of April 27, 1986, in Microsoft C 3.00, by D. C. Oshel, based on the original CP/M version of 11/11/84, in Toolworks C/80 3.1. ICHING.C -------- This version of the I Ching program correctly simulates divination by the 3 coins method; that is, it develops the hexagram from the first line to the last, BOTTOM TO TOP, per detailed instructions in "On Consulting the Oracle," Appendix I, The I Ching or Book of Changes, by Richard Wilhelm (Cary F. Baynes, transl.), Bollingen Series XIX, Princeton University Press, 1950, 1967. IBMIO.H ------- For your greater amusement, this source code contains a number of interesting, fast, low-level C routines which call the ROM-BIOS video services, including: cls() clear screen, home cursor clreos() clear to end of screen clreol() clear to end of line gotoxy() position cursor hidecursor() like it says savescreen() save the screen restorescreen() put it back fancykey() simple software-cursor-with-busy uppercase-keyin with typeahead-buffer-flush routine There is also a truly random random number generator here: acg() is an additive congruential generator a la Knuth; the periods of such generators are extremely large if they exist. The setrandom() function uses the system clock (hundredth seconds) to "effectively" randomize the acg() sequence even further -- the result is, the next number to come out of the generator is a function of pure, human, "hands on" luck. Very nice for games and the I Ching. David C. Oshel Ames, Iowa 50010 */ #define USE_ACG 1 #include "ibmio.h" #define TITLE "The I CHING (or Book of Changes) ver. 4.2\n" /*====================================*/ /* I Ching texts */ /*====================================*/ char *name( n ) int n; { static char *hexname[] = { {"Ch'ien / The Creative"}, {"K'un / The Receptive"}, {"Chun / Difficulty at the Beginning"}, {"Meng / Youthful Folly"}, {"Hsu / Waiting (Nourishment)"}, {"Sung / Conflict"}, {"Shih / The Army"}, {"Pi / Holding Together [Union]"}, {"Hsiao Ch'u / The Taming Power of the Small"}, {"Lu / Treading [Conduct]"}, {"T'ai / Peace"}, {"P'i / Standstill [Stagnation]"}, {"T'ung Jen / Fellowship with Men"}, {"Ta Yu / Possession in Great Measure"}, {"Ch'ien / Modesty"}, {"Yu / Enthusiasm"}, {"Sui / Following"}, {"Ku / Work on What Has Been Spoiled [Decay]"}, {"Lin / Approach"}, {"Kuan / Contemplation (View)"}, {"Shih Ho / Biting Through"}, {"Pi / Grace"}, {"Po / Splitting Apart"}, {"Fu / Return (The Turning Point)"}, {"Wu Wang / Innocence (The Unexpected)"}, {"Ta Ch'u / The Taming Power of the Great"}, {"I / The Corners of the Mouth (Providing Nourishment)"}, {"Ta Kuo / Preponderance of the Great"}, {"K'an / The Abysmal (Water)"}, {"Li / The Clinging, Fire"}, {"Hsien / Influence (Wooing)"}, {"Heng / Duration"}, {"Tun / Retreat"}, {"Ta Chuang / The Power of the Great"}, {"Chin / Progress"}, {"Ming I / Darkening of the Light"}, {"Chia Jen / The Family [The Clan]"}, {"K'uei / Opposition"}, {"Chien / Obstruction"}, {"Hsieh / Deliverance"}, {"Sun / Decrease"}, {"I / Increase"}, {"Kuai / Breakthrough (Resoluteness)"}, {"Kou / Coming to Meet"}, {"Ts'ui / Gathering Together [Massing]"}, {"Sheng / Pushing Upward"}, {"K'un / Oppression (Exhaustion)"}, {"Ching / The Well"}, {"Ko / Revolution (Molting)"}, {"Ting / The Cauldron"}, {"Chen / The Arousing (Shock, Thunder)"}, {"Ken / Keeping Still, Mountain"}, {"Chien / Development (Gradual Progress)"}, {"Kuei Mei / The Marrying Maiden"}, {"Feng / Abundance [Fullness]"}, {"Lu / The Wanderer"}, {"Sun / The Gentle (The Penetrating, Wind)"}, {"Tui / The Joyous, Lake"}, {"Huan / Dispersion [Dissolution]"}, {"Chieh / Limitation"}, {"Chung Fu / Inner Truth"}, {"Hsiao Kuo / Preponderance of the Small"}, {"Chi Chi / After Completion"}, {"Wei Chi / Before Completion"} }; return ( hexname[ n ] ); } /* end: name */ judge1( n ) int n; { switch (n) { case 1: { printf("THE CREATIVE works sublime success.\n"); printf("Furthering through perseverance.\n"); break;} case 2: { printf("THE RECEPTIVE brings about sublime success.\n"); printf("Furthering through the perseverance of a mare.\n"); printf("If the superior man undertakes something and tries to lead,\n"); printf("He goes astray;\n"); printf("But if he follows, he finds guidance.\n"); printf("It is favorable to find friends in the west and south,\n"); printf("To forego friends in the east and north.\n"); printf("Quiet perseverance brings good fortune.\n"); break;} case 3: { printf("DIFFICULTY AT THE BEGINNING works supreme success.\n"); printf("Furthering through perseverance.\n"); printf("Nothing should be undertaken.\n"); printf("It furthers one to appoint helpers.\n"); break;} case 4: { printf("YOUTHFUL FOLLY has success.\n"); printf("It is not I who seek the young fool;\n"); printf("The young fool seeks me.\n"); printf("At first I inform him.\n"); printf("If he asks two or three times, it is importunity.\n"); printf("If he importunes, I give him no information.\n"); printf("Perseverance furthers.\n"); break;} case 5: { printf("WAITING. If you are sincere,\n"); printf("You have light and success.\n"); printf("Perseverance brings good fortune.\n"); printf("It furthers one to cross the great water.\n"); break;} case 6: { printf("CONFLICT. You are sincere\n"); printf("And are being obstructed.\n"); printf("A cautious halt halfway brings good fortune.\n"); printf("Going through to the end brings misfortune.\n"); printf("It furthers one to see the great man.\n"); printf("It does not further one to cross the great water.\n"); break;} case 7: { printf("THE ARMY. The army needs perseverance\n"); printf("And a strong man.\n"); printf("Good fortune without blame.\n"); break;} case 8: { printf("HOLDING TOGETHER brings good fortune.\n"); printf("Inquire of the oracle once again\n"); printf("Whether you possess sublimity, constancy, and perseverance;\n"); printf("Then there is no blame.\n"); printf("Those who are uncertain gradually join.\n"); printf("Whoever comes too late\n"); printf("Meets with misfortune.\n"); break;} case 9: { printf("THE TAMING POWER OF THE SMALL\n"); printf("Has success.\n"); printf("Dense clouds, no rain from our western region.\n"); break;} case 10: { printf("TREADING. Treading upon the tail of the tiger.\n"); printf("It does not bite the man. Success.\n"); break;} case 11: { printf("PEACE. The small departs,\n"); printf("The great approaches.\n"); printf("Good fortune. Success.\n"); break;} case 12: { printf("STANDSTILL. Evil people do not further\n"); printf("The perseverance of the superior man.\n"); printf("The great departs; the small approaches.\n"); break;} case 13: { printf("FELLOWSHIP WITH MEN in the open.\n"); printf("Success.\n"); printf("It furthers one to cross the great water.\n"); printf("The perseverance of the superior man furthers.\n"); break;} case 14: { printf("POSSESSION IN GREAT MEASURE.\n"); printf("Supreme success.\n"); break;} case 15: { printf("MODESTY creates success.\n"); printf("The superior man carries things through.\n"); break;} case 16: { printf("ENTHUSIASM. It furthers one to install helpers\n"); printf("And to set armies marching.\n"); break;} case 17: { printf("FOLLOWING has supreme success.\n"); printf("Perseverance furthers. No blame.\n"); break;} case 18: { printf("WORK ON WHAT HAS BEEN SPOILED\n"); printf("Has supreme success.\n"); printf("It furthers one to cross the great water.\n"); printf("Before the starting point, three days.\n"); printf("After the starting point, three days.\n"); break;} case 19: { printf("APPROACH has supreme success.\n"); printf("Perseverance furthers.\n"); printf("When the eighth month comes,\n"); printf("There will be misfortune.\n"); break;} case 20: { printf("CONTEMPLATION. The ablution has been made,\n"); printf("But not yet the offering.\n"); printf("Full of trust they look up to him.\n"); break;} case 21: { printf("BITING THROUGH has success.\n"); printf("It is favorable to let justice be administered.\n"); break;} case 22: { printf("GRACE has success.\nIn small matters\n"); printf("It is favorable to undertake something.\n"); break;} case 23: { printf("SPLITTING APART. It does not further one\nTo go anywhere.\n"); break;} case 24: { printf("RETURN. Success.\nGoing out and coming in without error.\n"); printf("Friends come without blame.\nTo and fro goes the way.\n"); printf("On the seventh day comes return.\n"); printf("It furthers one to have somewhere to go.\n"); break;} case 25: { printf("INNOCENCE. Supreme success.\nPerseverance furthers.\n"); printf("If someone is not as he should be,\nHe has misfortune,\n"); printf("And it does not further him\nTo undertake anything.\n"); break;} case 26: { printf("THE TAMING POWER OF THE GREAT.\nPerseverance furthers.\n"); printf("Not eating at home brings good fortune.\n"); printf("It furthers one to cross the great water.\n"); break;} case 27: { printf("THE CORNERS OF THE MOUTH.\n"); printf("Perseverance brings good fortune.\n"); printf("Pay heed to the providing of nourishment\n"); printf("And to what a man seeks\nTo fill his own mouth with.\n"); break;} case 28: { printf("PREPONDERANCE OF THE GREAT.\n"); printf("The ridgepole sags to the breaking point.\n"); printf("It furthers one to have somewhere to go.\nSuccess.\n"); break;} case 29: { printf("The Abysmal repeated.\n"); printf("If you are sincere, you have success in your heart,\n"); printf("And whatever you do succeeds.\n"); break;} case 30: { printf("THE CLINGING. Perseverance furthers.\nIt brings success.\n"); printf("Care of the cow brings good fortune.\n"); break;} }} judge2( n ) int n; { switch (n) { case 31: { printf("Influence. Success.\nPerseverance furthers.\n"); printf("To take a maiden to wife brings good fortune.\n"); break;} case 32: { printf("DURATION. Success. No blame.\nPerseverance furthers.\n"); printf("It furthers one to have somewhere to go.\n"); break;} case 33: { printf("RETREAT. Success.\nIn what is small, perseverance furthers.\n"); break;} case 34: { printf("THE POWER OF THE GREAT. Perseverance furthers.\n"); break;} case 35: { printf("PROGRESS. The powerful prince\n"); printf("Is honored with horses in large numbers.\n"); printf("In a single day he is granted audience three times.\n"); break;} case 36: { printf("DARKENING OF THE LIGHT. In adversity\n"); printf("It furthers one to be persevering.\n"); break;} case 37: { printf("THE FAMILY. The perseverance of the woman furthers.\n"); break;} case 38: { printf("OPPOSITION. In small matters, good fortune.\n"); break;} case 39: { printf("OBSTRUCTION. The southwest furthers.\n"); printf("The northeast does not further.\n"); printf("It furthers one to see the great man.\n"); printf("Perseverance brings good fortune.\n"); break;} case 40: { printf("DELIVERANCE. The southwest furthers.\n"); printf("If there is no longer anything where one has to go,\n"); printf("Return brings good fortune.\n"); printf("If there is still something where one has to go,\n"); printf("Hastening brings good fortune.\n"); break;} case 41: { printf("DECREASE combined with sincerity\n"); printf("Brings about supreme good fortune\nWithout blame.\n"); printf("One may be persevering in this.\n"); printf("It furthers one to undertake something.\n"); printf("How is this to be carried out?\n"); printf("One may use two small bowls for the sacrifice.\n"); break;} case 42: { printf("INCREASE. It furthers one\nTo undertake something.\n"); printf("It furthers one to cross the great water.\n"); break;} case 43: { printf("BREAK-THROUGH. One must resolutely make the matter known\n"); printf("At the court of the king.\n"); printf("It must be announced truthfully. Danger.\n"); printf("It is necessary to notify one's own city.\n"); printf("It does not further to resort to arms.\n"); printf("It furthers one to undertake something.\n"); break;} case 44: { printf("COMING TO MEET. The maiden is powerful.\n"); printf("One should not marry such a maiden.\n"); break;} case 45: { printf("GATHERING TOGETHER. Success.\n"); printf("The king approaches his temple.\n"); printf("It furthers one to see the great man.\n"); printf("This brings success. Perseverance furthers.\n"); printf("To bring great offerings creates good fortune.\n"); printf("It furthers one to undertake something.\n"); break;} case 46: { printf("PUSHING UPWARD has supreme success.\n"); printf("One must see the great man.\nFear not.\n"); printf("Departure toward the south\nBrings good fortune.\n"); break;} }} judge3( n ) int n; { switch (n) { case 47: { printf("OPPRESSION. Success. Perseverance.\n"); printf("The great man brings about good fortune.\nNo blame.\n"); printf("When one has something to say,\nIt is not believed.\n"); break;} case 48: { printf("THE WELL. The town may be changed,\n"); printf("But the well cannot be changed.\n"); printf("It neither decreases nor increases.\n"); printf("They come and go and draw from the well.\n"); printf("If one gets down almost to the water\n"); printf("And the rope does not go all the way,\n"); printf("Or the jug breaks, it brings misfortune.\n"); break;} case 49: { printf("REVOLUTION. On your own day\nYou are believed.\n"); printf("Supreme success,\nFurthering through perseverance.\n"); printf("Remorse disappears.\n"); break;} case 50: { printf("THE CAULDRON. Supreme good fortune.\nSuccess.\n"); break;} case 51: { printf("SHOCK brings success.\nShock comes -- oh, oh!\n"); printf("Laughing words -- ha, ha!\n"); printf("The shock terrifies for a hundred miles,\n"); printf("And he does not let fall the sacrificial spoon and chalice.\n"); break;} case 52: { printf("KEEPING STILL. Keeping his back still\n"); printf("So that he no longer feels his body.\n"); printf("He goes into his courtyard\nAnd does not see his people.\n"); printf("No blame.\n"); break;} case 53: { printf("DEVELOPMENT. The maiden\nIs given in marriage.\n"); printf("Good fortune.\nPerseverance furthers.\n"); break;} case 54: { printf("THE MARRYING MAIDEN.\nUndertakings bring misfortune.\n"); printf("Nothing that would further.\n"); break;} case 55: { printf("ABUNDANCE has success.\nThe king attains abundance.\n"); printf("Be not sad.\nBe like the sun at midday.\n"); break;} case 56: { printf("THE WANDERER. Success through smallness.\n"); printf("Perseverance brings good fortune\nTo the wanderer.\n"); break;} case 57: { printf("THE GENTLE. Success through what is small.\n"); printf("It furthers one to have somewhere to go.\n"); printf("It furthers one to see the great man.\n"); break;} case 58: { printf("THE JOYOUS. Success.\nPerseverance is favorable.\n"); break;} case 59: { printf("DISPERSION. Success.\nThe king approaches his temple.\n"); printf("It furthers one to cross the great water.\n"); printf("Perseverance furthers.\n"); break;} case 60: { printf("LIMITATION. Success.\n"); printf("Galling limitation must not be persevered in.\n"); break;} case 61: { printf("INNER TRUTH. Pigs and fishes.\nGood fortune.\n"); printf("It furthers one to cross the great water.\n"); printf("Perseverance furthers.\n"); break;} case 62: { printf("PREPONDERANCE OF THE SMALL. Success.\nPerseverance furthers.\n"); printf("Small things may be done; great things should not be done.\n"); printf("The flying bird brings the message:\nIt is not well to strive upward,\n"); printf("It is well to remain below.\nGreat good fortune.\n"); break;} case 63: { printf("AFTER COMPLETION. Success in small matters.\n"); printf("Perseverance furthers.\nAt the beginning good fortune,\n"); printf("At the end disorder.\n"); break;} case 64: { printf("BEFORE COMPLETION. Success.\n"); printf("But if the little fox, after nearly completing the crossing,\n"); printf("Gets his tail in the water,\nThere is nothing that would further.\n"); break;} } } judgement(n) int n; { judge1(n); judge2(n); judge3(n); } /* end: judgement */ /*=============================================*/ /* I Ching Simulation: 3 coins method */ /*=============================================*/ hexagram( num ) int num; { register int n; static char hexnumber[] = { 63, 0, 17, 34, 23, 58, 2, 16, 55, 59, 7, 56, 61, 47, 4, 8, 25, 38, 3, 48, 41, 37, 32, 1, 57, 39, 33, 30, 18, 45, 28, 14, 60, 15, 40, 5, 53, 43, 20, 10, 35, 49, 31, 62, 24, 6, 26, 22, 29, 46, 9, 36, 52, 11, 13, 44, 54, 27, 50, 19, 51, 12, 21, 42 }; for ( n = 0; n < 64; n++) if (num == (int) hexnumber[ n ]) break; return ( n ); } /* end: hexagram */ get_hexagram( c, m ) int *c, *m; { static int line, coin, hex[6], hex2[6]; for ( line = 0; line < 6; line++ ) { hex[line] = 0; for ( coin = 0; coin < 3; coin++ ) hex[line] += tosscoin(); switch ( hex[line] ) { case 6: { hex2[line] = 7; break; } case 7: { hex2[line] = 7; break; } case 8: { hex2[line] = 8; break; } case 9: { hex2[line] = 8; break; } } } *m = *c = 0; for ( line = 5; line >= 0; line--) { *m *= 2; *m += repose( hex2[line] ); *c *= 2; *c += repose( hex[line] ); switch ( hex[line] ) { case 6: { printf("อออxอออ\n"); break; } case 7: { printf("อออออออ\n"); break; } case 8: { printf("อออ อออ\n"); break; } case 9: { printf("อออoอออ\n"); break; } } } *c = hexagram( *c ); *m = hexagram( *m ); } /* end: get_hexagram */ repose(n) int n; { return (( n == 6 || n == 8) ? 0 : 1); } /* end: repose */ showhexagrams() { static int h1, h2; long ltime; cls(); gotoxy( 49, 0 ); time( <ime ); cprintf( "%s", ctime( <ime ) ); gotoxy(0,0); get_hexagram( &h1, &h2 ); printf("%d. %s\n\n",h1+1,name(h1)); judgement( h1 + 1 ); if (h1 != h2) { printf("\n(More) "); fancykey( clocktime ); gotoxy( 0,8 ); clreos(); printf("Changing to...\n%d. %s\n\n",h2+1,name(h2)); judgement( h2 + 1 ); return(1); } return(0); } /* end: showhexagrams */ /*===================================================*/ /* Here is Main */ /*===================================================*/ main() { static char q[] = "\nDo you wish to consult the I Ching? "; union REGS x; char *screensavebuffer; int n; vid_init(); screensavebuffer = (char *) malloc( 4000 ); savescreen( screensavebuffer, &x ); printf( TITLE ); if ( yes2( q, clocktime )) { n = showhexagrams(); while ( yes2( q, clocktime )) n = showhexagrams(); gotoxy( 0, n? 11: 8 ); clreos(); printf("FOR FURTHER INFORMATION, consult the Wilhelm translation\n"); printf("of The I Ching, Bollingen Series XIX, Princeton University\n"); printf("Press, 1950, 1967.\n\nI Ching Program by David C. Oshel\n"); printf("\nHit any key to continue "); fancykey( clocktime ); } restorescreen( screensavebuffer, &x ); free( screensavebuffer ); setcursize( 10,12 ); } /* end: main */