PDA

View Full Version : CS Help again, Still Simple



bullet2urbrain
06-07-2006, 08:47 PM
Hey all, have to make a rock paper scissors game against the computer, heres what i've got. There is just a problem with accessing the function. I am missing a parameter or an Argument somewhere.


import java.util.Random;
/**
* ComputerChooser is a class that can be used to help play the game
* of Scissors - Paper - Rock.
*
* (put in any information your TA requests for your labs)
*
* @author William Austad and (put your name here)
* @version (fill in the date)
*
*/
public class computerChooser
{
// instance variables - replace the example below with your own
private Random rand;

/**
* Constructor for objects of class ComputerChooser
*/
public computerChooser()
{
rand = new Random();
}

/**
* getSPR returns one of the characters, 'S', 'P' or 'R'.
* It is equally likely to return each of the 3 possible
* return values.
*
* @return one of 'S', 'P' or 'R'
*/
public char getSPR()
{

// The student is to complete this function
// Use rand to generate a pseudorandom number from 0 through 2


int n = rand.nextInt(2);

if (n==0){
return('S');
}else if (n==1){
return('P');
}else{
return('R');
}
}
}




--------------That is the program for the Computer's Choice------------

import java.util.Scanner;
/**
*
* This is the function with the logic for HW5
* it goes through a couple counters and has input
*
* * @author William Austad and James Mikolich
* * @version 5/31/06
* * Section A02
* */
public class MenuProg
{
private static Scanner scan;

public static void main(String [] args)
{
computerChooser Jim = new computerChooser();

int tie = 0;
int userWin = 0;
int puterWin = 0;

// initialize the Scanner
scan = new Scanner(System.in);

do {
switch(getMenuResponse()) {
case 0: userGetScissor(); break;
case 1: userGetPaper(); break;
case 2: userGetRock(); break;

default: System.err.println("How did we get here?");
} // of switch
if((Jim.getSPR('R')) && (userGetRock())){
tie++;
}else if (Jim.getSPR('R') && userGetPaper()){
userWin++;
}else if (Jim.getSPR('R') && userGetScissor()){
puterWin++;
}else if (Jim.getSPR('P') && userGetPaper()){
tie++;
} else if (Jim.getSPR('P') && userGetScissor()){
userWin++;
} else if (Jim.getSPR('P') && userGetRock()){
puterWin++;
} else if (Jim.getSPR('S') && userGetSicssor()){
tie++;
} else if (Jim.getSPR('S') && userGetPaper()){
puterWin++;
} else if (Jim.getSPR('S') && userGetRock()){
userWin++;
}

} while(wantsToContinue());
}

public static char userGetRock()
{
return('R');
}

public static char userGetPaper()
{
return('P');
}

public static char userGetScissor()
{
return('S');
}
private static void displayMenu()
{
System.out.println("Welcome to Rock Paper Scissors!");
System.out.println("Enter 0 for Rock, 1 for Paper, and 2 for Scissors.");
}

private static boolean wantsToContinue()
{
char yesOrNo;
do{
System.out.print("Do you want to play again? (Y/N)");
String response = scan.next();

yesOrNo = Character.toUpperCase(response.charAt(0));

}while (yesOrNo != 'S' && yesOrNo != 'P' && yesOrNo != 'R');
return (yesOrNo == 'Y');
}

private static boolean isGoodResponse(int resp)
{
return((resp <= 2) && (resp >= 0));
}

private static int getMenuResponse()
{
int response = -1;
boolean done = false;

do{
displayMenu();
if(scan.hasNextInt()){
response = scan.nextInt();

if(isGoodResponse(response)){
done = true;
}
} else {
String toJim = scan.next();
System.err.println(toJim + "is not an int.");
}
}while (!done);
return(response);
}


}

---That is the logic and stuff, there's a problem with the getSPR method---


Thanks in advance guys, you are awesome.

eshbach
06-07-2006, 09:20 PM
I redid the MenuProg class. It seems to work just fine. Here's the code. anything you want clarified, just ask.


