Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. package jogo;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4.  
  5. public class Master3 {
  6.  
  7. public Master3 () {
  8.  
  9. Scanner input = new Scanner(System.in);
  10.  
  11. Random rand = new Random();
  12.  
  13. char letras [] = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k'};
  14.  
  15. char chave_vitoria [] = new char [4];
  16.  
  17. char board [][] = new char [8][8];
  18.  
  19. System.out.println("Bem-vindo ao MasterMind");
  20. System.out.println("As letras possiveis de jogar são |a|s|d|f|g|h|j|k|");
  21. System.out.println("Têm 8 tentativas e a chave de vitoria contêm 4 letras");
  22. System.out.println("Have fun!");
  23.  
  24.  
  25. for (int i = 0; i < 4; i++) {
  26. int randomizer = rand.nextInt(letras.length);
  27. while (letras[randomizer] == 0) {
  28. randomizer = rand.nextInt(letras.length);
  29.  
  30. }
  31. chave_vitoria[i] = letras[randomizer];
  32. letras[randomizer] = 0;
  33. System.out.print(chave_vitoria[i]);
  34. }
  35.  
  36. for (int w = 0; w <= 7; w++) {
  37.  
  38. System.out.println("");
  39.  
  40. System.out.println("Introduza a sua tentativa");
  41.  
  42. String tentativa = input.nextLine();
  43.  
  44. char input_utilizador [] = tentativa.toCharArray();
  45. int contador_vitoria = 0;
  46.  
  47. for (int z = 0; z < input_utilizador.length; z++) {
  48. boolean c = inArray(input_utilizador[z], chave_vitoria);
  49. if (input_utilizador[z] == chave_vitoria[z]) {
  50. board[w][4 + z]= 'X';
  51. board[w][z] = input_utilizador[z];
  52. contador_vitoria++;
  53.  
  54. }
  55. else if (c == true) {
  56.  
  57. board[w][4 + z] = 'O';
  58. board[w][z] = input_utilizador[z];
  59.  
  60. }
  61. else {
  62. board[w][4 + z] = '-';
  63. board[w][z] = input_utilizador[z];
  64. }
  65. }
  66. for (int m = 0; m < board.length; m++) {
  67. for (int n = 0; n < board.length; n++) {
  68. System.out.print("|" + board[m][n] + "|");
  69. if (n == 3) {
  70. System.out.print(" ");
  71. }
  72. }
  73. System.out.println("");
  74. }
  75. if (w == 7) {
  76. System.out.println("Ficou sem tentativas");
  77. System.out.println("Perdeu");
  78. System.exit(0);
  79. }
  80. if (contador_vitoria == 4) {
  81. System.out.println("");
  82. System.out.println("Acertou em todas as posições parabens");
  83. System.exit(0);
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90.  
  91. public static boolean inArray (char element, char[] array) {
  92. for (int i = 0; i < 4; i++) {
  93. if (element == array[i]) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement