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.
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.