samedi 26 mars 2016

Displaying replaced string in correct place

I'm trying to do a memory game. And I'm pretty much done.

The user can see a row of cards between 1-8. Like this:

[1][2][3][4][5][6][7][8]

The user is supposed to choose two cards.

Example

The user choose card number [5] and gets his first random letter [A]. Then he choose his second card number [8] and gets his second random letter [A]

The issue The problem is that when you choosed two matching cards, these cards are suppose to be replaced with an unique message. So I made a method called:

setCardNumber(String number)

This method do following:

  1. It takes the Integer that the user has choosen.
  2. It converts the Integer to a String.
  3. It uses an if statement and controls which number the user choosed and returns the desired message ("TestOne", "TestTwo", etc).

    But! It does not do this in the correct line.

    This is the output

     Please choose a card:
    
    3 
    
    [ D ]
    
    
    Choose your second card
    
    
    2
    
      [ D ]
    
    Congatulation! 
    
    Both cards have the same letter! You earned one point!
    
    [ TestEight ]
    
    [ TestSeven ]
    
    You have : 1 points!
    
    Cards on table
        [ 3 ]    [ 7 ]    [ 8 ]    [ 1 ]    [ 6 ]    [ 4 ]    [ 2 ]    [ 5 ]
    
    

As you can see, [ TestEight ] and [ TestSeven ] did not replace the third (3) and second (2) index, but wrote it above the cards on table

And the second problem is that the numbers does not come in order (
[1]    [2]    [3]    [4]    [5]    [6]    [7]    [8]
)

This is the code I'm working on:

MainActivity.class

package memorygame2;

import static java.lang.String.format;
import static java.lang.String.format;
import static java.lang.String.format;
import static java.lang.String.format;
import static java.lang.String.format;
import static java.lang.String.format;
import static java.lang.String.format;
import static java.lang.String.format;




    public class MainActivity {

        public static void main(String[] args) {

           int points = 0;
                int i = 0;


                Deck deck = new Deck();
                DataHandler data = new DataHandler(); 

                        //Ask users name.
                        data.setName();

                        //Return name.
                        data.getName();


                        System.out.println("\n Cards on table");
                        for(i = 0; i < deck.numberOfCards(); i++){


                          Card cards = deck.getOneSpecificCard(i);
                          int cardNumber = cards.getOneSpecificNumber();
                          System.out.print(format("%10s","[ " + cardNumber + " ]"));

                          }
                        deck.shuffleCards();

                        while(data.playGame = true){
                        System.out.println("\n Please choose a card:");
                        data.setFirstUserInput();
                        data.getFirstUserInput();

    if(data.getFirstUserInput() <= deck.numberOfCards()){

                        int firstUserInputIndex = data.getFirstUserInput()-1;
                        Card firstCard = deck.getOneSpecificCard(firstUserInputIndex);
                        String first = firstCard.toString(); 
                        System.out.println(format("%10s","\033[F[ " + first + " ]"));

                        System.out.println("Choose your second card");

                        data.setSecondUserInput();
                        int secondUserInputIndex = data.getSecondUserInput()-1;
                        if(data.getSecondUserInput() <= deck.numberOfCards()){
                             Card secondCard = deck.getOneSpecificCard(secondUserInputIndex);
                               if(data.getFirstUserInput()-1 != data.getSecondUserInput()-1){
                                   String second = secondCard.toString(); 
                                   System.out.println(format("%10s","\033[F[ " + second + " ]"));


                                if(first.equals(second)){
                                     System.out.println("\nCongatulations! You earned one point!");
                                     points += 1;
                                     deck.setCardLetterEmpty(data.getFirstUserInput()-1);
                                     deck.setCardLetterEmpty(data.getSecondUserInput()-1);
                                     deck.setCardNumberEmpty(data.getFirstUserInput()-1);
                                     deck.setCardNumberEmpty(data.getSecondUserInput()-1);                                     
                                     System.out.println("You have : " + points + " points!");

                                      System.out.println("\n Cards on table");
                                      for (Card c: deck.getCards()) {
                                      System.out.print((format("%10s","\033[F[ " + c.getLetter())+ " ]"));

                                      }


                                      System.out.println("\n Cards on table");
                                      for (Card c: deck.getCards()) {
                                      System.out.print((format("%10s","\033[F[ " + c.getOneSpecificNumber())+ " ]"));

                                      }



                                }

                         }else{ System.out.println("You have already chosen that number");}

                       } else{System.out.println("Please choose a number between 1-8");}

                    }  else{ System.out.println("Please choose a number between 1-8");}

            } 

          }

    }

