Skip to content

Commit

Permalink
Assignment 2 and Lab 4
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyDC committed Oct 28, 2016
1 parent f7fbd08 commit 30e5c3e
Show file tree
Hide file tree
Showing 15 changed files with 39,625 additions and 0 deletions.
215 changes: 215 additions & 0 deletions TCSS 143/Assignments/Assignment 2/LostPuppy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*
* TCSS 143 - Winter 2016
* Instructor: Raghavi Sakpal
* Assignment #2 - LostPuppy Game
*/

import java.util.Random;
import java.util.Scanner;

/**
* Contains methods for use in PuppyPlay driver
*
* @author Skylar Cowan [email protected]
* @version 25 October 2016
*/

public class LostPuppy {
private char[][] myHidingPlaces; // Rows = #floors, Columns = #rooms on floor
private int myFloorLocation;
private int myRoomLocation;
private char myWinner;
private boolean myFound;

/**
* Constructor for lost puppy, creates LostPuppy object and generates
* a random floor/room location for the puppy based on array dimensions.
* All values are initialized within the object for the game.
*
* @param numFloors number of floors (must be above 0)
* @param numRooms number of rooms per floor (must be above 0)
*/
public LostPuppy(int numFloors, int numRooms) {
int row = validateFloors(numFloors);
int col = validateRooms(numRooms);
this.myHidingPlaces = new char[row][col];
for (int floor = 0; floor < myHidingPlaces.length; floor++) {
for (int room = 0; room < myHidingPlaces[floor].length; room++) {
myHidingPlaces[floor][room] = ' ';
}
}

Random rn = new Random();
myFloorLocation = rn.nextInt(row); // Generate random floor location for puppy
myRoomLocation = rn.nextInt(col); // Generate random room location for puppy
myHidingPlaces[myFloorLocation][myRoomLocation] = 'P'; // Set puppy location
myWinner = ' '; // Set myWinner to empty
myFound = false; // Set myFound false
}

/**
* Checks if the given floor and room have already been searched.
*
* @param floor the floor to search
* @param room the room to search
* @return true if room has been searched, false otherwise
*/
public boolean roomSearchedAlready(int floor, int room) {
if (myHidingPlaces[floor][room] == '1' || myHidingPlaces[floor][room] == '2') {
// Notify if room has already been searched
System.out.println("Sorry! That room has been searched already!");
return true;
} else {
return false;
}
}

/**
* Determines if the puppy is in the room on the given floor.
*
* @param floor the floor to check
* @param room the room to check
* @return true if puppy is here, false otherwise
*/
public boolean puppyLocation(int floor, int room) {
if (myHidingPlaces[floor][room] == 'P') {
return true;
}
else {
return false;
}
}

/**
* Determines if the given floor and room is within possible locations.
*
* @param floor number to check if in range
* @para room number to check if in range
* @return true if location is within the array, false otherwise
*/
public boolean indicesOK(int floor, int room) {
// Print correct range for guessing if either number was out of bounds
if ((floor < 0 || room < 0) || (floor > numberOfFloors() - 1 || room > numberOfRooms() - 1)) {
System.out.println("Numbers must be between [0-" + (numberOfFloors() - 1) + "] and [0-" +
(numberOfRooms() - 1) + "]!");
}
return ((floor < myHidingPlaces.length && room < myHidingPlaces[0].length) &&
(floor >= 0 && room >= 0));
}

/**
* Gets and returns the number of floors.
*
* @return number of floors in array
*/
public int numberOfFloors() {
return myHidingPlaces.length;
}

/**
* Gets and returns the number of rooms.
*
* @return number of rooms in array
*/
public int numberOfRooms() {
return myHidingPlaces[0].length;
}

/**
* Checks room to see if the room contains the puppy, if not, insert player number.
*
* @param floor the floor to check
* @param room the room to check
* @param player the player searching the room (1 or 2)
* @return true if puppy is in the room and player wins, false otherwise and player
* number is placed in room
*/
public boolean searchRoom(int floor, int room, char player) {
if (puppyLocation(floor, room)) {
myFound = true;
myWinner = player;
return true;
}
else {
myHidingPlaces[floor][room] = player;
return false;
}
}

/**
* Validates that the number of floors is greater than zero.
*
* @param numFloors the number of floors
* @return validated number of floors
*/
public int validateFloors(int numFloors) {
Scanner input = new Scanner(System.in);
int floors = numFloors;
while (floors < 1) {
System.out.println("Oops! The number of floors you entered is not valid" +
"\n" + "Please enter another number above 0:");
floors = input.nextInt();
}
return floors;
}

/**
* Validates that the number of rooms is greater than zero.
*
* @param numRooms the number of rooms
* @return validated number of rooms
*/
public int validateRooms(int numRooms) {
Scanner input = new Scanner(System.in);
int rooms = numRooms;
while (rooms < 1) {
System.out.println("Oops! The number of rooms you entered is not valid" +
"\n" + "Please enter another number above 0:");
rooms = input.nextInt();
}
return rooms;
}

/**
* Converts LostPuppy two-dimensional array into a string and
* prints it out in a nicely formatted display.
*
* @return printed out LostPuppy game object
*/
public String toString() {
int floors = myHidingPlaces.length;
int rooms = myHidingPlaces[0].length;

System.out.print(" ");
// Print top of the grid
for (int i = 0; i < rooms; i++) {
System.out.print("___");
}
for (int j = 0; j < rooms - 1; j++) {
System.out.print("_");
}
System.out.print("\n");

// Print out floors and rooms with their contents
for (int floor = floors - 1; floor >= 0; floor--) {
System.out.print("| ");
for (int room = 0; room <= rooms - 1; room++) {
if (myHidingPlaces[floor][room] == 'P' && myFound) {
System.out.print("" + myWinner + "" + myHidingPlaces[floor][room] + "| ");
}
else if (myHidingPlaces[floor][room] == 'P' && !myFound) {
System.out.print(" | ");
}
else {
System.out.print(myHidingPlaces[floor][room] + " | ");
}
}
System.out.print("\n");
for (int i = 0; i < rooms; i++) {
System.out.print("|___");
}
System.out.print("|\n");
}
return "";
}
}
Binary file not shown.
77 changes: 77 additions & 0 deletions TCSS 143/Assignments/Assignment 2/PuppyPlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@


