home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 2 BBS
/
02-BBS.zip
/
othl-111.zip
/
Othello.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-07-26
|
32KB
|
1,243 lines
///////////////////////////////////////////////////////////////////////////
// Othello.c
//
// Copyright (c) 1994 by John Morris All Rights Reserved
///////////////////////////////////////////////////////////////////////////
//
// This Othello door source code is meant to be an example of an OS/2
// type door which will run under Adept under menu option 802, or under
// GMon/2 using the OS/2 door redirection flag.
//
// Notice there is no special com routines used here! This file is the
// entire program!
//
// Under Adept and GMon/2 redirection, STDIN, STDOUT, and STDERR
// are redirected thru unnamed pipes to the parent program. The parent program
// then routes the information to the proper place (com port, or named pipe)
//
// When running the program from Adept or from GMon/2 you will see the output
// appear on the regular BBS screen.
//
// This program (through no fault of my own.. it appears to be a quirk of the
// Watcom C32 10.0 IO library) operates slightly differently when used from
// the OS/2 command line than from Adept or GMon/2 (IO is controlled more
// precisely from the latter two)
//
// I'll make more notes on the code added to make the program work more
// efficiently thru pipes.
//
// Please keep jokes about the game itself to yourself <grin>
//
// Compiler command:
//
// wcl386 /3r /w3 /l=os2v2 othello.c
//
// Sample Command line: (from an Adept menu)
//
// Othello d:\Adept\System\Line{LN\Session.Info
//
// command line contains 1 parm. The name of the Session.Info file!
//
///////////////////////////////////////////////////////////////////////////
#define INCL_DOS
#include <os2.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>
#include <share.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <time.h>
#include <malloc.h>
#include <math.h>
#include <myquirks.h> // my own C programming quirks
// Input defs
#define INPUTUCASE 0x00000001
#define TURBO 0x00000002
// ANSI colors defs..
#define BLACK 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define MAGENTA 5
#define CYAN 6
#define WHITE 7
///////////////////////////////////////////////////////////////////////////
// Global variables (begin with 'o')
static int oBlack = 0;
static int oBoard[10][10];
static int oWasBoard[10][10];
static char *oColumnLetters = "ABCDEFGH";
static int oComputer = 0;
static int oComputerPieces = 0;
static int oCornersSacrificed = 0;
static int oHandicap = 0;
static int oHandicapRecipient = 0;
static int oHuman = 0;
static int oHumanPieces = 0;
static int oMoveRow = 0;
static int oNextRow[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
static int oMoveCol = 0;
static int oNextCol[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
static int oSpacesTaken = 0;
static int oPiecesTaken = 0;
static char oPieceType[3][5];
static int oRed = 0;
static int oGoodGuy = 0;
static int oBadGuy = 0;
static int oZ = 0;
static int oANSIRow[8] = { 4, 6, 8, 10, 12, 14, 16, 18 };
static int oANSICol[8] = { 15, 21, 27, 33, 39, 45, 51, 57 };
static BOOL oBoardDisplayed = FALSE;
static BOOL oColor = FALSE;
static BOOL oBright = FALSE;
static int oForeColor = 7;
static int oBackColor = 0;
static char oTempString[512];
static int oArgLen = 0;
static int oArgValue = 0;
static char *Copyright = {"\n\rCopyright (c) 1994 by John Morris All Rights Reserved\n\r" };
///////////////////////////////////////////////////////////////////////////
// game functions
static int PlayGame(void);
static int PlayAgain(void);
static void TellPieces(void);
static int NextToOpponent(void);
static void TakePieces(BOOL ChangeBoard);
static void DisplayBoard(void);
static void WipeLine(int RowNumber);
static void AskHandicap(void);
static void HumanGetsHandicap(void);
static void AskCorners(void);
static void EndGame(void);
///////////////////////////////////////////////////////////////////////////
// IO procs
int YorN(char *YorNQuestion);
void OutPut(char *String, int NumLineFeeds);
int Input(char *Prompt, int Flags, int MaxLength);
void WipeLine(int Row);
void ANSIString(int ForeColor, int BackColor);
void BuffFile(char *FileToView);
ULONG Random(ULONG Low, ULONG High);
ULONG mult(ULONG p, ULONG q);
void Randomize(void);
float Zero2OneRand(void);
static void ReadSessionInfo(char *SessionInfoName);
void main(int ArgC, char **ArgV)
{
// Session.Info Filename should have been passed as the only
// command on the command line
if (ArgC > 1)
ReadSessionInfo(ArgV[1]);
/////////////////////////////////////////////
// no buffering on Std IO
//
// an important step. eliminate the compilers file IO
// buffering.. everything should go thru the pipes NOW!
// let the parent program buffer things.
//
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
/////////////////////////////////////////////
// set translation mode
//
// Also everything should be binary in nature
// make that is so.
//
setmode(fileno(stdin), O_BINARY);
setmode(fileno(stdout), O_BINARY);
setmode(fileno(stderr), O_BINARY);
// Run Othello
PgmMain();
return;
}
static void PgmMain(void)
{
int Neutral = 0;
Randomize();
// clear screen
fprintf(stderr, "\r\n " __DATE__ " " __TIME__);
fprintf(stderr, Copyright);
fprintf(stderr, "\r\n");
OutPut("\r\nO T H E L L O", 0);
oForeColor = YELLOW;
if (YorN("\r\n\r\nDo you require instructions ([Y]/N)? "))
{
oForeColor = CYAN;
OutPut("\r\nOTHELLO is played on an 8 X 8 checker board,", 1);
OutPut("rows numbered 1 to 8 and columns A to H.", 1);
OutPut("The initial configuration is all blank, except", 1);
OutPut("for the center four squares, which form the pattern", 1);
OutPut(" 0 X", 1);
OutPut(" X 0", 1);
OutPut("Place your piece so that it 'outflanks'", 1);
OutPut("mine, creating a horizontal, vertical, or diagonal", 1);
OutPut("run of my pieces bounded at each end by at least", 1);
OutPut("one of yours. This will 'flip' my pieces ", 1);
OutPut("converting them to yours. You must capture a", 1);
OutPut("piece if possible. If you have no legal move", 1);
OutPut("forfeit your turn by entering 0,0.", 2);
oForeColor = MAGENTA;
OutPut("Scoring is as follows:", 1);
OutPut("No handicap -- (X'S - O'S) * 10", 1);
OutPut("You take handicap -- ((X'S - O'S) * 10) - (80 * # corners taken)", 1);
OutPut("You give handicap -- ((X'S - O'S) * 10) + (2.5 * # corners given)", 1);
OutPut("If you quit before game's end, I'll deduct a 1000 points!", 2);
}
oBlack = -1;
Neutral = 0;
oRed = 1;
if (NOT oColor)
{
strcpy(oPieceType[oBlack + 1] , "X");
strcpy(oPieceType[Neutral + 1] , ".");
strcpy(oPieceType[oRed + 1] , "O");
}
do
{
if (NOT PlayGame())
break;
} while (PlayAgain());
EndGame();
}
static int PlayGame(void)
{
int LastPiecesTaken;
long GamePoints;
int Lasti;
int Lastj;
char *Column;
for (oMoveRow = 0; oMoveRow < 10; oMoveRow++)
{
for (oMoveCol = 0; oMoveCol < 10; oMoveCol++)
{
oBoard[oMoveRow][oMoveCol] = 0;
oWasBoard[oMoveRow][oMoveCol] = 0;
}
}
oBoard[4][4] = oRed;
oBoard[5][5] = oRed;
oBoard[4][5] = oBlack;
oBoard[5][4] = oBlack;
oComputerPieces = 2;
oHumanPieces = 2;
oSpacesTaken = 4;
oZ = FALSE;
oHandicap = 0;
oBoardDisplayed = FALSE;
oForeColor = GREEN;
sprintf(oTempString, "You are %s, and I am %s. Enter X if you want %s: ",
oColor ? "Red" : "O",
oColor ? "Black" : "X",
oColor ? "Black" : "X");
Input(oTempString, INPUTUCASE, 1);
oComputer = oBlack;
oHuman = oRed;
if (*oTempString == 'X')
{
oComputer = oRed;
oHuman = oBlack;
}
AskHandicap();
oForeColor = YELLOW;
if (YorN("Would you like to move first ([Y]/N)? "))
{
DisplayBoard();
HumanMove:
oGoodGuy = oHuman;
oBadGuy = oComputer;
AskMove:
do
{
do
{
oForeColor = WHITE;
if (oColor)
WipeLine(23);
sprintf(oTempString, "Your move, (row,column), %sF)orfeit, Q)uit? ", oColor ? "R)edraw, " : "");
Input(oTempString, INPUTUCASE, 4);
if (*oTempString == 'F')
goto IsForfeit;
if (*oTempString == 'Q')
{
if (oColor)
WipeLine(22);
OutPut("Chickened out, huh!?.", 1);
return (TRUE);
}
if (oColor AND *oTempString == 'R')
{
oBoardDisplayed = FALSE;
for (oMoveRow = 0; oMoveRow < 10; oMoveRow++)
{
for (oMoveCol = 0; oMoveCol < 10; oMoveCol++)
{
oWasBoard[oMoveRow][oMoveCol] = 0;
}
}
DisplayBoard();
goto AskMove;
}
if (NOT strchr(oTempString, ','))
{
oForeColor = RED;
if (NOT oColor)
OutPut("Row,Column with a comma...", 1);
goto AskMove;
}
if (oArgLen != 3 )
{
oForeColor = RED;
if (NOT oColor)
OutPut("Try again...", 1);
goto AskMove;
}
} while (oArgValue < 0 OR oArgValue > 8 );
oMoveRow = (int) oArgValue;
Column = strchr(oTempString, ',') + 1;
if (oMoveRow)
{
if (Column = strchr(oColumnLetters, *Column))
{
oMoveCol = (int) (Column - oColumnLetters) + 1;
// oMoveRow,oMoveCol is human move...
goto g540;
}
else
goto AskMove;
}
IsForfeit:
oForeColor = MAGENTA;
if (oColor)
WipeLine(23);
} while (NOT YorN("Are you forfeiting your turn ([Y]/N)? "));
if (NOT oZ)
{
oZ = TRUE;
goto ComputerMove;
}
}
else
{
ComputerMove:
LastPiecesTaken = -1;
Lasti = 0;
Lastj = 0;
oGoodGuy = oComputer;
oBadGuy = oHuman;
oMoveRow = 0;
if (oColor)
WipeLine(21);
do
{
oMoveRow++;
for (oMoveCol = 1; oMoveCol <= 8; oMoveCol++)
{
if (oBoard[oMoveRow][oMoveCol] == 0)
{
if (NextToOpponent())
{
TakePieces(FALSE);
if (oPiecesTaken != 0)
{
// computer likes corners....
if (((oMoveRow - 1) * (oMoveRow - 8)) == 0)
oPiecesTaken += 4;
if (((oMoveCol - 1) * (oMoveCol - 8)) == 0)
oPiecesTaken += 4;
// doesn't like corners inside corners
if (Zero2OneRand() > 0.32)
{
if (((oMoveRow - 2) * (oMoveRow - 7)) == 0)
oPiecesTaken >>= 1;
if (((oMoveCol - 2) * (oMoveCol - 7)) == 0)
oPiecesTaken >>= 1;
}
if (oPiecesTaken >= LastPiecesTaken)
{
LastPiecesTaken = oPiecesTaken;
Lasti = oMoveRow;
Lastj = oMoveCol;
}
}
}
}
}
} while (oMoveRow != 8);
if (LastPiecesTaken > 0)
{
oZ = FALSE;
oForeColor = MAGENTA;
if (oColor)
WipeLine(21);
sprintf(oTempString , "I will move to %d, %c", Lasti, oColumnLetters[Lastj - 1]);
// if (NOT oColor)
OutPut(oTempString, oColor ? 0 : 1);
DosSleep(1000);
oMoveRow = Lasti;
oMoveCol = Lastj;
TakePieces(TRUE);
oComputerPieces = oComputerPieces + oPiecesTaken + 1 ;
oHumanPieces -= oPiecesTaken;
oSpacesTaken++;
sprintf(oTempString , "That gives me %d of your pieces", oPiecesTaken);
if (NOT oColor)
OutPut(oTempString, 1);
oForeColor = CYAN;
TellPieces();
if (NOT oColor)
DosSleep(1000);
DisplayBoard();
if (oHumanPieces)
{
if (oSpacesTaken != 64)
goto HumanMove;
}
}
else
{
oForeColor = RED;
if (oColor)
WipeLine(22);
OutPut("I forfeit my move", 1);
if (NOT oZ)
{
oZ = TRUE;
goto HumanMove;
}
}
}
EndCheck:
OutPut("", 1);
TellPieces();
if (NOT oColor)
DosSleep(1000);
if (oColor)
{
WipeLine(21);
OutPut("\x1B[21;1H", 0);
}
if (oHumanPieces == oComputerPieces)
{
OutPut("The game is a draw!!", 1);
return (TRUE);
}
if (oHumanPieces > oComputerPieces)
{
OutPut("Great job! You win!!", 1);
}
else
{
OutPut("GOTCHA! I won that one", 1);
}
GamePoints = ((oHumanPieces - oComputerPieces + oHandicap) * 100);
sprintf(oTempString , "You earned %ld points for that game", GamePoints);
if (NOT oColor)
OutPut(oTempString, 1);
return (TRUE);
g540:
if (oBoard[oMoveRow][oMoveCol])
{
oForeColor = WHITE;
if (oColor)
WipeLine(22);
OutPut("Sorry that square is occupied. Try again!", 1);
DosSleep(1000);
goto AskMove;
}
if (NOT NextToOpponent())
{
oForeColor = RED;
if (oColor)
WipeLine(22);
OutPut("Sorry you are not next to one of my pieces. Try again!", 1);
DosSleep(1000);
goto AskMove;
}
TakePieces(FALSE);
if (oPiecesTaken <= 0)
{
oForeColor = RED;
if (oColor)
WipeLine(22);
OutPut("Sorry that doesn't flank a row. Try again!", 1);
DosSleep(1000);
goto AskMove;
}
oZ = FALSE;
sprintf(oTempString , "That gives you %d of my pieces", oPiecesTaken);
if (NOT oColor)
OutPut(oTempString, 1);
TakePieces(TRUE);
oHumanPieces = oHumanPieces + oPiecesTaken + 1 ;
oComputerPieces -= oPiecesTaken;
oSpacesTaken++;
oForeColor = CYAN;
TellPieces();
DosSleep(1000);
DisplayBoard();
if (oComputerPieces)
{
if (oSpacesTaken != 64)
goto ComputerMove;
}
goto EndCheck;
}
static void TellPieces(void)
{
if (oColor)
OutPut("\x1B[20;17H\x1B[K", 0);
sprintf(oTempString , "You have %d pieces, and I have %d pieces", oHumanPieces, oComputerPieces);
OutPut(oTempString, 1);
}
static int PlayAgain(void)
{
OutPut("", 1);
oForeColor = YELLOW;
if (oColor)
WipeLine(23);
if (YorN("Do you want to play another game ([Y]/N)? "))
return (TRUE);
OutPut("Thanks. Its been fun playing you.", 1);
OutPut("Exiting Othello, Bye", 1);
return (FALSE);
}
static int NextToOpponent(void)
{
int Index1;
int Index2;
for (Index1 = -1; Index1 <= 1; Index1++)
{
for (Index2 = -1; Index2 <= 1; Index2++)
{
if (oBoard[oMoveRow + Index1][oMoveCol + Index2] == oBadGuy)
{
return (TRUE);
}
}
}
return (FALSE);
}
static void TakePieces(BOOL ChangeBoard)
{
int Index;
int NextSpaceIndex;
int NextRowIndex;
int AdjacentRow;
int NextColIndex;
int AdjacentCol;
int s3;
oPiecesTaken = 0;
for (NextSpaceIndex = 0; NextSpaceIndex < 8; NextSpaceIndex++)
{
NextRowIndex = oNextRow[NextSpaceIndex]; // 0, -1, -1, -1, 0, 1, 1, 1
NextColIndex = oNextCol[NextSpaceIndex]; // 1, 1, 0, -1, -1, -1, 0, 1
AdjacentRow = oMoveRow + NextRowIndex;
AdjacentCol = oMoveCol + NextColIndex;
s3 = 0;
if (oBoard[AdjacentRow][AdjacentCol] == oBadGuy)
g750:
{
s3++;
AdjacentRow += NextRowIndex;
AdjacentCol += NextColIndex;
if (oBoard[AdjacentRow][AdjacentCol] == oGoodGuy)
{
oPiecesTaken += s3;
if (ChangeBoard)
{
AdjacentRow = oMoveRow;
AdjacentCol = oMoveCol;
for (Index = 0; Index <= s3; Index++)
{
oBoard[AdjacentRow][AdjacentCol] = oGoodGuy;
AdjacentRow += NextRowIndex;
AdjacentCol += NextColIndex;
}
}
}
else
{
if (oBoard[AdjacentRow][AdjacentCol])
goto g750;
}
goto g770;
}
g770:;
}
return;
}
static void DisplayBoard(void)
{
int ArrayIndex1;
int ArrayIndex2;
if (NOT oColor)
OutPut(" A B C D E F G H ", 2);
else
oBackColor = WHITE;
if (oColor AND (NOT oBoardDisplayed))
{
oBoardDisplayed = TRUE;
BuffFile("Othello.Scn");
}
for (ArrayIndex1 = 1; ArrayIndex1 <= 8; ArrayIndex1++)
{
if (NOT oColor)
sprintf(oTempString , "%d", ArrayIndex1);
for (ArrayIndex2 = 1; ArrayIndex2 <= 8; ArrayIndex2++)
{
if (NOT oColor)
{
strcat(oTempString , " ");
strcat(oTempString , oPieceType[oBoard[ArrayIndex1][ArrayIndex2] + 1]);
}
else
{
if (oBoard[ArrayIndex1][ArrayIndex2] != oWasBoard[ArrayIndex1][ArrayIndex2])
{
oWasBoard[ArrayIndex1][ArrayIndex2] = oBoard[ArrayIndex1][ArrayIndex2];
oForeColor = (oBoard[ArrayIndex1][ArrayIndex2] == oRed) ? RED : BLACK;
sprintf(oTempString, "\x1B[%d;%dH█████", oANSIRow[ArrayIndex1 - 1], oANSICol[ArrayIndex2 - 1]);
OutPut(oTempString, 0);
}
}
}
if (NOT oColor)
OutPut(oTempString, 2);
}
if (oColor)
{
oBackColor = BLACK;
WipeLine(22);
}
return;
}
static void WipeLine(int RowNumber)
{
char WipeString[30];
sprintf(WipeString, "\x1B[%d;1H\x1B[K\x1B[13C", RowNumber);
OutPut(WipeString, 0);
return;
}
static void AskHandicap(void)
{
oForeColor = GREEN;
if (YorN("Do you wish to be given a handicap (i.e. Do you want a headstart) ([Y]/N)? "))
{
HumanGetsHandicap();
}
else
{
if (YorN("Do you wish to give me a handicap (i.e. Do you want a hard time) ([Y]/N)? "))
{
oHandicapRecipient = oComputer;
AskCorners();
oHandicap = oCornersSacrificed * 2.5;
oComputerPieces += oCornersSacrificed;
}
}
return;
}
static void HumanGetsHandicap(void)
{
oHandicapRecipient = oHuman;
AskCorners();
oHandicap = oCornersSacrificed * (-8);
oHumanPieces += oCornersSacrificed;
return;
}
static int CornerCol[4] = { 1, 1, 8, 8 };
static int CornerRow[4] = { 1, 8, 8, 1 };
static void AskCorners(void)
{
int Index;
int ArrayIndex1;
int ArrayIndex2;
Input("How many corners (1-4) ", 0, 1);
oCornersSacrificed = (int) oArgValue;
if (oCornersSacrificed > 4)
{
HumanGetsHandicap();
return;
}
if (oCornersSacrificed < 1)
{
HumanGetsHandicap();
return;
}
for (Index = 0; Index < oCornersSacrificed; Index++)
{
ArrayIndex1 = CornerCol[Index];
ArrayIndex2 = CornerRow[Index];
oBoard[ArrayIndex1][ArrayIndex2] = oHandicapRecipient;
}
oSpacesTaken += oCornersSacrificed;
return;
}
void EndGame(void)
{
exit (0);
}
//////////////////////////////////////////////////////////
// Output uses printf for its dirty work.. line feeds
// are done separately in a loop according to the number
// of LF's asked for.
//
// If the user has ANSI color, then a color string is sent
// before each line (remember this is an example....)
void OutPut(char *String, int NumLineFeeds)
{
int Index;
ANSIString(oForeColor, oBackColor);
printf(String);
for (Index = 0; Index < NumLineFeeds; Index++)
printf("\r\n");
fflush(stdout);
return;
}
/////////////////////////////////////////////////////////
// Input - we want to control the maximum length of the
// input, and possibly as for upper case, or get keystrokes
// in hot key mode.
//
int Input(char *Prompt, int Flags, int MaxLength)
{
int Index = 0;
int CharIn;
time_t SleepTime;
OutPut(Prompt, 0);
memset(oTempString, 0, 512);
if (MaxLength > 511)
MaxLength = 511;
SleepTime = time(NULL) + 240L;
while (1)
{
// this call works different from OS/2 command
// (versus Adept or GMon/2 redirection)
CharIn = getchar();
if (CharIn != EOF)
{
SleepTime = time(NULL) + 240L;
if (CharIn == '\r' || CharIn == '\n')
break;
if (CharIn == '\b' && Index)
{
printf("\b \b");
Index--;
continue;
}
// Ctrl-Y deletes all input
if (CharIn == 25 && Index)
{
while (Index--)
printf("\b \b");
continue;
}
if (NOT isprint(CharIn))
continue;
fputc(CharIn, stdout);
fflush(stdout);
oTempString[Index++] = (char) CharIn;
oTempString[Index] = '\0';
if (Index >= MaxLength)
{
if (Flags & TURBO)
{
break;
}
continue;
}
}
else
{
DosSleep(65);
// we just fell asleep
if (SleepTime < time(NULL))
exit(1);
}
}
if (Flags & INPUTUCASE)
strupr(oTempString);
// store value of string, if any
oArgValue = atoi(oTempString);
// store length of string, if any
oArgLen = strlen(oTempString);
OutPut("", 1);
return (0);
}
////////////////////////////////////////////////////////
// returns TRUE if Y (default) ... FALSE if N
int YorN(char *YorNQuestion)
{
int temp;
do
{
Input(YorNQuestion, TURBO | INPUTUCASE, 1);
temp = (int) *oTempString;
if ((temp == 0) OR (temp == 'Y') OR (temp == 'N'))
break;
} while(1);
*oTempString = '\0';
if ((temp == 'Y') OR (temp == 0))
return (TRUE);
else
return (FALSE);
}
void ANSIString(int ForeColor, int BackColor)
{
int StrIndex;
char ANSIStrBuff[20];
if (NOT oColor)
return;
StrIndex = sprintf(ANSIStrBuff, "\x1B[%s", oBright ? "1;" : "");
StrIndex += sprintf(ANSIStrBuff + StrIndex, "%d;", ForeColor + 30);
StrIndex += sprintf(ANSIStrBuff + StrIndex, "%d", BackColor + 40);
sprintf(ANSIStrBuff + StrIndex, "m");
if (strlen(ANSIStrBuff) < 4) // ie. '\x1B[m'
*ANSIStrBuff = '\0';
printf(ANSIStrBuff);
fflush(stdout);
return;
}
//////////////////////////////////////////////////////////////////////
// Read text file and display it to user
//
void BuffFile(char *FileToView)
{
int BuffFileHandle, GraphicsHold;
ULONG BufferSize = 512;
char *FileBuffer;
char BuffFileName[80];
char *ThisLine, *NextLine, *EndBuffer;
int BytesRead;
strcpy(BuffFileName, FileToView);
BuffFileHandle = sopen(BuffFileName, O_RDWR | O_TEXT, SH_DENYNO);
if (BuffFileHandle == -1) // no file present
return;
BufferSize = (ULONG) (filelength(BuffFileHandle) + 2);
FileBuffer = (char *) calloc(1, BufferSize);
if (FileBuffer == NULL)
{
close(BuffFileHandle);
return;
}
GraphicsHold = oColor; // color always off here.. so remember
// stop ANSI color string from being displayed
// in front of each line of file
oColor = FALSE; // graphics status
BytesRead = read(BuffFileHandle, FileBuffer, BufferSize - 2);
if (BytesRead > 1)
{
EndBuffer = FileBuffer + BytesRead;
ThisLine = FileBuffer;
while (ThisLine)
{
NextLine = strchr(ThisLine, '\n');
if (NextLine)
*NextLine++ = '\0';
OutPut(ThisLine, 1);
ThisLine = NextLine;
if (ThisLine >= EndBuffer)
break;
}
}
close(BuffFileHandle);
free(FileBuffer); // free file buffer
oColor = GraphicsHold; // remember graphics mode
return;
}
//////////// Random Number functions ///////////////
float Zero2OneRand(void)
{
short rand_result;
rand_result = rand();
return ((float) rand_result / 32767.0);
}
ULONG rand_a;
void Randomize(void)
{
srand((ULONG) time(NULL));
rand_a = (ULONG) rand();
srand(Random(1, (ULONG) 65535));
return;
}
// ***************************************************************************
// * These routines are random number generators 2 have a setable range *
// * of numbers, and one returns a float between 0 and 1 *
// ***************************************************************************
ULONG rand_m = 100000000;
ULONG rand_m1 = 10000;
ULONG rand_b = 31415821;
ULONG Random(ULONG Low, ULONG High)
{
rand_a = (ULONG) ((mult((ULONG) rand_a, (ULONG) rand_b) + 1) MOD rand_m);
High = (ULONG) ((((rand_a / rand_m1) * (ULONG) High) / rand_m1) + 1);
High -= Low;
return (High);
}
ULONG mult(ULONG p, ULONG q)
{
ULONG multie;
ULONG p0, p1, q0, q1;
p0 = (p MOD (ULONG) rand_m1);
p1 = (p / (ULONG) rand_m1);
q0 = (q MOD (ULONG) rand_m1);
q1 = (q / (ULONG) rand_m1);
multie = (ULONG) (((((p0 * q1) + (p1 * q0)) MOD rand_m1) * rand_m1 + (p0 * q0)) MOD rand_m);
return (multie);
}
//////////////////////////////////////////////////////////
// Code to extract color (yes/no) flag from Session info
///////////////////////////////////////////////////////////
char *SkipWhiteSpace(char *String);
char *SkipNonWhiteSpace(char *String);
void ReadSessionInfo(char *SessionInfoName)
{
FILE *SessionInfoHandle;
char Input[301];
int Key;
char *Keyword;
char *Data;
char *NewLine;
SessionInfoHandle = fopen(SessionInfoName, "rt");
if (SessionInfoHandle == NULL)
{
fprintf(stderr, "\n\rSession.Info interface. ");
fprintf(stderr, "Can't open '%s'\n\r", SessionInfoName);
DosSleep(2000);
exit(1);
}
while (NOT feof(SessionInfoHandle))
{
if (NOT fgets(Input, 301, SessionInfoHandle))
break;
if (*Input == ';')
{
// comment line
continue;
}
NewLine = strchr(Input, '\n');
if (NewLine)
*NewLine = '\0';
Keyword = Input;
Data = SkipNonWhiteSpace(Keyword);
if (*Data)
{
*Data++ = '\0';
Data = SkipWhiteSpace(Data);
// Data now points to data!
}
// look for ANSICOLOR keyword,
// and act on it.
if (NOT stricmp(Keyword, "ANSICOLOR"))
{
oColor = atoi(Data);
// all done.. ferget rest o' file
break;
}
}
fclose(SessionInfoHandle);
return;
}
static char *SkipWhiteSpace(char *String)
{
char *p = String;
while (*p == ' ' || *p == '\t')
p++;
return (p);
}
static char *SkipNonWhiteSpace(char *String)
{
char *p = String;
while (*p && *p != ' ' && *p != '\t')
p++;
return (p);
}