Nim Multiplayer Game ( 2 players + AIs)

By manish17xxx on May 24, 2017

Rules to play -
There are 4 boxes with a certain number of gems (chosen randomly). When your turn comes, you can pick a maximum of 3 number of gems from any box. Your task is to force your opponent to pick the last remaining gem.

Commands:-

!nim - Start the game!

!pick (first-letter-of-box) (No-of-gems) - Pick gems from any of the boxes ( Make sure there are enough number of gems present)
Eg: !pick G 3 will pick 3 gems from the gold box.

!replace - Replace yourself with an AI Player who will be playing instead of you.

If no player joins, then an AI Player will be joining by default. It is a 2-player game.

Here is the snippet-

############################################################
##       Nim Multiplayer Version Script ( with AI Players)            ##
##       Version: 1.0              ##
##       By: manish17               ##
##       irc.lunarirc.net   #LunarIRC #LunarGames   User - manish  
  Follow my blog : https://manishsnippets.blogspot.com ##
############################################################
on *:TEXT:!nim*:#: {
  if (%nim != on) && (%ongoing == $null) {
    set %ongoing Nim
    set %nim on
    set %aiplayers Pro Man.Lucy Fir
    set %players $nick
    set %playing 1
    set %chan $chan
    timer 1 1 msg $chan 6 A Game of Nim has been started by %players $+ . 10 There are certain number of gems in each box. You can pick a maximum of 3 gems from any box on your turn. Your task is to force the opponent to pick the last remaining gem.
    timer 1 3 msg %chan 11 Type !accept to fight against him. 17 seconds left...
    timer 1 20 nobody
  }
}

alias -l nobody {
  timers off
  timer 1 1 msg %chan 7 Looks like nobody accepted the challenge. 3 AI Player $gettok(%aiplayers,1,46) is joining the game.
  inc %playing
  set %players %players $+ . $+ $gettok(%aiplayers,1,46)
  set %valid 0
  timer 1 3 msg %chan 9  $gettok(%players,2,46) has accepted the challenge of $gettok(%players,1,46) and now, it's time to start the game of Nim.
  timer 1 5 startgame
}
on *:TEXT:!accept*:#: {
  if (%nim == on) && (%playing == 1) && ($nick != %players) {
    timers off  
    inc %playing
    set %players %players $+ . $+ $nick
    set %valid 0
    timer 1 1 msg %chan 9  $nick has accepted the challenge of $gettok(%players,1,46) and now, it's time to start the game of Nim.
    timer 1 3 startgame
  }
}

alias -l startgame {
  timers off
  set %list G.S.B.L
  set %boxes Gold.Silver.Bronze.Lead
  set %gold 5
  set %silver $rand(6,7)
  set %bronze $rand(7,9)
  set %lead $rand(5,9)
  set %total $calc(%gold + %silver + %bronze + %lead)
  set %gems %gold $+ . $+ %silver $+ . $+ %bronze $+ . $+ %lead
  set %turn 1
  set %tplayer $gettok(%players,1,46)

  timer 1 2 msg %chan 2 The number of gems selected in the boxes are : %gold %silver %bronze %lead
  timer 1 4 showgems
  timer 1 5 msg %chan 6 Its %tplayer $+ 's turn to start the game.
  if ($istok(%aiplayers,%tplayer,46) ) { timer 1 5 ailogic }
  timer 1 5 notice %tplayer To pick gems, type !pick <first letter of box> <no of gems>
  timer 1 5 set %started on
  timer 1 45 timeont
}

alias -l showgems {
  msg %chan 6 Current Gems : 7Gold: $gettok(%gems,1,46) 15Silver: $gettok(%gems,2,46) 5Bronze: $gettok(%gems,3,46) 6Lead: $gettok(%gems,4,46)
}

alias -l checkvalidity {
  if ($1 == $null) || ($2 == $null) || (!$istok(%list,$1,46)) {
    notice %tplayer The correct syntax is !pick <First letter of box> <No of gems>
  }
  elseif ($2 !isnum) || ($2 <= 0) || ($2 > 3) {
    notice %tplayer You should pick 1,2 or 3 number of gems from a particular box.
  }
  else {
    set %choice $2
    set %box $findtok(%list,$1,1,46)
    set %boxed $gettok(%boxes,%box,46)
    set %tgem $gettok(%gems,%box,46)
    if ($calc(%tgem - $2) < 0) {
      notice $nick You cannot pick more gems than there are in the box.
    }
    else {
      inc %valid
    }
  }
}

