Swords and Hammers
Tags: javascript, webdev, learning, tutorial
Published: 2023-11-07 13:14:02


This is part 2 in a series.  Please read part 1 first.

So last time I went over the challenge I set out for myself. I have come up with the name sword and hammers. I feel this represents what happens well. I would also like to talk about why I am doing this. Which is first and foremost for me to learn. But hopefully others can learn  from this as well or better yet notice bugs or improvements that can be made. With that out of the way it's time to talk about the implementation. The nitty gritty so to speak.


I have split the javascript code up into 2 parts: game logic and UI logic. Today I am talking strictly about game logic. Which is just a collection of functions that when called statically could play the game within the console. First I create some global objects that will form the basis of the game.  These objects will be manipulated as the game is played.


## Player Objects

So what do we actually need? It has 2 players who have stats such as hit points and gold. So I create 2 identical objects. These objects have a fair amount of properties, some of which are arrays that grow and shrink as the game is played. 


```js

let playerOne = {

  hp: 20,

  gold: 10,

  building:'none',

  totalAttack: 0,

  totalDefend: 0,

  workers: [],

  fighters: [],

  graveyard: []

  }


let playerTwo= {

  hp: 20,

  gold: 10,

  building:'none',

  totalAttack: 0,

  totalDefend: 0,

  workers: [],

  fighters: [],

  graveyard:[]

  }

```

Let me break down what each of these properties are for and how they affect the game. 


- Hp and gold are numbers representing how much health and resources a player has respectfully.

 

- The building property will have 2 states: the string 'none' or an object representing a unit selected by that player for creation in the build phase.


- TotalAttack and totalDefend are numbers representing the number of fighter units with the action attack or defend respectively. 


- Workers is an array containing the individual objects for each worker unit that the player has. 


- Fighters is also an array with individual objects for each fighter unit the player has. 


- The graveyard is an array as units are killed of their objects are pushed to this array.




## Game Object

There is also a game object. It has the following properties.

- turn which is a number representing what turn the game is on. Default value is  0. 

- active is a reference to the player object whose turn it is. The default is playeOne. Since this is a reference we can manipulate the properties within active and have the changes also appear in the actual player object. 

- p1select has a default value of the  string 'none' meaning the playerOne has not selected to build any units this turn. Or it is an object representing the unit selected by the playerOne for creation. This object will then be passed to the validation algorithm to see if the player has enough gold to build it and has no other units with the same name.

- P2select is the same as p1select but applies to playerTwo instead.


```js

let game = {

  turn:0,

  active : playerOne

  p1Select:'none',

  p2Select: 'none',

}

```


## Unit Objects

The next challenge was the unit objects. I needed two, one worker and one fighter. I opted to use object constructor functions for each. There will be many of each unit for each player but they will all have the same properties. 


```js

function worker(input) {

  this.type = 'workers',

  this.cost = 5,

  this.action = "none",

  this.name = input

  }


function fighter(input){

  this.type = 'fighters'

  this.cost = 10,

  this.action = "none",

  this.name = input

  }

```

The functions take in one argument which is a string. This string is a unique name assigned by the player that will be used to identify that specific unit from others in the same array. I feel the names are a fun way to give more meaning to the game. Losing your first fighter 'jimmy the slayer' has a slightly bigger emotional impact compared to one less number in a list. Let's talk about the properties.

- Type is a string that tells us what type the unit is. The string remains constant and is never manipulated. This is used by the testing algorithm to determine if a unit is a worker or a fighter.

- Cost a number telling us how much gold it will cost tye player to make this unit

- Action is a string whose default is 'none' meaning the unit has no assigned action for this turn. Each unit has 2 possible actions represented by strings. Workers can have 'gold' meaning they produce 1 gold this turn or 'sac' meaning they are sent to live in the castle and give the player 1 hit point. Fighters can have 'attack' meaning they will do 1 damage to the other player. Or 'defend' which cancels out an attack from one of the other players' fighters. 

- Name is a unique string that identifies this unit from others in its array. This string is passed as an argument to the function when the object is created.

## Wrapping up
And there we have it. These global objects form the basis of the game.  All we need now are some functions to manipulate them. Thats a story for another day though. So check back because next time we will deep dive into the validation algorithm and, if we have time, talk about the functions that make the battle system work. I hope this article provides some value to you as writing really helped me learned a lot. See you next time.