Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package uno;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Deque;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.ListIterator;
- import java.util.Scanner;
- import java.util.Vector;
- /**
- *
- * @author Natalia Diaz, Juliana L
- */
- public class codigoEquipo10 {
- static Deque<String> mazo = new LinkedList<>();
- static Deque<String> cJugadas = new LinkedList<>();
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Vector<LinkedList<String>> manos = new Vector<>();
- Vector<Vector<Byte>> colorManos = new Vector<>();
- byte jugadores = 0, posicion = 0;
- boolean sw = true, hayJugada = false;
- String jugada = "", maxColor = "", cartaAux = "";
- llenarMazo();
- barajarMazo(mazo);
- System.out.println("Ingrese el número de oponentes (un entero entre 1 y 3)");
- jugadores = sc.nextByte();
- while (jugadores < 1 || jugadores > 3) { //ingresar un número de oponentes válidos
- System.out.println("Por favor ingresa un número entre 1 y 3 :)");
- jugadores = sc.nextByte();
- }
- repartirCartas(jugadores, manos);
- contarColores(manos, colorManos);
- while (sw) { //jugar mientras no haya ganador
- if (cJugadas.isEmpty()) { //primera carta jugada
- cJugadas.addFirst(mazo.pollFirst());
- System.out.println("La carta inicial es: " + cJugadas.peekFirst());
- }
- //juega el usuario
- 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.");
- posicion = sc.nextByte();
- while (posicion < -1 || posicion > (manos.get(0).size() - 1)) { //ingresar una posición valida o -1
- System.out.println("Por favor ingresa un número entre -1 y " + (manos.get(0).size() - 1) + " :)");
- posicion = sc.nextByte();
- }
- if (posicion != -1) {
- jugada = manos.get(0).get(posicion);
- if (validarJugada(jugada, cJugadas.peekFirst())) {
- cJugadas.addFirst(jugada);
- System.out.println("El jugador 1 (usuario) tiró " + jugada);
- manos.get(0).remove(posicion);
- if (manos.get(0).isEmpty()) {
- // sw = false;
- System.out.println("¡El ganador es el jugador 1 (usuario)!");
- break;
- }
- } else {
- System.out.println("Intentaste poner una carta inválida, arrastras una carta y pierdes el turno >:)");
- validarMazo();
- manos.get(0).add(mazo.pollFirst());
- System.out.println(manos.get(0) + " Y recuerda que perdiste el turno >:)");
- }
- } else {
- validarMazo();
- manos.get(0).add(mazo.pollFirst());
- System.out.println("El jugador 1 (usuario) arrastra");
- }
- //juega la máquina varias veces
- for (int i = 1; i < manos.size(); i++) {
- maxColor = colorMax(colorManos.get(i - 1));
- System.out.println("La mano del jugador " + (i + 1) + " es: " + manos.get(i)
- + "\nColor max: " + maxColor + ". Hay que comentar esta linea :P ");
- ListIterator<String> itManoMaq = manos.get(i).listIterator();
- while (itManoMaq.hasNext() && !hayJugada) { //iterar sobre una mano
- String carta = itManoMaq.next();
- String colorCarta = Character.toString(carta.charAt(0));
- String colorLast = Character.toString(cJugadas.peekFirst().charAt(0));
- String numCarta = Character.toString(carta.charAt(1));
- String numLast = Character.toString(cJugadas.peekFirst().charAt(1));
- if (colorCarta.equals(maxColor)
- && (maxColor.equals(colorLast) || numCarta.equals(numLast))) { //me bajo del color más numeroso
- cJugadas.addFirst(carta);
- switch (colorCarta) {
- case "R":
- (colorManos.get(i - 1)).set(0, (byte) (((colorManos.get(i - 1)).get(0)) - 1));
- break;
- case "G":
- (colorManos.get(i - 1)).set(1, (byte) (((colorManos.get(i - 1)).get(1)) - 1));
- break;
- case "B":
- (colorManos.get(i - 1)).set(2, (byte) (((colorManos.get(i - 1)).get(2)) - 1));
- break;
- default:
- (colorManos.get(i - 1)).set(3, (byte) (((colorManos.get(i - 1)).get(3)) - 1));
- break;
- }
- System.out.println("El jugador " + (i + 1) + " tiró " + carta);
- itManoMaq.remove();
- hayJugada = true;
- } else if (validarJugada(carta, cJugadas.peekFirst())) { //...o de cualquiera, si no tengo opción
- cartaAux = carta;
- }
- }
- if (!hayJugada) { //si no tengo jugada inteligente...
- if (!cartaAux.isEmpty()) { //valido si tengo en auxiliar cualquier carta
- cJugadas.addFirst(cartaAux);
- switch (Character.toString(cartaAux.charAt(0))) {
- case "R":
- (colorManos.get(i - 1)).set(0, (byte) (((colorManos.get(i - 1)).get(0)) - 1));
- break;
- case "G":
- (colorManos.get(i - 1)).set(1, (byte) (((colorManos.get(i - 1)).get(1)) - 1));
- break;
- case "B":
- (colorManos.get(i - 1)).set(2, (byte) (((colorManos.get(i - 1)).get(2)) - 1));
- break;
- default:
- (colorManos.get(i - 1)).set(3, (byte) (((colorManos.get(i - 1)).get(3)) - 1));
- break;
- }
- System.out.println("El jugador " + (i + 1) + " tiró " + cartaAux);
- manos.get(i).remove(cartaAux);
- } else { // sino, arrastro
- validarMazo();
- switch (Character.toString((mazo.peekFirst()).charAt(0))) {
- case "R":
- (colorManos.get(i - 1)).set(0, (byte) (((colorManos.get(i - 1)).get(0)) + 1));
- break;
- case "G":
- (colorManos.get(i - 1)).set(1, (byte) (((colorManos.get(i - 1)).get(1)) + 1));
- break;
- case "B":
- (colorManos.get(i - 1)).set(2, (byte) (((colorManos.get(i - 1)).get(2)) + 1));
- break;
- default:
- (colorManos.get(i - 1)).set(3, (byte) (((colorManos.get(i - 1)).get(3)) + 1));
- break;
- }
- manos.get(i).add(mazo.pollFirst());
- System.out.println("El jugador " + (i + 1) + " arrastra");
- }
- }
- if (manos.get(i).size() == 1) {
- System.out.println("El jugador " + (i + 1) + " grita: UNOOOOOO!");
- } else if (manos.get(i).isEmpty()) {
- System.out.println("¡El ganador es el jugador " + (i + 1) + "!");
- sw = false;
- break;
- }
- hayJugada = false;
- cartaAux = "";
- }
- if (sw) {
- System.out.println("************************************************************************************");
- System.out.println("La última jugada es " + cJugadas.peekFirst());
- }
- }
- }
- public static String colorMax(Vector<Byte> colorMano) {
- byte max = 0, posMax = 0;
- for (byte i = 0; i < 4; i++) {
- if (colorMano.get(i) > max) {
- max = colorMano.get(i);
- posMax = i;
- }
- }
- switch (posMax) {
- case 0:
- return "R";
- case 1:
- return "G";
- case 2:
- return "B";
- default:
- return "Y";
- }
- }
- public static void contarColores(Vector<LinkedList<String>> manos, Vector<Vector<Byte>> colorManos) {
- String carta = "";
- byte blue = 0, yellow = 0, red = 0, green = 0;
- for (int i = 1; i < manos.size(); i++) {
- ListIterator<String> itMano = manos.get(i).listIterator();
- while (itMano.hasNext()) {
- carta = itMano.next();
- switch (Character.toString(carta.charAt(0))) {
- case "R":
- red++;
- break;
- case "B":
- blue++;
- break;
- case "Y":
- yellow++;
- break;
- default:
- green++;
- break;
- }
- }
- colorManos.add(new Vector<>(Arrays.asList(red, green, blue, yellow)));
- blue = 0;
- red = 0;
- yellow = 0;
- green = 0;
- }
- }
- public static boolean validarJugada(String cartaIn, String cartaLast) {
- String colorIn = Character.toString(cartaIn.charAt(0));
- String colorLast = Character.toString(cartaLast.charAt(0));
- String numIn = Character.toString(cartaIn.charAt(1));
- String numLast = Character.toString(cartaLast.charAt(1));
- return colorIn.equals(colorLast) || numIn.equals(numLast);
- }
- public static void repartirCartas(byte jugadores, Vector<LinkedList<String>> manos) {
- for (int i = 0; i <= jugadores; i++) {
- LinkedList<String> auxManos = new LinkedList<>();
- for (int j = 0; j < 7; j++) {
- auxManos.add(mazo.pollFirst());
- }
- manos.add(auxManos);
- }
- }
- public static void validarMazo() {
- if (mazo.isEmpty()) {
- String ultimaCarta = cJugadas.pollFirst();
- barajarMazo(cJugadas);
- mazo.addAll(cJugadas);
- cJugadas = new LinkedList<>();
- cJugadas.addFirst(ultimaCarta);
- }
- }
- public static void barajarMazo(Deque<String> deck) {
- Collections.shuffle((List<?>) deck);
- }
- public static void llenarMazo() {
- for (int i = 0; i < 2; i++) {
- mazo.addAll(Arrays.asList("Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8", "Y9",
- "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9",
- "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9",
- "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement