Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

Project 4 Showcase

Posted on October 22, 2024March 3, 2025 By William Jojo
Uncategorized
            if (player == 'S') {
                if (computer == 'B') {
                    System.out.println("Computer wins with " + names[rand] + ".");
                } else if (computer == 'P') {
                    System.out.println("Computer had " + names[rand] + ". You won with " + names[choices.indexOf(player)] + ".");
                    wins++;
                } else {
                    System.out.println("It's a draw!");
                }
            } else if (player == 'P') {
                if (computer == 'S') {
                    System.out.println("Computer wins with " + names[rand] + ".");
                } else if (computer == 'B') {
                    System.out.println("Computer had " + names[rand] + ". You won with " + names[choices.indexOf(player)] + ".");
                    wins++;
                } else {
                    System.out.println("It's a draw!");
                }
            } else if (player == 'B') {
                if (computer == 'P') {
                    System.out.println("Computer wins with " + names[rand] + ".");
                } else if (computer == 'S') {
                    System.out.println("Computer had " + names[rand] + ". You won with " + names[choices.indexOf(player)] + ".");
                    wins++;
                } else {
                    System.out.println("It's a draw!");
                }
            } else {
                System.out.println("Invalid choice.");

            }
        // Game Loop
        for (int game = 1; game <= numGames; game++) {
            System.out.println("\nGame #" + game + ": Choose B (Boulder), P (Parchment), or S (Shears): ");
            char player = Character.toUpperCase(input.next().charAt(0)); //Making lower case to upper case
            char computer = choices.charAt(rand.nextInt(3));

            // Invalid login check
            if (choices.indexOf(player) == -1) {
                System.out.println("Invalid choice! Game skipped.");
                game--; // Game repeats on invalid login
                continue;
            }

            // Show selections
            System.out.println("You chose: " + player + " | Computer chose: " + computer);

            // Determine the winner
            if (player == computer) {
                System.out.println("It's a draw!");
            } else if ((player == 'B' && computer == 'S') ||
                    (player == 'P' && computer == 'B') ||
                    (player == 'S' && computer == 'P')) {
                System.out.println("You win!");

                wins++;

            } else {
                System.out.println("Computer wins!");
            }
        }
    //Initiate game
    for (game = 1; game <= numGames; game++) {

    //Computer's choice
        rand = (int) (Math.random() * choices.length());
        computer = choices.charAt(rand);   //converts computer's choice to a char

    //Player's choice
        System.out.print("Enter your choice (R, P, or S): ");
        player = kb.next().toUpperCase().charAt(0);  //converts input into a char and lets the program read it as uppercased

    //Invalid inputs
        if ( choices.indexOf(player) == -1 )
            System.out.println("Invalid input!");

    //Tie
        else if (player == computer)
            System.out.println("It's a tie!");

    //Player Wins
        else if (player == 'R' && computer == 'S' ||
                player == 'P' && computer == 'R' ||
                player == 'S' && computer == 'P') {
            System.out.println("Computer chose " + names[rand] + ". Player wins!");
            wins = wins + 1;}
        else System.out.println("Computer wins with " + names[rand] + "!");
    }
        if (wins == 1)
    System.out.println("You won " + wins + " game out of " + numGames + "!");

        else System.out.println("You won " + wins + " games out of " + numGames + "!");
    }}
// this is a sweet code gem
result = (player == computer) ? "It's a draw!"
     : (player == 'B' && computer == 'S')
    || (player == 'S' && computer == 'P')
    || (player == 'P' && computer == 'B') ? "You won!" : "Computer wins!";

Post navigation

❮ Previous Post: Variations on a theme – 111 – Project 1
Next Post: Project 5 Showcase ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.