6 Replies - 982 Views - Last Post: 27 January 2014 - 09:11 AM Rate Topic: -----

#1 astrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-June 13

War card Game,Tie help

Posted 23 June 2013 - 09:35 PM

I'm stuck with my War game. What I need to implement is my 'tie' portion to my code. Basically once tie is declared each player “puts down 10 cards each” and compares the 11th card to see if there is a clear winner. Process repeating if the 11th card is a tie aswell...

So far i have this.
public class war {

	public static void main(String[] args)
	{ 
		game();
		//war2.discards();
		
	}
	public static void game()
	  {
		card player1 = new card(0,0); // random card 1
		card player2= new card(0,0); // random card 2
		
		// sets the randomized value by calling Random function
		player1.setValue(RandomSuit());
		player2.setValue(RandomSuit());
		player1.setSuit(RandomRank());
		player2.setSuit(RandomRank());
		//
		
		
			while(player1.getFace() == player2.getFace()) // if cards have same rank, change player2
			{
			player2.setSuit(RandomRank());	
			}	
		
		// print out statement
			
		System.out.println("Player 1: "+player1+"\n"+"Player 2: "+player2);
		
		if(player1.getRank() < player2.getRank())
		{
			System.out.println("Player 2 WINS!");
		}
		else if (player1.getRank() > player2.getRank())
		{
			System.out.println("Player 1 WINS!");
		}
		else
		{
			System.out.println("TIE!");
			war2.discards();
			player1.setValue(RandomSuit());
			player2.setValue(RandomSuit());
			player1.setSuit(RandomRank());
			player2.setSuit(RandomRank());
			System.out.println("Player 1: "+player1+"\n"+"Player 2: "+player2);

		}
	  }

public class war2 
{
	
	public static void discards()
	{
		card discards=new card(0,0);
		
		
		for (int discard=1; discard<=10; discard++)
		{
			int r=war.RandomRank();
			int s=war.RandomSuit();
			discards.setSuit(r);
			discards.setValue(s);
			System.out.println(discards.toString());
		}
	}		
	
}


In this code if its a tie it'll display 10 cards from my loop from my war2 class and then display the 11 card. so what i need to do again is run it through the if statements i have. I've tried making the if statements into its own function so i could call it repeatedly but i've failed so far. All my 'tie' code is suppose to be in war2.java according to my online assignment.
Any suggestions on what i should do?
much thanks

Is This A Good Question/Topic? 0
  • +

Replies To: War card Game,Tie help

#2 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: War card Game,Tie help

Posted 24 June 2013 - 07:50 AM

We can't help you much without the rest of your classes and without you telling us what is exactly wrong with the program. You're just telling us it's failing. How so? We need more information.

A few things i've noticed in your current code so far though is your declaring integers inside a for loop, declare it outside the block and initialize it within the loop.

Also, class names should begin with a capital letter, i.e. ClassName or Class. Instead of calling your main war class, war, maybe call it WarGame, or something along those lines. It avoids confusion using war and war2, plus it just looks ridiculous. I see you have a class called 'card' name it 'Card'.

Don't make your discards method the same name as the name you give when you call your card class.

        while(player1.getFace() == player2.getFace()) {
            player2.setSuit(RandomRank());
        }



Looking at this it doesn't seem like you have a very effective way of dealing out the deck to two players, because two players should not have the same suit with the same card value. You may want to fix that.

Plus your methods should be named with camelCase, like randomRank or randomSuit.

edit: Formatting

This post has been edited by schutzzz: 24 June 2013 - 07:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 astrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-June 13

Re: War card Game,Tie help

Posted 24 June 2013 - 10:42 AM

View Postschutzzz, on 24 June 2013 - 07:50 AM, said:

We can't help you much without the rest of your classes and without you telling us what is exactly wrong with the program. You're just telling us it's failing. How so? We need more information.


I never said it was failing, and the only class missing is the card class which is the basic one that seems to be available everywhere on the web.

I need help trying to implement my tie portion of the game. Right now if the two cards are tied it'll display 10 cards as discards and then the 11th card will be used to compare who see who is the winner.
else
 {
  System.out.println("TIE!");
  war2.discards();
  player1.setValue(RandomSuit());
  player2.setValue(RandomSuit());
  player1.setSuit(RandomRank());
  player2.setSuit(RandomRank());
  System.out.println("Player 1: "+player1+"\n"+"Player 2: "+player2);
   }


What i need help with is to see if theirs a way to jump back up to the start of the if statement to redo the if check or help with creating a function that just checks to see who wins. AS i need it to recheck the 11th card to see the winning outcome, and if its another tie i have to do the process all over.

Does this make sense of what i need help with??
Thanks for mentioning the naming conventions thing. I'll make sure to stick it better
Was This Post Helpful? 0
  • +
  • -

#4 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: War card Game,Tie help

Posted 24 June 2013 - 11:10 AM

I need to see your Card class. you can't just say find one online, and if that's what you did that's considered plagiarism.
Was This Post Helpful? 0
  • +
  • -

#5 astrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-June 13

Re: War card Game,Tie help

Posted 24 June 2013 - 11:17 AM

public class card 
{
	//private datafields
	private int value;
	private int suit;
	
	
	// string array to keep the options of the suits and ranks.
	private String suits[] = {"Hearts", "Clubs", "Spades", "Diamonds"}; // suits [0-3] to call
	private String ranks[] = {"Ace","2","3","4","5","6","7","8","9","10","Jack", "Queen", "King"}; // ranks [0-12] to call
	
	// public get/set methods
	card(int suit, int rank) //modified from default constructor
	{
		        this.value=rank;
		        this.suit=suit;
	}
	public String getValue()
	{
		return ranks[value];// get rank value/name from stored array
	}
	public int getRank()// get integer value of Rank 
	{
		return value;
	}
	public int getFace()// integer value of suit
	{
		return suit;
	}
	
	public void setValue(int currentValue)
	{
		// dont use 1 and 13 like assignment due to using an array. 1 is technically value 2
		if (currentValue > 12 )
		{value = 0;}
		else if(currentValue < 0)
		{value=0;}
		else value=currentValue;
		
		
	}
	
	public String getSuit()
	{
	return suits[suit]; // get suit name from stored array
	}
	public void setSuit(int currentSuit)

	{
	suit = currentSuit;
	}	
	public @Override String toString()// makes printing a card easier in code
	{
		          return ranks[value] + " of " + suits[suit];
	}
}//end class

Was This Post Helpful? 0
  • +
  • -

#6 astrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-June 13

Re: War card Game,Tie help

Posted 24 June 2013 - 11:30 AM

I figured it out. I changed the else which was meant for is the two cards are equal to a while for it and its now works... looks sloppy but works so now i just gotta start cleaning up the code

while(player1.getRank()== player2.getRank())
		{
			System.out.println("TIE!");
			war2.discards();
			player1.setValue(RandomSuit());
			player2.setValue(RandomSuit());
			player1.setSuit(RandomRank());
			player2.setSuit(RandomRank());
			System.out.println("Player 1: "+player1+"\n"+"Player 2: "+player2);	
			
		}


Was This Post Helpful? 0
  • +
  • -

#7 maddog17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-January 14

Re: War card Game,Tie help

Posted 27 January 2014 - 09:11 AM

do you mind posting your deck class too . i wanna see how this works and mine doesnt seem to be working for some reason.

This post has been edited by andrewsw: 27 January 2014 - 09:44 AM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1