Anda di halaman 1dari 8

FINAL

PROJECT
By:
Adlawan, Dominic Bien
Decamotan, Edison
Galceran, Quint
import java.util.*;
public class FeedDoggy
{
public static Scanner c= new Scanner (System.in);

public static void main (String args[])


{
System.out.println("NUMBER INDICATIONS: ");
System.out.println(" 2 is the DOG IF initial state is not yet the answer");
System.out.println(" 1 is the AGENT");
System.out.println(" 3 indicates that the Dog and the Agent are already in the same room");

int rooms;
System.out.print("\nEnter number of rooms: ");
rooms= c.nextInt();

//FOR DOG
String dog; int d; int dd; int valid=1; int dogcount=0; //valid checks user's input: 1 for correct and 0
for wrong inputs
System.out.println("\nMark with '2' the room where the DOG is initially at, '0' for the two other
rooms:");
dog= c.next();

if (dog.length()<(rooms) || dog.length()>rooms ){ //if user inputs less/ more than no. of rooms,
program terminates
System.out.println("Input lacks/ exceeds, re-run program!");
return;
}

for (d=0; d<dog.length(); d++){ //checks if user inputs 2's and 0's only
if (dog.charAt(d)!='2' && dog.charAt(d)!='0'){
valid=0;
}
}

if (valid==0){ //if user inputs other than 2's and 0's, program terminates
System.out.println("Input must be 2's and 0's only, re-run program!");
return;
}

for (dd=0; dd<dog.length(); dd++){ //counta no. of dogs the user inputs
if (dog.charAt(dd)=='2'){
dogcount++;
}
}

//FOR AGENT
String agent; int a, aa; int valid1= 1; int agentcount=0;
System.out.println("Mark '1' the room where the AGENT is initially at, '0' for the other rooms: ");
agent= c.next();

if (agent.length()<(rooms) || agent.length()>rooms ){ //if user inputs less/ more than no. of


rooms, program terminates
System.out.println("Input lacks/ exceeds, re-run program!");
return;
}

for (a=0; a<agent.length(); a++){ //checks if user inputs 1's and 0's only
if (agent.charAt(a)!='1' && agent.charAt(a)!='0'){
valid1=0;
}
}

if (valid1==0){ //if user inputs other than 1's and 0's, program terminates
System.out.println("Input must be 1's and 0's only, re-run program!");
return;
}

for (aa=0; aa<agent.length(); aa++){


if (agent.charAt(aa)=='1'){
agentcount++;
}
}

if (dogcount>1 || dogcount<1 ){ //if user has less/ more than dogs and agents, program
terminates
System.out.println("\nONLY 1 DOG AND 1 AGENT SHOULD BE IN THE LOCATION. PLEASE RUN
THE PROGRAM AGAIN.");
return;
}else if( agentcount>1 || agentcount<1){
System.out.println("\nONLY 1 DOG AND 1 AGENT SHOULD BE IN THE LOCATION. PLEASE RUN
THE PROGRAM AGAIN.");
return;
}
else{
System.out.println("\nOnly 1 Dog and 1 Agent found.");
}

int ddd; int aaa;

System.out.println("\nMERGING THE DOG AND AGENT LOCATION");


System.out.println(" GIVES THE RESULT BELOW:");

for (ddd=0; ddd<rooms; ddd++){ //finds the room of dog and agent
if (dog.charAt(ddd)=='2'){
break;
}
}
for (aaa=0; aaa<rooms; aaa++){
if (agent.charAt(aaa)=='1'){
break;
}
}

int room; int found; int cont=1; //cont checks if dog and agent arent in same room yet, reads the
next lines below
for (room=0; room<rooms; room++){

if (ddd==room && aaa==room){ //if in the same room already, prints "found" statement and
terminates the program.

System.out.print("\t");
for (found=0; found<rooms; found++){
if (ddd==found){
System.out.print(3);
}else{
System.out.print("0");
}
}
System.out.print("\n");
System.out.println("\nAGENT ALREADY FOUND THE DOG. DOG FED!");
//if dog and agent are in the same room the value of cont will set to 0
cont=0;
break;
}
}

int room1; int roomDog=0; int roomAgent= 0;


if (cont==1){ //if value of cont didnt change to 0 and still 1, reads this next block of codes
System.out.print("\t");
for (room1=0; room1<rooms; room1++){
if (ddd==room1){
//dog.chaAt(ddd) = 2
System.out.print(dog.charAt(ddd));
roomDog= room1+1;
}else if (aaa==room1){
//dog.charAt(aaa) = 1
System.out.print(agent.charAt(aaa));
roomAgent= room1+1;
}else {
System.out.print("0");
}
}
System.out.print("\n");
System.out.println("\nDog is in room number: "+roomDog);
System.out.println("Agent is in room number: "+roomAgent);

//calls the method search


search(rooms,ddd, aaa, dog, agent);
}
}

//a method for searching the dog's room


public static void search(int rooms,int ddd,int aaa,String dog,String agent){
int room1; int i; int roomDog=0; int roomAgent=0;
System.out.println("\nInitial State:");
System.out.print("\t");

for (room1=0; room1<rooms; room1++){ //prints no. of room the dog and agent are
if (ddd==room1){
System.out.print(dog.charAt(ddd));
}else if (aaa==room1){
System.out.print(agent.charAt(aaa));
}else {
System.out.print("0");
}
}
System.out.println("\n");

int room; int found; int cont=1;


do{
for (room=0; room<rooms; room++){ //checks if agent and dog are already in the same room
if (ddd==room && aaa==room){
System.out.print("\t");
for (found=0; found<rooms; found++){
if (ddd==found){
System.out.print(3);
}else{
System.out.print("0");
}
}
System.out.print("\n");
System.out.println("AGENT ALREADY FOUND THE DOG. DOG FED!");
cont=0;
break;
}
}

//searching the dog's room


if (cont==1){
System.out.println("\nAgent is searching for the Dog:");
System.out.print("\t");
for (i=0; i<rooms; i++){
if (ddd==i){
System.out.print(2);
roomDog= i+1;
}else if (aaa==i){
System.out.print(1);
roomAgent= i+1;
}else {
System.out.print("0");
}
}
System.out.println("\nPress any key to continue ...");
String key= c.nextLine();
i=0;
if (roomAgent<roomDog){ //if the agent's room is at the left room(ex. 1002, 0120,01002 ),
aaa ( aaa is the variable name of the room of the agent ) will increment by 1.
aaa++;
}
else if (roomAgent>roomDog){ //if the agent's room is at the right room(ex. 2001,
0210,02001 ), aaa ( aaa is the variable name of the room of the agent ) will decrement by 1.
aaa--;
}
//by incrementing and decrementing the agent's room, its room will change until it finds the
dog's room
roomAgent=0;
roomDog=0;
cont=1;
}
}while (ddd!=aaa);

int j; int k=0;


System.out.println("\n\nAGENT FOUND THE DOG.");
for (j=0; j<rooms; j++){
if (ddd==j && aaa==j){
System.out.print("\t");
for (k=0; k<rooms; k++){
if (ddd==k){
System.out.print(3);
}else{
System.out.print("0");
}
}
System.out.print("\n");
System.out.println("\nDOG FED!");

break;
}
}
}
}

Anda mungkin juga menyukai