Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- import "os"
- import "strconv"
- func main() {
- w := &war{}
- number_warrior, _ := strconv.Atoi(os.Args[1])
- kill_after, _ := strconv.Atoi(os.Args[2])
- w.create_battle(number_warrior, kill_after)
- fmt.Println("kill order:")
- w.start()
- }
- type warrior struct {
- is_alive bool
- }
- type war struct {
- number_warrior int
- warriors []warrior
- has_sword int
- is_fighting bool
- kill_after int
- number_of_kill int
- }
- func (w *war) create_battle(number_warrior int, kill_after int) {
- w.number_warrior = number_warrior
- w.has_sword = 0
- w.is_fighting = true
- w.kill_after = kill_after
- w.number_of_kill = 0
- for i := 0; i < w.number_warrior; i++ {
- w.warriors = append(w.warriors, warrior{true})
- }
- }
- func (w *war) kill_next() {
- postion := w.move_next_alive_person()
- fmt.Println(postion + 1)
- w.warriors[postion].is_alive = false
- w.number_of_kill += 1
- }
- func (w *war) pass_sword() {
- postion := w.move_next_alive_person()
- if w.number_of_kill == w.number_warrior-1 {
- w.stop()
- fmt.Println("----------------------------")
- fmt.Println("Last person is alive:")
- fmt.Println(w.has_sword + 1)
- }
- w.has_sword = postion
- }
- func (w war) start() {
- for w.is_fighting == true {
- w.kill_next()
- w.pass_sword()
- }
- }
- func (w *war) stop() {
- w.is_fighting = false
- }
- func (w *war) move_next_alive_person() int {
- postion := (w.has_sword + 1) % (w.number_warrior)
- inc := 1
- for inc < w.kill_after && w.number_of_kill != w.number_warrior-1 {
- if w.warriors[postion].is_alive == true && postion != w.has_sword {
- inc = inc + 1
- }
- if inc < w.kill_after {
- postion = (postion + 1) % (w.number_warrior)
- }
- }
- return postion
- }
Add Comment
Please, Sign In to add comment