Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/csv"
- "fmt"
- "math/rand"
- "os"
- "strconv"
- "strings"
- "time"
- )
- type Player struct {
- Name string
- Speed int
- AttackCounter int
- MaxHealth int
- Health int
- Weapon string
- WeaponType int
- DealedDamage int
- DamageFixed int
- DamageVariable int
- ArmorName string
- ArmorHealth int
- }
- func printDamage(tick int, p1 *Player, p2 *Player, ph1 *Player, ph2 *Player) {
- sh1 := fmt.Sprintf("%d", ph1.Health)
- sh2 := fmt.Sprintf("%d", ph2.Health)
- if ph1.ArmorName != "'None'" {
- sh1 = fmt.Sprintf("(%d,%d)", ph1.ArmorHealth, ph1.Health)
- }
- if ph2.ArmorName != "'None'" {
- sh2 = fmt.Sprintf("(%d,%d)", ph2.ArmorHealth, ph2.Health)
- }
- fmt.Printf("%d,%s,%s,%s,%d,%s,%s", tick, p1.Name, p2.Name, p1.Weapon, p1.DealedDamage, sh1, sh2)
- if p1.DamageVariable > 0 && p1.DealedDamage == p1.DamageFixed+p1.DamageVariable {
- fmt.Printf(",critical")
- }
- fmt.Printf("\n")
- }
- func readFile(filename string) ([]*Player, int) {
- csvfile, err := os.Open(filename)
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- defer csvfile.Close()
- reader := csv.NewReader(csvfile)
- reader.FieldsPerRecord = -1 // see the Reader struct information below
- rawCSVdata, err := reader.ReadAll()
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- var players []*Player
- var counter int = 0
- // sanity check, display to standard output
- for _, each := range rawCSVdata {
- var armorNameIndex int = 5
- var damageIndex int = 4
- p := new(Player)
- p.Name = each[0]
- p.Speed, _ = strconv.Atoi(strings.Trim(each[1], " "))
- p.Health, _ = strconv.Atoi(strings.Trim(each[2], " "))
- p.MaxHealth = p.Health
- p.Weapon = strings.Trim(each[3], " ")
- if string(p.Weapon[0]) == "(" {
- p.Weapon = strings.Trim(p.Weapon, " (")
- p.WeaponType, _ = strconv.Atoi(strings.Trim(each[4], " )"))
- damageIndex++
- armorNameIndex++
- } else {
- p.WeaponType = 0
- }
- p.AttackCounter = 0
- fixed, err := strconv.Atoi(strings.Trim(each[damageIndex], " "))
- if err == nil {
- p.DamageFixed = fixed
- p.DamageVariable = 0
- } else {
- fixed, _ := strconv.Atoi(strings.Trim(each[damageIndex], " ("))
- variable, _ := strconv.Atoi(strings.Trim(each[damageIndex+1], " )"))
- p.DamageFixed = fixed
- p.DamageVariable = variable - fixed
- armorNameIndex++
- }
- if len(each) > armorNameIndex {
- p.ArmorName = each[armorNameIndex]
- p.ArmorHealth, _ = strconv.Atoi(strings.Trim(each[armorNameIndex+1], " "))
- } else {
- p.ArmorName = "'None'"
- p.ArmorHealth = 0
- }
- players = append(players, p)
- counter++
- }
- return players, counter
- }
- func main() {
- rand.Seed(time.Now().UnixNano())
- players, counter := readFile("combat.rpg")
- //type 3
- for idx := range players {
- otherIdx := counter - 1 - idx
- if players[idx].WeaponType == 3 {
- players[idx].Weapon, players[idx].WeaponType, players[idx].DamageFixed, players[idx].DamageVariable, players[otherIdx].Weapon, players[otherIdx].WeaponType, players[otherIdx].DamageFixed, players[otherIdx].DamageVariable = players[otherIdx].Weapon, players[otherIdx].WeaponType, players[otherIdx].DamageFixed, players[otherIdx].DamageVariable, players[idx].Weapon, players[idx].WeaponType, players[idx].DamageFixed, players[idx].DamageVariable
- }
- }
- if false {
- fmt.Printf("%v\n", players[0])
- fmt.Printf("%v\n", players[1])
- os.Exit(1)
- }
- tick := 1
- for {
- nothing := true
- for _, player := range players {
- if player.Health <= 0 {
- fmt.Printf("%d,%s,Dead\n", tick, player.Name)
- }
- }
- if players[0].Health <= 0 || players[1].Health <= 0 {
- break
- }
- for idx := range players {
- if tick%players[idx].Speed == 0 {
- otherIdx := counter - 1 - idx
- damage := players[idx].DamageFixed + rand.Intn(players[idx].DamageVariable+1)
- //type 2
- players[idx].AttackCounter++
- if players[idx].WeaponType == 2 {
- if players[idx].AttackCounter%2 == 0 {
- damage *= 2
- }
- }
- if damage <= players[otherIdx].ArmorHealth {
- players[otherIdx].ArmorHealth -= damage
- } else {
- players[otherIdx].Health -= damage - players[otherIdx].ArmorHealth
- players[otherIdx].ArmorHealth = 0
- }
- players[idx].DealedDamage = damage
- //type 1
- if players[idx].WeaponType == 1 {
- players[idx].Health += damage
- if players[idx].Health > players[idx].MaxHealth {
- players[idx].Health = players[idx].MaxHealth
- }
- }
- printDamage(tick, players[idx], players[counter-1-idx], players[0], players[1]) //vincolo a 2 giocatori
- nothing = false
- }
- }
- for idx := range players {
- if players[idx].Health < 0 {
- players[idx].Health = 0
- }
- }
- if nothing {
- fmt.Printf("%d,NOTHING\n", tick)
- }
- tick++
- }
- tie := true
- for _, player := range players {
- if player.Health > 0 {
- fmt.Println(player.Name)
- tie = false
- }
- }
- if tie {
- fmt.Println("TIE")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement