So es ist sonntag und ich probiere mich wieder mal am programmieren, hänge aber daran dass ich das ehemals 1. Element einer Liste an den schluss geben muss! Wie genau mach ich das? so wie ich das nun gelöst habe kommen alle Elemente an den Schluss! Bitte um Denkanstöße.
Autoquartett.java
Player-java
Cards.java
Autoquartett.java
Java:
public class Autoquartett {
public static void main(String[] args) {
Autoquartett game = new Autoquartett();
Player player0 = new Player("Player0");
Player player1 = new Player("Player1");
System.out.println("Number of cards per player:");
int numberofcards = Input.readInt();
System.out.println("Number of rounds:");
int numberofrounds = Input.readInt();
for(int i = 1;i<=numberofcards;i++)
{
Random rndspeed = new Random(30,600);
Random rndweight = new Random(800,2400);
Random rndpower = new Random(85,1001);
Cards car = new Cards("car"+i,rndspeed,rndweight,rndpower);
game.CreateCards(player0,car);
Random rndspeed1 = new Random(30,600);
Random rndweight1 = new Random(800,2400);
Random rndpower1 = new Random(85,1001);
Cards car1 = new Cards("car"+100+i,rndspeed1,rndweight1,rndpower1);
game.CreateCards(player1,car1);
}
Player winer = player0; //Start Variable für gewinner
for(int i = 1;i<=numberofrounds;i++)
{
game.print(player0,player1);
System.out.println("Round"+i);
winer = game.playround(player0,player1,winer);
System.out.println("der Gewinner ist:"+winer.getName());
}
}
// Konstruktor f?ºr leere Liste
public Autoquartett() {
}
public void CreateCards(Player player,Cards name) {
if(player == null) { return; }
if(player.cards == null) {
player.cards=name;
} else {
Cards cards = player.cards;
while(cards.next != null) {
cards = cards.next;
}
cards.next = name;
}
}
public void print(Player player0, Player player1) {
System.out.println("Player0:");
printCards(player0);
System.out.println("Player1");
printCards(player1);
}
private void printCards(Player player) {
Cards cards = player.cards;
for(int i = 1; cards != null; i++) {
System.out.println(" "+cards.getname()+" speed="+cards.getattribute(0)+"[km/h], weight="+cards.getattribute(1)+"[kg], power="+cards.getattribute(2)+"[PS]");
cards = cards.next;
}
}
public Player playround(Player player0, Player player1, Player winer)
{
Cards cardsp0 = player0.cards;
Cards cardsp1 = player1.cards;
Random choose = new Random(0,2);
int choosenr = choose.readInt();
if(winer.getName().equals(player0.getName()) == true)
{
int compare1 = cardsp0.getattribute(choosenr);
int compare2 = cardsp1.getattribute(choosenr);
System.out.print(player0.getName()+" says "+cardsp0.getDescription(choosenr)+"("+cardsp0.getname()+" speed="+cardsp0.getattribute(0)+"[km/h], weight="+cardsp0.getattribute(1)+"[kg], power="+cardsp0.getattribute(2)+"[PS]"+" vs "+cardsp1.getname()+" speed="+cardsp1.getattribute(0)+"[km/h], weight="+cardsp1.getattribute(1)+"[kg], power="+cardsp1.getattribute(2)+"[PS]"+").");
if(compare1<compare2)
{
System.out.println("He loses.");
Cards temp = player0.cards;
player0.cards = player0.cards.next;
player1.cards = player1.cards.next;
while(cardsp1.next != null)
{
cardsp1 = cardsp1.next;
}
cardsp1.next = temp;
winer = player1;
return winer;
}
else
{
System.out.println("He wins.");
cardsp0 = cardsp0.next;
winer = player0;
return winer;
}
}
return winer;
}
// printCards(aktuell);
}
Java:
public class Player {
private String name;
public Cards cards = null;
public Player(String name){
this.name = name;
}
public String getName(){
return name;
}
}
Cards.java
Java:
public class Cards {
public Cards next = null;
private int []attribute = new int[3];
private String name;
public Cards(String name,Random speed,Random weight,Random power){
this.attribute[0] = speed.readInt();
this.attribute[1] = weight.readInt();
this.attribute[2] = power.readInt();
this.name = name;
}
public int getattribute(int attnumber){
return attribute[attnumber];
}
public String getDescription(int attnumber){
if(attnumber == 0)
{
return "Speed";
}
if(attnumber == 1)
{
return "Weight";
}
if(attnumber == 2)
{
return "Power";
}
return "";
}
public String getname()
{
return name;
}
}