Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- public class Game {
- // Variables
- Player bot;
- Player user;
- CardsPack trapoula;
- // Constructors
- Game(){
- bot = new Player();
- user = new Player();
- trapoula = new CardsPack();
- }
- Game(Player user, CardsPack trapoula){
- this.bot = new Player("Bot");
- this.user= user;
- this.trapoula = trapoula;
- }
- // THE MOST USED CONSTRUCTOR WILL BE THE FOLLOWING
- Game(Player user){
- this.bot = new Player("Bot");
- this.user = user;
- this.trapoula = new CardsPack();
- trapoula.shuffle();
- }
- // Methods
- // Auxiliary Functions
- // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- public static void displayArrayList(ArrayList <Card> myList){
- for(int i=0; i<myList.size(); i++){
- System.out.print(myList.get(i).abbreviation + ", ");
- }
- System.out.println();
- }
- // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- public static void displayDeck(ArrayList <Card> deck){
- System.out.print("----> ");
- for(int i=0; i<deck.size(); i++){
- System.out.print(deck.get(i).abbreviation + " # ");
- }
- System.out.print(" <----");
- System.out.println();
- }
- // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- public void showStatus(){
- System.out.println(" ******** Game Status ********");
- System.out.println();
- System.out.println("Player 1 ----> " + bot.name);
- System.out.println("Player 2 ----> " + user.name);
- System.out.println("CardsPack trapoula status: ");
- trapoula.showStatus();
- }
- // ***************************************************************************************
- // **************************************************************************************
- public Card selectBotMovement(Player bot, Deck deck){
- // If deck has no cards down, then bot will decide randomly
- Random random = new Random();
- if(deck.downCards.size() == 0){
- int randomIndex1 = random.nextInt(bot.hand.size());
- return bot.hand.get(randomIndex1);
- }
- else{
- // 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
- Card lastDeckCard = deck.downCards.get(deck.downCards.size()-1);
- String lastDeckCardValue = lastDeckCard.value;
- ArrayList <String> botCardValues = new ArrayList <String> ();
- for(int i=0; i<bot.hand.size(); i++){
- botCardValues.add(bot.hand.get(i).value);
- }
- boolean match = false;
- for(int i=0; i<botCardValues.size(); i++){
- if(botCardValues.get(i).equals(lastDeckCardValue)){
- match = true;
- return bot.hand.get(i);
- }
- }
- if(match == false){
- int randomIndex2 = random.nextInt(bot.hand.size());
- return bot.hand.get(randomIndex2);
- }
- else{
- System.out.println("ERROR");
- return new Card("undefined", "undefined");
- }
- } // END OF ELSE-CASE
- } // END OF FUNCTION
- // Function that throws down the 4 first cards of the cardsPack
- public void firstStep(Deck deck){
- // First, I will scan the trapoula in order to find the first 4 cards
- ArrayList <Card> first4Cards = trapoula.returnFirstNCards(4);
- System.out.println("INITIALLY");
- trapoula.display();
- trapoula.cards.remove(0);
- trapoula.cards.remove(0);
- trapoula.cards.remove(0);
- trapoula.cards.remove(0);
- deck.addCardsInDeck(first4Cards);
- System.out.println("DECK");
- deck.display();
- System.out.println("TRAPOULA");
- trapoula.display();
- }
- public void give6CardsToPlayer(Player player){
- //ArrayList <Card> givenCards = trapoula.returnFirstNCards(6);
- //trapoula.removeFirstNCards(6);
- //player.addCardsInHand(givenCards);
- }
- public static void startGame(Game game, Deck deck, Player bot, Player user){
- game.firstStep(deck);
- game.give6CardsToPlayer(bot);
- game.give6CardsToPlayer(user);
- // 1. Checking the 1st round statistics
- System.out.println();
- System.out.println("^^^^^^^^ ROUND 1 ^^^^^^^^");
- System.out.println("Bot's first 6 cards: ");
- bot.showCards();
- System.out.println();
- System.out.println("User's first 6 cards: ");
- user.showCards();
- System.out.println();
- System.out.println("Trapoula status (must have been with 36 cards: )");
- game.trapoula.showStatus();
- System.out.println();
- System.out.println();
- Card botCardChoice = game.selectBotMovement(bot, deck);
- botCardChoice.showStatus();
- }
- // ***************************************************************************************
- // ***************************************************************************************
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // MAIN FUNCTION
- public static void main(String[] args) {
- // ***************************************************************************************
- // ***************************************************************************************
- // ***************************************************************************************
- // ***************************************************************************************
- /*
- // I try to create a game
- Player thomas = new Player("Thomas");
- // Create 4 cards
- Card card1 = new Card("1", "heart");
- Card card2 = new Card("2", "club");
- Card card3 = new Card("3", "diamond");
- Card card4 = new Card("4", "spade");
- // Create a cards array
- ArrayList <Card> thomasCards = new ArrayList <Card> ();
- thomasCards.add(card1);
- thomasCards.add(card2);
- thomasCards.add(card3);
- thomasCards.add(card4);
- // ADD CARDS
- thomas.addCardsInHand(thomasCards);
- // Create the game
- Game tryGame = new Game(thomas);
- tryGame.showStatus();
- // Now, I will do this
- // I will remove the first card of the game trapoula
- // And I will add it in thomas' cards array
- CardsPack thomasTrapoula =tryGame.trapoula;
- Card firstCard = thomasTrapoula.cards.get(0);
- thomasTrapoula.cards.remove(firstCard);
- thomas.addCardInHand(firstCard);
- // Check if this is done
- System.out.println();
- System.out.println();
- System.out.println("After removing first card from the deck");
- thomas.showCards();
- tryGame.showStatus();
- // Try the method grabCards
- Card grabbed1 = new Card("6", "heart");
- Card grabbed2 = new Card("6", "club");
- ArrayList <Card> grabbedList = new ArrayList <Card> ();
- grabbedList.add(grabbed1);
- grabbedList.add(grabbed2);
- thomas.grabCards(grabbedList);
- System.out.println();
- System.out.println();
- System.out.println("Trying the grabCards method: ");
- thomas.showStatus();
- */
- // Runs properly
- Player BOT = new Player();
- Player THOMAS = new Player("Thomas");
- Game THOMASgame = new Game(THOMAS);
- Deck THOMASdeck = new Deck();
- startGame(THOMASgame, THOMASdeck, BOT, THOMAS);
- System.out.println("END");
- // ***************************************************************************************
- // ***************************************************************************************
- // ***************************************************************************************
- // ***************************************************************************************
- } // END OF MAIN
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement