Anonymous avatar Anonymous committed 5e16147

This is a non-working attempt at a Snake project.

Comments (0)

Files changed (11)

Binary file added.

Binary file added.

Binary file added.

Binary file added.

+/Tde/x/l/v/c/k /V3.10 /LC:\BC5\LIB +
+-v+ C:\BC5\LIB\c0s.obj+
+-v+ snake.obj+
+-v+ danproj1.obj
+danproj1.exe
+
+-v+ C:\BC5\LIB\noehs.lib+
+-v+ C:\BC5\LIB\cs.lib
+
+#ifndef __INC_MOVEUP_H
+#define __INC_MOVEUP_H
+/*-------------------------------------------------------------------------*/
+#ifdef __cplusplus /* C++ compiler? */
+extern "C" { /* But these are C functions (non-mangled) */
+#endif /*__cplusplus*/
+
+void MoveVertical(int by); /* Move cursor up or down by, use new to go up, pos to do gown */
+BOOL AtEdge(void); /* Returns TRUE if cursor is at edge of screen */
+
+#ifdef __cplusplus
+}
+#endif /*__cplusplus*/
+/*-------------------------------------------------------------------------*/
+#endif /*__INC_MOVEUP_H*/
+/* Header to create a snake object (_S_SNAKE) */
+
+#ifndef __INC_SNAKE_H
+#define __INC_SNAKE_H
+/*-------------------------------------------------------------------------*/
+/* Constants for facing */
+#define SNAKE_FACE_NORTH   1
+#define SNAKE_FACE_EAST    2
+#define SNAKE_FACE_SOUTH   3
+#define SNAKE_FACE_WEST    4
+
+typedef struct _S_SNAKE_H /* Snake object tag */
+{
+	/* PRIVATE: Members in this section must not be touched by the outside */
+		unsigned int _length; /* Length of snake */
+	   unsigned int _bends; /* # bends in snake */
+	   BYTE _facing; /* Facing direction, see constants */
+      BOOL _alive; /* Specifies if snake is alive */
+   BOOL (*_IsKilled)(void); /* User installed proc that is called by the snake after every block, if it returns TRUE the snake willl die */
+
+   /* PUBLIC: Members in this section may be used from anywhere */
+   	unsigned int UnitDelay; /* The delay between one unit of snake length being drawn and the next */
+   unsigned int (*GetLength)(void*); /* Returns length of snake */
+   unsigned int (*GetNumBends)(void*); /* Returns # bends in snake */
+   void (*KillIt)(void*); /* Kills the snake */
+   void (*Execute)(void*); /* Brings the snake to life in the screen */
+   void (*InstallIsKilled)( void*,  BOOL(*)(void) ); /* Install a user IsKilled() routine */
+   BOOL (*IsAlive)(void*); /* Specifies if the snake is alive */
+
+} S_SNAKE, *PS_SNAKE; /* Typedef names to make a snake */
+
+void S_Snake_ConstructAndInstalledIsKilled(PS_SNAKE Object, BOOL(*UserIsKilled)(void)); /* Constructor with user installed is killed routine */
+void S_Snake_Construct(PS_SNAKE PObject); /* Ordinary constructor */
+void S_Snake_Destruct(PS_SNAKE PObject); /* Destructor */
+/*-------------------------------------------------------------------------*/
+#endif /*__INC_SNAKE_H*/
+/* This example program has been written by David Duncan Ross Palmer
+@ Daybo Logic as an example to Asicider.  To wait for a keyboard
+press (which is non-ANSI) this module is not portable, however the
+snake is ANSI C portable.  Note that kbhit() may be re-implemented
+on non-Borland C by writing an assember routine and using it */
+/*-------------------------------------------------------------------------*/
+#ifdef __BORLANDC__
+#	include <ConIO.H> /* ConIO is Borland C */
+#endif /*__BORLANDC__*/
+#include "Stds\DLStdInc.H" /* Daybo Logic standard inclusions header */
+#pragma hdrstop
+
+#include "Snake.H" /* Access to the object that is the snake */
+
+static BOOL KilledProc(void);
+static S_SNAKE Snake; /* Create instance of the snake */
+/*-------------------------------------------------------------------------*/
+int main()
+{
+   /* We are going to create an object called Snake which will
+   find it's way around the screen */
+   S_Snake_ConstructAndInstalledIsKilled(&Snake, KilledProc); /* Construct the snake object */
+
+   printf(" WARNING!  This snake is a bit mad and is liable to disappear up it's own arse!\n");
+   Snake.Execute(&Snake); /* Snake is ALIVE!!!!! */
+
+   S_Snake_Destruct(&Snake); /* Destroy the snake object */
+	return 0;
+}
+/*-------------------------------------------------------------------------*/
+static BOOL KilledProc()
+{
+	#ifdef __CONIO_H /* ConIO.H included? */
+	if (kbhit()) Snake.KillIt(&Snake); /* Kill snake when user presses a key */
+   #else /* No kbhit() to use! */
+   #error ("kbhit() is non-ANSI and must be re-written in assembler")
+   #endif /*__CONIO_H*/
+	return !Snake.IsAlive(&Snake);
+}
+/*-------------------------------------------------------------------------*/

