Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions org/asl/socketserver/games/DiceGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.asl.socketserver.games;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import org.asl.socketserver.AbstractGame;
import org.asl.socketserver.MenuInfo;
import org.asl.socketserver.Servable;

@MenuInfo(authors = {
"Vikram Chowdhary" }, version = "Jan 2018", title = "Dice", description = "Roll some dice.")


public class DiceGame extends AbstractGame implements Servable {

public DiceGame() {
//empty constructor
}

private static int roll() {
int result1 = (int) (Math.random() * 6 + 1);
int result2 = (int) (Math.random() * 6 + 1);
int result = result1 + result2;
return result;
}

@Override
public void serve(BufferedReader input, PrintWriter out) throws IOException {

int ROLLS = 0;

int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
int ten = 0;
int eleven = 0;
int twelve = 0;

// greeting
out.println("This program simulates the rolling of a pair of dice."
+ "\nYou enter the number of times you want the computer to 'roll' the dice."
+ "\nWatch out, very large numbers take a long time. In particular, numbers over 5000." + "\n"
+ "\nHow many rolls? ");
ROLLS = input.read();

// rolling + counting
for (int i = 0; i < ROLLS; i++) {
int roll = roll();
if (roll == 2) {
two++;
} else if (roll == 3) {
three++;
} else if (roll == 4) {
four++;
} else if (roll == 5) {
five++;
} else if (roll == 6) {
six++;
} else if (roll == 7) {
seven++;
} else if (roll == 8) {
eight++;
} else if (roll == 9) {
nine++;
} else if (roll == 10) {
ten++;
} else if (roll == 11) {
eleven++;
} else if (roll == 12) {
twelve++;
}
}

// calculating
double twoProp = ((double) two / ROLLS) * 100;
double threeProp = ((double) three / ROLLS) * 100;
double fourProp = ((double) four / ROLLS) * 100;
double fiveProp = ((double) five / ROLLS) * 100;
double sixProp = ((double) six / ROLLS) * 100;
double sevenProp = ((double) seven / ROLLS) * 100;
double eightProp = ((double) eight / ROLLS) * 100;
double nineProp = ((double) nine / ROLLS) * 100;
double tenProp = ((double) ten / ROLLS) * 100;
double elevenProp = ((double) eleven / ROLLS) * 100;
double twelveProp = ((double) twelve / ROLLS) * 100;

// construct message
out.println("2 -- " + two + "/" + ROLLS + " -- " + twoProp + "%" + "\n3 -- " + three + "/" + ROLLS + " -- "
+ threeProp + "%" + "\n4 -- " + four + "/" + ROLLS + " -- " + fourProp + "%" + "\n5 -- " + five + "/"
+ ROLLS + " -- " + fiveProp + "%" + "\n6 -- " + six + "/" + ROLLS + " -- " + sixProp + "%" + "\n7 -- "
+ seven + "/" + ROLLS + " -- " + sevenProp + "%" + "\n8 -- " + eight + "/" + ROLLS + " -- " + eightProp
+ "%" + "\n9 -- " + nine + "/" + ROLLS + " -- " + nineProp + "%" + "\n10 -- " + ten + "/" + ROLLS
+ " -- " + tenProp + "%" + "\n11 -- " + eleven + "/" + ROLLS + " -- " + elevenProp + "%" + "\n12 -- "
+ twelve + "/" + ROLLS + " -- " + twelveProp + "%");

}

}
111 changes: 111 additions & 0 deletions org/asl/socketserver/games/bagelsGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.asl.socketserver.games;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import org.asl.socketserver.AbstractGame;
import org.asl.socketserver.MenuInfo;
import org.asl.socketserver.Servable;

@MenuInfo(authors = {
"Vikram Chowdhary" }, version = "Jan 2018", title = "Bagels", description = "Guess a number.")

public class bagelsGame extends AbstractGame implements Servable {

public bagelsGame() {
//empty constructor
}

private static String generateAnswer() {
/*generate 3 numbers between 0 and 9
generated separately so that all 3 digits are present (000, 001, 012, 111)
*/
String dig1 = Integer.toString((int)(Math.random() * 9 + 0));
String dig2 = Integer.toString((int)(Math.random() * 9 + 0));
String dig3 = Integer.toString((int)(Math.random() * 9 + 0));
//separate digits added together
String ansStr = dig1 + dig2 + dig3;
//convert ansStr to int
return ansStr;
}

@Override
public void serve(BufferedReader input, PrintWriter out) throws IOException {

String guess = "";
//int guessInt = 0;
int count = 0;
String message = "";

//greeting + rules
out.println("Welcome to the Bagels Game."
+ "\nI am thinking of a 3-digit number."
+ "\nTry to guess my number and I will give you clues as follows:"
+ "\n PICO: one digit correct but in the wrong position"
+ "\n FERMI: one digit correct and in the right position"
+ "\n BAGELS: no digits correct");
//pt 2 of greeting
out.println("OK, I have a number in mind.");


// generate answer
String ANSWER = generateAnswer();
//System.out.println(ANSWER);
//convert to array
char[] ANSWERarray = ANSWER.toCharArray();



/* Start guessing
will loop until correct guess
needs to be redone with args??
*/
while (!guess.equals(ANSWER)) {
count++;
//gets guess
out.println("Enter guess #" + count + ": ");
//guessInt = input.nextInt();
guess = input.readLine();
if (guess.equals(ANSWER)) {
message = "You got it!";
break;
}

//make array w/ 3 slots
char[] guessArray = guess.toCharArray();


//PICO/FERMI/BAGEl
if (guessArray[0] == ANSWERarray[0]) {
message += "FERMI ";
} else if (guessArray[0] == ANSWERarray[1] || guessArray[0] == ANSWERarray[2]) {
message += "PICO ";
} else {
message += "BAGEL ";
}
if (guessArray[1] == ANSWERarray[1]) {
message += "FERMI ";
} else if (guessArray[1] == ANSWERarray[0] || guessArray[1] == ANSWERarray[2]) {
message += "PICO ";
} else {
message += "BAGEL ";
}
if (guessArray[2] == ANSWERarray[2]) {
message += "FERMI ";
} else if (guessArray[2] == ANSWERarray[0] || guessArray[2] == ANSWERarray[1]) {
message += "PICO ";
} else {
message += "BAGEL ";
}

out.println(message);
message = "";
}

//congratulations!
out.println("You got it!!");
/*String goAgain = readLine("Do you want to play again? (y/n) ");
if ()
*/
}
}