public class MenuProg
{
private static java.io.BufferedReader scan;

public static void main(String[] args)
{
scan = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

int tie = 0;
int userWin = 0;
int puterWin = 0;

boolean cont = true;
int selection = -1;

computerChooser compPlayer = new computerChooser();

System.out.println("Welcome to Rock Paper Scissors!");

while (cont)
{
while (!(selection == 0 || selection == 1 || selection == 2))
{
System.out.println("Enter 0 for Rock, 1 for Paper, and 2 for Scissors.");
selection = Integer.parseInt(getResponse());
}
char compPic = compPlayer.getSPR();
if (selection == 0)
{
if (compPic == 'R')
{
++tie;
System.out.println("You both picked rock, tie");
}
else if (compPic == 'P')
{
++puterWin;
System.out.println("Paper covers rock, you lose");
}
else
{
++userWin;
System.out.println("Rock crushes scissors, you win");
}
}
else if (selection == 1)
{
if (compPic == 'R')
{
++userWin;
System.out.println("Paper covers rock, you win");
}
else if (compPic == 'P')
{
++tie;
System.out.println("You both picked paper, tie");
}
else
{
++puterWin;
System.out.println("Scissors cut paper, you lose");
}
}
else if (selection == 2)
{
if (compPic == 'R')
{
++puterWin;
System.out.println("Rock crushes scissors, you lose");
}
else if (compPic == 'P')
{
++userWin;
System.out.println("Rock crushes scissors, you lose");
}
else
{
++tie;
System.out.println("You both picked scissors, tie");
}
}
System.out.println("You have won " + userWin + " times.");
System.out.println("You have tied " + tie + " times.");
System.out.println("You have lost " + puterWin + " times.\n");
cont = wantsToContinue();
selection = -1;
}
}


private static boolean wantsToContinue()
{
String response = "";
System.out.print("Do you want to play again? (Y/N)");
while (!response.equalsIgnoreCase("y") && !response.equalsIgnoreCase("n"))
{
response = getResponse();
if (response.equalsIgnoreCase("y"))
{
return true;
}
else if (response.equalsIgnoreCase("n"))
{
return false;
}
else
{
System.out.println("Please enter Y or N");
}
}
System.out.println();
return false;
}

private static String getResponse()
{
try
{
return scan.readLine();
}
catch (java.io.IOException ex)
{
System.err.println("An IO Error Has Occurred while getting the user response.");
}
return null;
}
}

bullet2urbrain
06-08-2006, 05:52 AM
How in the Heck did you do that???? :D

Im curious tho, what does io.bufferedreadscan do? obviously it works like a scanner but is there any difference,

also the "!" infront of everything you have is logical for "Not" correct?

Sorry this is a really simple minded CS Class, its the one for "non tech" majors who have to take one programming to get a degree. But thanks alot for your help, I credited you in the title but i dont know your full name so you were credited with Eshbach from XtremeSystems.org

Thanks a whole bunch, i only had to change a couple things to fit the more specific criteria of my program.


XS.org rocks my socks again

eshbach
06-09-2006, 02:37 AM
How in the Heck did you do that???? :D

Im curious tho, what does io.bufferedreadscan do? obviously it works like a scanner but is there any difference,

also the "!" infront of everything you have is logical for "Not" correct?

Sorry this is a really simple minded CS Class, its the one for "non tech" majors who have to take one programming to get a degree. But thanks alot for your help, I credited you in the title but i dont know your full name so you were credited with Eshbach from XtremeSystems.org

Thanks a whole bunch, i only had to change a couple things to fit the more specific criteria of my program.


XS.org rocks my socks again

the java scanner is a full-featured input class. you were just reading console input, so you don't really "need" a scanner, you just need a reader.

the buffered reader i used is very simple and makes reading console input extremely easy. it is constructed from an input stream reader, which is in turn constructed from an input sream (logically). in your case, the input stream is system.in (which is the counterpart to system.out, which you use to do your printing).

the ! is a logical NOT, that's correct.

happy to help :)