Advertisement
makispaiktis

CardsGame - Game.java - VERSION 2

Mar 17th, 2020 (edited)
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.75 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class Game {
  5.    
  6.     // Variables
  7.     Player bot;
  8.     Player user;
  9.     CardsPack trapoula;
  10.    
  11.     // Constructors
  12.     Game(){
  13.         bot = new Player();
  14.         user = new Player();
  15.         trapoula = new CardsPack();
  16.     }
  17.     Game(Player user, CardsPack trapoula){
  18.         this.bot = new Player("Bot");
  19.         this.user= user;
  20.         this.trapoula = trapoula;
  21.     }
  22.     // THE MOST USED CONSTRUCTOR WILL BE THE FOLLOWING
  23.     Game(Player user){
  24.         this.bot = new Player("Bot");
  25.         this.user = user;
  26.         this.trapoula = new CardsPack();
  27.         trapoula.shuffle();
  28.     }
  29.    
  30.    
  31.     // Methods
  32.    
  33.     // Auxiliary Functions
  34.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  35.     public static void displayArrayList(ArrayList <Card> myList){
  36.         for(int i=0; i<myList.size(); i++){
  37.             System.out.print(myList.get(i).abbreviation + ", ");
  38.         }
  39.         System.out.println();
  40.     }
  41.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  43.     public static void displayDeck(ArrayList <Card> deck){
  44.         System.out.print("---->    ");
  45.         for(int i=0; i<deck.size(); i++){
  46.             System.out.print(deck.get(i).abbreviation + " # ");
  47.         }
  48.         System.out.print("   <----");
  49.         System.out.println();
  50.     }
  51.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  52.    
  53.    
  54.     public void showStatus(){
  55.         System.out.println("    ******** Game Status ********");
  56.         System.out.println();
  57.         System.out.println("Player 1 ----> " + bot.name);
  58.         System.out.println("Player 2 ----> " + user.name);
  59.         System.out.println("CardsPack trapoula status: ");
  60.         trapoula.showStatus();
  61.     }
  62.    
  63.    
  64.     // ***************************************************************************************
  65.     // **************************************************************************************
  66.    
  67.    
  68.     public Card selectBotMovement(Player bot, Deck deck){
  69.         // If deck has no cards down, then bot will decide randomly
  70.         Random random = new Random();
  71.        
  72.         if(deck.downCards.size() == 0){
  73.             int randomIndex1 = random.nextInt(bot.hand.size());
  74.             return bot.hand.get(randomIndex1);
  75.         }
  76.        
  77.         else{
  78.             // In this case, bot has to check if one of his cards matches with the last card of deck in order to select this move
  79.             Card lastDeckCard = deck.downCards.get(deck.downCards.size()-1);
  80.             String lastDeckCardValue = lastDeckCard.value;
  81.             ArrayList <String> botCardValues = new ArrayList <String> ();
  82.             for(int i=0; i<bot.hand.size(); i++){
  83.                 botCardValues.add(bot.hand.get(i).value);
  84.             }
  85.            
  86.             boolean match = false;
  87.             for(int i=0; i<botCardValues.size(); i++){
  88.                 if(botCardValues.get(i).equals(lastDeckCardValue)){
  89.                     match = true;
  90.                     return bot.hand.get(i);
  91.                 }
  92.             }
  93.            
  94.             if(match == false){
  95.                 int randomIndex2 = random.nextInt(bot.hand.size());
  96.                 return bot.hand.get(randomIndex2);
  97.             }
  98.             else{
  99.                 System.out.println("ERROR");
  100.                 return new Card("undefined", "undefined");
  101.             }
  102.            
  103.         }   // END OF ELSE-CASE
  104.        
  105.     }   // END OF FUNCTION
  106.    
  107.    
  108.    
  109.    
  110.    
  111.    
  112.    
  113.    
  114.     // Function that throws down the 4 first cards of the cardsPack
  115.     public void firstStep(Deck deck){
  116.         // First, I will scan the trapoula in order to find the first 4 cards
  117.         ArrayList <Card> first4Cards = trapoula.returnFirstNCards(4);
  118.         System.out.println("INITIALLY");
  119.         trapoula.display();
  120.         trapoula.cards.remove(0);
  121.         trapoula.cards.remove(0);
  122.         trapoula.cards.remove(0);
  123.         trapoula.cards.remove(0);
  124.         deck.addCardsInDeck(first4Cards);
  125.         System.out.println("DECK");
  126.         deck.display();
  127.         System.out.println("TRAPOULA");
  128.         trapoula.display();
  129.     }
  130.    
  131.    
  132.     public void give6CardsToPlayer(Player player){
  133.         //ArrayList <Card> givenCards = trapoula.returnFirstNCards(6);
  134.         //trapoula.removeFirstNCards(6);
  135.         //player.addCardsInHand(givenCards);
  136.     }
  137.    
  138.    
  139.    
  140.     public static void startGame(Game game, Deck deck, Player bot, Player user){
  141.         game.firstStep(deck);
  142.         game.give6CardsToPlayer(bot);
  143.         game.give6CardsToPlayer(user);
  144.         // 1. Checking the 1st round statistics
  145.         System.out.println();
  146.         System.out.println("^^^^^^^^ ROUND 1 ^^^^^^^^");
  147.         System.out.println("Bot's first 6 cards: ");
  148.         bot.showCards();
  149.         System.out.println();
  150.         System.out.println("User's first 6 cards: ");
  151.         user.showCards();
  152.         System.out.println();
  153.         System.out.println("Trapoula status (must have been with 36 cards: )");
  154.         game.trapoula.showStatus();
  155.         System.out.println();
  156.         System.out.println();
  157.         Card botCardChoice = game.selectBotMovement(bot, deck);
  158.         botCardChoice.showStatus();
  159.     }
  160.    
  161.     // ***************************************************************************************
  162.     // ***************************************************************************************
  163.    
  164.    
  165.    
  166.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  167.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170.    
  171.     // MAIN FUNCTION
  172.     public static void main(String[] args) {
  173.        
  174.        
  175.        
  176.         // ***************************************************************************************
  177.         // ***************************************************************************************
  178.         // ***************************************************************************************
  179.         // ***************************************************************************************
  180.         /*
  181.         // I try to create a game
  182.         Player thomas = new Player("Thomas");
  183.         // Create 4 cards
  184.         Card card1 = new Card("1", "heart");
  185.         Card card2 = new Card("2", "club");
  186.         Card card3 = new Card("3", "diamond");
  187.         Card card4 = new Card("4", "spade");
  188.         // Create a cards array
  189.         ArrayList <Card> thomasCards = new ArrayList <Card> ();
  190.         thomasCards.add(card1);
  191.         thomasCards.add(card2);
  192.         thomasCards.add(card3);
  193.         thomasCards.add(card4);
  194.         // ADD CARDS
  195.         thomas.addCardsInHand(thomasCards);
  196.         // Create the game
  197.         Game tryGame = new Game(thomas);
  198.         tryGame.showStatus();
  199.         // Now, I will do this
  200.         // I will remove the first card of the game trapoula
  201.         // And I will add it in thomas' cards array
  202.         CardsPack thomasTrapoula =tryGame.trapoula;
  203.         Card firstCard = thomasTrapoula.cards.get(0);
  204.         thomasTrapoula.cards.remove(firstCard);
  205.         thomas.addCardInHand(firstCard);
  206.         // Check if this is done
  207.         System.out.println();
  208.         System.out.println();
  209.         System.out.println("After removing first card from the deck");
  210.         thomas.showCards();
  211.         tryGame.showStatus();
  212.         // Try the method grabCards
  213.         Card grabbed1 = new Card("6", "heart");
  214.         Card grabbed2 = new Card("6", "club");
  215.         ArrayList <Card> grabbedList = new ArrayList <Card> ();
  216.         grabbedList.add(grabbed1);
  217.         grabbedList.add(grabbed2);
  218.         thomas.grabCards(grabbedList);
  219.         System.out.println();
  220.         System.out.println();
  221.         System.out.println("Trying the grabCards method: ");
  222.         thomas.showStatus();
  223.         */
  224.         // Runs properly
  225.        
  226.         Player BOT = new Player();
  227.         Player THOMAS = new Player("Thomas");
  228.         Game THOMASgame = new Game(THOMAS);
  229.         Deck THOMASdeck = new Deck();
  230.         startGame(THOMASgame, THOMASdeck, BOT, THOMAS);
  231.         System.out.println("END");
  232.        
  233.        
  234.         // ***************************************************************************************
  235.         // ***************************************************************************************
  236.         // ***************************************************************************************
  237.         // ***************************************************************************************
  238.        
  239.     }   // END OF MAIN
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement