manish

Nim Multiplayer Game by manish

May 24th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 7.15 KB | None | 0 0
  1. ############################################################
  2. ##       Nim Multiplayer Version Script ( with AI Players)            ##
  3. ##       Version: 1.0              ##
  4. ##       By: manish               ##
  5. ##       irc.lunarirc.net   #LunarIRC #LunarGames   User - manish  
  6.   ## COMMANDS ##
  7. !nim - Start the game!
  8. !pick <first letter of box> <No of gems> - Pick gems from any of the boxes ( Make sure there are enough number of gems present)
  9. !replace - Replace yourself with an AI Player who will be playing instead of you.
  10. ############################################################
  11. on *:TEXT:!nim*:#: {
  12.   if (%nim != on) && (%ongoing == $null) {
  13.     set %ongoing Nim
  14.     set %nim on
  15.     set %aiplayers Pro Man.Lucy Fir
  16.     set %players $nick
  17.     set %playing 1
  18.     set %chan $chan
  19.     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.
  20.     timer 1 3 msg %chan 11 Type !accept to fight against him. 17 seconds left...
  21.     timer 1 20 nobody
  22.   }
  23. }
  24.  
  25. alias -l nobody {
  26.   timers off
  27.   timer 1 1 msg %chan 7 Looks like nobody accepted the challenge. 3 AI Player $gettok(%aiplayers,1,46) is joining the game.
  28.   inc %playing
  29.   set %players %players $+ . $+ $gettok(%aiplayers,1,46)
  30.   set %valid 0
  31.   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.
  32.   timer 1 5 startgame
  33. }
  34. on *:TEXT:!accept*:#: {
  35.   if (%nim == on) && (%playing == 1) && ($nick != %players) {
  36.     timers off  
  37.     inc %playing
  38.     set %players %players $+ . $+ $nick
  39.     set %valid 0
  40.     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.
  41.     timer 1 3 startgame
  42.   }
  43. }
  44.  
  45. alias -l startgame {
  46.   timers off
  47.   set %list G.S.B.L
  48.   set %boxes Gold.Silver.Bronze.Lead
  49.   set %gold 5
  50.   set %silver $rand(6,7)
  51.   set %bronze $rand(7,9)
  52.   set %lead $rand(5,9)
  53.   set %total $calc(%gold + %silver + %bronze + %lead)
  54.   set %gems %gold $+ . $+ %silver $+ . $+ %bronze $+ . $+ %lead
  55.   set %turn 1
  56.   set %tplayer $gettok(%players,1,46)
  57.  
  58.   timer 1 2 msg %chan 2 The number of gems selected in the boxes are : %gold %silver %bronze %lead
  59.   timer 1 4 showgems
  60.   timer 1 5 msg %chan 6 Its %tplayer $+ 's turn to start the game.
  61.   if ($istok(%aiplayers,%tplayer,46) ) { timer 1 5 ailogic }
  62.   timer 1 5 notice %tplayer To pick gems, type !pick <first letter of box> <no of gems>
  63.   timer 1 5 set %started on
  64.   timer 1 45 timeont
  65. }
  66.  
  67. alias -l showgems {
  68.   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)
  69. }
  70.  
  71. alias -l checkvalidity {
  72.   if ($1 == $null) || ($2 == $null) || (!$istok(%list,$1,46)) {
  73.     notice %tplayer The correct syntax is !pick <First letter of box> <No of gems>
  74.   }
  75.   elseif ($2 !isnum) || ($2 <= 0) || ($2 > 3) {
  76.     notice %tplayer You should pick 1,2 or 3 number of gems from a particular box.
  77.   }
  78.   else {
  79.     set %choice $2
  80.     set %box $findtok(%list,$1,1,46)
  81.     set %boxed $gettok(%boxes,%box,46)
  82.     set %tgem $gettok(%gems,%box,46)
  83.     if ($calc(%tgem - $2) < 0) {
  84.       notice $nick You cannot pick more gems than there are in the box.
  85.     }
  86.     else {
  87.       inc %valid
  88.     }
  89.   }
  90. }
  91.  
  92. on *:TEXT:!stopnim*:#: {
  93.   if ($nick isop %chan) {
  94.     end
  95.   }
  96. }
  97. on *:TEXT:!replace*:#: {
  98.   if ($nick == %tplayer) {
  99.     set %first $gettok(%aiplayers,1,46)
  100.     set %second $gettok(%aiplayers,2,46)
  101.     if $istok(%players,%first,46) { set %x 2 }
  102.     else { set %x 1 }
  103.     msg %chan 10 $nick has been replaced with $gettok(%aiplayers,%x,46) .
  104.     set %tplayer $gettok(%aiplayers,%x,46)
  105.     set %players $replace(%players,$nick,%tplayer)
  106.  
  107.     ailogic
  108.   }
  109. }
  110.  
  111. on *:TEXT:!pick*:#: {
  112.   if (%started == on) && ($nick == %tplayer) {
  113.     timers off
  114.     checkvalidity $2-
  115.     if (%valid == 1) {
  116.       set %valid 0
  117.       msg %chan 3  %tplayer has picked up $3 gems from the %boxed box.
  118.       set %new $calc(%tgem - $3)
  119.       set %gems $deltok(%gems,%box,46)
  120.       set %gems $instok(%gems,%new,%box,46)
  121.       set %total $calc(%total - $3)
  122.       timer 1 3 showgems
  123.       if (%total == 1) { timer 1 4 finishgame }
  124.       elseif (%total == 0) { timer 1 4 loseyou }
  125.       else {
  126.         inc %turn
  127.         if (%turn > 2) { set %turn 1 }
  128.         set %tplayer $gettok(%players,%turn,46)
  129.         timer 1 5 msg %chan 6 Its %tplayer $+ 's turn to continue the game.
  130.         if ($istok(%aiplayers,%tplayer,46) ) { timer 1 5 ailogic }
  131.         timer 1 5 notice %tplayer To pick gems, type !pick <first letter of box> <no of gems>
  132.         timer 1 45 timeont
  133.  
  134.       }
  135.     }
  136.  
  137.   }
  138. }
  139.  
  140.  
  141. alias -l loseyou {
  142.   timers off
  143.   timer 1 1 msg %chan 4 %tplayer picked up all the gems, and so he has lost the game eventually. R.I.P!!
  144.   set %players $remtok(%players,%tplayer,1,46)
  145.   timer 1 3 msg %chan 13%players has won the game, earning 20 coins!
  146.   timer 1 3 coins %players 20
  147.   timer 1 5 end
  148. }
  149. alias -l finishgame {
  150.   coins %tplayer 20
  151.   timers off
  152.   timer 1 3 msg %chan 6 There is only 1 gem remaining and so, 5  %tplayer wins the game, earning 20 coins.
  153.   timer 1 4 end
  154. }
  155.  
  156. alias -l end {
  157.   timers off
  158.   msg %chan 10 Game has been finished/stopped.
  159.   unset %ongoing %started %nim %playing %players %turn %chan
  160. }
  161.  
  162. alias -l timeont {
  163.   set %ok 1
  164.   msg %chan 6 %tplayer ran out of time, and so he will be making a default move.
  165.   getmove
  166. }
  167.  
  168. alias -l ailogic {
  169.   timers off
  170.   if (%total >= 9 ) { set %ok $rand(2,3) }
  171.   elseif (%total >= 5 ) { set %ok $rand(1,2) }
  172.   elseif (%total == 4) && ($istok(%gems,3,46)) { set %ok 3 }
  173.   else { set %ok 1 }
  174.   getmove
  175. }
  176.  
  177. alias -l getmove {
  178.   if ($gettok(%gems,1,46) >= %ok) { set %bc 1 | set %bx G }
  179.   elseif ($gettok(%gems,2,46) >= %ok) { set %bc 2 | set %bx S }
  180.   elseif ($gettok(%gems,3,46) >= %ok) { set %bc 3 | set %bx B }
  181.   else { set %bc 4 | set %bx L }
  182.   set %tgem $gettok(%gems,%bc,46)
  183.   set %boxed $gettok(%boxes,%bc,46)
  184.   set %new $calc(%tgem - %ok)
  185.   set %gems $deltok(%gems,%bc,46)
  186.   set %gems $instok(%gems,%new,%bc,46)
  187.   set %total $calc(%total - %ok)
  188.   timer 1 3 msg %chan  11 %tplayer picks up %ok gems from the %boxed box.
  189.   timer 1 5 showgems
  190.   if (%total == 1) { timer 1 4 finishgame }
  191.   elseif (%total == 0) { timer 1 4 loseyou }
  192.   else {
  193.     inc %turn
  194.     if (%turn > 2) { set %turn 1 }
  195.     set %tplayer $gettok(%players,%turn,46)
  196.     timer 1 5 msg %chan 6 Its %tplayer $+ 's turn to continue the game.
  197.     if ($istok(%aiplayers,%tplayer,46) ) { timer 1 5 ailogic }
  198.     timer 1 45 timeont
  199.   }
  200. }
  201.  
  202. alias -l coins {
  203.   if ($istok(%aiplayers,$1-2,46)) { return }
  204.   elseif ($read(coins.txt,w,* $+ $1 $+ *)) {
  205.     set %n $readn $+
  206.     set %data $read(coins.txt,w,* $+ $1 $+ *)
  207.     set %current $gettok(%data,2,32)
  208.     set %new $calc(%current + $2)
  209.     set %data $replace(%data,%current,%new)
  210.     write -dl $+ %n coins.txt
  211.     write coins.txt %data
  212.   }
  213.   else {
  214.     write coins.txt $1 $2
  215.   }
  216. }
Add Comment
Please, Sign In to add comment