WarPlayer Card Game Help

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 789 Views - Last Post: 01 November 2011 - 09:34 PM Rate Topic: -----

#1 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

WarPlayer Card Game Help

Posted 01 November 2011 - 02:55 PM

Goal:
Inside the play game method, each player should take a series of turns. Here is where you should right the logic to compare whether player1 or player2 won each turn. After turns have been taken, return the winner of the game. Since you now know loops, you can add logic into playGame() that makes a game involve multiple turns.

Code that I have:
WarPlayer class:

import java.util.Scanner;
import java.util.Random;

public class WarPlayer 
{
	private boolean user; //True if player is not a computer.
	public WarPlayer(boolean isComputer)
	{
		user = ! isComputer;
	}
	public boolean takeTurn()
	{
		if(! user)
		{
			Random r = new Random();
			int A = 0;
			int J = 10;
			int Q = 11;
			int K = 12;
			r.nextInt(12);
		}
		else
		{
			Scanner sc = new Scanner(System.in);
			String input = sc.nextLine();
		}
		return true;
	}
}




WarGame class

public class WarGame 
{
    private WarPlayer player1;
	private WarPlayer player2;
	public WarGame(boolean player1, boolean player2)
	{
		this.player1 = new WarPlayer(player1);
		this.player2 = new WarPlayer(player2);
	}
	public void playGame()
	{
		player1.takeTurn();
		player2.takeTurn();
	}
}





public class GameTesterClass 
{
	public static void main(String[] args)
	{
		WarPlayer player1 = new WarPlayer(true);
		WarPlayer player2 = new WarPlayer(true);
	}
}






I just want to know a little bit of code for the play game method that tells whoever has the highest card (A, 2-9, J, Q, K) wins. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: WarPlayer Card Game Help

#2 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 03:42 PM

I just want to know how to write the code. Can anyone help me just start the method. I keep on getting errors.
Was This Post Helpful? 0
  • +
  • -

#3 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 05:16 PM

Ill follow up on this question after you answer this question. You want to know how to compare both players cards? Is this right?
Was This Post Helpful? 0
  • +
  • -

#4 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 05:28 PM

yeah I want the play method to tell which player has the highest card and then prints that the player who has the highest card wins.
Was This Post Helpful? 0
  • +
  • -

#5 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 05:53 PM

Okay well first, we have to convert each card to a number. Therefore, we can compare them easier.
So...
two = 1
three = 2
four = 3
five = 4
six = 5
seven = 6
eight = 7
nine = 8
ten = 10
jack = 11
queen = 12
king = 13
ace = 14

Setting those values is the easy part. Just turn them into integer variables. Now for the comparison...

First, use a random to select a number out of a possible 14 for each player. Lets call them card1 and card2. Then, do this...

if(card1 > card2)
    return card1;
else if(card1 == card2)
    return 0;
else
    return card3;



This would then return the winner of the comparison. Make this code part of a method called compare with a constructor that accepts card1 and card2, and have it return and integer. From there, all you have to do is handle the number it gives you in the desired way. For example, if it return 0, its a tie.
Was This Post Helpful? 0
  • +
  • -

#6 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 07:05 PM

are you talking about something like this.

public class WarGame 
{
    private WarPlayer player1;
	private WarPlayer player2;
	public WarGame(boolean player1, boolean player2)
	{
		this.player1 = new WarPlayer(player1);
		this.player2 = new WarPlayer(player2);
	}
	public WarPlayer playGame()
	{
		player1.takeTurn();
		player2.takeTurn();
		
		int two = 0;
		int three = 1;
		int four = 2;
		int five = 3;
		int six = 4;
		int seven = 5;
		int eight = 6;
		int nine = 7;
		int ten = 8;
		int jack = 9;
		int queen = 10;
		int king = 11;
		int ace = 12;

		if(player1 > player2)
			System.out.println("Player1 won!");
			else if(player1 == player2)
			System.out.println("Tie");

	}
}



But I am still getting ans error:
The operator > is undefined for the argument type(s) WarPlayer, WarPlayer

What should I do to correct it. THANKS AGAIN.
Was This Post Helpful? 0
  • +
  • -

#7 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 07:18 PM

Does the error print out the number line of the program?

Actually I think i see the problem, you are comparing two booleans using a ">" symbol, which cannot be done. You need to create two different numbers, from 0 to 14, using a random number generator. This resembles what card each player has. You take one of the numbers, call it card1. (This is associated to player 1) The other number call card 2. Then replace the comparison lines with this...

(card1 > card2)	           
   System.out.println("Player1 won!")
   else if(card1 == card2)
   System.out.println("Tie");