Card.class

package memorygame2;

import static java.lang.String.format;

public class Card {

        private String cardLetter;
        private Integer cardNumber;
        private String nullValue;

        public Card(String cardLetter, Integer cardNumber, String nullValue){
            this.cardLetter = cardLetter;
            this.cardNumber = cardNumber;
            this.nullValue = nullValue;
        }


        public String getLetter(){ return cardLetter;}

        public Integer getOneSpecificNumber() { return cardNumber;}

        public String getNullValue() { return nullValue;}

        public void setCardLetter(String letter){
            cardLetter = letter; 
        }

        public void setCardNumber(String number){ 



           number = cardNumber.toString();

           if((number).equals("1")){
           System.out.print((format("%10s","\033[F[ " + "TestOne"+" ]")));
           }
           if((number).equals("2")){
           System.out.print((format("%10s","\033[F[ " + "TestTwo"+" ]")));
           }
           if((number).equals("3")){
           System.out.print((format("%10s","\033[F[ " + "TestThree"+" ]")));
           }
           if((number).equals("4")){
           System.out.print((format("%10s","\033[F[ " + "TestFour"+" ]")));
           }
           if((number).equals("5")){
           System.out.print((format("%10s","\033[F[ " + "TestFive"+" ]")));
           }
           if((number).equals("6")){
           System.out.print((format("%10s","\033[F[ " + "TestSix"+" ]")));
           }
           if((number).equals("7")){
           System.out.print((format("%10s","\033[F[ " + "TestSeven"+" ]")));
           }
           if((number).equals("8")){
           System.out.print((format("%10s","\033[F[ " + "TestEight"+" ]")));


           }
        }

Deck.class

package memorygame2;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

    public class Deck {

    private List<Card> cards = new ArrayList(); 

     public Deck(){


        Card firstCard = new Card("A",1,"");
        Card secondCard = new Card("A",2,"");
        Card thirdCard = new Card("B",3,"");
        Card forthCard = new Card("B",4,"");
        Card fifthCard = new Card("C",5,"");
        Card sixthCard = new Card("C",6,"");
        Card seventhCard = new Card("D",7,"");
        Card eightCard = new Card("D",8,"");

        cards.add(firstCard);
        cards.add(secondCard);
        cards.add(thirdCard);
        cards.add(forthCard);
        cards.add(fifthCard);
        cards.add(sixthCard);
        cards.add(seventhCard);
        cards.add(eightCard);

    }


     public Card getOneSpecificCard(Integer i){
         return cards.get(i);
     }

     public List<Card> getCards(){
         return cards;
     }

     public String getCardLetters(String cardLetter){
         return cardLetter;
     }

     public String setNullValue(String nullValue){
         return nullValue;
     }

     public void setCardLetterEmpty(Integer i){ 
         cards.get(i).setCardLetter("");
     }

     public void setCardNumberEmpty(Integer i){
         cards.get(i).setCardNumber("");
     }     


    public Integer numberOfCards(){
        return cards.size();
    }

       public void shuffleCards(){
       Collections.shuffle(cards);
   }


}

DataHandler.java

package memorygame2;

import java.util.Scanner;


public class DataHandler {


    private String secondPrint;
    private String print;
    private Integer firstUserInput;
    private Integer secondUserInput;
    private String name;
    boolean playGame = true;
    MainActivity main = new MainActivity(); 
    Scanner scanner = new Scanner(System.in);

     public void setFirstUserInput(){
       firstUserInput = scanner.nextInt();
    }

    public Integer getFirstUserInput(){
        return firstUserInput;
    }

    public String getPrintOutFirstInput(){
        return print;
    }

    public void setSecondUserInput(){
       secondUserInput = scanner.nextInt();
    }

     public Integer getSecondUserInput(){
        return secondUserInput;
    }

     public String getPrintOutSecondInput(){
          return secondPrint;
        }

       public void setName(){
          System.out.println("Vad heter du?");
           name = scanner.nextLine();
      }

        public String getName(){
          return name;
      }

        public void restart(){
            playGame =! playGame;
        }

}

The first issue resolve itself if I replace my

setCardNumber(String number) with this:

cardNumber = null;
number = cardNumber + "";

But unfortunately this time I get null as text inside of the brackets as output and I cannot set the message to be different for every case. E.g.

Cards on table
    [ 3 ]    **[ null ]**    **[ null ]**    [ 1 ]    [ 6 ]    [ 4 ]    [ 2 ]    [ 5 ]

Aucun commentaire:

Enregistrer un commentaire