import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
private User user;
private Computer computer;
private int userScore;
private int computerScore;
private int numberOfGames;
private enum Move {
ROCK, PAPER, SCISSORS;
/**
* порівнює поточний хід з переданим в параметрі otherMove і визначає
* перемогу, поразку або нічию.
*
* @param otherMove
* хід, з яким порівнюється поточний
* @return 1 якщо поточний хід б'є інший, -1 якщо інший хід б'є поточний,
* 0 у разі нічиєї
*/
public int compareMoves(Move otherMove) {
/ / нічия
if (this == otherMove)
return 0;
switch (this) {
case ROCK:
return (otherMove == SCISSORS ? 1 : -1);
case PAPER:
return (otherMove == ROCK ? 1 : -1);
case SCISSORS:
return (otherMove == PAPER ? 1 : -1);
}
// цей код не повинен виконуватися ніколи
return 0;
}
}
private class User {
private Scanner inputScanner;
public User() {
inputScanner = new Scanner(System.in);
}
public Move getMove() {
// виведемо запит на введення
System.out. print( " камінь, ножиці або папір? ");
/ / прочитаємо введення користувача
String userInput = inputScanner.nextLine();
userInput = userInput.toUpperCase();
char firstLetter = userInput.charAt(0);
if (firstLetter == 'до' || firstLetter == 'н' || firstletter == 'б') {
/ / введення коректний
switch (firstLetter) {
case 'к':
return Move.ROCK;
case 'Н':
return Move.PAPER;
case 'Б':
return Move.SCISSORS;
}
}
/ / введення некоректне. Виведемо запит на введення знову.
return getMove();
}
public boolean playAgain() {
System.out.print( " хочете зіграти ще раз? ");
String userInput = inputScanner.nextLine();
userInput = userInput.toUpperCase();
return userInput.charAt(0) == 'Y';
}
}
private class Computer {
public Move getMove() {
Move[] moves = Move.values();
Random random = new Random();
int index = random.nextInt(moves.length);
return moves[index];
}
}
public RockPaperScissors() {
user = new User();
computer = new Computer();
userScore = 0;
computerScore = 0;
numberOfGames = 0;
}
public void startGame() {
System.out. println( " камінь, ножиці, папір!");
/ / отримання ходів
Move userMove = user.getMove();
Move computerMove = computer.getMove();
System.out.println("\пВаш хід" + userMove + ".");
System.out.println("хід комп'ютера" + computerMove + ".\n");
/ / порівняння ходів і визначення переможця
int compareMoves = userMove.compareMoves(computerMove);
switch (compareMoves) {
case 0: / / нічия
System.out.println("Tie!");
break;
case 1: / / переміг гравець
System.out.println(userMove + " beats " + computerMove + ". Ви перемогли!");
userScore++;
break;
case -1: / / переміг комп'ютер
System.out.println(computerMove + " beats " + userMove + ". Ви програли.");
computerScore++;
break;
}
numberOfGames++;
/ / запропонуємо користувачеві зіграти ще раз
if (user.playAgain()) {
System.out.println();
startGame();
} else {
printGameStats();
}
}
/**
* висновок статистики. Нічиї враховуються як полпобеди
* при підрахунку відсотка перемог.
*/
private void printGameStats() {
int wins = userScore;
int losses = computerScore;
int ties = numberOfGames - userScore - computerScore;
double percentageWon = (wins + ((double) ties) / 2) / numberOfGames;
/ / висновок лінії
System.out.print("+");
printDashes(68);
System.out.println("+");
/ / висновок заголовків таблиці
System.out.printf("| %6s | %6s | %6s | %12s | %14s |\n",
"WINS", "LOSSES", "TIES", "GAMES PLAYED", "PERCENTAGE WON");
/ / висновок лінії
System.out.print("|");
printDashes(10);
System.