on *:TEXT:!stopnim*:#: {
  if ($nick isop %chan) {
    end
  }
}
on *:TEXT:!replace*:#: {
  if ($nick == %tplayer) {
    set %first $gettok(%aiplayers,1,46)
    set %second $gettok(%aiplayers,2,46)
    if $istok(%players,%first,46) { set %x 2 }
    else { set %x 1 }
    msg %chan 10 $nick has been replaced with $gettok(%aiplayers,%x,46) .
    set %tplayer $gettok(%aiplayers,%x,46)
    set %players $replace(%players,$nick,%tplayer)

    ailogic
  }
}

on *:TEXT:!pick*:#: {
  if (%started == on) && ($nick == %tplayer) {
    timers off
    checkvalidity $2-
    if (%valid == 1) {
      set %valid 0
      msg %chan 3  %tplayer has picked up $3 gems from the %boxed box.
      set %new $calc(%tgem - $3)
      set %gems $deltok(%gems,%box,46)
      set %gems $instok(%gems,%new,%box,46)
      set %total $calc(%total - $3)
      timer 1 3 showgems
      if (%total == 1) { timer 1 4 finishgame }
      elseif (%total == 0) { timer 1 4 loseyou }
      else {
        inc %turn
        if (%turn > 2) { set %turn 1 }
        set %tplayer $gettok(%players,%turn,46)
        timer 1 5 msg %chan 6 Its %tplayer $+ 's turn to continue the game.
        if ($istok(%aiplayers,%tplayer,46) ) { timer 1 5 ailogic }
        timer 1 5 notice %tplayer To pick gems, type !pick <first letter of box> <no of gems>
        timer 1 45 timeont

      }
    }

  }
}

alias -l loseyou {
  timers off
  timer 1 1 msg %chan 4 %tplayer picked up all the gems, and so he has lost the game eventually. R.I.P!!
  set %players $remtok(%players,%tplayer,1,46)
  timer 1 3 msg %chan 13%players has won the game, earning 20 coins!
  timer 1 3 coins %players 20
  timer 1 5 end
}
alias -l finishgame {
  coins %tplayer 20
  timers off
  timer 1 3 msg %chan 6 There is only 1 gem remaining and so, 5  %tplayer wins the game, earning 20 coins.
  timer 1 4 end
}

alias -l end {
  timers off
  msg %chan 10 Game has been finished/stopped.
  unset %ongoing %started %nim %playing %players %turn %chan
}

alias -l timeont {
  set %ok 1
  msg %chan 6 %tplayer ran out of time, and so he will be making a default move.
  getmove
}

alias -l ailogic {
  timers off
  if (%total >= 9 ) { set %ok $rand(2,3) }
  elseif (%total >= 5 ) { set %ok $rand(1,2) }
  elseif (%total == 4) && ($istok(%gems,3,46)) { set %ok 3 }
  else { set %ok 1 }
  getmove
}

alias -l getmove {
  if ($gettok(%gems,1,46) >= %ok) { set %bc 1 | set %bx G }
  elseif ($gettok(%gems,2,46) >= %ok) { set %bc 2 | set %bx S }
  elseif ($gettok(%gems,3,46) >= %ok) { set %bc 3 | set %bx B }
  else { set %bc 4 | set %bx L }
  set %tgem $gettok(%gems,%bc,46)
  set %boxed $gettok(%boxes,%bc,46)
  set %new $calc(%tgem - %ok)
  set %gems $deltok(%gems,%bc,46)
  set %gems $instok(%gems,%new,%bc,46)
  set %total $calc(%total - %ok)
  timer 1 3 msg %chan  11 %tplayer picks up %ok gems from the %boxed box.
  timer 1 5 showgems
  if (%total == 1) { timer 1 4 finishgame }
  elseif (%total == 0) { timer 1 4 loseyou }
  else {
    inc %turn
    if (%turn > 2) { set %turn 1 }
    set %tplayer $gettok(%players,%turn,46)
    timer 1 5 msg %chan 6 Its %tplayer $+ 's turn to continue the game.
    if ($istok(%aiplayers,%tplayer,46) ) { timer 1 5 ailogic }
    timer 1 45 timeont
  }
}

alias -l coins {
  if ($istok(%aiplayers,$1-2,46)) { return }
  elseif ($read(coins.txt,w,* $+ $1 $+ *)) {
    set %n $readn $+
    set %data $read(coins.txt,w,* $+ $1 $+ *)
    set %current $gettok(%data,2,32)
    set %new $calc(%current + $2)
    set %data $replace(%data,%current,%new)
    write -dl $+ %n coins.txt
    write coins.txt %data
  }
  else {
    write coins.txt $1 $2
  }
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.