Sorry, forgot the 
. Here it is again.
(card1 > card2)
System.out.println("Player1 won!")
else if(card1 == card2)
System.out.println("Tie"); 


Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 07:20 PM

Your WarPlayer class should contain an instance variable (like the user variable) and it should save the data from the input, whether random, or user input. Then you should create a getter method to retrieve the value and do

if(player1.getValue() == player2.getValue())

Was This Post Helpful? 0
  • +
  • -

#9 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 07:55 PM

ok so now I have got something like this:

import java.util.Random;
public class WarGame 
{
    private WarPlayer player1;
	private WarPlayer player2;
	public WarGame(boolean player1, boolean player2)
	{
		this.player1 = new WarPlayer(player1);
		this.player2 = new WarPlayer(player2);
	}
	public String playGame()
	{
		player1.takeTurn();
		player2.takeTurn();
		
		int two = 0;
		int three = 1;
		int four = 2;
		int five = 3;
		int six = 4;
		int seven = 5;
		int eight = 6;
		int nine = 7;
		int ten = 8;
		int jack = 9;
		int queen = 10;
		int king = 11;
		int ace = 12;

		Random r = new Random();
		int card1 = r.nextInt(12);
		int card2 = r.nextInt(12);
		String s = "";

		if(card1 > card2)
		{
			s = "Player1 won!";
		}
		else if(card1 < card2)
		{
			s = "Player2 won!";
		}
		else if(player1 == player2)
		{
			s = "Tie";
		}
		return s;
	}
}





public class GameTesterClass 
{
	public static void main(String[] args)
	{
		WarGame player1 = new WarGame(true, true);
		player1.playGame();
	}
}




and the same warplayer class.

There are no error but when I try to run it, it doesn't prints anything. Can you tell me what am I doing wrong? Thanks
Was This Post Helpful? 0
  • +
  • -

#10 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 08:01 PM

oh wait it does prints this:
Player2 won!

but no matter how many times I run it, it always says: Player2 won!
However, I want it to be something like this:
if one user is a human and the other is a computer:
Please enter a card:
5
Computer's card 2.
Player/human won!

or something like that.
Can you tell me what should I do?
Was This Post Helpful? 0
  • +
  • -

#11 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 08:15 PM

Okay, give me a few minutes and I will respond back with a detailed answer.
Was This Post Helpful? 0
  • +
  • -

#12 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 08:24 PM

Before I do that, I want toy to do the following.

Make a class in WarPlayer called "input" that returns an integer. Let the user know that he can only enter the following strings...

"ace"
"king"
"queen"
"jack"
"10"
"9"
"8"
"7"
"6"
"5"
"4"
"3"
"2"

You could even make it so if the user does not enter in one of these strings, it asks for another card.

Then, make multiple "if" statements that will convert the string to an integer using the following table...

two = 1
three = 2
four = 3
five = 4
six = 5
seven = 6
eight = 7
nine = 8
ten = 10
jack = 11
queen = 12
king = 13
ace = 14.

Then, return the integer that was inputted. Once finished, post it back.

This post has been edited by taskforce141: 01 November 2011 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#13 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 08:38 PM

ok I am almost done
Was This Post Helpful? 0
  • +
  • -

#14 jot121  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 144
  • Joined: 30-October 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 08:55 PM

here is the code:
public int input()
	{
		String a = "ace";
		String b ="king";
		String c = "queen";
		String d = "jack";
		String e = "10";
		String f = "9";
		String g = "8";
		String h = "7";
		String i = "6";
		String j = "5";
		String k = "4";
		String l = "3";
		String m = "2";
		int p = 0;
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();
		if(input == "ace")
		{
			p = 14;
		}
		else if(input == "king")
		{
			p = 13;
		}
		else if(input == "queen")
		{
			p = 12;
		}
		else if(input == "jack")
		{
			p = 11;
		}
		else if(input == "10")
		{
			p = 10;
		}
		else if(input == "9")
		{
			p = 8;
		}
		else if(input == "8")
		{
			p = 7;
		}
		else if(input == "7")
		{
			p = 6;
		}
		else if(input == "6")
		{
			p = 5;
		}
		else if(input == "5")
		{
			p = 4;
		}
		else if(input == "4")
		{
			p = 3;
		}
		else if(input == "3")
		{
			p = 2;
		}
		else if(input == "2")
		{
			p = 1;
		}		
		return p;
	}
}



it doesn't print anything. If I test it and type like ace or 10 it doesn't print 14 or 10.
Was This Post Helpful? 0
  • +
  • -

#15 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: WarPlayer Card Game Help

Posted 01 November 2011 - 09:11 PM

Almost finished. The reason it didn't print anything during your test was because when you compare strings for equality, you have to do the .equals method.

Ex) string1.equals(string2)

which returns a boolean true or false.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2