Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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.  
  26.  
  27. for (int i=0;i<4;i++) {
  28.  
  29. int randomizer = rand.nextInt(letras.length);
  30. while(letras[randomizer]==0) {
  31. randomizer = rand.nextInt(letras.length);
  32.  
  33. }
  34. chave_vitoria[i]=letras[randomizer];
  35. letras[randomizer]=0;
  36. System.out.print(chave_vitoria[i]);
  37.  
  38.  
  39.  
  40. }
  41.  
  42.  
  43. for (int w=0;w<=7;w++) {
  44.  
  45.  
  46. System.out.println("");
  47.  
  48. System.out.println("Introduza a sua tentativa");
  49.  
  50. String tentativa = input.nextLine();
  51.  
  52. char input_utilizador [] = tentativa.toCharArray();
  53. int contador_vitoria=0;
  54.  
  55. for(int z=0;z<input_utilizador.length;z++) {
  56.  
  57. boolean c = inArray(input_utilizador[z],chave_vitoria);
  58.  
  59.  
  60. if (input_utilizador[z]==chave_vitoria[z]) {
  61. board[w][4+z]= 'X';
  62. board[w][z] = input_utilizador[z];
  63.  
  64. contador_vitoria++;
  65.  
  66. }
  67. else if (c==true) {
  68.  
  69. board[w][4+z] = 'O';
  70. board[w][z] = input_utilizador[z];
  71.  
  72. }
  73.  
  74. else {
  75.  
  76. board[w][4+z] = '-';
  77. board[w][z] = input_utilizador[z];
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. }
  87.  
  88. for (int m = 0;m<board.length;m++) {
  89.  
  90. for (int n = 0;n<board.length;n++) {
  91. System.out.print("|" + board[m][n] + "|");
  92. if(n==3) {
  93. System.out.print(" ");
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. }
  101. System.out.println("");
  102. }
  103.  
  104.  
  105.  
  106. if (w==7) {
  107.  
  108. System.out.println("Ficou sem tentativas");
  109. System.out.println("Perdeu");
  110. System.exit(0);
  111.  
  112. }
  113.  
  114. if (contador_vitoria==4) {
  115. System.out.println("");
  116. System.out.println("Acertou em todas as posições parabens");
  117. System.exit(0);
  118. }
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. public static boolean inArray (char element,char [] array) {
  134. for(int i=0;i<4;i++) {
  135. if (element==array[i]) {
  136. return true;
  137. }
  138.  
  139.  
  140. }
  141.  
  142. return false;
  143.  
  144. }
  145.  
  146.  
  147.  
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement