Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.84 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package uno;
  7.  
  8. import java.util.Arrays;
  9. import java.util.Collections;
  10. import java.util.Deque;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.ListIterator;
  14. import java.util.Scanner;
  15. import java.util.Vector;
  16.  
  17. /**
  18. *
  19. * @author Natalia Diaz, Juliana L
  20. */
  21. public class codigoEquipo10 {
  22.  
  23. static Deque<String> mazo = new LinkedList<>();
  24. static Deque<String> cJugadas = new LinkedList<>();
  25.  
  26. public static void main(String[] args) {
  27. Scanner sc = new Scanner(System.in);
  28. Vector<LinkedList<String>> manos = new Vector<>();
  29. Vector<Vector<Byte>> colorManos = new Vector<>();
  30. byte jugadores = 0, posicion = 0;
  31. boolean sw = true, hayJugada = false;
  32. String jugada = "", maxColor = "", cartaAux = "";
  33.  
  34. llenarMazo();
  35. barajarMazo(mazo);
  36.  
  37. System.out.println("Ingrese el número de oponentes (un entero entre 1 y 3)");
  38. jugadores = sc.nextByte();
  39. while (jugadores < 1 || jugadores > 3) { //ingresar un número de oponentes válidos
  40. System.out.println("Por favor ingresa un número entre 1 y 3 :)");
  41. jugadores = sc.nextByte();
  42. }
  43.  
  44. repartirCartas(jugadores, manos);
  45. contarColores(manos, colorManos);
  46.  
  47. while (sw) { //jugar mientras no haya ganador
  48. if (cJugadas.isEmpty()) { //primera carta jugada
  49. cJugadas.addFirst(mazo.pollFirst());
  50. System.out.println("La carta inicial es: " + cJugadas.peekFirst());
  51. }
  52.  
  53. //juega el usuario
  54. System.out.println("Su mazo es: " + manos.get(0) + "\nDigite la posición de la carta que desea jugar (número entre 0 y " + (manos.get(0).size() - 1) + ") o -1 para arrastrar una carta.");
  55. posicion = sc.nextByte();
  56. while (posicion < -1 || posicion > (manos.get(0).size() - 1)) { //ingresar una posición valida o -1
  57. System.out.println("Por favor ingresa un número entre -1 y " + (manos.get(0).size() - 1) + " :)");
  58. posicion = sc.nextByte();
  59. }
  60.  
  61. if (posicion != -1) {
  62. jugada = manos.get(0).get(posicion);
  63. if (validarJugada(jugada, cJugadas.peekFirst())) {
  64. cJugadas.addFirst(jugada);
  65. System.out.println("El jugador 1 (usuario) tiró " + jugada);
  66. manos.get(0).remove(posicion);
  67.  
  68. if (manos.get(0).isEmpty()) {
  69. // sw = false;
  70. System.out.println("¡El ganador es el jugador 1 (usuario)!");
  71. break;
  72. }
  73. } else {
  74. System.out.println("Intentaste poner una carta inválida, arrastras una carta y pierdes el turno >:)");
  75. validarMazo();
  76. manos.get(0).add(mazo.pollFirst());
  77. System.out.println(manos.get(0) + " Y recuerda que perdiste el turno >:)");
  78. }
  79. } else {
  80. validarMazo();
  81. manos.get(0).add(mazo.pollFirst());
  82. System.out.println("El jugador 1 (usuario) arrastra");
  83. }
  84.  
  85. //juega la máquina varias veces
  86. for (int i = 1; i < manos.size(); i++) {
  87. maxColor = colorMax(colorManos.get(i - 1));
  88. System.out.println("La mano del jugador " + (i + 1) + " es: " + manos.get(i)
  89. + "\nColor max: " + maxColor + ". Hay que comentar esta linea :P ");
  90.  
  91. ListIterator<String> itManoMaq = manos.get(i).listIterator();
  92. while (itManoMaq.hasNext() && !hayJugada) { //iterar sobre una mano
  93. String carta = itManoMaq.next();
  94. String colorCarta = Character.toString(carta.charAt(0));
  95. String colorLast = Character.toString(cJugadas.peekFirst().charAt(0));
  96. String numCarta = Character.toString(carta.charAt(1));
  97. String numLast = Character.toString(cJugadas.peekFirst().charAt(1));
  98.  
  99. if (colorCarta.equals(maxColor)
  100. && (maxColor.equals(colorLast) || numCarta.equals(numLast))) { //me bajo del color más numeroso
  101. cJugadas.addFirst(carta);
  102. switch (colorCarta) {
  103. case "R":
  104. (colorManos.get(i - 1)).set(0, (byte) (((colorManos.get(i - 1)).get(0)) - 1));
  105. break;
  106. case "G":
  107. (colorManos.get(i - 1)).set(1, (byte) (((colorManos.get(i - 1)).get(1)) - 1));
  108. break;
  109. case "B":
  110. (colorManos.get(i - 1)).set(2, (byte) (((colorManos.get(i - 1)).get(2)) - 1));
  111. break;
  112. default:
  113. (colorManos.get(i - 1)).set(3, (byte) (((colorManos.get(i - 1)).get(3)) - 1));
  114. break;
  115. }
  116. System.out.println("El jugador " + (i + 1) + " tiró " + carta);
  117. itManoMaq.remove();
  118. hayJugada = true;
  119. } else if (validarJugada(carta, cJugadas.peekFirst())) { //...o de cualquiera, si no tengo opción
  120. cartaAux = carta;
  121. }
  122. }
  123.  
  124. if (!hayJugada) { //si no tengo jugada inteligente...
  125. if (!cartaAux.isEmpty()) { //valido si tengo en auxiliar cualquier carta
  126. cJugadas.addFirst(cartaAux);
  127. switch (Character.toString(cartaAux.charAt(0))) {
  128. case "R":
  129. (colorManos.get(i - 1)).set(0, (byte) (((colorManos.get(i - 1)).get(0)) - 1));
  130. break;
  131. case "G":
  132. (colorManos.get(i - 1)).set(1, (byte) (((colorManos.get(i - 1)).get(1)) - 1));
  133. break;
  134. case "B":
  135. (colorManos.get(i - 1)).set(2, (byte) (((colorManos.get(i - 1)).get(2)) - 1));
  136. break;
  137. default:
  138. (colorManos.get(i - 1)).set(3, (byte) (((colorManos.get(i - 1)).get(3)) - 1));
  139. break;
  140. }
  141. System.out.println("El jugador " + (i + 1) + " tiró " + cartaAux);
  142. manos.get(i).remove(cartaAux);
  143. } else { // sino, arrastro
  144. validarMazo();
  145. switch (Character.toString((mazo.peekFirst()).charAt(0))) {
  146. case "R":
  147. (colorManos.get(i - 1)).set(0, (byte) (((colorManos.get(i - 1)).get(0)) + 1));
  148. break;
  149. case "G":
  150. (colorManos.get(i - 1)).set(1, (byte) (((colorManos.get(i - 1)).get(1)) + 1));
  151. break;
  152. case "B":
  153. (colorManos.get(i - 1)).set(2, (byte) (((colorManos.get(i - 1)).get(2)) + 1));
  154. break;
  155. default:
  156. (colorManos.get(i - 1)).set(3, (byte) (((colorManos.get(i - 1)).get(3)) + 1));
  157. break;
  158. }
  159. manos.get(i).add(mazo.pollFirst());
  160. System.out.println("El jugador " + (i + 1) + " arrastra");
  161. }
  162. }
  163.  
  164. if (manos.get(i).size() == 1) {
  165. System.out.println("El jugador " + (i + 1) + " grita: UNOOOOOO!");
  166. } else if (manos.get(i).isEmpty()) {
  167. System.out.println("¡El ganador es el jugador " + (i + 1) + "!");
  168. sw = false;
  169. break;
  170. }
  171.  
  172. hayJugada = false;
  173. cartaAux = "";
  174. }
  175.  
  176. if (sw) {
  177. System.out.println("************************************************************************************");
  178. System.out.println("La última jugada es " + cJugadas.peekFirst());
  179. }
  180. }
  181. }
  182.  
  183. public static String colorMax(Vector<Byte> colorMano) {
  184. byte max = 0, posMax = 0;
  185. for (byte i = 0; i < 4; i++) {
  186. if (colorMano.get(i) > max) {
  187. max = colorMano.get(i);
  188. posMax = i;
  189. }
  190. }
  191.  
  192. switch (posMax) {
  193. case 0:
  194. return "R";
  195. case 1:
  196. return "G";
  197. case 2:
  198. return "B";
  199. default:
  200. return "Y";
  201. }
  202. }
  203.  
  204. public static void contarColores(Vector<LinkedList<String>> manos, Vector<Vector<Byte>> colorManos) {
  205. String carta = "";
  206. byte blue = 0, yellow = 0, red = 0, green = 0;
  207. for (int i = 1; i < manos.size(); i++) {
  208. ListIterator<String> itMano = manos.get(i).listIterator();
  209. while (itMano.hasNext()) {
  210. carta = itMano.next();
  211. switch (Character.toString(carta.charAt(0))) {
  212. case "R":
  213. red++;
  214. break;
  215. case "B":
  216. blue++;
  217. break;
  218. case "Y":
  219. yellow++;
  220. break;
  221. default:
  222. green++;
  223. break;
  224. }
  225. }
  226.  
  227. colorManos.add(new Vector<>(Arrays.asList(red, green, blue, yellow)));
  228. blue = 0;
  229. red = 0;
  230. yellow = 0;
  231. green = 0;
  232. }
  233. }
  234.  
  235. public static boolean validarJugada(String cartaIn, String cartaLast) {
  236. String colorIn = Character.toString(cartaIn.charAt(0));
  237. String colorLast = Character.toString(cartaLast.charAt(0));
  238. String numIn = Character.toString(cartaIn.charAt(1));
  239. String numLast = Character.toString(cartaLast.charAt(1));
  240.  
  241. return colorIn.equals(colorLast) || numIn.equals(numLast);
  242. }
  243.  
  244. public static void repartirCartas(byte jugadores, Vector<LinkedList<String>> manos) {
  245. for (int i = 0; i <= jugadores; i++) {
  246. LinkedList<String> auxManos = new LinkedList<>();
  247. for (int j = 0; j < 7; j++) {
  248. auxManos.add(mazo.pollFirst());
  249. }
  250. manos.add(auxManos);
  251. }
  252. }
  253.  
  254. public static void validarMazo() {
  255. if (mazo.isEmpty()) {
  256. String ultimaCarta = cJugadas.pollFirst();
  257. barajarMazo(cJugadas);
  258. mazo.addAll(cJugadas);
  259. cJugadas = new LinkedList<>();
  260. cJugadas.addFirst(ultimaCarta);
  261. }
  262. }
  263.  
  264. public static void barajarMazo(Deque<String> deck) {
  265. Collections.shuffle((List<?>) deck);
  266. }
  267.  
  268. public static void llenarMazo() {
  269. for (int i = 0; i < 2; i++) {
  270. mazo.addAll(Arrays.asList("Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8", "Y9",
  271. "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9",
  272. "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9",
  273. "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9"));
  274. }
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement