REV0 C++ (Visual Studio on Windows) 405
#include"stdafx.h"
#include<stdlib.h>
#include<time.h>
int main(){srand(time(NULL));char i,h=rand()%19,w=rand()%19,p=19,d=0,q,e,m[]="e@LwQMQOSOLT";while(p-h&&p-w){for(i=3;i--;){q=(p+m[p%4*3+i])%20;if(q==w)puts("you smell a wumpus");if(q==h)puts("you feel a breeze");}scanf_s("%d",&i);e=(d+i/10)*m[p%4]%3;q=(p+m[p%4*3+e])%20;if(i%5){if(q==w){puts("YOU KILLED THE WUMPUS!");h=p;}else{puts("arrow missed");w=(w+m[w%4*3+rand()%3])%20;}}else{p=q;d=e;if(p==h)puts("YOU FELL IN A HOLE!");}if(p==w)puts("THE WUMPUS GOT YOU!");}}
Below is a playthrough, demonstrating that (provided you don't start right next to a hazard) with correct play you can always win. The player feels a breeze, turns back and does a complete counterclockwise loop. As it takes him exactly 5 moves to feel a breeze again, he knows the hole to his right, and gets as far away as possible. Similarly, when he smells the wumpus, not knowing whether it is right or left, he turns back and does a clockwise loop. It takes him 5 moves to smell the wumpus again, so he knows it is to the left and shoots with certainty.
If he had looped the other way he would have found the wumpus sooner and known it was in the same direction he was turning.
REV1 C (GCC on Cygwin), 431-35% bonus = 280.15
#define u(t,s,c) if(t){puts(s);c;}
i,d,e,a,b;main(){srand(time(0));char q,p=19,h=rand()%p,w=rand()%p,*m="e@LwQMQOSOLT-\\/\n \v ";
while(p-h&&p-w){
for(i=3;i--;){q=(p+m[p%4*3+i])%20;u(q==w,"you smell a wumpus",a|=2<<p)u(q==h,"you feel a breeze",b|=1<<p)}
for(i=20;i--;)printf("%c%c",i==p?m[d+12]:48+(a>>i&2)+(b>>i&1),m[i%4+15]);
scanf("%d",&i);e=(d+i/10)*m[p%4]%3;q=(p+m[p%4*3+e])%20;
if(i%5){u(q-w,"arrow missed",w=(w+m[w%4*3+rand()%3])%20;a=0)else u(1,"YOU KILLED THE WUMPUS!",h=p)}
else{p=q;d=e;u(p==h,"YOU FELL IN A HOLE!",)}
u(p==w,"THE WUMPUS GOT YOU!",)}}
Newlines added for clarity. The changes from Rev 0 are as follows:
A big thankyou to @Dennis for recommending GCC compiler on Cygwin Linux emulator for Windows. This compiler does not require the include
s in the rev 0 program, and it allows default int
type for variables and main.
This is a life-changing golfing tip!
Additionally running in Linux means that \f
does cause the cursor to move down without doing a carriage return (unlike in Windows where it just produces a printable symbol.) This has allowed considerable shortening of the printf statement that prints the board
Several additional tips from Dennis in the comments, and one of my own: change of condition when checking if arrow hit the wumpus: if(q==w)
>if(q-w)
(..else.. is reversed)
Addition of graphic display showing the information the player knows about where a wumpus is smelt / a breeze is felt to claim the 35% bonus. (I deleted the old debug version of this which showed the exact position of the wumpus and hole. It can be seen in the edit history.)
REV2 C (GCC on Cygwin), 389-35% bonus = 252.85
#define Q(N) (N+"QTLOQMQOSOLT"[N%4*3+e])%20
#define P printf(
i,d,e,a,b;main(){int p=19,q=srand(&p),h=rand()%p,w=rand()%p;
while(p-h&&p-w){
for(e=3;e--;){q=Q(p);q-w||P"You smell a wumpus\n",a|=2<<p);q-h||P"You feel a breeze\n",b|=1<<p);}
for(i=20;i--;)P"%c%c",i-p?48+(a>>i&2)+(b>>i&1):"-\\/"[d],"\n \v "[i%4]);
scanf("%d",&i);e=(d+i/9)*"edde"[p%4]%3;q=Q(p);
if(i%5){e=rand()%3;w=q-w?P"Your arrow didn't hit anything\n",a=0)&Q(w):(p=20);}
else p=q,d=e;
}
P p-20?p-w?"YOU FELL IN A HOLE!\n":"THE WUMPUS GOT YOU!\n":"YOU KILLED THE WUMPUS!\n");}
Thanks again to Dennis for refactoring my code:
Char constant m[]
replaced with literals (I didn't know you could index a literal.)
Seeding of random numbers with stack variable (system dependent, some systems randomise memory allocation as a security measure.)
Macro with puts
replaced with a macro with printf
and additional code which must be executed when the message displayed placed inside printf
arguments (advantage taken of the face that printf does not print the last few arguments if there aren't enough format specfiers in the format string.) if
replaced by ||
Calculation of new position of player/wumpus placed inside new macro.
Win/lose messages placed outside while
loop. if
replaced by conditional operator.
Use of conditional operator in line for shooting arrow. If the player misses, this requires both printing a message and adjusting wumpus position. Dennis offered a couple of ways of combining printf
and the calculation of wumpus position into a single expression, but I have gone with one of my own. printf
returns the number of characters printed, which for Your arrow didn't hit anything\n
is 31 (11111 binary.) So, 31&Q(w)==Q(w)
.
My other contribution to this edit has been elimination of some unnecessary brackets.
Output
Here the player has already found where the Wumpus is, but chooses to do a thorough explore to find out exactly where the pit is, too. Unlike my old debug version which showed where the wumpus and pit were throughout the game, this shows only the rooms where the player has visited and felt a breeze (1) smelt the wumpus (2) or both (3). (If the player shoots an arrow and misses, the variable a
containing the wumpus position info is reset.)
ICOSAHEDRON REPRESENTATION
Note: this section is based on rev 1
My star feature! There is no graph in my code. To explain how it works, see the world map below. Any point on the icosahedron can be represented by a latitude 0-3 and a longitude 0-4 (or a single number, long*4+lat
.) The longitude line marked on the map passes only through those faces with longitude zero, and the latitude line passes through the centre of the faces with latitude zero.
The player can be oriented on 3 possible axes, represented by the symbols as follows: north-south-
northeast-southwest\
northwest-southeast /
. In any given room he has exactly one exit on each of these axes available to him. In the display shown the player makes a complete clockwise loop. It is generally easy to identify from the player marking where he came from, and therefore where he is allowed to go to.
The one case that is a little difficult for the uninitiated eye is the fourth one. When you see a slant in one of these polar rows, the player has come from the polar cell nearest the outside end of the slant and is facing generally towards the equator. Thus the player is facing southeast and his options are: 15(SOUTH, the cell to the right) 25(northEAST, the cell above) or 35(northWEST, the cell below.)
So, basically I map the icosahedron to a 5x4 grid, with cells numbered 19 to 0 in the order they are printed. The move is made by adding or subtracting from the current position, depending on the player's latitude and direction, per the table below.
If the player goes off the bottom (west) of the board, he comes back on the top (east) side and vice versa, so his position is taken modulo 20. Generally the moves are coded into m[] by adding ascii 80 (P
) to the raw value giving the characters shown below, but principle any multiple of 20 can be added without affecting the operation.
Table of addition values for moves
Direction Symbol Latitude 0 1 2 3 Latitude 0 1 2 3
0, N-S - 1 -1 1 -1 Q O Q O
1, NE-SW \ -4 1 -1 4 L Q O T
2, NW-SE / 4 -3 3 -4 T M S L
The player's input (divided by 10 to remove the second digit) is added to his current direction and taken modulo 3 to get his new direction. This works fine in the majority of cases. However there is an issue when he is in a polar room and moves toward the pole. It will be clear when folding the map below that if he leaves the room facing "northeast" he will enter the new square facing "southeast" so a correction must be made. This is done in the line e=(d+i/10)*m[p%4]%3;
by the multiplication by m[p%4]
. The first four values of m[] are selected such that, in addition to their function above, they also have the characteristic m[1]%3==m[2]%3==1
and m[0]%3==m[3]%3==2
. This leaves the direction alone for the equatorial rooms and applies the necessary correction for polar rooms.
The logical time to do the correction would be after the move. However to save characters it is done before the move. Therefore certain values in m[] must be transposed. So the last 2 characters are LT
instead of TL
per the table above for example.
UNGOLFED CODE
this is rev 1 code, which is less obfuscated than rev 2.
This will run on GCC / Linux. I have included in the comments the extra code needed to make it run on Visual studio / Windows. It's a big difference!
//Runs on gcc/linux. For visual studio / windows, change printf(...)
//to printf(" %c%c%c",9*(i%4==1),i==p?m[d+12]:48+(a>>i&2)+(b>>i&1),10*!(i%2)) and uncomment the following lines
//#include"stdafx.h"
//#include<stdlib.h>
//#include<time.h>
//#pragma warning(once:996;once:430) //allow the use of scanf instead of scanf_s, allow default type=int.
//Though rather than using the pragma, it is shorter to follow compiler recommendation and use scanf_s and int.
#define u(t,s,c) if(t){puts(s);c;} //if(test){puts(string);additional code;}
i, //player input, loop counter
d,e, //current and proposed direction
a,b; //bit flags for where wumpus smelt / breeze felt
main(){
srand(time(0));
char q,p=19,h=rand()%p,w=rand()%p, //Initialise player, hole and wumpus. q stores proposed player position.
*m="e@LwQMQOSOLT-\\/\n \f "; //Chars 0-11: movetable. Chars 12-14:symbol for player. Chars 15-18: graphics format.
while(p-h&&p-w){
// Print warnings
for(i=3;i--;){q=(p+m[p%4*3+i])%20;u(q==w,"you smell a wumpus",a|=2<<p)u(q==h,"you feel a breeze",b|=1<<p)}
// graphic display
for(i=20;i--;)printf("%c%c",i==p?m[d+12]:48+(a>>i&2)+(b>>i&1),m[i%4+15]);
// Get player input and work out direction and room
scanf("%d",&i);
e=(d+i/10)*m[p%4]%3;
q=(p+m[p%4*3+e])%20;
// i%5 is false if player inputs 5 (move player) otherwise true (shoot arrow)
if(i%5)
{u(q-w,"arrow missed",w=(w+m[w%4*3+rand()%3])%20;a=0)else u(1,"YOU KILLED THE WUMPUS!",h=p)}
else{p=q;d=e;u(p==h,"YOU FELL IN A HOLE!",)}
u(p==w,"THE WUMPUS GOT YOU!",)
}
}
ISSUES AND CURIOISITIES
I have taken advantage of the point mentioned by @professorfish, if the wumpus and pit start in random places, there is no need for the player to start in a random place. The player always starts in room 19 facing north.
I understand that as the wumpus is "unaffected by the pit" the wumpus can start in, or enter the room where the pit is. In general this simplifies things except for one point. I have no specific variable to indicate the game is over; it is over when the player coincides with the wumpus or pit. So when the player wins I display the winning message but move the pit to the player to kludge out of the loop! I can't put the player in the pit as the wumpus might be there and I would get a message about the wumpus that I don't want.
The rev0program worked perfectly in visual studio, but the IDE said "stack corrupted around variable i" on exit. This is because scanf is trying to put an int
into a char.
Dennis reported incorrect behaviour on his Linux machine because of this. Anyway it is fixed by use of the correct type in rev 1.
The line for displaying the board in rev 0 is clumsy and appears slightly different on other platforms. In printf(" %c%c%c")
the middle %c is the printable character displayed. The last %c is either ASCII 0 or ASCII 10 (\n, newline with carriage return in Windows.) There appears to be no character in Windows that works in the console, that will go down a line without giving a carriage return. If there was I wouldn't need the first c% (ASCII 0, or ASCII 9 tab before latitude 1 character. Tabs are notoriously undefined in their behaviour.) The leading space improves formatting (puts latitude 3 & 2 characters nearer latitude 1 character.) Rev 1 has a revison of this line which uses a \f formfeed character and therefore needs no format character at the start of the printf. This makes it shorter, but the \f does not work in Windows.