gambuzzi

rpgInc incremental kata - soluzione sporca

May 11th, 2015
14
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "encoding/csv"
  5.     "fmt"
  6.     "math/rand"
  7.     "os"
  8.     "strconv"
  9.     "strings"
  10.     "time"
  11. )
  12.  
  13. type Player struct {
  14.     Name           string
  15.     Speed          int
  16.     AttackCounter  int
  17.     MaxHealth      int
  18.     Health         int
  19.     Weapon         string
  20.     WeaponType     int
  21.     DealedDamage   int
  22.     DamageFixed    int
  23.     DamageVariable int
  24.     ArmorName      string
  25.     ArmorHealth    int
  26. }
  27.  
  28. func printDamage(tick int, p1 *Player, p2 *Player, ph1 *Player, ph2 *Player) {
  29.     sh1 := fmt.Sprintf("%d", ph1.Health)
  30.     sh2 := fmt.Sprintf("%d", ph2.Health)
  31.  
  32.     if ph1.ArmorName != "'None'" {
  33.         sh1 = fmt.Sprintf("(%d,%d)", ph1.ArmorHealth, ph1.Health)
  34.     }
  35.     if ph2.ArmorName != "'None'" {
  36.         sh2 = fmt.Sprintf("(%d,%d)", ph2.ArmorHealth, ph2.Health)
  37.     }
  38.  
  39.     fmt.Printf("%d,%s,%s,%s,%d,%s,%s", tick, p1.Name, p2.Name, p1.Weapon, p1.DealedDamage, sh1, sh2)
  40.     if p1.DamageVariable > 0 && p1.DealedDamage == p1.DamageFixed+p1.DamageVariable {
  41.         fmt.Printf(",critical")
  42.     }
  43.     fmt.Printf("\n")
  44. }
  45.  
  46. func readFile(filename string) ([]*Player, int) {
  47.  
  48.     csvfile, err := os.Open(filename)
  49.  
  50.     if err != nil {
  51.         fmt.Println(err)
  52.         os.Exit(1)
  53.     }
  54.  
  55.     defer csvfile.Close()
  56.  
  57.     reader := csv.NewReader(csvfile)
  58.  
  59.     reader.FieldsPerRecord = -1 // see the Reader struct information below
  60.  
  61.     rawCSVdata, err := reader.ReadAll()
  62.  
  63.     if err != nil {
  64.         fmt.Println(err)
  65.         os.Exit(1)
  66.     }
  67.  
  68.     var players []*Player
  69.     var counter int = 0
  70.     // sanity check, display to standard output
  71.     for _, each := range rawCSVdata {
  72.         var armorNameIndex int = 5
  73.         var damageIndex int = 4
  74.         p := new(Player)
  75.         p.Name = each[0]
  76.         p.Speed, _ = strconv.Atoi(strings.Trim(each[1], " "))
  77.         p.Health, _ = strconv.Atoi(strings.Trim(each[2], " "))
  78.         p.MaxHealth = p.Health
  79.  
  80.         p.Weapon = strings.Trim(each[3], " ")
  81.         if string(p.Weapon[0]) == "(" {
  82.             p.Weapon = strings.Trim(p.Weapon, " (")
  83.             p.WeaponType, _ = strconv.Atoi(strings.Trim(each[4], " )"))
  84.             damageIndex++
  85.             armorNameIndex++
  86.         } else {
  87.             p.WeaponType = 0
  88.         }
  89.  
  90.         p.AttackCounter = 0
  91.  
  92.         fixed, err := strconv.Atoi(strings.Trim(each[damageIndex], " "))
  93.         if err == nil {
  94.             p.DamageFixed = fixed
  95.             p.DamageVariable = 0
  96.         } else {
  97.             fixed, _ := strconv.Atoi(strings.Trim(each[damageIndex], " ("))
  98.             variable, _ := strconv.Atoi(strings.Trim(each[damageIndex+1], " )"))
  99.             p.DamageFixed = fixed
  100.             p.DamageVariable = variable - fixed
  101.             armorNameIndex++
  102.         }
  103.  
  104.         if len(each) > armorNameIndex {
  105.             p.ArmorName = each[armorNameIndex]
  106.             p.ArmorHealth, _ = strconv.Atoi(strings.Trim(each[armorNameIndex+1], " "))
  107.         } else {
  108.             p.ArmorName = "'None'"
  109.             p.ArmorHealth = 0
  110.         }
  111.  
  112.         players = append(players, p)
  113.         counter++
  114.     }
  115.     return players, counter
  116. }
  117.  
  118. func main() {
  119.     rand.Seed(time.Now().UnixNano())
  120.  
  121.     players, counter := readFile("combat.rpg")
  122.  
  123.     //type 3
  124.     for idx := range players {
  125.         otherIdx := counter - 1 - idx
  126.         if players[idx].WeaponType == 3 {
  127.             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
  128.         }
  129.     }
  130.  
  131.     if false {
  132.         fmt.Printf("%v\n", players[0])
  133.         fmt.Printf("%v\n", players[1])
  134.         os.Exit(1)
  135.     }
  136.  
  137.     tick := 1
  138.  
  139.     for {
  140.         nothing := true
  141.  
  142.         for _, player := range players {
  143.             if player.Health <= 0 {
  144.                 fmt.Printf("%d,%s,Dead\n", tick, player.Name)
  145.             }
  146.         }
  147.  
  148.         if players[0].Health <= 0 || players[1].Health <= 0 {
  149.             break
  150.         }
  151.  
  152.         for idx := range players {
  153.             if tick%players[idx].Speed == 0 {
  154.                 otherIdx := counter - 1 - idx
  155.                 damage := players[idx].DamageFixed + rand.Intn(players[idx].DamageVariable+1)
  156.  
  157.                 //type 2
  158.                 players[idx].AttackCounter++
  159.                 if players[idx].WeaponType == 2 {
  160.                     if players[idx].AttackCounter%2 == 0 {
  161.                         damage *= 2
  162.                     }
  163.                 }
  164.  
  165.                 if damage <= players[otherIdx].ArmorHealth {
  166.                     players[otherIdx].ArmorHealth -= damage
  167.                 } else {
  168.                     players[otherIdx].Health -= damage - players[otherIdx].ArmorHealth
  169.                     players[otherIdx].ArmorHealth = 0
  170.                 }
  171.  
  172.                 players[idx].DealedDamage = damage
  173.  
  174.                 //type 1
  175.                 if players[idx].WeaponType == 1 {
  176.                     players[idx].Health += damage
  177.                     if players[idx].Health > players[idx].MaxHealth {
  178.                         players[idx].Health = players[idx].MaxHealth
  179.                     }
  180.                 }
  181.  
  182.                 printDamage(tick, players[idx], players[counter-1-idx], players[0], players[1]) //vincolo a 2 giocatori
  183.                 nothing = false
  184.             }
  185.         }
  186.  
  187.         for idx := range players {
  188.             if players[idx].Health < 0 {
  189.                 players[idx].Health = 0
  190.             }
  191.         }
  192.  
  193.         if nothing {
  194.             fmt.Printf("%d,NOTHING\n", tick)
  195.         }
  196.  
  197.         tick++
  198.     }
  199.  
  200.     tie := true
  201.     for _, player := range players {
  202.         if player.Health > 0 {
  203.             fmt.Println(player.Name)
  204.             tie = false
  205.         }
  206.     }
  207.     if tie {
  208.         fmt.Println("TIE")
  209.     }
  210.  
  211. }
RAW Paste Data