r/java•
[deleted]

Trying to switch players in a simple game

edit: skinnylove solved my problem. Thanks!

I'm trying to make a game in which two players attack each other until one's health reaches zero. My problem is that I cannot figure out how to switch between players. I thought I was on the right path but after a couple hours of work I'm right back to where I started. If anybody could offer some advice, I would be ecstatic.

This is the class that stores information about the robots: import java.util.Scanner;

public class Robot { private String sName; private int iHealth; private int iArmor; private int iWeapon; private int iRemainingPoints = 0; private int iTotalPoints = 1000; private String sStats; private Scanner in = new Scanner(System.in);

public Robot()
{
	System.out.println("What is your robot's name?");
	sName = in.next();
	CreateRobot();
}

public Robot(String sName)
{
	System.out.println("Alright Player 2, please enter your stats:");
	CreateRobot();
}

public void CreateRobot()
{
	System.out.println("Would you like " + sName + " to have custom (press 1) or preconfigured (press 2) stats?");
	sStats = in.next();
	if (sStats.equalsIgnoreCase("1"))
	{
		System.out.println("You have a total of " + (iTotalPoints - iRemainingPoints) + " points, please enter your health points: ");
		iHealth = in.nextInt();
		iRemainingPoints = iHealth;
		System.out.println("You have a total of " + (iTotalPoints - iRemainingPoints) + " points remaining, please enter your armor points: ");
		iArmor = in.nextInt();
		iRemainingPoints = iHealth + iArmor;
		iWeapon = (iTotalPoints - iRemainingPoints);
	}
	else
	{
		System.out.println("Press 1 for an Armor Bot, 2 for a Health Bot, or 3 for a Weapon Bot.");
		sStats = in.next();
		if (sStats.equalsIgnoreCase("1"))
		{
			ArmorBot();
		}
		else if (sStats.equalsIgnoreCase("2"))
		{
			HealthBot();
		}
		else
		{
			WeaponBot();
		}	
	}
	System.out.println("Health: " + iHealth + " Armor: " + iArmor + " Weapon: " + iWeapon);
}


void ArmorBot()
{
	iHealth = 250;
	iArmor = 500;
	iWeapon = 250;
}

void HealthBot()
{
	iHealth = 500;
	iArmor = 250;
	iWeapon = 250;
}

void WeaponBot()
{
	iHealth = 250;
	iArmor = 250;
	iWeapon = 500;
}
void Heal()
{
	iHealth += 50;
}

}

And this is the class that plays the game: import java.util.Scanner;

public class Game { Robot oRobot01 = new Robot(); Robot oRobot02 = new Robot();

private String sUserInput;
private boolean DeadPlayer = false;
private Scanner in = new Scanner(System.in);

public Game()
{
	
	System.out.println("Welcome to Battle Bots! Please configure your characters.");
	Battle();
}

public void Battle()
{
	while (DeadPlayer == false)
	{
		
		System.out.println("Would you like to attack (press 1) or heal (press 2)?");
		sUserInput = in.next();
		if (sUserInput.equalsIgnoreCase("1"))
		{
			System.out.println("You dealt 100 points of damage!");
		}
		else
		{
			System.out.println("You healed 50 points of damage!");
		}
		
	}
	System.out.println("A player has died.");
}

}

Posted Dec 2, 2011, 12:58 AM UTC · edited

12
Loading comments…