C++ to Java Translation Help

C++ Code --> Java format

Page 1 of 1

1 Replies - 1348 Views - Last Post: 20 November 2007 - 01:16 AM Rate Topic: -----

#1 bkpatel7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 07

C++ to Java Translation Help

Posted 19 November 2007 - 09:45 PM

Hello,

I need help translating this code, I was just exploring the differences between and Java and C++ on my own, but am stuck. If anyone could help in any way and also briefly explain what they did I would appreciate it. I tried to do some on my own, so the code is a mix as of now.

#include <iostream>
#include <stdlib.h> //for random number generation
#include <string>

using namespace std;

public void shoot(bool& targetAlive, double accuracy, int& number_alive);

string startDuel();
string startDuel_alternative_strategy();

class Duel
{

public static void main()
{
	//Strategy 1
	int NumberOfDuels = 10000;
	int aaron_wins=0, bob_wins=0, charlie_wins=0;
	string winner = "";

	do
	{
		winner = startDuel();

		//cout << "The winner is " << winner << endl;

		if(winner == "Aaron")
			aaron_wins++;
		else if (winner == "Bob")
			bob_wins++;
		else
			charlie_wins++;

		NumberOfDuels--;
	}while (NumberOfDuels > 0);


	//Output everyone's winning probability for strategy 1
	System.out.println("Using first strategy: \n");
	System.out.println("Aaron has " + aaron_wins/1000.0*100 + "% of chance of winning.\n");
	System.out.println("Bob has " + bob_wins/1000.0*100 + "% of chance of winning.\n");
	System.out.println("Charlie has " + charlie_wins/1000.0*100 + "% of chance of winning.\n");


	//Strategy 2
	NumberOfDuels = 10000;
	int aaron_wins=0, bob_wins=0, charlie_wins=0;
	winner = "";

	do
	{
		//Start duel using 2nd strategy that might favor Aaron
		winner = startDuel_alternative_strategy();

		//cout << "The winner is " << winner << endl;

		if(winner == "Aaron")
			aaron_wins++;
		else if (winner == "Bob")
			bob_wins++;
		else
			charlie_wins++;

		NumberOfDuels--;
	}while (NumberOfDuels > 0);


	//Output everyone's winning probability for strategy 2
	System.out.println("\nUsing second strategy: \n");
	System.out.println("Aaron has " = aaron_wins/1000.0*100 + "% of chance of winning.\n");
	System.out.println("Bob has " + bob_wins/1000.0*100 + "% of chance of winning.\n");
	System.out.println("Charlie has " + charlie_wins/1000.0*100 + "% of chance of winning.\n");


	return 0;
}


/*
Function shoot()
if random number generated is less than accuracy,
target is killed and no longer alive
*/
public static void shoot(boolean targetAlive, double accuracy, int number_alive)
{
	double random_num = rand();

	random_num = random_num/RAND_MAX;

	if (random_num < accuracy)
	{
		targetAlive = false;
		number_alive--;
		//printf("Number of alive is %d\n", number_alive);
	}
}



public startDuel()
{
	//At the beginning all three are alive
	bool aaronAlive = true;
	bool bobAlive = true;
	bool charlieAlive = true;
	int num_alive = 3;

	do
	{
		if (aaronAlive)
		{
			if (charlieAlive)
				shoot(charlieAlive, 1/3.0, num_alive);
			else if (bobAlive)
				shoot(bobAlive, 1/3.0, num_alive);
			else
			;
		}


		if (bobAlive)
		{
			if (charlieAlive)
				shoot(charlieAlive, 0.5, num_alive);
			else if (aaronAlive)
				shoot(aaronAlive, 0.5, num_alive);
			else
			;
		}
		if(charlieAlive)
		{
			if (bobAlive)
				shoot(bobAlive, 1.0, num_alive);
			else if (aaronAlive)
				shoot(aaronAlive, 1.0, num_alive);
			else
			;
		}


	}while(num_alive > 1);

	if (aaronAlive)
		return "Aaron";
	else if(bobAlive)
		return "Bob";
	else
		return "Charlie";

}

string startDuel_alternative_strategy()
{
	//At the beginning all three are alive
	bool aaronAlive = true;
	bool bobAlive = true;
	bool charlieAlive = true;
	int num_alive = 3;

	string winner = "";
	int round = 1;

	do
	{


		if (aaronAlive)
		{
			if (round == 1)
			;
			else
			{
				if (charlieAlive)
					shoot(charlieAlive, 1/3.0, num_alive);
				else if (bobAlive)
					shoot(bobAlive, 1/3.0, num_alive);
				else
				;
			}
		}



		if (bobAlive)
		{
			if (charlieAlive)
				shoot(charlieAlive, 0.5, num_alive);
			else if (aaronAlive)
				shoot(aaronAlive, 0.5, num_alive);
			else
			;
		}
		if(charlieAlive)
		{
			if (bobAlive)
				shoot(bobAlive, 1.0, num_alive);
			else if (aaronAlive)
				shoot(aaronAlive, 1.0, num_alive);
			else
			;
		}

		round++;

	}while(num_alive > 1);

	if (aaronAlive)
		return "Aaron";
	else if(bobAlive)
		return "Bob";
	else
		return "Charlie";

}
}


Is This A Good Question/Topic? 0
  • +

Replies To: C++ to Java Translation Help

#2 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ to Java Translation Help

Posted 20 November 2007 - 01:16 AM

Well, since you're trying to get this into Java code, you may have better luck with posting this in the Java forum...though who knows, you may not. Let's see what happens to it here, then maybe an admin can move it over for you.

There is actually quite a similar thread over in the Java forum that may give you some ideas. The approach of the OP wasn't exactly the same, but there are a fair number of suggestions from the other members that may help you out.

Of course, if you're looking for an exact translation, my advice would be to just write the java version from scratch, and not try to just substitute in the parts that you've figured out how to translate. Work in sections, and just leave things blank if you can't figure them out. Then start to fill in the blanks, and ask specific questions about those sections as you get stuck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1