-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ai' of github.com:Cardshifter/Cardshifter into
client-server Conflicts: cardshifter-api/client-server-documentation.txt cardshifter-api/pom.xml cardshifter-api/src/main/java/com/cardshifter/server/incoming/LoginMessage.java cardshifter-api/src/main/java/com/cardshifter/server/outgoing/WelcomeMessage.java cardshifter-core/src/main/java/com/cardshifter/core/Game.java cardshifter-core/src/main/java/com/cardshifter/core/Zone.java cardshifter-core/src/main/java/com/cardshifter/core/console/CommandLineOptions.java cardshifter-server/src/main/java/com/cardshifter/server/model/Handlers.java cardshifter-server/src/main/java/com/cardshifter/server/model/IncomingHandler.java cardshifter-server/src/main/java/com/cardshifter/server/model/MessageHandler.java cardshifter-server/src/main/java/com/cardshifter/server/model/Server.java cardshifter-server/src/main/java/com/cardshifter/server/model/ServerGame.java cardshifter-server/src/main/java/com/cardshifter/server/model/TCGGame.java
- Loading branch information
Showing
18 changed files
with
483 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
cardshifter-core/src/main/java/com/cardshifter/ai/CardshifterAI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.cardshifter.ai; | ||
|
||
import com.cardshifter.core.Player; | ||
import com.cardshifter.core.UsableAction; | ||
|
||
public interface CardshifterAI { | ||
UsableAction getAction(Player player); | ||
} |
47 changes: 47 additions & 0 deletions
47
cardshifter-core/src/main/java/com/cardshifter/ai/CompleteIdiot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.cardshifter.ai; | ||
|
||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.cardshifter.core.Player; | ||
import com.cardshifter.core.TargetAction; | ||
import com.cardshifter.core.Targetable; | ||
import com.cardshifter.core.UsableAction; | ||
|
||
public class CompleteIdiot implements CardshifterAI { | ||
|
||
private final Random random = new Random(); | ||
|
||
@Override | ||
public UsableAction getAction(Player player) { | ||
Stream<UsableAction> actions = player.getActions().values().stream(); | ||
Stream<UsableAction> cardActions = player.getGame().getZones().stream() | ||
.flatMap(zone -> zone.getCards().stream()) | ||
.flatMap(card -> card.getActions().values().stream()); | ||
|
||
Stream<UsableAction> allActions = Stream.concat(actions, cardActions).filter(action -> action.isAllowed()) | ||
.filter(action -> setTargetIfPossible(action)); | ||
List<UsableAction> list = allActions.collect(Collectors.toList()); | ||
|
||
if (list.isEmpty()) { | ||
return null; | ||
} | ||
return list.get(random.nextInt(list.size())); | ||
} | ||
|
||
private boolean setTargetIfPossible(UsableAction action) { | ||
if (action instanceof TargetAction) { | ||
TargetAction targetAction = (TargetAction) action; | ||
List<Targetable> targets = targetAction.findTargets(); | ||
if (targets.isEmpty()) { | ||
return false; | ||
} | ||
targetAction.setTarget(targets.get(random.nextInt(targets.size()))); | ||
return true; | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
/target | ||
|
||
### Ignore Netbeans workspace | ||
nb-configuration.xml | ||
nbactions.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
cardshifter-fx/src/main/java/com/cardshifter/fx/CardNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.cardshifter.fx; | ||
|
||
import com.cardshifter.core.Card; | ||
import com.cardshifter.core.LuaTools; | ||
import com.cardshifter.core.TargetAction; | ||
import com.cardshifter.core.Targetable; | ||
import com.cardshifter.core.UsableAction; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javafx.event.ActionEvent; | ||
import javafx.event.EventHandler; | ||
import javafx.scene.Group; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.paint.Color; | ||
import javafx.scene.shape.Rectangle; | ||
|
||
public class CardNode { | ||
|
||
private final int sizeX; | ||
private final int sizeY; | ||
private final String name; | ||
private final Card card; | ||
private final FXMLGameController controller; | ||
|
||
private final Group cardGroup; | ||
|
||
public CardNode(int sizeX, int sizeY, String name, Card card, FXMLGameController controller) { | ||
this.sizeX = sizeX; | ||
this.sizeY = sizeY; | ||
this.name = name; | ||
this.card = card; | ||
this.controller = controller; | ||
this.cardGroup = new Group(); | ||
this.createCard(); | ||
} | ||
|
||
public Group getCardGroup() { | ||
return this.cardGroup; | ||
} | ||
|
||
private void createCard() { | ||
this.createCardBackground(); | ||
this.createCardArt(); | ||
this.createCardIDLabel(); | ||
this.createCardPropertyLabelsGroup(); | ||
if(this.isCardActive() == true) { | ||
this.createCardActivateButton(); | ||
} | ||
} | ||
|
||
private void createCardBackground() { | ||
Rectangle activeBackground = new Rectangle(-10,-10,170,240); | ||
activeBackground.setFill(Color.BLACK); | ||
if(this.isCardActive() == true) { | ||
activeBackground.setFill(Color.YELLOW); | ||
} | ||
cardGroup.getChildren().add(activeBackground); | ||
} | ||
|
||
private void createCardArt() { | ||
Rectangle cardBack = new Rectangle(0,0,150,220); | ||
cardBack.setFill(Color.FIREBRICK); | ||
cardGroup.getChildren().add(cardBack); | ||
} | ||
|
||
private void createCardIDLabel() { | ||
Label cardIdLabel = new Label(); | ||
cardIdLabel.setText(String.format("CardID = %d", card.getId())); | ||
cardIdLabel.setTextFill(Color.WHITE); | ||
cardGroup.getChildren().add(cardIdLabel); | ||
} | ||
|
||
private void createCardPropertyLabelsGroup() { | ||
//This gets the text from Lua and makes labels for each property | ||
int stringIndex = 0; | ||
List<String> stringList = new ArrayList<>(); | ||
LuaTools.processLuaTable(card.data.checktable(), (k, v) -> stringList.add(k + ": " + v)); | ||
for (String string : stringList) { | ||
Group cardTextStrings = new Group(); | ||
cardTextStrings.setTranslateY(25 + (stringIndex * 25)); | ||
cardGroup.getChildren().add(cardTextStrings); | ||
|
||
Label cardStringLabel = new Label(); | ||
cardStringLabel.setText(string); | ||
cardStringLabel.setTextFill(Color.WHITE); | ||
cardTextStrings.getChildren().add(cardStringLabel); | ||
stringIndex++; | ||
} | ||
} | ||
|
||
private void createCardActivateButton() { | ||
//only make a button when the card is active | ||
Button button = new Button(); | ||
//button.setStyle("-fx-background-color:transparent"); | ||
button.minWidth(100); | ||
button.minHeight(100); | ||
button.prefWidth(100); | ||
button.prefHeight(100); | ||
button.setOnAction(this::buttonClick); | ||
cardGroup.getChildren().add(button); | ||
} | ||
|
||
private void buttonClick(ActionEvent event) { | ||
System.out.println("Trying to Perform Action"); | ||
List<UsableAction> cardActions = card.getActions().values().stream().filter(UsableAction::isAllowed).collect(Collectors.toList()); | ||
for (UsableAction action : cardActions) { | ||
if (action.isAllowed()) { | ||
if (action instanceof TargetAction) { | ||
TargetAction targetAction = (TargetAction) action; | ||
List<Targetable> targets = targetAction.findTargets(); | ||
if (targets.isEmpty()) { | ||
return; | ||
} | ||
|
||
//int targetIndex = Integer.parseInt(input.nextLine()); | ||
int targetIndex = 0; | ||
if (targetIndex < 0 || targetIndex >= cardActions.size()) { | ||
return; | ||
} | ||
|
||
//TODO: add a check to make sure the target is valid// | ||
Targetable target = targets.get(targetIndex); | ||
targetAction.setTarget(target); | ||
targetAction.perform(); | ||
} | ||
else action.perform(); | ||
} | ||
this.controller.render(); | ||
} | ||
} | ||
|
||
private boolean isCardActive() { | ||
List<UsableAction> cardActions = card.getActions().values().stream().filter(UsableAction::isAllowed).collect(Collectors.toList()); | ||
return cardActions.size() > 0; | ||
} | ||
} |
Oops, something went wrong.