我正在为课堂制作一个基本的文字格斗游戏项目。在我解决第二个问题之前,我正试图找到第一个工作选项。我班上的老师不会详细解释,他希望学生自己解决。我只是无法弄清楚如何调用匹配类。我在其他程序中以同样的方式完成它,由于某种原因它在这个程序中不起作用。我被老师给了比赛课,我必须上大班(BattleBots)。这就是我到目前为止所拥有的。 提前谢谢。
public class BattleBots;
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
Random rand=new Random();
Match match1 = new Match();
String bot1;
String bot2;
int autoOffense1=0;
int autoDefense1=0;
int autoOffense2=0;
int autoDefense2=0;
System.out.println("*********************************************************");
System.out.println("* Battle Bots *");
System.out.println("*********************************************************");
int z=0;
while(z<4)
{
System.out.println("1 - Auto generate bot");
System.out.println("2 - Customize bot");
System.out.println("3 - Start Match!!!");
System.out.println("4 - Exit");
System.out.print("Please choose an option: ");
z=in.nextInt();
if (z==1)
{
util=new Utilities();
System.out.print("Please enter name for bot 1: ");
bot1=(in.next());
System.out.print("Please enter name for bot 2: ");
bot2=(in.next());
autoOffense1 = (util.roll(10,5,10));
autoDefense1 = (util.roll(10,5,10));
autoOffense2 = (util.roll(10,5,10));
autoDefense2 = (util.roll(10,5,10));
System.out.println("Bot "+bot1);
System.out.println(" Offense = "+autoOffense1);
System.out.println(" Defense = "+autoDefense1);
System.out.println("Bot "+bot2);
System.out.println(" Offense = "+autoOffense2);
System.out.println(" Defense = "+autoDefense2);
}
if (z==2)
{
//option2
}
if (z==3)
{
match1.startMatch();
}
}
package battlebots;
import java.util.Random;
import java.util.Scanner;
public class Match //do not modify this class
{
private String b1;
private int b1O;
private int b1D;
private int b1HP;
private String b2;
private int b2O;
private int b2D;
private int b2HP;
private Utilities util;
public Match(String bot1, int b1Offense, int b1Defense, int b1HitPoints, String bot2, int b2Offense, int b2Defense, int b2HitPoints)
{
util = new Utilities();
b1=bot1;
b1O=b1Offense;
b1D=b1Defense;
b1HP=b1HitPoints;
b2=bot2;
b2O=b2Offense;
b2D=b2Defense;
b2HP=b2HitPoints;
}
private void attack(int bot)
{
Random rand = new Random();
if (bot == 1)
{
int offense=util.roll(b1O);
int defense=util.roll(b2D);
if (offense > defense)
{
int damage=rand.nextInt(Math.abs(offense-defense))+1;
b2HP-=damage;
System.out.println("\t\t"+b1+"'s attack ("+offense+") suceeded dealing "+damage+" points! "+b2+" failed to defend ("+defense+")!");
System.out.println("\t\t"+b2+" has "+b2HP+" points left");
}
else
System.out.println("\t\t"+b1+"'s attack ("+offense+") failed! "+b2+" suceeded at defending ("+defense+")!");
}
else
{
int offense=util.roll(b2O);
int defense=util.roll(b1D);
if (offense > defense)
{
int damage=rand.nextInt(Math.abs(offense-defense)+1);
b1HP-=damage;
System.out.println("\t\t"+b2+"'s attack ("+offense+") suceeded dealing "+damage+" points! "+b1+" failed to defend ("+defense+")!");
System.out.println("\t\t"+b1+" has "+b1HP+" points left");
}
else
System.out.println("\t\t"+b2+"'s attack ("+offense+") failed! "+b1+" suceeded at defending ("+defense+")!");
}
}
public String startMatch()
{
Scanner in=new Scanner(System.in);
int round=0;
System.out.println("\n\n * * * * * Start Match!!! * * * * *");
System.out.println(b1+" O="+b1O+" D="+b1D+" HP="+b1HP);
System.out.println(b2+" O="+b2O+" D="+b2D+" HP="+b2HP);
System.out.println("Press 's' to start match!");
String str=in.next();
while ((b1HP > 0) && (b2HP > 0))
{
round++;
System.out.println("\nRound "+round);
if (util.rollInitiative(b1O,b2O) == 1)
{
System.out.println("\t"+b1+" got initiative and it is attacking!");
attack(1);
attack(2);
}
else
{
System.out.println("\t"+b2+" got initiative and it is attacking!");
attack(2);
attack(1);
}
}
if (b1HP > 0)
return b1;
else
return b2;
}
}
答案 0 :(得分:0)
匹配参数:
public Match(String bot1, int b1Offense, int b1Defense, int b1HitPoints, String bot2, int b2Offense, int b2Defense, int b2HitPoints)
尝试:
Match match1 = new Match("bot1", 5, 5, 10, "bot2", 5, 5, 10);