Skip to content

Instantly share code, notes, and snippets.

@TannerRogalsky
Created December 25, 2011 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TannerRogalsky/1518545 to your computer and use it in GitHub Desktop.
Save TannerRogalsky/1518545 to your computer and use it in GitHub Desktop.
Old Hunt the Wumpuse IRC bot
import org.jibble.pircbot.*;
import java.util.Random;
import java.util.ArrayList;
public class MyBot extends PircBot {
public String channel; // The name of the primary channel.
public ArrayList map; // The map data. 0 = empty, 1 = wumpus, 2 = shaft, 3 = bats, 4 = player.
public String player; // The player's nickname.
public boolean gameInProgress; // Whether or not there is a game in progress.
public boolean moving, shooting; // Whether the player is moving or shooting, respectively.
public int arrows; // How many arrows the player has left.
public MyBot(String myChannel) {
this.setName("WumpusBot");
this.setLogin("WeTheWumpi");
channel = myChannel;
gameInProgress = false;
moving = false;
shooting = false;
}
public void onConnect() {
sendMessage(channel, "Greetings, " + channel + ".");
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
message = message.toLowerCase().trim();
if ((message.equalsIgnoreCase("!quit")) && (hostname.equals ("private-A895A092.home.cgocable.net")))
disconnect();
else if ((message.equalsIgnoreCase("!wumpus")) && (!gameInProgress) || (message.equalsIgnoreCase("!start")) && (!gameInProgress)){
sendMessage (channel, "Oh noes!");
player = sender;
arrows = 5;
gameInProgress = true;
generateMap (20);
sendMessage (channel, "You'll still have to find me, " + sender + "! The game starts NOW!");
actionText (getPlayerRoom());
} else if ((message.charAt(0) == 'm') && (sender.equals (player))){
int moveTo = getDigits(message);
int moveFrom = getPlayerRoom();
if (isValidTunnel(getTunnels(moveFrom), moveTo)){
if (map.get(moveTo).equals(1)){
sendMessage (channel, "Muahaha! The Wumpus got ya!");
endGame("The game is over. You lose.");
}else if (map.get(moveTo).equals(2)){
sendMessage (channel, "You fell down a shaft. ;_;");
endGame("The game is over. You lose.");
}else if (map.get(moveTo).equals(3)){
sendMessage (channel, "Zap! Elsewhereville for you!");
map.set (moveFrom, 0);
addObject (4);
actionText (getPlayerRoom());
} else {
map.set (moveTo, 4);
map.set (moveFrom, 0);
moving = false;
actionText (moveTo);
}
}else {
sendMessage (channel, "That is not a valid room.");
}
} else if((message.charAt(0) == 's') && (sender.equals (player))){
int shootTo = getDigits(message);
if (isValidTunnel(getTunnels(getPlayerRoom()), shootTo)){
if (shootTo == getWumpusRoom ()){
sendMessage (channel, "Noooo! You got me!");
endGame("The game is over. You win.");
} else {
shooting = false;
map.set(getWumpusRoom(), 0);
addObject(1);
sendMessage (channel, "You missed the Wumpus.");
arrows--;
sendMessage (channel, "You have " + arrows + " arrows left.");
if (arrows == 0) endGame("The game is over. You lose.");
else actionText (getPlayerRoom());
}
}else{
sendMessage (channel, "That is not a valid room.");
}
} else if ((message.equalsIgnoreCase ("!end")) && (sender.equals (player))
|| (message.equalsIgnoreCase ("!end")) && (hostname.equals ("private-A895A092.home.cgocable.net")) && (gameInProgress)) {
sendMessage (channel, "Well I can't hunt myself, now, can I?");
gameInProgress = false;
player = "";
sendMessage (channel, "The game is over!");
}
}
public void onDisconnect() {
System.exit (0);
}
public void onKick(String channel, String kickerNick, String kickerLogin,
String kickerHostname, String recipientNick, String reason) {
if (recipientNick.equals (getNick())){
joinChannel (channel);
sendMessage (channel, "Bite me, " + kickerNick + ".");
}
}
// Generates a random map.
public void generateMap (int mapSize) {
map = new ArrayList (mapSize);
for (int i = 0 ; i < mapSize ; i++)
map.add (0);
addObject (1);
addObject (4);
addObject (2);
addObject (2);
addObject (3);
addObject (3);
}
// Adds objects to the map.
public void addObject (int objectType){
Random r = new Random ();
int space = r.nextInt (20);
while (!map.get(space).equals (0))
space = r.nextInt (20);
map.set (space, objectType);
}
// Returns the room that the player is in.
public int getPlayerRoom () {
for (int i = 0; i < map.size(); i++){
if (map.get(i).equals(4))
return i;
}
return -1;
}
// Returns the room that the Wumpus is in.
public int getWumpusRoom () {
for (int i = 0; i < map.size(); i++){
if (map.get(i).equals(1))
return i;
}
return -1;
}
// Returns a list of tunnels from a given room.
public ArrayList getTunnels (int room){
ArrayList tunnels = new ArrayList();
if (room - 5 >= 0)
tunnels.add (room - 5);
if ((room != 4) && (room != 9) && (room != 14) && (room != 19))
tunnels.add (room + 1);
if ((room != 0) && (room != 5) && (room != 10) && (room != 15))
tunnels.add (room - 1);
if (room + 5 < 20)
tunnels.add (room + 5);
return tunnels;
}
// Returns whether or not there is a hazard in an adjoining room.
public boolean isObjectClose (int object){
int playerRoom = getPlayerRoom();
boolean[] trues = new boolean[4];
if (playerRoom - 5 >= 0)
trues[0] = map.get(playerRoom - 5).equals(object);
if ((playerRoom != 4) && (playerRoom != 9) && (playerRoom != 14) && (playerRoom != 19))
trues[1] = map.get(playerRoom + 1).equals(object);
if ((playerRoom != 0) && (playerRoom != 5) && (playerRoom != 10) && (playerRoom != 15))
trues[2] = map.get(playerRoom - 1).equals(object);
if (playerRoom + 5 < 20)
trues[3] = map.get(playerRoom + 5).equals(object);
return ((trues[0]) || (trues[1]) || (trues[2]) || (trues[3]));
}
// Displays the majority of the game text.
public void actionText (int playerRoom){
sendMessage (channel, "********************");
if (isObjectClose(2)){sendMessage (channel, "You feel a draught.");}
if (isObjectClose(3)){sendMessage (channel, "Bats nearby.");}
if (isObjectClose(1)){sendMessage (channel, "You can smell the Wumpus!");}
sendMessage (channel, "You are in room " + playerRoom + ".");
sendMessage (channel, "Tunnels lead to rooms " + getTunnels(playerRoom).toString());
sendMessage (channel, "Move or shoot? (M or S)");
}
// Returns whether the chosen tunnel is valid or not.
public boolean isValidTunnel (ArrayList tunnels, int moveTo){
int x = 0;
for (int i = 0; i < tunnels.size(); i++){
if ((int)Integer.parseInt(tunnels.get(i).toString()) == moveTo){
x++;
}
}
return x == 1;
}
// Parses the digits from a message.
public int getDigits (String text){
char[] chars;
chars = text.toCharArray();
text = "";
for (int i = 0; i < chars.length; i++){
if (Character.isDigit(chars[i])){
text += chars[i];
}
}
return (int)Integer.parseInt(text);
}
// Resets the game vars and displays a message.
public void endGame (String text){
gameInProgress = false;
moving = false;
shooting = false;
player = "";
sendMessage (channel, text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment