#How to get my code to let me input something?

1 messages ยท Page 2 of 1

wispy nebula
#

i see. i'm adding it in the if statements right?

#

for the outcomes

timber coral
#

yes, numPlayerWins++

wispy nebula
#

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++;
trail basinBOT
wispy nebula
#

do i call the displayStats function afterwards?

timber coral
wispy nebula
#

outside of the if statements?

timber coral
#

yes

timber coral
#

we don't want it repeat

wispy nebula
#

ah ok

timber coral
#

check if the code works

wispy nebula
#

i think it works

timber coral
#

show output

wispy nebula
#

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
trail basinBOT
wispy nebula
#

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++;
trail basinBOT
timber coral
#

you are filling the entire array with a single move

wispy nebula
#

ah lol. do i put that in the if statement?

timber coral
#

you have to take in account that you already are in a loop

wispy nebula
#

i see

timber coral
#

so you don't need the for

#

it would be just

#

playerMoves[numGames] = playerMove;

wispy nebula
#

ohh i get it

#

do that for the cpu too?

timber coral
#

yes

wispy nebula
#

it will just add to the next slot in the array?

timber coral
#

same code but different variables

wispy nebula
#

got it

timber coral
#

tell me if you got IndexOutOfBoundsException

wispy nebula
#

nope

#

do i use the displayStats function to print the array?

timber coral
#

it prints all the stats

#

put it after the while loop

#

and before the thanks message

wispy nebula
#

game.displayStats();?

timber coral
#

yes

wispy nebula
#

ok it works

timber coral
#

show output

#

if there are bugs

wispy nebula
#

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> ```
trail basinBOT
wispy nebula
#

it records the q part

timber coral
wispy nebula
#
    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++;

    }```
trail basinBOT
wispy nebula
#

this is what i have

timber coral
#

show main and isValid too

wispy nebula
#
    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();
    }```
trail basinBOT
wispy nebula
#

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.
    }```
trail basinBOT
wispy nebula
#

ah it needs to be !move.equals?

timber coral
#

I know whats the issue

timber coral
timber coral
#

while(!m.equals(QUIT))

#

lets replace it with while(true)

#

let's add the if in the while

#

that checks if its equal

timber coral
#

before checking if its valid

wispy nebula
#

so i add another if statemnt in the while loop?

wispy nebula
#
    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();
    }```
trail basinBOT
timber coral
wispy nebula
#

ah ok

#

i tried running it and it doesn't quit

timber coral
wispy nebula
#

ok got it

timber coral
wispy nebula
#
    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();
    }```
trail basinBOT
timber coral
#

m.equals must be before isValidMove

timber coral
wispy nebula
#

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> ```
trail basinBOT
timber coral
#

the percentages are correct

wispy nebula
#

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%```
trail basinBOT
wispy nebula
#

do i just put it in the while loop?

timber coral
timber coral
#

It's not saving your moves correctly

wispy nebula
#

is it the array size?

timber coral
#

yeah try [numGames-1]

#

waitt

#

let me check

wispy nebula
#

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>   ```
trail basinBOT
timber coral
#

nah they are correctly added

wispy nebula
#

ok nice

#

lemme check if there's anything else

timber coral
#

maybe its increasing somewhere else

wispy nebula
#
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> ```
trail basinBOT
timber coral
#

damn its worse

wispy nebula
#

lol

timber coral
#

let is as it was

#

can you use breakpoints?

#

idk about visual code

wispy nebula
#

how do i use breakpoints?

timber coral
wispy nebula
#

huh i'm not sure what they do

#

like the breakpoints

timber coral
wispy nebula
#

ah i see

#

how do i use junit?

timber coral
#

ask Simon, he knows more about that

wispy nebula
#

ah ok

timber coral
#

how to debug stuff

wispy nebula
#

i tried it now i think

#

it says 12/15 tests passed

timber coral
#

show what failed

wispy nebula
#
    @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);
    }```
trail basinBOT
wispy nebula
#

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.

timber coral
#

to test that we should fix the winner method

#

it only works for rock paper scissors

wispy nebula
#

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
    }```
trail basinBOT
wispy nebula
#

do i just make the strings into the array positions?

timber coral
wispy nebula
#

for ties?

timber coral
#

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

wispy nebula
#

ah ok

#

so if playerMove == cpuMove?

#

then i put tie

timber coral
#

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

wispy nebula
#

ill try that

timber coral
#

it gets the location of the String in the array

wispy nebula
#

it says arrays cannot be resolved

timber coral
wispy nebula
#

wait i think my professor doesn't let me import anything else

timber coral
#

damn

wispy nebula
timber coral
#

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

wispy nebula
#
        for (i = 0; i < possibleMoves.length; i++) {
            if (playerMove.equals(possibleMoves[i])) {
                playerIndex = i;
            } else {
                playerIndex = -1;
            }
        }```
trail basinBOT
timber coral
wispy nebula
#

ah ok

timber coral
#

playerIndex must be declared with -1;

#

this for will change if its in the array

wispy nebula
#

do i declare playerIndex with -1 before the for loop?

timber coral
#

same with cpu

wispy nebula
#

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;
            }
        }```
trail basinBOT
timber coral
wispy nebula
#

ok

timber coral
#

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

wispy nebula
#

outside of the for loop?

timber coral
#

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

wispy nebula
#

i see

#

ill try to run it now

#

wait what do i do with the distance variable afterwards?

timber coral
#

then, check if its bigger than the half the length of possibleMoves

#

and return player won if it is

#

else, return cpu won

wispy nebula
#

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;
        }```
trail basinBOT
timber coral
wispy nebula
#

ah ok

timber coral
#

and just put else at the end, not else if

#

so compiler does not yell at you for not returning

wispy nebula
#

lol ok

#

i think the junit tester broke

timber coral
#

damn

wispy nebula
#

wait nvm

#

but it's getting the wrong wins and losses

timber coral
#

show

wispy nebula
#
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> ```
trail basinBOT
wispy nebula
#

i chose scissors and cpu chose paper but they won

timber coral
#

show the code

#

for me it works

wispy nebula
#
    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
    }
}
trail basinBOT
timber coral
wispy nebula
#

ah ok

#

ok it works now

#

ill try junit now

#

it still doesn't pass the pokemon tests

timber coral
#

strange

wispy nebula
#

i think the isValid needs to rewritten?

timber coral
#

what are the tests that failed

wispy nebula
#

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.```
timber coral
#

that 4

wispy nebula
#
    @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

timber coral
#

it expects a TIE_OUTCOME

#

but determineWinner only sets tie if its 0

#

if its the same

wispy nebula
#

ah i see

#

so i need to put != as well?

timber coral
#

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

wispy nebula
#

ok

#

how do i do that with the playerIndex and cpuIndex?

timber coral
#

if we do that there

wispy nebula
#

huh ok i can try putting this

#
        if (playerMove.equals("water") && cpuMove.equals("ice") {
            return TIE_OUTCOME;
        }```
timber coral
#

I don't know about pokemon so idk what's the hierarchy of elements

wispy nebula
#

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

timber coral
#

I don't know how to solve them

wispy nebula
#

can we put the tie at the bottom?

#

for the else

#

so that it will tie in any other case

timber coral
wispy nebula
#

yea

timber coral
#

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

wispy nebula
#

it says the expected is [2]

#

but the result was [1]

timber coral
#

yeah, because it could be because it has two ties, not one

timber coral
#

try asking Zabuzard or TaldenNZ

#

they know better

wispy nebula
#

ok i can try but lemme check first

#

it says ground beats fire

timber coral
#

so not a tie

wispy nebula
#

right

#

ah wait this is the array

#
String[] pokemon = {"electric", "ground", "ice", "fire", "water"};```
#

it's supposed to tie

timber coral
#

fire gets beaten by water

#

but not by ice (its tie) right?

wispy nebula
#

yes

#

or ice should win

timber coral
#

only with the one that is in front or behind

wispy nebula
#

ah wait

#

the one in front beats the one behind it

#

and the laste one is beaten by the first one

timber coral
#

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

wispy nebula
#

ah ok. just replace everything with this?

timber coral
#

keep the for loop

wispy nebula
#

ok nice it worked

#

but it failed one test

timber coral
#

show

wispy nebula
#

oh mb

#

i took out the invalid outcome

timber coral
#

do you have this:java if (playerIndex == -1 || cpuIndex == -1) { return INVALID_INPUT_OUTCOME; }

wispy nebula
#

yea i just took it out on accident

#

it says unreachable code now

timber coral
wispy nebula
#
        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;
        }```
trail basinBOT
timber coral
#
  1. the for loop
#
  1. check if its invalid
#
  1. calculate the one behind and ahead
#
  1. the ifs
timber coral
wispy nebula
#

ok ill try it

#

ok it works now

timber coral
#

all the checks right

wispy nebula
#

yea

timber coral
#

we can delete the

#

if .equals

#

at the top

wispy nebula
#

where is that?

wispy nebula
#

ah ok i did

timber coral
#

how its the assignment

#

is there any other checks

wispy nebula
#

i dont think so

#

there's only 15

#

but there is another tester on the grading site

timber coral
wispy nebula
#

on gradescope. i'll try to see if it passes

timber coral
#

ok

wispy nebula
#

i got 47/100

timber coral
wispy nebula
#

it doesn't tell me i think

#

i think the default is the rock paper scissors game

timber coral
#

thats a lot

wispy nebula
#

i think it was due yesterday so it's half credit now

timber coral
wispy nebula
#

yea

#

it would be 94/100

#

this also failed

hard sigil
#

Wow what is going on in here, 1356 posts ๐Ÿ˜„

timber coral
#

is comparing what you have with what you should have in the console

#

"but was"

wispy nebula
#

ah ok

timber coral
#

just the test things

hard sigil
#

Pokemon game? fun

wispy nebula
#

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> ```
trail basinBOT
wispy nebula
#

this is what i get for 2 invalid inputs

timber coral
#

oh

#

I see

timber coral
timber coral
wispy nebula
#

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

timber coral
#

show full pic

#

or paste text

wispy nebula
timber coral
#

it's the array that is wrong

#

show your playRPS

wispy nebula
#

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++;

    }  ```
trail basinBOT
timber coral
#

I don't think it will solve anything but

#

just in case

wispy nebula
#

ok ill try

#

it still the same

timber coral
#

Oh I see

#

for some reason

#

the bot gives a different reply

wispy nebula
#

is it not supposed to display stats with 2 invalid moves?

#

im not sure what the text below the failed test is

timber coral
#

in your test

#

the bot replies with ground

#

but it expected it to reply with fire

#

I don't know how to fix that

wispy nebula
#

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

timber coral
#

no, it fine

#

its the cpu that gave a different response

#

can you ask your prof about that?

#

maybe he designed the test wrong

wispy nebula
#

huh it works for everyone else i think

#

since no one posted in the discussion

timber coral
wispy nebula
#

i have 6 more days for late submission

timber coral
#

oh

#

late submission

wispy nebula
#

yea this is for half credit

#

how do u fix the other error?

timber coral
wispy nebula
#

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

trail basinBOT
timber coral
timber coral
wispy nebula
#

ah maybe

#

i think it's when there's an invalid input

#

that it doesn't work sometimes

timber coral
#

lets try something

#

the while loop copy it and comment it

#

just in case

wispy nebula
#

oh i missed this

timber coral
#

I was going to tell this

#

because of the bot move

wispy nebula
#

ah right

timber coral
#

so String cpum = game.genCPUMove()

#

put it just after the nextLine

#

and send cpum to the playRPS

#

instead of genCPU

wispy nebula
timber coral
#

in main()

wispy nebula
#

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();
    }```
timber coral
#

looks good

wispy nebula
#

alright

wispy nebula
#

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```
trail basinBOT
timber coral
#

yk this only tells if it passed or failed

wispy nebula
#

i dont think im allowed to view it

#

since i can just put the exceptions for the ones that failed

timber coral
wispy nebula
#

ok ill check

#

i think the error is in determineWinner

timber coral
#

seems easier to solve

wispy nebula
#

yea

timber coral
#

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

wispy nebula
#

lol yea im not sure either

#

do i just make an if statement for that?

timber coral
#

like we did with playerIndex

#

same thing

#

but with move and == null

#

put it at the top

#

of the function

wispy nebula
#

ok got it

#

can i use ||?

timber coral
#

and == null

wispy nebula
#

wait for the isValidMove or in main?

wispy nebula
#

wait it doesnt work

timber coral
wispy nebula
#
        if (playerIndex == -1 || cpuIndex == -1) {
            return INVALID_INPUT_OUTCOME;
        }
        if (playerIndex == null) {
            return INVALID_INPUT_OUTCOME;
        }```
trail basinBOT
timber coral
#

not with playerIndex

#

with playerMove and cpuMove

#

ints cannot be null

wispy nebula
#

i see

timber coral
#

also

#

it should be

#

before the for loop

#

to avoid NullPointerException

wispy nebula
#

ok i put it before the loop now

#

still didn't work

timber coral
wispy nebula
#
    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;
        }


    }```
trail basinBOT
timber coral
#

if (playerIndex == -1 || cpuIndex == -1) {
return INVALID_INPUT_OUTCOME;
}

#

should be after loop

wispy nebula
#

ok it passed the ones that failed earilier

#

but it made a lot of other ones fail

wispy nebula
timber coral
#

does it work?

wispy nebula
#

nope it still doesnt work

timber coral
wispy nebula
#
    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;
        }


    }```
trail basinBOT
timber coral
#

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

wispy nebula
#

isn't it the same?

timber coral
#

no

#

check it again

wispy nebula
#

ohh ok i see it now

#

ok nice everything passeed

timber coral
#

nice

wispy nebula
#

alright thanks so much for the help

#

that was really hard

timber coral
#

did your professor tell that you have to check for nulls?

wispy nebula
#

no i dont think so

timber coral
wispy nebula
#

it doesn't say that anywhere in the instructions

timber coral
#

well at least its solved now

#

๐Ÿ‘Œ

wispy nebula
#

yep you helped me out a lot

#

thank you so much

timber coral
#

you can close the thread with /help-thread close

fervent osprey
#

bro we are doing the same thing and i am soo confused

wispy nebula
wispy nebula