Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. public void promptUser(){
  2. Scanner scan = new Scanner(System.in);
  3. System.out.print("Pick a coordinate [row col] or press [q] to quit.");
  4. int row = 0;
  5. int row = 0;
  6. String line = scan.nextLine().trim();
  7. String[] contents = line.split(" ");
  8. if (contents[0] == "q"){
  9. isRunning = false;
  10. System.exit(0);
  11. }// if quit
  12. if (contents.length < 1 || contents.length > 2){
  13. System.out.print("Invalid Response. Try again.");
  14. promptUser();
  15. }// if wrong amount of input
  16. else {
  17. row = Integer.parseInt(contents[0]);
  18. col = Integer.parseInt(contents[1]);
  19. }// parse ints else
  20. if (inBounds(row, col) == true){
  21. if(!(board[row][col] == 'c'){
  22. click(row, col);
  23. }// has been clicked
  24. }// in bounds?
  25. else{
  26. System.out.print("Invalid response. Try again.");
  27. promptUser();
  28. }// else error
  29. }// promptUser method
  30.  
  31. Exception in thread "main" java.lang.NumberFormatException: For input string: "q"
  32. at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  33. at java.lang.Integer.parseInt(Integer.java:481)
  34. at java.lang.Integer.parseInt(Integer.java:514)
  35. at Minesweeper.promptUser(Minesweeper.java:197)
  36. at Driver.main(Driver.java:12)
  37.  
  38. public void initialize(){
  39. isRunning = true;
  40. board = new char[this.rows][this.cols];
  41. for (int i = 0; i < board.length; i++){
  42. for(int j = 0; j < board[i].length; j++){
  43. board[i][j] = 'e';
  44.  
  45. }
  46. }
  47. mineBoard = new boolean[this.rows][this.cols];
  48.  
  49. for (int i = 0; i < mineBoard.length; i++){
  50. for (int j = 0; j < mineBoard[i].length; j++){
  51. mineBoard[i][j] = false;
  52. }
  53. }
  54. Random bob = new Random();
  55. Random sally = new Random();
  56. for (int i = mines; i > 0; i--){
  57. int mineX = bob.nextInt(10);
  58. int mineY = sally.nextInt(10);
  59. if (mineBoard[mineX][mineY] == false){
  60. mineBoard[mineX][mineY] = true;
  61. }
  62. else{
  63. i++;
  64. }
  65. }
  66.  
  67. }
  68.  
  69. private boolean inBounds(int row, int col){
  70. if (row < 0 || row > board.length){
  71. return false;
  72. }
  73. if (col < 0 || col > board[0].length){
  74. return false;
  75. }else
  76. return true;
  77. }
  78.  
  79. contents[0].equals("q")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement