Guest User

Untitled

a guest
Nov 21st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4. import "os"
  5. import "strconv"
  6.  
  7. func main() {
  8. w := &war{}
  9. number_warrior, _ := strconv.Atoi(os.Args[1])
  10. kill_after, _ := strconv.Atoi(os.Args[2])
  11. w.create_battle(number_warrior, kill_after)
  12. fmt.Println("kill order:")
  13. w.start()
  14. }
  15.  
  16. type warrior struct {
  17. is_alive bool
  18. }
  19.  
  20. type war struct {
  21. number_warrior int
  22. warriors []warrior
  23. has_sword int
  24. is_fighting bool
  25. kill_after int
  26. number_of_kill int
  27. }
  28.  
  29. func (w *war) create_battle(number_warrior int, kill_after int) {
  30. w.number_warrior = number_warrior
  31. w.has_sword = 0
  32. w.is_fighting = true
  33. w.kill_after = kill_after
  34. w.number_of_kill = 0
  35. for i := 0; i < w.number_warrior; i++ {
  36. w.warriors = append(w.warriors, warrior{true})
  37. }
  38. }
  39.  
  40. func (w *war) kill_next() {
  41. postion := w.move_next_alive_person()
  42. fmt.Println(postion + 1)
  43. w.warriors[postion].is_alive = false
  44. w.number_of_kill += 1
  45. }
  46.  
  47. func (w *war) pass_sword() {
  48. postion := w.move_next_alive_person()
  49. if w.number_of_kill == w.number_warrior-1 {
  50. w.stop()
  51. fmt.Println("----------------------------")
  52. fmt.Println("Last person is alive:")
  53. fmt.Println(w.has_sword + 1)
  54. }
  55. w.has_sword = postion
  56. }
  57.  
  58. func (w war) start() {
  59. for w.is_fighting == true {
  60. w.kill_next()
  61. w.pass_sword()
  62. }
  63. }
  64.  
  65. func (w *war) stop() {
  66. w.is_fighting = false
  67. }
  68.  
  69. func (w *war) move_next_alive_person() int {
  70. postion := (w.has_sword + 1) % (w.number_warrior)
  71. inc := 1
  72. for inc < w.kill_after && w.number_of_kill != w.number_warrior-1 {
  73. if w.warriors[postion].is_alive == true && postion != w.has_sword {
  74. inc = inc + 1
  75. }
  76.  
  77. if inc < w.kill_after {
  78. postion = (postion + 1) % (w.number_warrior)
  79. }
  80. }
  81. return postion
  82. }
Add Comment
Please, Sign In to add comment