AI Lab Assignment
AI Lab Assignment
male(ali).
male(ajmal).
male(waqas).
male(abid).
male(child1).
female(sara).
female(najma).
female(naila).
female(faria).
female(child2).
parent(sara, ajmal).
parent(ali, najma).
parent(child1, ali).
parent(child1, sara).
parent(child2, ali).
parent(child2, sara).
married(ali, sara).
married(sara, ali).
married(waqas, naila).
married(naila, waqas).
married(abid, faria).
Page 1|9
married(faria, abid).
sister_in_law_of(SInLaw,SiblingInLaw):-
married(Person,SiblingInLaw),sister_of(SInLaw,Person).
sister_in_law_of(SInLaw,SiblingInLaw):-brother_of(Brother,SiblingInLaw),married(Brother,
SInLaw).
sister_in_law_of(SInLaw,SiblingInLaw):-married(Person,SiblingInLaw),
brother_of(Brother,Person), married(SInLaw,Brother).
descendent_of(Descendent,Person) :- parent_of(Person,Descendent).
descendent_of(Descendent,Person):-parent_of(MaleChild,Descendent),
descendent_of(MaleChild,Person).
insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT).
insert(X,[Y|T],[X,Y|T]):-X=<Y.
insert(X,[],[X]).
Page 2|9
Problem 3: Recursive functions in LISP [3 marks]
This problem involves writing Lisp code recursively (what else is there?).
Write two functions, each taking two arguments. One is name BEFORE and one is named
AFTER. The first argument is an atom and the second is a list in both cases. You may assume
that the atom occurs in the list. BEFORE Returns a list containing the part of the list before the
first occurrence of the atom; AFTER returns the part of the list coming after the first occurrence
of the atom.
Here are some examples:
A 90 80 75 70
B 35 85 55 65
C 125 95 90 95
D 45 110 95 115
E 50 100 90 100
The problem is to assign each worker to at most one task, with no two workers performing the
same task, while minimizing the total cost. Since there are more workers than tasks, one worker
will not be assigned a task.
// TicTacToeLight.java
import ch.aplu.jgamegrid.*;
import ch.aplu.tcp.*;
import java.awt.Color;
import javax.swing.JOptionPane;
public class TicTacToeLight extends GameGrid
implements GGMouseListener, TcpNodeListener
{
private String sessionID = "ama&19td&ap";
private final String nickname = "tic";
private TcpNode tcpNode = new TcpNode();
private boolean isMyMove = false;
private char mySign;
private char teammateSign;
public TicTacToeLight()
{
super(3, 3, 80, Color.blue, null, false);
setBgColor(Color.lightGray);
setTitle("TicTacToe Light");
addStatusBar(30);
addMouseListener(this, GGMouse.lClick);
Page 4|9
tcpNode.addTcpNodeListener(this);
show();
connect();
}
public boolean mouseEvent(GGMouse mouse)
{
if (!isMyMove)
return true;
Location loc = toLocationInGrid(mouse.getX(), mouse.getY());
if (getOneActorAt(loc) != null)
{
setStatusText("Cell occupied!");
return true;
}
addMark(mySign, loc);
tcpNode.sendMessage("" + loc.x + loc.y); // Send move
if (isOver())
return true;
setStatusText("Wait for teammate's move.");
isMyMove = false;
return true;
}
private void connect()
{
String id = requestEntry("Enter room name (more than 3 characters):");
sessionID = sessionID + id;
tcpNode.connect(id, nickname);
}
public void messageReceived(String sender, String text)
{
int x = text.charAt(0) - 48; // We get ASCII code of number
int y = text.charAt(1) - 48;
Location loc = new Location(x, y);
addMark(teammateSign, loc);
if (isOver())
return;
isMyMove = true;
setStatusText("It's your move.");
Page 5|9
}
private void addMark(char sign, Location loc)
{
if (sign == 'X')
addActor(new MarkX(), loc);
else
addActor(new MarkO(), loc);
}
private boolean isBoardFull()
{
return getActors().size() == 9;
}
private void gameOver(String reason)
{
setStatusText(reason);
isMyMove = false;
addActor(new Actor("sprites/gameover.gif"), new Location(1, 1));
}
private char getMark(int i, int k)
{
Actor actor = getOneActorAt(new Location(i, k));
if (actor == null)
return ' ';
else
return (actor instanceof MarkX) ? 'X' : 'O';
}
private boolean isOver()
{
// Convert board state into string pattern
StringBuffer pattern = new StringBuffer();
// Horizontal
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < 3; k++)
{
pattern.append(getMark(i, k));
}
pattern.append(','); // Separator
}
Page 6|9
// Vertical
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
pattern.append(getMark(i, k));
}
pattern.append(',');
}
// Diagonal
for (int i = 0; i < 3; i++)
pattern.append(getMark(i, i));
pattern.append(',');
for (int i = 0; i < 3; i++)
pattern.append(getMark(i, 2 - i));
if (pattern.toString().contains("XXX"))
{
gameOver("Game over. " + (mySign == 'X' ? "You won " : "Teamate won"));
return true;
}
if (pattern.toString().contains("OOO"))
{
gameOver("Game over. " + (mySign == 'O' ? "You won " : "Teamate won"));
return true;
}
if (isBoardFull())
{
gameOver("Game over. It's a draw");
return true;
}
return false;
}
public void nodeStateChanged(TcpNodeState state)
{
}
public void statusReceived(String text)
{
if (text.contains("In session:--- (0)")) // we are first player
{
setStatusText("Connected. Wait for teammate.");
mySign = 'O';
Page 7|9
teammateSign = 'X';
}
else if (text.contains("In session:--- (1)")) // we are second player
{
setStatusText("It's your move.");
mySign = 'X';
teammateSign = 'O';
isMyMove = true; // Second player starts
}
else if (text.contains("In session:--- ")) // we are next player
{
setStatusText("Game in progress. Terminating now...");
TcpTools.delay(4000);
System.exit(0);
}
}
private String requestEntry(String prompt)
{
String entry = "";
while (entry.length() < 3)
{
entry = JOptionPane.showInputDialog(null, prompt, "");
if (entry == null)
System.exit(0);
}
return entry.trim();
}
public static void main(String[] args)
{
new TicTacToeLight();
}
}
Page 8|9
import numpy as np
Page 9|9