#How to get my code to let me input something?
1 messages ยท Page 2 of 1
yes, numPlayerWins++
like this?
System.out.printf(CPU_MOVE, cpuMove);
int outcome = determineWinner(playerMove, cpuMove);
if (outcome == PLAYER_WIN_OUTCOME) {
numPlayerWins++;
System.out.println(PLAYER_WIN);
} else if (outcome == CPU_WIN_OUTCOME) {
numCPUWins++;
System.out.println(CPU_WIN);
} else if (outcome == TIE_OUTCOME) {
numTies++;
System.out.println(TIE);
}
numGames++;
Detected code, here are some useful tools:
do i call the displayStats function afterwards?
wait
before the numGames++, add the moves to the arrays
outside of the if statements?
yes
we call that at the end of the while input thing
we don't want it repeat
ah ok
check if the code works
i think it works
show output
i tried to make it output the array
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> java RPS
Let's play! What's your move? (Type the move or q to quit)
f
test
That is not a valid move. Please try again.
rock
test
Me: rock, You: rock
I chose rock. It's a tie.
[Ljava.lang.String;@7699a589
rock
test
Me: rock, You: scissors
I chose scissors. You win.
[Ljava.lang.String;@58372a00
Detected code, here are some useful tools:
i don't think im supposed to do that
is this array right?
System.out.printf(CPU_PLAYER_MOVES, playerMove, cpuMove);
System.out.printf(CPU_MOVE, cpuMove);
int outcome = determineWinner(playerMove, cpuMove);
if (outcome == PLAYER_WIN_OUTCOME) {
numPlayerWins++;
System.out.println(PLAYER_WIN);
} else if (outcome == CPU_WIN_OUTCOME) {
numCPUWins++;
System.out.println(CPU_WIN);
} else if (outcome == TIE_OUTCOME) {
numTies++;
System.out.println(TIE);
}
String [] playerMoves = new String [numGames + 1];
int i;
for (i = 0; i < numGames; i++) {
playerMoves[i] = playerMove;
}
numGames++;
Detected code, here are some useful tools:
no
you are filling the entire array with a single move
ah lol. do i put that in the if statement?
you have to take in account that you already are in a loop
i see
yes
it will just add to the next slot in the array?
to the cpu array
same code but different variables
got it
tell me if you got IndexOutOfBoundsException
no
it prints all the stats
put it after the while loop
and before the thanks message
game.displayStats();?
yes
ok it works
alright
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> java RPS
Let's play! What's your move? (Type the move or q to quit)
rock
I chose rock. It's a tie.
rock
I chose scissors. You win.
paper
I chose paper. It's a tie.
scissors
I chose paper. You win.
q
I chose paper. Thanks for playing!
Our most recent games were:
Me: paper, You: q
Me: paper, You: scissors
Me: paper, You: paper
Me: scissors, You: rock
Me: rock, You: rock
Our overall stats are:
I won: 0.00%
You won: 40.00%
We tied: 40.00%
Thanks for playing!
Our most recent games were:
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> ```
Detected code, here are some useful tools:
it records the q part
let's check the playRPS method
public void playRPS(String playerMove, String cpuMove) {
// TODO
// Use determineWinner to determine who won
// Record the moves made
// Add one to the appropriate statistics
// Add appropriate Javadoc method header
System.out.printf(CPU_MOVE, cpuMove);
int outcome = determineWinner(playerMove, cpuMove);
if (outcome == PLAYER_WIN_OUTCOME) {
numPlayerWins++;
System.out.println(PLAYER_WIN);
} else if (outcome == CPU_WIN_OUTCOME) {
numCPUWins++;
System.out.println(CPU_WIN);
} else if (outcome == TIE_OUTCOME) {
numTies++;
System.out.println(TIE);
}
playerMoves[numGames] = playerMove;
cpuMoves[numGames] = cpuMove;
numGames++;
}```
Detected code, here are some useful tools:
this is what i have
show main and isValid too
public static void main(String[] args) {
// If command line args are provided use those as the possible moves
String[] moves = new String[args.length];
if (args.length >= MIN_POSSIBLE_MOVES) {
System.arraycopy(args, 0, moves, 0, args.length);
} else {
moves = RPS.DEFAULT_MOVES;
}
// Create new game and scanner
RPS game = new RPS(moves);
Scanner in = new Scanner(System.in);
System.out.println(PROMPT_MOVE);
String m = "x";
while (!m.equals(QUIT)) {
m = in.nextLine();
//System.out.println("test");
if (!game.isValidMove(m)) {
continue;
}
game.playRPS(m, game.genCPUMove());
}
game.displayStats();
// While user does not input "q", play game
//System.out.println(GAME_NOT_IMPLEMENTED); // remove this
// TODO: Insert the code to play the game.
// See the writeup for an example of the game play.
// Hint: call the methods we/you have already written
// to do most of the work! And don't forget Javadoc.
in.close();
}```
Detected code, here are some useful tools:
this is my main
public boolean isValidMove(String move) {
// TODO
// Use a loop here
for (String m: possibleMoves) {
if (m.equals(move) || move.equals(QUIT)) {
return true;
}
}
System.out.println(INVALID_INPUT);
return false; // dummy return value so code compiles.
}```
Detected code, here are some useful tools:
strange
oh
ah it needs to be !move.equals?
I know whats the issue
yeah, lets put it in another place
now this
while(!m.equals(QUIT))
lets replace it with while(true)
let's add the if in the while
that checks if its equal
if(m.equals(QUIT)) break;
before checking if its valid
so i add another if statemnt in the while loop?
yes
public static void main(String[] args) {
// If command line args are provided use those as the possible moves
String[] moves = new String[args.length];
if (args.length >= MIN_POSSIBLE_MOVES) {
System.arraycopy(args, 0, moves, 0, args.length);
} else {
moves = RPS.DEFAULT_MOVES;
}
// Create new game and scanner
RPS game = new RPS(moves);
Scanner in = new Scanner(System.in);
System.out.println(PROMPT_MOVE);
String m = "x";
while (true) {
m = in.nextLine();
//System.out.println("test");
if (!game.isValidMove(m)) {
continue;
}
if (m.equals(QUIT)) {
break;
}
game.playRPS(m, game.genCPUMove());
}
game.displayStats();
// While user does not input "q", play game
//System.out.println(GAME_NOT_IMPLEMENTED); // remove this
// TODO: Insert the code to play the game.
// See the writeup for an example of the game play.
// Hint: call the methods we/you have already written
// to do most of the work! And don't forget Javadoc.
in.close();
}```
Detected code, here are some useful tools:
lets put the is valid move after the equals quit
and delete this || move.equals(QUIT)
ok got it
now show your updated code
public static void main(String[] args) {
// If command line args are provided use those as the possible moves
String[] moves = new String[args.length];
if (args.length >= MIN_POSSIBLE_MOVES) {
System.arraycopy(args, 0, moves, 0, args.length);
} else {
moves = RPS.DEFAULT_MOVES;
}
// Create new game and scanner
RPS game = new RPS(moves);
Scanner in = new Scanner(System.in);
System.out.println(PROMPT_MOVE);
String m = "x";
while (true) {
m = in.nextLine();
//System.out.println("test");
if (m.equals(QUIT)) {
break;
}
if (!game.isValidMove(m)) {
continue;
}
game.playRPS(m, game.genCPUMove());
}
game.displayStats();
// While user does not input "q", play game
//System.out.println(GAME_NOT_IMPLEMENTED); // remove this
// TODO: Insert the code to play the game.
// See the writeup for an example of the game play.
// Hint: call the methods we/you have already written
// to do most of the work! And don't forget Javadoc.
in.close();
}```
Detected code, here are some useful tools:
m.equals must be before isValidMove
yeah now run the code
ok it works
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> javac RPS.java
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> java RPS
Let's play! What's your move? (Type the move or q to quit)
rock
I chose rock. It's a tie.
paper
I chose scissors. I win.
scissors
I chose paper. You win.
rock
I chose paper. I win.
q
Thanks for playing!
Our most recent games were:
Me: paper, You: rock
Me: paper, You: scissors
Me: scissors, You: paper
Me: rock, You: rock
Our overall stats are:
I won: 50.00%
You won: 25.00%
We tied: 25.00%
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> ```
Detected code, here are some useful tools:
the percentages are correct
how do i get it to print the beginning line every time?
like this
$ java RPS
Let's play! What's your move? (Type the move or q to quit)
rock
I chose scissors. You win.
Let's play! What's your move? (Type the move or q to quit)
rock
I chose rock. It's a tie.
Let's play! What's your move? (Type the move or q to quit)
rock
I chose paper. I win.
Let's play! What's your move? (Type the move or q to quit)
q
Thanks for playing!
Our most recent games were:
Me: paper, You: rock
Me: rock, You: rock
Me: scissors, You: rock
Our overall stats are:
I won: 33.33%
You won: 33.33%
We tied: 33.33%```
Detected code, here are some useful tools:
do i just put it in the while loop?
put the print PROMPT in the while loop, before nextLine
I think there is a small issue with this but I'm not sure
It's not saving your moves correctly
is it the array size?
i think it broke
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> java RPS
Let's play! What's your move? (Type the move or q to quit)
r
That is not a valid move. Please try again.
Let's play! What's your move? (Type the move or q to quit)
s
That is not a valid move. Please try again.
Let's play! What's your move? (Type the move or q to quit)
rock
I chose rock. It's a tie.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 100
at RPSAbstract.playRPS(RPSAbstract.java:97)
at RPS.main(RPS.java:43)
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> ```
Detected code, here are some useful tools:
nah they are correctly added
try not increasing numGames
maybe its increasing somewhere else
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> java RPS
Let's play! What's your move? (Type the move or q to quit)
f
That is not a valid move. Please try again.
Let's play! What's your move? (Type the move or q to quit)
d
That is not a valid move. Please try again.
Let's play! What's your move? (Type the move or q to quit)
rock
I chose rock. It's a tie.
Let's play! What's your move? (Type the move or q to quit)
paper
I chose scissors. I win.
Let's play! What's your move? (Type the move or q to quit)
scissors
I chose paper. You win.
Let's play! What's your move? (Type the move or q to quit)
rock
I chose paper. I win.
Let's play! What's your move? (Type the move or q to quit)
q
Thanks for playing!
Our most recent games were:
Our overall stats are:
I won: Infinity%
You won: Infinity%
We tied: Infinity%
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> ```
Detected code, here are some useful tools:
damn its worse
lol
how do i use breakpoints?
I only know how to use them in Netbeans, some people know in Eclipse and intelliJ
it lets you run the code line by line
ask Simon, he knows more about that
ah ok
how to debug stuff
@Test
public void testPlayPokemon() {
pokemonGame.playRPS("water", "fire");
assertEquals(1, pokemonGame.numPlayerWins);
assertEquals("water", pokemonGame.playerMoves[0]);
assertEquals("fire", pokemonGame.cpuMoves[0]);
assertEquals(1, pokemonGame.numGames);
pokemonGame.playRPS("fire", "ice");
assertEquals(2, pokemonGame.numPlayerWins);
assertEquals("fire", pokemonGame.playerMoves[1]);
assertEquals("ice", pokemonGame.cpuMoves[1]);
assertEquals(2, pokemonGame.numGames);
pokemonGame.playRPS("water", "electric");
assertEquals(1, pokemonGame.numCPUWins);
assertEquals("water", pokemonGame.playerMoves[2]);
assertEquals("electric", pokemonGame.cpuMoves[2]);
assertEquals(3, pokemonGame.numGames);
pokemonGame.playRPS("fire", "water");
assertEquals(2, pokemonGame.numCPUWins);
assertEquals("fire", pokemonGame.playerMoves[3]);
assertEquals("water", pokemonGame.cpuMoves[3]);
assertEquals(4, pokemonGame.numGames);
pokemonGame.playRPS("water", "ice");
assertEquals(1, pokemonGame.numTies);
assertEquals("water", pokemonGame.playerMoves[4]);
assertEquals("ice", pokemonGame.cpuMoves[4]);
assertEquals(5, pokemonGame.numGames);
pokemonGame.playRPS("fire", "ground");
assertEquals(2, pokemonGame.numTies);
assertEquals("fire", pokemonGame.playerMoves[5]);
assertEquals("ground", pokemonGame.cpuMoves[5]);
assertEquals(6, pokemonGame.numGames);
}```
Detected code, here are some useful tools:
i think i need to make it like this
Here is how you should determine the outcome: All of the possible moves are stored in the array possibleMoves. Each element in the array beats the previous element in the array, and the end wraps around to the beginning. All other pairings lead to a tie. This means each move beats one move and loses to one move. For example, in the move set { "elephant", "alligator", "hedgehog", "mouse" } mouse beats hedgehog, hedgehog beats alligator, alligator beats elephant, and elephant beats mouse (all other pairings tie). Your code should be able to handle any array of possible moves with at least 3 elements.
ohh
to test that we should fix the winner method
it only works for rock paper scissors
i see
public int determineWinner(String playerMove, String cpuMove) {
// TODO
if (playerMove.equals(cpuMove)) {
return TIE_OUTCOME;
}
switch (playerMove) {
case "rock" -> {
if (cpuMove.equals("scissors")) {
return PLAYER_WIN_OUTCOME;
} else if (cpuMove.equals("paper")) {
return CPU_WIN_OUTCOME;
}
}
case "paper" -> {
if (cpuMove.equals("rock")) {
return PLAYER_WIN_OUTCOME;
} else if (cpuMove.equals("scissors")) {
return CPU_WIN_OUTCOME;
}
}
case "scissors" -> {
if (cpuMove.equals("paper")) {
return PLAYER_WIN_OUTCOME;
} else if (cpuMove.equals("rock")) {
return CPU_WIN_OUTCOME;
}
}
}
return INVALID_INPUT_OUTCOME;
//return 0; // replace this when you implement the method
}```
Detected code, here are some useful tools:
do i just make the strings into the array positions?
you have to delete the if at the top and the entire switch
for ties?
yes
and the switch
now
we have to use a thing thats calculate the distance between the player move and cpu move
if its 0, both are equal and its tie
no
lets calculate the index and distance
there is a quick way to do that
int playerIndex = Arrays.asList(possibleMoves).indexOf(playerMove);```
this converts the possible moves array to a list and uses a method that is useful for this
indexOf
ill try that
it gets the location of the String in the array
it says arrays cannot be resolved
import java.util.Arrays
wait i think my professor doesn't let me import anything else
damn
we will use a for loop then
for (int i = 0; i < possibleMoves.length; i++)
there we put
if (playerMove.equals(possibleMoves[i]) playerIndex = i;
it does the same thing
then another if for the cpuMove
for (i = 0; i < possibleMoves.length; i++) {
if (playerMove.equals(possibleMoves[i])) {
playerIndex = i;
} else {
playerIndex = -1;
}
}```
Detected code, here are some useful tools:
remove the else I got it wrong
ah ok
do i declare playerIndex with -1 before the for loop?
yes
same with cpu
ok got it
like this?
int i;
int playerIndex = -1;
int cpuIndex = -1;
for (i = 0; i < possibleMoves.length; i++) {
if (playerMove.equals(possibleMoves[i])) {
playerIndex = i;
}
if (cpuMove.equals(possibleMoves[i])) {
cpuIndex = i;
}
}```
Detected code, here are some useful tools:
we can put the int i = 0 in the for, instead of declaring it before
ok
now after the index are set
check if one of them is invalid
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}```
-1 means it didn't found the move in the array
outside of the for loop?
yes, after
now this is the most complicated stuff:java // Calculate the "distance" between player's move and cpu's move int distance = (cpuIndex - playerIndex + possibleMoves.length) % possibleMoves.length;
it does stuff so that it calculates the distance to the player
and based on that it will calculate if it wins
i see
ill try to run it now
wait what do i do with the distance variable afterwards?
we check if its 0, and return tie if it is
then, check if its bigger than the half the length of possibleMoves
and return player won if it is
else, return cpu won
is this right?
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}
int distance = (cpuIndex - playerIndex + possibleMoves.length) % possibleMoves.length;
if (distance == 0) {
return TIE_OUTCOME;
} else if (distance >= possibleMoves.length) {
return PLAYER_WIN_OUTCOME;
} else if (distance <= possibleMoves.length) {
return CPU_WIN_OUTCOME;
}```
Detected code, here are some useful tools:
its bigger than, not bigger or equal
ah ok
and just put else at the end, not else if
so compiler does not yell at you for not returning
damn
show
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> java RPS
Let's play! What's your move? (Type the move or q to quit)
rock
I chose rock. It's a tie.
Let's play! What's your move? (Type the move or q to quit)
paper
I chose scissors. I win.
Let's play! What's your move? (Type the move or q to quit)
scissors
I chose paper. I win.
Let's play! What's your move? (Type the move or q to quit)
q
Thanks for playing!
Our most recent games were:
Me: paper, You: scissors
Me: scissors, You: paper
Me: rock, You: rock
Our overall stats are:
I won: 66.67%
You won: 0.00%
We tied: 33.33%
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> ```
Detected code, here are some useful tools:
i chose scissors and cpu chose paper but they won
public int determineWinner(String playerMove, String cpuMove) {
// TODO
int playerIndex = -1;
int cpuIndex = -1;
for (int i = 0; i < possibleMoves.length; i++) {
if (playerMove.equals(possibleMoves[i])) {
playerIndex = i;
}
if (cpuMove.equals(possibleMoves[i])) {
cpuIndex = i;
}
}
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}
int distance = (cpuIndex - playerIndex + possibleMoves.length) % possibleMoves.length;
if (distance == 0) {
return TIE_OUTCOME;
} else if (distance > possibleMoves.length) {
return PLAYER_WIN_OUTCOME;
} else {
return CPU_WIN_OUTCOME;
}
/*if (playerMove.equals(cpuMove)) {
return TIE_OUTCOME;
}
switch (playerMove) {
case "rock" -> {
if (cpuMove.equals("scissors")) {
return PLAYER_WIN_OUTCOME;
} else if (cpuMove.equals("paper")) {
return CPU_WIN_OUTCOME;
}
}
case "paper" -> {
if (cpuMove.equals("rock")) {
return PLAYER_WIN_OUTCOME;
} else if (cpuMove.equals("scissors")) {
return CPU_WIN_OUTCOME;
}
}
case "scissors" -> {
if (cpuMove.equals("paper")) {
return PLAYER_WIN_OUTCOME;
} else if (cpuMove.equals("rock")) {
return CPU_WIN_OUTCOME;
}
}
}/* */
//return INVALID_INPUT_OUTCOME;
//return 0; // replace this when you implement the method
}
}
Detected code, here are some useful tools:
it's else if (distance > possibleMoves.length / 2)
ah ok
ok it works now
ill try junit now
it still doesn't pass the pokemon tests
strange
i think the isValid needs to rewritten?
it may be with the array
what are the tests that failed
actually some of it works
I chose rock. It's a tie.
I chose scissors. It's a tie.
I chose fire. You win.
I chose ice. You win.
I chose electric. I win.
I chose water. I win.
I chose ice. You win.
I chose rock. You win.
I chose scissors. You win.
I chose paper. You win.
I chose paper. I win.
wait
I chose fire. You win.
I chose ice. You win.
I chose electric. I win.
I chose water. I win.
I chose ice. You win.
I chose rock. You win.
I chose scissors. You win.
I chose paper. You win.
I chose paper. I win.
I chose rock. I win.
I chose scissors. I win.
4
That is not a valid move. Please try again.```
that 4
@Test
public void testDetermineWinnerPokemon() {
int outcome = pokemonGame.determineWinner("electric", "water");
assertEquals(PLAYER_WIN_OUTCOME, outcome);
outcome = pokemonGame.determineWinner("ice", "fire");
assertEquals(CPU_WIN_OUTCOME, outcome);
outcome = pokemonGame.determineWinner("water", "ice");
assertEquals(TIE_OUTCOME, outcome);
outcome = pokemonGame.determineWinner("ground", "ground");
assertEquals(TIE_OUTCOME, outcome);
}
// Helper function to test standard output from main
public void checkStandardOutput(String inputString, String[] args, String expectedOutput) {
// Initalize output and input streams
PrintStream origOut = System.out;
InputStream origIn = System.in;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream newps = new PrintStream(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(inputString.getBytes());
// Replace output and input streams for the test so we can feed input
// and compare the output
try {
System.setIn(bais);
} catch (Exception e) {
// shouldn't happen
}
System.setOut(newps);
RPS.main(args);
System.out.flush();
// Set output and input stream to original streams
System.setOut(origOut);
try {
System.setIn(origIn);
} catch (Exception e) {
// shouldn't happen
}
String stuOutput = baos.toString().replaceAll("\\R", "\n");
assertEquals(expectedOutput, stuOutput);
}```
at RPSTester.testDetermineWinnerPokemon(RPSTester.java:189)
I chose ground. I[t's a tie.
Let's play! What's your move? (Type the move or q to quit)```
@Test
public void testDetermineWinnerPokemon() {
int outcome = pokemonGame.determineWinner("electric", "water");
assertEquals(PLAYER_WIN_OUTCOME, outcome);
outcome = pokemonGame.determineWinner("ice", "fire");
assertEquals(CPU_WIN_OUTCOME, outcome);
outcome = pokemonGame.determineWinner("water", "ice");
assertEquals(TIE_OUTCOME, outcome);
outcome = pokemonGame.determineWinner("ground", "ground");
assertEquals(TIE_OUTCOME, outcome);
}```
the water and ice one failed
oh
it expects a TIE_OUTCOME
but determineWinner only sets tie if its 0
if its the same
It's a bit of cheat but at the top check if player is water and cpu is ice and return tie
lets add exceptions like that
it will probably mess up with the RPS game
if we do that there
lets do like this
huh ok i can try putting this
if (playerMove.equals("water") && cpuMove.equals("ice") {
return TIE_OUTCOME;
}```
yeah
I don't know about pokemon so idk what's the hierarchy of elements
lol me neither
i think in the array, the element after the current one will beat it
and anything else is a tie
it passed the water and ice test
this one failed
can we put the tie at the bottom?
for the else
so that it will tie in any other case
at the java } else if (distance > possibleMoves.length / 2) { return PLAYER_WIN_OUTCOME; } else { return CPU_WIN_OUTCOME; }?
yea
nah, because its checking the distance
its fine at the beginning
the problem is the array that doesn't count correctly
does fire and ground beat each other or tie
if they tie it might be an issue
yeah, because it could be because it has two ties, not one
that being this
try asking Zabuzard or TaldenNZ
they know better
so not a tie
right
ah wait this is the array
String[] pokemon = {"electric", "ground", "ice", "fire", "water"};```
it's supposed to tie
so
fire gets beaten by water
but not by ice (its tie) right?
only with the one that is in front or behind
ah wait
the one in front beats the one behind it
and the laste one is beaten by the first one
Im trying to re code it to only compare with the one ahead or behind
int oneAhead = (playerIndex + 1) % possibleMoves.length; // Move in front (clockwise)
int oneBehind = (playerIndex - 1 + possibleMoves.length) % possibleMoves.length;
// Determine who won based on the distance
if (cpuIndex == oneAhead) {
return CPU_WIN_OUTCOME;
} else if (cpuIndex == oneBehind) {
return PLAYER_WIN_OUTCOME;
} else {
return TIE_OUTCOME;
}```
I think this one works
ah ok. just replace everything with this?
from the distance to the ifs
keep the for loop
show
do you have this:java if (playerIndex == -1 || cpuIndex == -1) { return INVALID_INPUT_OUTCOME; }
show
int oneAhead = (playerIndex + 1) % possibleMoves.length; // Move in front (clockwise)
int oneBehind = (playerIndex - 1 + possibleMoves.length) % possibleMoves.length;
// Determine who won based on the distance
if (cpuIndex == oneAhead) {
return CPU_WIN_OUTCOME;
} else if (cpuIndex == oneBehind) {
return PLAYER_WIN_OUTCOME;
} else {
return TIE_OUTCOME;
}
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}```
Detected code, here are some useful tools:
this is the order:
- the for loop
- check if its invalid
- calculate the one behind and ahead
- the ifs
move it
all the checks right
yea
where is that?
this
ah ok i did
what is
on gradescope. i'll try to see if it passes
ok
we have to know what they check
i think it was due yesterday so it's half credit now
like, 0 to 50?
Wow what is going on in here, 1356 posts ๐
show the output
is comparing what you have with what you should have in the console
"but was"
ah ok
we are close to finishing
just the test things
Pokemon game? fun
lol yea smth like that
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> javac RPS.java
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> java RPS
Let's play! What's your move? (Type the move or q to quit)
e
That is not a valid move. Please try again.
Let's play! What's your move? (Type the move or q to quit)
r
That is not a valid move. Please try again.
Let's play! What's your move? (Type the move or q to quit)
q
Thanks for playing!
Our most recent games were:
Our overall stats are:
I won: NaN%
You won: NaN%
We tied: NaN%
PS C:\Users\Simon\Documents\UCSD\CSE 12\cse12-fa24-pa1-RPS-starter-main\cse12-fa24-pa1-RPS-starter-main\starter> ```
Detected code, here are some useful tools:
this is what i get for 2 invalid inputs
whats the output you should have
according to this
it says If there is an invalid move, do not update any instance variables in the game. Do not store the player move or the CPU move in the playerMoves and cpuMoves arrays. Do not update any of the game stats (numGames, numTies, numPlayerWins, numCPUWins). (Hint: Check if the move is valid before you call playRPS()and if it is not, do not call playRPS()).
ok
public void playRPS(String playerMove, String cpuMove) {
// TODO
// Use determineWinner to determine who won
// Record the moves made
// Add one to the appropriate statistics
// Add appropriate Javadoc method header
System.out.printf(CPU_MOVE, cpuMove);
int outcome = determineWinner(playerMove, cpuMove);
if (outcome == PLAYER_WIN_OUTCOME) {
numPlayerWins++;
System.out.println(PLAYER_WIN);
} else if (outcome == CPU_WIN_OUTCOME) {
numCPUWins++;
System.out.println(CPU_WIN);
} else if (outcome == TIE_OUTCOME) {
numTies++;
System.out.println(TIE);
}
playerMoves[numGames] = playerMove;
cpuMoves[numGames] = cpuMove;
numGames++;
} ```
Detected code, here are some useful tools:
let's try putting the playerMoves[numGames] = playerMove
before the ifs
I don't think it will solve anything but
just in case
is it not supposed to display stats with 2 invalid moves?
im not sure what the text below the failed test is
in your test
the bot replies with ground
but it expected it to reply with fire
I don't know how to fix that
ah wait
i think the top part i need it to execute outside of the while loop as well
it says ```org.opentest4j.AssertionFailedError: Standard output ==> expected: <Let's play! What's your move? (Type the move or q to quit)
wait nvm
no, it fine
its the cpu that gave a different response
can you ask your prof about that?
maybe he designed the test wrong
what's the max date to send the assignment
i have 6 more days for late submission
what
this one [001 *FAILED 2.00] pokemon, invalid [002 PASSED 3.00] pokemon, ties, different choices [003 PASSED 0.50] default, invalid input, both, non-null [004 *FAILED 1.00] default, invalid input, any, null [005 PASSED 1.00] default, invalid input, any, same [006 PASSED 1.00] default, player wins, normal #1 [007 PASSED 1.00] default, player wins, normal #2 [008 PASSED 1.50] default, player wins, wrap around [009 PASSED 1.00] default, CPU wins, normal #1 [010 PASSED 1.00] default, CPU wins, normal #2 [011 PASSED 1.50] default, CPU wins, wrap around [012 PASSED 2.00] default, ties [013 PASSED 0.50] default, invalid input, player, non-null [014 PASSED 0.50] default, invalid input, CPU, non-null [015 PASSED 3.00] pokemon, player wins [016 PASSED 1.50] pokemon, ties, same choices [017 PASSED 3.00] pokemon, CPU wins
Detected code, here are some useful tools:
it doesn't tell what the tests do
it might be the same reason as this
ah maybe
i think it's when there's an invalid input
that it doesn't work sometimes
ah right
so String cpum = game.genCPUMove()
put it just after the nextLine
and send cpum to the playRPS
instead of genCPU
in the while loop?
like this?
public static void main(String[] args) {
// If command line args are provided use those as the possible moves
String[] moves = new String[args.length];
if (args.length >= MIN_POSSIBLE_MOVES) {
System.arraycopy(args, 0, moves, 0, args.length);
} else {
moves = RPS.DEFAULT_MOVES;
}
// Create new game and scanner
RPS game = new RPS(moves);
Scanner in = new Scanner(System.in);
String m = "x";
while (true) {
System.out.println(PROMPT_MOVE);
m = in.nextLine();
String cpuM = game.genCPUMove();
//System.out.println("test");
if (m.equals(QUIT)) {
break;
}
if (!game.isValidMove(m)) {
continue;
}
game.playRPS(m, cpuM);
}
game.displayStats();
// While user does not input "q", play game
//System.out.println(GAME_NOT_IMPLEMENTED); // remove this
// TODO: Insert the code to play the game.
// See the writeup for an example of the game play.
// Hint: call the methods we/you have already written
// to do most of the work! And don't forget Javadoc.
in.close();
}```
looks good
alright
it fixed this part
but this one still failed
[002 PASSED 3.00] pokemon, ties, different choices
[003 PASSED 0.50] default, invalid input, both, non-null
[004 *FAILED 1.00] default, invalid input, any, null
[005 PASSED 1.00] default, invalid input, any, same
[006 PASSED 1.00] default, player wins, normal #1
[007 PASSED 1.00] default, player wins, normal #2
[008 PASSED 1.50] default, player wins, wrap around
[009 PASSED 1.00] default, CPU wins, normal #1
[010 PASSED 1.00] default, CPU wins, normal #2
[011 PASSED 1.50] default, CPU wins, wrap around
[012 PASSED 2.00] default, ties
[013 PASSED 0.50] default, invalid input, player, non-null
[014 PASSED 0.50] default, invalid input, CPU, non-null
[015 PASSED 3.00] pokemon, player wins
[016 PASSED 1.50] pokemon, ties, same choices
[017 PASSED 3.00] pokemon, CPU wins```
Detected code, here are some useful tools:
whats the error log
yk this only tells if it passed or failed
i dont think im allowed to view it
since i can just put the exceptions for the ones that failed
check your assignment, if the professor tells you must do x y way, etc
yea
it also has a problem with nulls
but it doesn't make sense to check with nulls
the program itself doesn't allow it
but anyway
yeah return invalid if one of them is null
like we did with playerIndex
same thing
but with move and == null
put it at the top
of the function
wait for the isValidMove or in main?
oh in playerIndex ok i see
wait it doesnt work
show
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}
if (playerIndex == null) {
return INVALID_INPUT_OUTCOME;
}```
Detected code, here are some useful tools:
no
not with playerIndex
with playerMove and cpuMove
ints cannot be null
i see
show code
public int determineWinner(String playerMove, String cpuMove) {
// TODO
int playerIndex = -1;
int cpuIndex = -1;
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}
if (playerMove == null || cpuMove == null) {
return INVALID_INPUT_OUTCOME;
}
for (int i = 0; i < possibleMoves.length; i++) {
if (playerMove.equals(possibleMoves[i])) {
playerIndex = i;
}
if (cpuMove.equals(possibleMoves[i])) {
cpuIndex = i;
}
}
int oneAhead = (playerIndex + 1) % possibleMoves.length;
int oneBehind = (playerIndex - 1 + possibleMoves.length) % possibleMoves.length;
if (cpuIndex == oneAhead) {
return CPU_WIN_OUTCOME;
} else if (cpuIndex == oneBehind) {
return PLAYER_WIN_OUTCOME;
} else {
return TIE_OUTCOME;
}
}```
Detected code, here are some useful tools:
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}
should be after loop
ok ill change that
does it work?
nope it still doesnt work
show code
public int determineWinner(String playerMove, String cpuMove) {
// TODO
int playerIndex = -1;
int cpuIndex = -1;
for (int i = 0; i < possibleMoves.length; i++) {
if (playerMove.equals(possibleMoves[i])) {
playerIndex = i;
}
if (cpuMove.equals(possibleMoves[i])) {
cpuIndex = i;
}
}
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}
if (playerMove == null || cpuMove == null) {
return INVALID_INPUT_OUTCOME;
}
int oneAhead = (playerIndex + 1) % possibleMoves.length;
int oneBehind = (playerIndex - 1 + possibleMoves.length) % possibleMoves.length;
if (cpuIndex == oneAhead) {
return CPU_WIN_OUTCOME;
} else if (cpuIndex == oneBehind) {
return PLAYER_WIN_OUTCOME;
} else {
return TIE_OUTCOME;
}
}```
Detected code, here are some useful tools:
you just put as it was before
nah I'll fix it
public int determineWinner(String playerMove, String cpuMove) {
if (playerMove == null || cpuMove == null) {
return INVALID_INPUT_OUTCOME;
}
int playerIndex = -1;
int cpuIndex = -1;
for (int i = 0; i < possibleMoves.length; i++) {
if (playerMove.equals(possibleMoves[i])) {
playerIndex = i;
}
if (cpuMove.equals(possibleMoves[i])) {
cpuIndex = i;
}
}
if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}
int oneAhead = (playerIndex + 1) % possibleMoves.length;
int oneBehind = (playerIndex - 1 + possibleMoves.length) % possibleMoves.length;
if (cpuIndex == oneAhead) {
return CPU_WIN_OUTCOME;
} else if (cpuIndex == oneBehind) {
return PLAYER_WIN_OUTCOME;
} else {
return TIE_OUTCOME;
}
}```
done
isn't it the same?
nice
yeah
did your professor tell that you have to check for nulls?
no i dont think so
damn
it doesn't say that anywhere in the instructions
you can close the thread with /help-thread close
bro we are doing the same thing and i am soo confused
ok i'll close it later
lol u go to the same school as me?