Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I need a little help with a boolean method.

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default I need a little help with a boolean method.

    For my project, the goal is to make a memory game that keeps track of 2 players and their scores. I have the game set up to generate and then shuffle a deck of 52 normal playing cards, and for my matching all I'm concerned about is the numeric part of the card(i.e. the 6 in 6 of diamonds). I just about have the entire game done, but I'm having some trouble coming up with a valid boolean method that will tell the players whether or not a card has already been matched or not. I need the method to basically permanently have a card as flipped, or true in booleans case, if it has already been matched with another card. I can't simply remove the card because I'm using an arrayList and removing it would mess up the indexes for my arrayList. My code is below. Can anyone help me out with this? I should also say that I've tried about 3 different methods of doing this, and none of them seem to work. I can't seem to get the emthod to flip over a specific index.

    import java.util.*;
    public class Card
    {
    	private int value = 0;
    	private int suit = 0;
     
     
    	public Card(int value, int suit) 
    	{
    		this.value = value;
    		this.suit = suit;
    	}
     
    	public String toString() 
    	{
    		return getValue() + " of " + getSuit();
    	}
     
    	public String getValue()
    	{ 
    		if (value == 1)
    		{
    			return "Ace";
    		}
    		else if (value == 11)
    		{
    			return "Jack";
    		}
    		else if (value == 12)
    		{
    			return "Queen";
    		}
    		else if (value == 13)
    		{
    			return "King";
    		}
    		else
    			return new Integer(value).toString();
    	}
     
    	public String getSuit()
    	{
    		if(suit == 1)
    		{
    			return "Spades";
    		}
    		else if(suit == 2)
    		{
    			return "Clubs";
    		}
    		else if(suit == 3)
    		{
    			return "Hearts";
    		}
    		else
    		{
    			return "Diamonds";
    		}
    	}
     
    	public void setSuit(int suit)
    	{
    		this.suit = suit;
    	}
     
     
     
     
     
     
     
     
     
     
     
    }

    import java.util.*;
    public class Game
    {
    	private ArrayList<Card> cardDeck;
    	private int cards = 52;
    	private Player player1;
    	private Player player2;
    	private String playerOne = null;
    	private String playerTwo = null;
    	private String card1, card2 = null;
    	private int choice1, choice2 = 0;
    	private int score = 0;
    	private Scanner keyboard = new Scanner(System.in);
     
    	public Game() 
    	{
     
    	}
     
    	public void deckGenerate()
    	{
    		int i, j = 0;
     
    		for (i = 1; i <= 4; i++)
    		{
    			for(j = 1; j <= 13; j++)
    			{
    				Card card = new Card(i ,j);
    				cardDeck.add(card);
    			}
    		}
    	}
     
    	public void deckShuffle()
    	{
    		int i = 0;
    		Random randomCard = new Random();
     
    		for(i = 0; i < cards-1; i++)
    		{
    			int newIndex = randomCard.nextInt(cards-i)+i;
    			Card tempCard = cardDeck.get(i);
    			cardDeck.set(i, cardDeck.get(newIndex));
    			cardDeck.set(newIndex, tempCard);
    		}
    	}
     
    	public void gameStart()
    	{
    		cardDeck = new ArrayList<Card>(cards);
     
    		deckGenerate();
    		deckShuffle();
     
    		System.out.println("this is a 2 player memory game. The goal of this game is to reach 100 points before your opponent.");
    		System.out.println("You choose 2 cards from a deck of 52, and for each match you receive 20 points. You will not match every card.");
     
    		System.out.println("Enter player one's name.");
    		playerOne = keyboard.nextLine();
    		System.out.println("Enter player two's name.");
    		playerTwo = keyboard.nextLine();
     
    		player1 = new Player(playerOne);
    		player2 = new Player(playerTwo);
    		Player current = player1;
     
    		do
    		{
    			gamePlay(current);
     
    			if (current == player1)
    				current = player2;
    			else
    				current = player1;
     
     
    		}while (gameEnd(score) == false);
     
    	}
     
    	public void gamePlay(Player current)
    	{
    		System.out.println("Please choose your first number, between 1 and 52.");
    		choice1 = keyboard.nextInt();
    		card1 = variableSetOne(choice1);
     
    		System.out.println("Please choose your second number, between 1 and 52.");
    		choice2 = keyboard.nextInt();
    		card2 = variableSetTwo(choice2);
    		System.out.println(card1);
    		System.out.println(card2);
     
    		if (card1.equals(card2))
    		{
    			System.out.println("Its a match!");
    			score = score + 20;
    		}
    		else
    			System.out.println("Sorry, its not a match.");
     
    		System.out.println("Your current score is " + score);
     
    	}
     
    	public boolean gameEnd(int score)
    	{
    		if (score == 100)
    			return true;
    		else
    			return false;
     
    	}
     
    	public String variableSetOne(int choice1)
    	{
    		String pick = null;
     
    		if (choice1 == 1)
    			pick = cardDeck.get(0).getValue();
    		else if (choice1 == 2)
    			pick = cardDeck.get(1).getValue();
    		else if (choice1 == 3)
    			pick = cardDeck.get(2).getValue();
    		else if (choice1 == 4)
    			pick = cardDeck.get(3).getValue();
    		else if (choice1 == 5)
    			pick = cardDeck.get(4).getValue();
    		else if (choice1 == 6)
    			pick = cardDeck.get(5).getValue();
    		else if (choice1 == 7)
    			pick = cardDeck.get(6).getValue();
    		else if (choice1 == 8)
    			pick = cardDeck.get(7).getValue();
    		else if (choice1 == 9)
    			pick = cardDeck.get(8).getValue();
    		else if (choice1 == 10)
    			pick = cardDeck.get(9).getValue();
    		else if (choice1 == 11)
    			pick = cardDeck.get(10).getValue();
    		else if (choice1 == 12)
    			pick = cardDeck.get(11).getValue();
    		else if (choice1 == 13)
    			pick = cardDeck.get(12).getValue();
    		else if (choice1 == 14)
    			pick = cardDeck.get(13).getValue();
    		else if (choice1 == 15)
    			pick = cardDeck.get(14).getValue();
    		else if (choice1 == 16)
    			pick = cardDeck.get(15).getValue();
    		else if (choice1 == 17)
    			pick = cardDeck.get(16).getValue();
    		else if (choice1 == 18)
    			pick = cardDeck.get(17).getValue();
    		else if (choice1 == 19)
    			pick = cardDeck.get(18).getValue();
    		else if (choice1 == 20)
    			pick = cardDeck.get(19).getValue();
    		else if (choice1 == 21)
    			pick = cardDeck.get(20).getValue();
    		else if (choice1 == 22)
    			pick = cardDeck.get(21).getValue();
    		else if (choice1 == 23)
    			pick = cardDeck.get(22).getValue();
    		else if (choice1 == 24)
    			pick = cardDeck.get(23).getValue();
    		else if (choice1 == 25)
    			pick = cardDeck.get(24).getValue();
    		else if (choice1 == 26)
    			pick = cardDeck.get(25).getValue();
    		else if (choice1 == 27)
    			pick = cardDeck.get(26).getValue();
    		else if (choice1 == 28)
    			pick = cardDeck.get(27).getValue();
    		else if (choice1 == 29)
    			pick = cardDeck.get(28).getValue();
    		else if (choice1 == 30)
    			pick = cardDeck.get(29).getValue();
    		else if (choice1 == 31)
    			pick = cardDeck.get(30).getValue();
    		else if (choice1 == 32)
    			pick = cardDeck.get(31).getValue();
    		else if (choice1 == 33)
    			pick = cardDeck.get(32).getValue();
    		else if (choice1 == 34)
    			pick = cardDeck.get(33).getValue();
    		else if (choice1 == 35)
    			pick = cardDeck.get(34).getValue();
    		else if (choice1 == 36)
    			pick = cardDeck.get(35).getValue();
    		else if (choice1 == 37)
    			pick = cardDeck.get(36).getValue();
    		else if (choice1 == 38)
    			pick = cardDeck.get(37).getValue();
    		else if (choice1 == 39)
    			pick = cardDeck.get(38).getValue();
    		else if (choice1 == 40)
    			pick = cardDeck.get(39).getValue();
    		else if (choice1 == 41)
    			pick = cardDeck.get(40).getValue();
    		else if (choice1 == 42)
    			pick = cardDeck.get(41).getValue();
    		else if (choice1 == 43)
    			pick = cardDeck.get(42).getValue();
    		else if (choice1 == 44)
    			pick = cardDeck.get(43).getValue();
    		else if (choice1 == 45)
    			pick = cardDeck.get(44).getValue();
    		else if (choice1 == 46)
    			pick = cardDeck.get(45).getValue();
    		else if (choice1 == 47)
    			pick = cardDeck.get(46).getValue();
    		else if (choice1 == 48)
    			pick = cardDeck.get(47).getValue();
    		else if (choice1 == 49)
    			pick = cardDeck.get(48).getValue();
    		else if (choice1 == 50)
    			pick = cardDeck.get(49).getValue();
    		else if (choice1 == 51)
    			pick = cardDeck.get(50).getValue();
    		else if (choice1 == 52)
    			pick = cardDeck.get(51).getValue();
     
    		return pick;
    	}
     
    	public String variableSetTwo(int choice2)
    	{
    		String pick = null;
     
    		if (choice2 == 1)
    			pick = cardDeck.get(0).getValue();
    		else if (choice2 == 2)
    			pick = cardDeck.get(1).getValue();
    		else if (choice2 == 3)
    			pick = cardDeck.get(2).getValue();
    		else if (choice2 == 4)
    			pick = cardDeck.get(3).getValue();
    		else if (choice2 == 5)
    			pick = cardDeck.get(4).getValue();
    		else if (choice2 == 6)
    			pick = cardDeck.get(5).getValue();
    		else if (choice2 == 7)
    			pick = cardDeck.get(6).getValue();
    		else if (choice2 == 8)
    			pick = cardDeck.get(7).getValue();
    		else if (choice2 == 9)
    			pick = cardDeck.get(8).getValue();
    		else if (choice2 == 10)
    			pick = cardDeck.get(9).getValue();
    		else if (choice2 == 11)
    			pick = cardDeck.get(10).getValue();
    		else if (choice2 == 12)
    			pick = cardDeck.get(11).getValue();
    		else if (choice2 == 13)
    			pick = cardDeck.get(12).getValue();
    		else if (choice2 == 14)
    			pick = cardDeck.get(13).getValue();
    		else if (choice2 == 15)
    			pick = cardDeck.get(14).getValue();
    		else if (choice2 == 16)
    			pick = cardDeck.get(15).getValue();
    		else if (choice2 == 17)
    			pick = cardDeck.get(16).getValue();
    		else if (choice2 == 18)
    			pick = cardDeck.get(17).getValue();
    		else if (choice2 == 19)
    			pick = cardDeck.get(18).getValue();
    		else if (choice2 == 20)
    			pick = cardDeck.get(19).getValue();
    		else if (choice2 == 21)
    			pick = cardDeck.get(20).getValue();
    		else if (choice2 == 22)
    			pick = cardDeck.get(21).getValue();
    		else if (choice2 == 23)
    			pick = cardDeck.get(22).getValue();
    		else if (choice2 == 24)
    			pick = cardDeck.get(23).getValue();
    		else if (choice2 == 25)
    			pick = cardDeck.get(24).getValue();
    		else if (choice2 == 26)
    			pick = cardDeck.get(25).getValue();
    		else if (choice2 == 27)
    			pick = cardDeck.get(26).getValue();
    		else if (choice2 == 28)
    			pick = cardDeck.get(27).getValue();
    		else if (choice2 == 29)
    			pick = cardDeck.get(28).getValue();
    		else if (choice2 == 30)
    			pick = cardDeck.get(29).getValue();
    		else if (choice2 == 31)
    			pick = cardDeck.get(30).getValue();
    		else if (choice2 == 32)
    			pick = cardDeck.get(31).getValue();
    		else if (choice2 == 33)
    			pick = cardDeck.get(32).getValue();
    		else if (choice2 == 34)
    			pick = cardDeck.get(33).getValue();
    		else if (choice2 == 35)
    			pick = cardDeck.get(34).getValue();
    		else if (choice2 == 36)
    			pick = cardDeck.get(35).getValue();
    		else if (choice2 == 37)
    			pick = cardDeck.get(36).getValue();
    		else if (choice2 == 38)
    			pick = cardDeck.get(37).getValue();
    		else if (choice2 == 39)
    			pick = cardDeck.get(38).getValue();
    		else if (choice2 == 40)
    			pick = cardDeck.get(39).getValue();
    		else if (choice2 == 41)
    			pick = cardDeck.get(40).getValue();
    		else if (choice2 == 42)
    			pick = cardDeck.get(41).getValue();
    		else if (choice2 == 43)
    			pick = cardDeck.get(42).getValue();
    		else if (choice2 == 44)
    			pick = cardDeck.get(43).getValue();
    		else if (choice2 == 45)
    			pick = cardDeck.get(44).getValue();
    		else if (choice2 == 46)
    			pick = cardDeck.get(45).getValue();
    		else if (choice2 == 47)
    			pick = cardDeck.get(46).getValue();
    		else if (choice2 == 48)
    			pick = cardDeck.get(47).getValue();
    		else if (choice2 == 49)
    			pick = cardDeck.get(48).getValue();
    		else if (choice2 == 50)
    			pick = cardDeck.get(49).getValue();
    		else if (choice2 == 51)
    			pick = cardDeck.get(50).getValue();
    		else if (choice2 == 52)
    			pick = cardDeck.get(51).getValue();
     
    		return pick;
    	}
     
     
    }

    import java.util.*;
    public class Player
    {
    	private String playerName = null;
     
    	public Player(String playerName)
    	{
    		this.playerName = playerName;
    	}
     
    	public String getName()
    	{
    		return playerName;
    	}
     
     
     
     
    }

    import java.util.*;
    public class PrimaryGame
    {
    	public static void main(String[] args)
    	{
    		Game newGame = new Game();
     
    		newGame.gameStart();	
     
     
     
     
    	}
     
     
     
    }
    Last edited by bankston13; April 30th, 2012 at 07:00 PM.


  2. #2
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: I need a little help with a boolean method.

    Umm the answer is kinda obvious, you kinda already said it create a new List.
    public class Stuff {
     
    	List<String> cards = new ArrayList<String>();
     
    	public void addCardToList (String someCard)
    	{
    		cards.add(someCard);
    	}
     
    	public boolean checkIfMatched(String someCard)
    	{
    		if(cards.contains(someCard))
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
     
    	}
     
    }
    f34r th3 kut3 1z

  3. The Following User Says Thank You to hackthisred For This Useful Post:

    bankston13 (April 30th, 2012)

  4. #3
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: I need a little help with a boolean method.

    Now that I think about that method, it kind of was right in front of my face wasn't it . So essentially, I could do something along the lines of making a new list that adds the 2 cards at the indexes represented by the 2 card variables, and then check that list after every card selection to see if its already been flipped.

  5. The Following User Says Thank You to bankston13 For This Useful Post:

    hackthisred (May 1st, 2012)

Similar Threads

  1. Boolean method issue
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2011, 11:26 PM
  2. Method Equals, Boolean...
    By boys8goods in forum Object Oriented Programming
    Replies: 1
    Last Post: June 25th, 2011, 03:34 PM
  3. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 10:18 AM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM