Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicTacToe {
  4.  
  5. private final String COMPUTER = "computer";
  6. private final String PLAYER = "player";
  7.  
  8. /**
  9. * Asks the user to choose a letter ('X' or 'O')
  10. * and checks if the input is valid.
  11. */
  12. public void askLetter() {
  13. System.out.println("Please enter 'X' or 'O': ");
  14. Scanner input = new Scanner(System.in);
  15. String letter = input.nextLine().toUpperCase().trim();
  16. while (!letter.equals("X") && !letter.equals("O")) {
  17. System.out.println("Please enter 'X' or 'O': ");
  18. letter = input.nextLine().toUpperCase().trim();
  19. }
  20. input.close();
  21. }
  22.  
  23.  
  24. /**
  25. * Asks the user to choose who goes first.
  26. * @return 'computer' or 'player'
  27. */
  28. public String decide() {
  29. //System.out.println("Choose who goes first('player' or 'computer'): ");
  30. Scanner input = new Scanner(System.in);
  31. String first = input.nextLine().toUpperCase().trim();
  32. while (!first.equals(COMPUTER) && !first.equals(PLAYER)) {
  33. System.out.println("Choose who goes first('player' or 'computer'): ");
  34. first = input.nextLine().toUpperCase().trim();
  35. }
  36. input.close();
  37. return first;
  38. }
  39.  
  40. public static void main(String[] args) {
  41. TicTacToe tictac = new TicTacToe();
  42. tictac.askLetter();
  43. tictac.decide();
  44. }
  45. }
  46.  
  47. public static void main(String[] args) {
  48. Scanner scanner = new Scanner(System.in);
  49. TicTacToe tictac = new TicTacToe(scanner);
  50. tictac.askLetter();
  51. tictac.decide();
  52. // You might want to close the scanner here, but you don't really have to.
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement