Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Game simulation in model.test WORKS, FULLY
Browse files Browse the repository at this point in the history
  • Loading branch information
Tikidoodlep1 committed Apr 26, 2024
1 parent ca113a7 commit dcadf4f
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 46 deletions.
41 changes: 33 additions & 8 deletions src/engine/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Card {
imageURL: string = "";
private cost: number;
private isReady: boolean;
private location: BoardPos | string = CardLocations.Deck;
location: BoardPos | string = CardLocations.Deck;

constructor(
name: string,
Expand All @@ -36,7 +36,7 @@ export class Card {
this.cost = cost;
this.landscapeType = landscapeType;
this.turnPlayed = Game.getInstance().currentTurn;
this.isReady = true;
this.isReady = false;
}

setImageUrl(URL: string): Card {
Expand Down Expand Up @@ -87,15 +87,20 @@ export class Card {
}

play(_target: BoardPos, _playerId: number) {
console.log("Calling play in class Card");
console.log("Calling play(target: BoardPos, playerId: number) in class Card, don't do that!");
this.displayCard();
return false;
}

death() {
console.log("Calling death() on " + this.name);
if (this.ownerId != null) {
Game.getInstance().getPlayerById(this.ownerId).discardPile.push(this);
this.moveCard(CardLocations.Discard);
//this.moveCard(CardLocations.Discard); //Doesn't actually move the cards from the board...
if(this.location instanceof BoardPos) {
console.log(this.name + " has died from lane " + (this.location.posId+1));
this.location.removeCreature();
}
}
}

Expand Down Expand Up @@ -197,6 +202,11 @@ export class Card {
);
}

equals(other: Card): boolean {
return this.name == other.name && this.flavorText == other.flavorText && this.cardType == other.cardType && this.cost == other.cost
&& this.landscapeType == other.landscapeType;
}

//Null Card Constant
static getNull(): Card {
return new Card("Null", "You shouldn't be seeing this!", 99, 0, LandscapeType.NULL);
Expand All @@ -223,6 +233,11 @@ export class Creature extends Card {
}

Attack(Target: Creature | Player) {
if(!this.getIsReady()) {
console.log("Creature not ready!");
return false;
}

if (Target instanceof Creature) {
Target.defense -= this.attack;
if (Target.defense <= 0) {
Expand All @@ -232,18 +247,23 @@ export class Creature extends Card {
if (this.defense <= 0) {
this.death();
}
this.setIsReady(false);
return true;
} else {
Target.hp -= this.attack;
this.setIsReady(false);
return true;
}
}

override play(pos: BoardPos, playerId: number) {
console.log("Playing Creature " + this.name + " at pos " + pos.posId + " on player " + playerId + "'s side of the board");
if (pos.creature.name == Creature.getNull().name) {
if(pos.setCreature(this)) {
Game.getInstance().getPlayerById(playerId).actions -= this.getCost();
return true;
}
if(pos.setCreature(this)) {
this.location = pos;
this.ownerId = playerId;
return true;
}
}
return false;
}
Expand All @@ -259,6 +279,11 @@ export class Creature extends Card {
);
}

override equals(other: Creature): boolean {
return this.name == other.name && this.flavorText == other.flavorText && this.cardType == other.cardType && this.getCost() == other.getCost()
&& this.landscapeType == other.landscapeType && this.maxDefense == other.maxDefense;
}

//Creature Constants
static getNull(): Creature {
return new Creature(
Expand Down
62 changes: 45 additions & 17 deletions src/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ test("Game is Playable", () => {
getCardFromCardMap("Bog Bum"), getCardFromCardMap("Fly Swatter"), getCardFromCardMap("Dark Angel"), getCardFromCardMap("Bog Bum"),
getCardFromCardMap("Fly Swatter"), getCardFromCardMap("Dark Angel"), getCardFromCardMap("Bog Bum"), getCardFromCardMap("Fly Swatter")];

assert(deck2.map((card: Card) => {return card.name != Card.getNull().name}));
var tempDeck: Card[] = [getCardFromCardMap("Dark Angel"), getCardFromCardMap("Bog Bum"), getCardFromCardMap("Fly Swatter"), getCardFromCardMap("Dark Angel"),
getCardFromCardMap("Bog Bum"), getCardFromCardMap("Fly Swatter"), getCardFromCardMap("Dark Angel"), getCardFromCardMap("Bog Bum"),
getCardFromCardMap("Fly Swatter"), getCardFromCardMap("Dark Angel"), getCardFromCardMap("Bog Bum"), getCardFromCardMap("Fly Swatter")];

var swampLand: Landscape = new Landscape("Swamp", "Goopy!", LandscapeType.Swamp);

Game.startNewGame();

//Game Test
Game.getInstance().addGameEventListener(GetCardTargetEvent, (evt: Event) => {
console.log("Recieved event with name " + evt.type);
//console.log("Recieved event with name " + evt.type);
if(evt instanceof GetBoardPosTargetEvent) {
console.log("Recieved event of Type GetBoardPosTargetEvent!");
//console.log("Recieved event of Type GetBoardPosTargetEvent!");
var player: Player = Game.getInstance().getPlayerById(evt.executorId);
var didExecute = false;
var playerBoard: BoardPos[] | undefined = Game.getInstance().board.getSideByOwnerId(player.id)
Expand All @@ -58,7 +60,7 @@ test("Game is Playable", () => {
});

Game.getInstance().addGameEventListener(PlayCardEventName, (evt: Event) => {
console.log("Recieved event with name " + evt.type);
console.log("Recieved PlayCardEvent event with name " + evt.type);
// if(evt instanceof PlayCardEvent) {
// var board: BoardPos[] | undefined = Game.getInstance().board.getSideByOwnerId(Game.getInstance().getPlayerById(evt.executorId).id);
// if(typeof(board) != "undefined") {
Expand All @@ -79,21 +81,40 @@ test("Game is Playable", () => {

assert(Game.getInstance().currentPlayer == Game.getInstance().getPlayerById(0));

Game.getInstance().currentPlayer.deck = deck1;
Game.getInstance().currentPlayer.drawCard(6, false);
Game.getInstance().getPlayerById(0).setDeck(deck1);
Game.getInstance().getPlayerById(1).setDeck(deck2);

// console.log("===================================== SHUFFLED DECKS =====================================");
// var deck1String = "";
// tempDeck.map((card: Card) => {deck1String += (card.name + ", ")});
// console.log("Pre-shuffle Deck: {" + deck1String.slice(0, deck1String.length - 2) + "}, length " + tempDeck.length);
// deck1String = "";
// Game.getInstance().getPlayerById(0).deck.map((card: Card) => {deck1String += (card.name + ", ")});
// console.log("Player 1's Deck: {" + deck1String.slice(0, deck1String.length - 2) + "}, length " + Game.getInstance().getPlayerById(0).deck.length);
// deck1String = "";
// Game.getInstance().getPlayerById(1).deck.map((card: Card) => {deck1String += (card.name + ", ")});
// console.log("Player 2's Deck: {" + deck1String.slice(0, deck1String.length - 2) + "}, length " + Game.getInstance().getPlayerById(1).deck.length);

Game.getInstance().getPlayerById(0).drawCard(6, false);
Game.getInstance().getPlayerById(1).drawCard(5, false);

assert(Game.getInstance().currentPlayer.hand.length == 6);

for(var i = 0; i < 25; i++) {
console.log("Current player's hand: ");
Game.getInstance().currentPlayer.hand.map((card: Card) => console.log(card.name))

for(var i = 0; i < 50; i++) {
console.log("====================================== TURN " + (i+1) + " ======================================");
console.log("Game Phase: " + Game.getInstance().turnPhase + ", Actual Game turn: " + (Game.getInstance().currentTurn+1));
console.log("My HP: " + Game.getInstance().currentPlayer.hp + ", Their HP: " + Game.getInstance().getOtherPlayer(Game.getInstance().currentPlayer.id).hp);
var cardHand = "";
Game.getInstance().currentPlayer.hand.map((card: Card) => {cardHand += (card.name + ", ")});
console.log("Current player's hand: [" + cardHand.slice(0, cardHand.length - 2) + "]");

var playerHand: Card[] = Game.getInstance().currentPlayer.hand;
for(var j = 0; j < playerHand.length; j++) {
var card: Card = playerHand[j];
if(Game.getInstance().currentPlayer.actions >= card.getCost() && card.cardType == CardType.Creature) {
//console.log("Trying to play card " + card.name);
if(Game.getInstance().playCard(card, Game.getInstance().currentPlayer.id)) {
console.log("Trying to play card " + card.name);
//console.log("Trying to play card " + card.name);
}
}
}
Expand All @@ -106,25 +127,30 @@ test("Game is Playable", () => {

assert(typeof(mySide) != "undefined" && typeof(theirSide) != "undefined");
assert(mySide.length == theirSide.length);
console.log("My Side");

var mySideString = "";
for(var j = 0; j < mySide.length; j++) {
console.log(mySide[j].creature.name + ", ");
mySideString += (mySide[j].creature.name + "{A:" + mySide[j].creature.attack + "--D:" + mySide[j].creature.defense + "}" + ", ");
}
console.log("My Side: [" + mySideString.slice(0, mySideString.length - 2) + "]");

console.log("Their Side:");
var theirSideString = "";
for(var j = 0; j < theirSide.length; j++) {
console.log(theirSide[j].creature.name + ", ");
theirSideString += (theirSide[j].creature.name + "{A:" + theirSide[j].creature.attack + "--D:" + theirSide[j].creature.defense + "}" + ", ");
}
console.log("Their Side: [" + theirSideString.slice(0, theirSideString.length - 2) + "]");

//Do attacks
if(typeof(mySide) != "undefined" && typeof(theirSide) != "undefined") {
for(var j = 0; j < mySide.length; j++) {
var myPos: BoardPos = mySide[j];
var theirPos: BoardPos = theirSide[j];
if(myPos.creature != Creature.getNull()) {
if(theirPos.creature == Creature.getNull()) {
if(!myPos.creature.equals(Creature.getNull())) {
if(!theirPos.creature.equals(Creature.getNull())) {
console.log("My " + myPos.creature.name + " is attacking their " + theirPos.creature.name + " in lane " + (myPos.posId+1) + "!");
myPos.creature.Attack(theirPos.creature);
}else {
console.log("My " + myPos.creature.name + " is attacking their hero from lane " + myPos.posId + "!");
myPos.creature.Attack(Game.getInstance().getOtherPlayer(Game.getInstance().currentPlayer.id));
}
}
Expand All @@ -135,5 +161,7 @@ test("Game is Playable", () => {
console.log("Player " + Game.getInstance().currentPlayer.id + " has won the game!");
break;
}

Game.getInstance().enterNextPhase();
}
});
Loading

0 comments on commit dcadf4f

Please sign in to comment.