#!/usr/bin/perl -w
#------------------------------------------------------------#
# Final Project: Propositional Logic in the Wumpus World
# Class: Artificial Intelligence
# Semester: Spring 2003
# Author: Gary W. Johnson, Jr.
#------------------------------------------------------------#
#  This is the test program for the Wumpus World.
#  It is an interactive, menu-driven interface that
#  allows the user to direct the agent in whatever
#  manner he best chooses.  After each action, the
#  agent will run his sensors to get his percepts from
#  the Environment, update his knowledge base and
#  understanding of the world state, and display this
#  information on the screen.
#
#  The intention of this program is to demonstrate to
#  the user in real-time, what kind of information may
#  be deduced about a given environment by an agent
#  based on propositional logic.  By observing changes
#  in the knowledge base and the believed state of the
#  world, the user can follow the logical assertions
#  and negations that reduce disjunctive statements (guesses)
#  into proper assertions in their own right.
#
#------------------------------------------------------------#

use strict;
use WumpusWorld;

# Environment Functions:
# called: $myEnvironment = new Environment($rows, $cols);
# called: $myEnvironment->displayState();

# Agent Functions:
# called: $myAgent = new Agent();
# called: $myAgent->getPercepts($myEnvironment);
# called: $myAgent->updateKB();
# called: $myAgent->updateState();
# called: $myAgent->displayState($myEnviroment);
# called: $myAgent->shootArrow($myEnvironment);
# called: $myAgent->grabGold($myEnvironment);
# called: $myAgent->turnLeft($myEnvironment);
# called: $myAgent->turnRight($myEnvironment);
# called: $myAgent->stepForward($myEnvironment);

# Create new Environment as a 4x4 matrix.
# Pits will be placed in 20% of the rooms.
# The wumpus and the gold will each be placed in a randomly chosen room as well.
# Pits, the wumpus, and the gold may overlap.
# In all situations, room [0,0] is off limits.

my $cave = new Environment(4,4);	# Warning: the game has not been tested 
					# with dimensions other than 4x4.
					# Experiment at your own risk.

# Create a new Agent
# He will begin in room [0,0], facing to the east.
# The Agent knows his current location, his orientation,
# and whether or not he is still carrying an arrow to shoot.
# His beliefs about the world are held in an array called @state
# His Knowledge Base (propositions) are held in an array called KB

my $tim = new Agent();

# Clear the screen and display the current state of the Environment.
system("clear");
$cave->displayState();

# Run the Agent's sensors to detect [Stench,Breeze,Glitter,Bump,Scream]
# and insert his new beliefs about the Environment into his knowledge base.

$tim->getPercepts($cave);
$tim->updateKB();
$tim->updateState();

# Display the Agent's current view of the world and the
# propositions currently contained within his knowledge base.

$tim->displayState($cave);

# Pass commands to the Agent, allow him to interact with 
# his Environment, run his sensors, and display both 
# the real state of the Environment and his view of it.

my $command;
while (1) {
	print "[(f)orward,(l)eft,(r)ight,(g)rab,(s)hoot]\nCommand? ";
	chomp($command = <STDIN>);
	system("clear");
	if    ($command eq 'f') { $tim->stepForward($cave); }
	elsif ($command eq 'l') { $tim->turnLeft($cave); }
	elsif ($command eq 'r') { $tim->turnRight($cave); }
	elsif ($command eq 'g') { $tim->grabGold($cave); }
	elsif ($command eq 's') { $tim->shootArrow($cave); }
	$cave->displayState();
	$tim->getPercepts($cave);
	$tim->updateKB();
	$tim->updateState();
	$tim->displayState($cave);
}