import java.util.*;
/**
* This program is used as a driver program to play the game from the
* class LostPuppy.
*
* A puppy is lost in a multi-floor building represented in the class
* LostPuppy.class. Two players will take turns searching the building
* by selecting a floor and a room where the puppy might be.
*
* @author David Schuessler
* @version Fall 2013
*/


public class PuppyPlay{
/**
* @param args may contain file names in an array of type String
*/
public static void main(String[] args){
Scanner s = new Scanner(System.in);
LostPuppy game;
int totalFloors;
int totalRooms;
int floor;
int room;
char[] players = {'1', '2'};
int playerIndex;
boolean found = false;
Random rand = new Random();

do {
System.out.print("To find the puppy, we need to know:\n"
+ "\tHow many floors are in the building\n"
+ "\tHow many rooms are on the floors\n\n"
+ "Please enter the number of floors: ");
totalFloors = s.nextInt();
System.out.print("Please enter the number of rooms on the floors: ");
totalRooms = s.nextInt();
s.nextLine(); // Consume previous newline character

// Start the game
game = new LostPuppy(totalFloors, totalRooms);

// Pick starting player
playerIndex = rand.nextInt(2);

System.out.println("\nFloor and room numbers start at zero '0'");

do {

do {
System.out.println("\nPlayer " + players[playerIndex]
+ ", enter floor and room to search separated by a space: ");
floor = s.nextInt();
room = s.nextInt();

//for testing, use random generation of floor and room
//floor = rand.nextInt(totalFloors);
//room = rand.nextInt(totalRooms);
} while (!game.indicesOK(floor, room)
|| game.roomSearchedAlready(floor, room));

found = game.searchRoom(floor, room, players[playerIndex]);
playerIndex = (playerIndex + 1) % 2;
System.out.println("\n[" + floor + "], [" + room + "]");
System.out.println(game.toString());
s.nextLine();
} while (!found);

playerIndex = (playerIndex + 1) % 2;
System.out.println("Great job player " + players[playerIndex] +"!");
System.out.println("Would you like to find another puppy [Y/N]? ");
} while (s.nextLine().equalsIgnoreCase("Y"));
}
}
39 changes: 39 additions & 0 deletions TCSS 143/Labs/Lab 4/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Program Name: Author.java
Programmer: Skylar Cowan
Date: 10/19/2016
Purpose: To be able to use the Author object in TestAuthor.java
storing the name, email and gender of the Author
*/

public class Author {
private String name;
private String email;
private char gender;

public Author (String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
}

public String getName() {
return this.name;
}

public String getEmail() {
return this.email;
}

public char getGender() {
return this.gender;
}

public void setEmail(String email) {
this.email = email;
}

public String toString() {
return this.getName() + " (" + this.getGender() + ") at " + this.getEmail();
}
}
Loading

0 comments on commit 30e5c3e

Please sign in to comment.