Binary file added.

+/* To move the cursor up by one requires a Borland specific function,
+gotoxy(), I certainly wasn't going to use this function directly because
+it's not portable.  This module will need to be replaced if the code
+must be compiled on a non-Borland compiler.  Under Win32 using a console
+API function would do.  Under another DOS compiler some assembler will
+have to be written. */
+
+#ifndef __BORLANDC__
+#	error ("Code must be re-written to work on a non-Borland compiler, see info in file")
+#endif /*__BORLANDC__*/
+
+#include <ConIO.H>
+#include "Stds\DLStdInc.H" /* Daybo Logic standard inclusions header */
+#pragma hdrstop
+
+#include "MoveUp.H"
+/*-------------------------------------------------------------------------*/
+void MoveVertical(int by)
+{
+	/* Send us pos to move down, new to move up */
+   gotoxy(wherex(), wherey() + by); /* Move vertically 'by' */
+}
+/*-------------------------------------------------------------------------*/
+BOOL AtEdge(void)
+{
+	if (wherex()-1 == 1) /* At left of screen? */
+   	return TRUE;
+   if (wherex()-1 == 80) /* At right of screen? */
+   	return TRUE;
+   if (wherey()-1 == 1) /* At top of screen? */
+   	return TRUE;
+   if (wherey()-1 == 25) /* At bottom of screen */
+   	return TRUE;
+
+   return FALSE; /* Somewhere in the screen */
+}
+/*-------------------------------------------------------------------------*/
+/* The snake lives!  A snake will slowley use up the entire screen!  Ha! */
+/*-------------------------------------------------------------------------*/
+#include <StdLib.H> /* For rand() */
+#include "Stds\DLStdInc.H" /* Daybo Logic standard inclusions header */
+#pragma hdrstop
+
+#include "MoveUp.H" /* Compiler specifc "how to move the cursor up" */
+#include "Replace\SleepX.H" /* Allows our fast processor pause */
+#include "Snake.H" /* Our header */
+
+static void S_Snake_Link(PS_SNAKE PObject); /* Hooks C funcs to be member functions, cannot be used from outside */
+static void S_Snake_Init(PS_SNAKE PObject); /* Initialzes all data (or defaults it) */
+static unsigned int S_Snake_GetLength(void*); /* Returns length of snake */
+static unsigned int S_Snake_GetNumBends(void*); /* Returns # bends in snake */
+static void S_Snake_KillIt(void*); /* Kills the snake */
+static void S_Snake_Execute(void*); /* Brings the snake to life in the screen */
+static void S_Snake_InstallIsKilled( void*,  BOOL(*)(void) ); /* Install a user IsKilled() routine */
+static BOOL S_Snake_IsAlive(void*); /* Specifies if the snake is alive */
+static BYTE NewFace(PS_SNAKE Snake); /* Reconmends a new direction to face */
+static BOOL NewSuggestionOK(const PS_SNAKE Snake, const BYTE NewDir);
+static BOOL CrashSelf(const BYTE NewDir);
+static BOOL CrashWall(const BYTE NewDir);
+
+#define BLOCKCHAR (178) /* Character that will be used as the snake block */
+#define DELAYTIME (250) /* Specifies how many milliseconds are wasted before drawing another block */
+/*-------------------------------------------------------------------------*/
+static void S_Snake_Link(PS_SNAKE PObject)
+{
+	/* We will create a VTABLE, effectively */
+	PObject->GetLength = S_Snake_GetLength;
+   PObject->GetNumBends = S_Snake_GetNumBends;
+   PObject->KillIt = S_Snake_KillIt;
+   PObject->Execute = S_Snake_Execute;
+   PObject->InstallIsKilled = S_Snake_InstallIsKilled;
+   PObject->IsAlive = S_Snake_IsAlive;
+}
+/*-------------------------------------------------------------------------*/
+static void S_Snake_Init(PS_SNAKE PObject)
+{
+	PObject->_length = 0; /* Not even drawn yet */
+   PObject->_bends = 0; /* Not even drawn yet */
+   PObject->_facing = SNAKE_FACE_EAST; /* We will have hit going east initially */
+   PObject->_IsKilled = NULL; /* No user IsKilled installed yet */
+}
+/*-------------------------------------------------------------------------*/
+void S_Snake_ConstructAndInstalledIsKilled(PS_SNAKE PObject, BOOL(*UserIsKilled)(void))
+{
+	S_Snake_Link(PObject); /* Link member functions by creating VTABLE */
+   S_Snake_Init(PObject); /* Initialize members */
+   PObject->InstallIsKilled(PObject, UserIsKilled); /* Install user procedure */
+}
+/*-------------------------------------------------------------------------*/
+void S_Snake_Construct(PS_SNAKE PObject)
+{
+	S_Snake_Link(PObject); /* Link member functions by creating VTABLE */
+   S_Snake_Init(PObject); /* Init members */
+}
+/*-------------------------------------------------------------------------*/
+void S_Snake_Destruct(PS_SNAKE PObject)
+{
+	PObject->KillIt(PObject); /* Kill the snake manually */
+}
+/*-------------------------------------------------------------------------*/
+static unsigned int S_Snake_GetLength(void* PObject)
+{
+	PS_SNAKE s = (S_SNAKE*)PObject; /* Cast object pointer */
+   return s->_length; /* Give length of snake to caller */
+}
+/*-------------------------------------------------------------------------*/
+static unsigned int S_Snake_GetNumBends(void* PObject)
+{
+	PS_SNAKE s = (S_SNAKE*)PObject; /* Cast object pointer */
+   return s->_bends; /* Tell user how many bends are in the snake */
+}
+/*-------------------------------------------------------------------------*/
+static void S_Snake_KillIt(void* PObject)
+{
+	PS_SNAKE s = (S_SNAKE*)PObject; /* Cast object pointer */
+   s->_alive = FALSE; /* Kill snake manually */
+}
+/*-------------------------------------------------------------------------*/
+static void S_Snake_Execute(void* PObject)
+{
+	PS_SNAKE s = (S_SNAKE*)PObject; /* Cast object pointer */
+   s->_alive = TRUE; /* Just made it alive by calling Execute() */
+
+   while (s->_alive /* Internal flag say we are alive */ &&
+   			((s->_IsKilled) ? (!s->_IsKilled()) : 1) ) /* And user routine says we can run */
+   {
+   	switch (s->_facing)
+      {
+      	case SNAKE_FACE_NORTH : /* Goes up from current pos */
+         {
+         	const char prnBuff[] = { '\b', BLOCKCHAR, 0x00 };
+         	MoveVertical(-1); /* Moves cursor up by one block */
+            printf(prnBuff);
+         	break;
+         }
+      	case SNAKE_FACE_EAST : /* Goes right from pos */
+         {
+         	const char prnBuff[] = { BLOCKCHAR, 0x00 };
+   			printf(prnBuff); /* Put block on display to right of pos */
+            break;
+         }
+         case SNAKE_FACE_SOUTH : /* Goes down from pos */
+         {
+         	const char prnBuff[] = { '\b', BLOCKCHAR, 0x00 };
+            MoveVertical(1); /* Moves cursor down by one block */
+            printf(prnBuff);
+            break;
+         }
+         case SNAKE_FACE_WEST : /* Goes left from pos */
+         {
+         	const char prnBuff[] = { '\b', '\b', BLOCKCHAR, 0x00 };
+            printf(prnBuff);
+            break;
+         }
+      } /* end switch compound */
+      if (rand() % 10 == 0 || AtEdge())
+      	s->_facing = NewFace(s); /* Generates a new direction to face */
+
+      SleepMS(DELAYTIME); /* Waste some time (so fast processors don't stop the user seeing us move */
+   } /* end of while */
+}
+/*-------------------------------------------------------------------------*/
+static void S_Snake_InstallIsKilled( void* PObject,  BOOL(*UserIsKilled)(void) )
+{
+	PS_SNAKE s = (S_SNAKE*)PObject; /* Cast object pointer */
+   s->_IsKilled = UserIsKilled; /* Set pointer to new routine */
+}
+/*-------------------------------------------------------------------------*/
+static BOOL S_Snake_IsAlive(void* PObject)
+{
+	PS_SNAKE s = (S_SNAKE*)PObject; /* Cast object pointer */
+   return s->_alive; /* Give alive flag */
+}
+/*-------------------------------------------------------------------------*/
+static BYTE NewFace(PS_SNAKE Snake)
+{
+	/* We must determine which way the snake should now face */
+
+   BYTE newFaceSuggestion;
+
+   do
+   {
+      int RndFace = rand(); /* Get a random value */
+      if (RndFace % 8 == 0) /* Divisible by eight? */
+         newFaceSuggestion = SNAKE_FACE_NORTH;
+      else if (RndFace % 4 == 0) /* Divisible by four? */
+         newFaceSuggestion = SNAKE_FACE_EAST;
+      else if (RndFace % 2 == 0) /* Divisible by two? */
+         newFaceSuggestion = SNAKE_FACE_SOUTH;
+      else
+         newFaceSuggestion = SNAKE_FACE_WEST;
+   } while (!NewSuggestionOK(Snake, newFaceSuggestion)); /* The new direction must be verified by another function */
+
+   return newFaceSuggestion; /* Suggest to caller */
+}
+/*-------------------------------------------------------------------------*/
+static BOOL NewSuggestionOK(const PS_SNAKE Snake, const BYTE NewDir)
+{
+	/* The new suggestion for the facing direction must not be the
+   opposite way that the direction is facing (back into itself) otherwise
+   the user will not see the movement. */
+
+   if (Snake->_facing == SNAKE_FACE_WEST && NewDir == SNAKE_FACE_EAST)
+   	return FALSE; /* Not acceptable */
+   if (Snake->_facing == SNAKE_FACE_EAST && NewDir == SNAKE_FACE_WEST)
+   	return FALSE; /* Not acceptable */
+   if (Snake->_facing == SNAKE_FACE_SOUTH && NewDir == SNAKE_FACE_NORTH)
+   	return FALSE; /* Not acceptable */
+   if (Snake->_facing == SNAKE_FACE_NORTH && NewDir == SNAKE_FACE_SOUTH)
+   	return FALSE; /* Not acceptable */
+
+   /* Better now check that the snake does not crash into itself or the side */
+   if (CrashSelf(NewDir) || CrashWall(NewDir)) return FALSE; /* Not acceptable */
+
+   return TRUE; /* The move is acceptable */
+}
+/*-------------------------------------------------------------------------*/
+static BOOL CrashSelf(const BYTE NewDir)
+{
+	/* I can't be bothered to implement this function */
+	return FALSE;
+}
+/*-------------------------------------------------------------------------*/
+static BOOL CrashWall(const BYTE NewDir)
+{
+	/* I can't be arsed to implement this function */
+   return FALSE;
+}
+/*-------------------------------------------------------------------------*/
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.