[楓之谷私服]小遊戲系列NPC腳本(2) – BJ、GEMS、OX遊戲、Mastermind、猜數字

最後更新於 2021 年 10 月 17 日

同系列上一篇:[楓之谷私服]小遊戲系列NPC腳本(1) – 21點、寶石連線、排列組合、踩地雷、老虎機

因為都是從其他地方收集來的,所以不是我本人寫的程式碼,看了一下還有待優化,有興趣的話可以自行修改。

Black Jack

來源:Sharky

asRGs9U [楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
/**
 *
 * @author Sharky
 * @info BlackJack NPC
 */
importPackage(java.lang);
var debug = false; // set to true for debugging checks to appear in your .bat files
var status = 0,
  bet,
  selected;
var whichFunction = { HowToPlay: false, PlayGame: false, SpendChips: false };
var playingAction = { Hit: false, Stay: false };
var CardsInDeck = [[], []],
  CardsInPlayerHand = [[], []],
  CardsInDealerHand = [[], []];
var prizes = [[4001008], [10]]; // Item 4001008 (Pass) costs 10 Chips
// var prizes = [[enter, item, ids, here, like, so], [enter, amount, of, chips, needed, for, corresponding, item, here, like, so]];

function start() {
  cm.sendSimple(
    "你好!我是#rBlackJack莊家#k.#b \r\n\t#L0#我該怎麼玩BlackJack?#l \r\n\t#L1#我想玩Black Jack!#l \r\n\t#L2#我想用我的Black Jack 籌碼換取獎品!#l"
  );
}

function action(m, t, s) {
  if (m == -1 || (m != 1 && whichFunction["PlayGame"])) {
    cm.dispose();
    return;
  } else {
    if (m == 1) {
      status++;
    } else {
      status--;
    }
    if (
      !whichFunction["HowToPlay"] &&
      !whichFunction["PlayGame"] &&
      !whichFunction["SpendChips"]
    ) {
      whichFunction[
        s == 0 ? "HowToPlay" : s == 1 ? "PlayGame" : "SpendChips"
      ] = true;
    }
    if (whichFunction["HowToPlay"]) {
      HowToPlay();
    } else if (whichFunction["PlayGame"]) {
      PlayGame(s);
    } else if (whichFunction["SpendChips"]) {
      SpendChips(s);
    }
  }
}

function HowToPlay() {
  if (status == 1) {
    if (debug) {
      System.out.println("HowToPlay() is progressing...");
    }
    cm.sendNext(
      "好吧,我看到我們這裡有一個菜鳥!不用擔心,我會很樂意教您如何玩 #rBlackJack#k!"
    );
  } else if (status == 2) {
    cm.sendNextPrev(
      "系統會給您 #b籌碼#k玩遊戲。這些#b籌碼#k 是給您用來下注的!在發給您任何牌之前,您必須先下注。如果您贏得一輪,您的籌碼將增加一倍,如果您只抽兩張牌就達到#bBlack Jack#k(21點),您的籌碼將被返還2.5倍!"
    );
  } else if (status == 3) {
    cm.sendNextPrev(
      "在遊戲開始時,您將獲得2張卡。計算這些卡並加總值。 #b遊戲的目標是當您將手中的所有卡牌加在一起時獲得21點(或接近21點)。#k. 完成此操作後,您可以選擇#bHit#k (用來抽出另一張牌), 或 #bStay#k. 如果您選擇#bhit#k, 請記住,超過21點將導致您#r爆掉#k, 或將游戲輸給發牌人。如果您認為自己已經接近21,並且希望#bstay#k, 您將和發牌者將比較大小,最接近21者勝!"
    );
  } else if (status == 4) {
    cm.sendPrev("請記住,如果輸了,您給發牌者的所有#b籌碼#k都會輸掉!祝好運!");
  } else if (status == 5) {
    cm.dispose();
  }
}

function PlayGame(s) {
  if (status == 1) {
    if (debug) {
      System.out.println("PlayGame() is progressing...");
    }
    if (cm.getPlayer().getBlackJackChips() == 0) {
      cm.getPlayer().gainBlackJackChips(5);
    }
    cm.sendGetNumber(
      "您目前有 #b" +
        cm.getPlayer().getBlackJackChips() +
        " BlackJack 籌碼#k. 多少 #b籌碼#k 你想在本輪投注?",
      1,
      1,
      cm.getPlayer().getBlackJackChips()
    );
  } else if (status == 2) {
    if (s > 0 && s <= cm.getPlayer().getBlackJackChips()) {
      // packet edits
      bet = s;
      if (debug) {
        System.out.println("Deck Initializing...");
      }
      InitializeGame();
      var totP = GetTotalCardValue("Player"),
        totD = GetTotalCardValue("Dealer");
      if (
        totD == 21 &&
        CardsInDealerHand[0].length == 2 &&
        totP == 21 &&
        CardsInPlayerHand[0].length == 2
      ) {
        Finish("Tie");
      } else if (totP == 21 && CardsInPlayerHand[0].length == 2) {
        Finish("Player21");
      } else if (totD == 21 && CardsInDealerHand[0].length == 2) {
        Finish("Dealer21");
      } else {
        DisplayHandWithProgression();
      }
    }
  } else if (status > 2) {
    playingAction[s == 0 ? "Hit" : "Stay"] = true;
    var totP, totD;
    if (playingAction["Stay"]) {
      if (debug) {
        System.out.println("玩家選擇 Stay.");
      }
      if (totD < 16) {
        CompleteDealer();
        if (debug) {
          System.out.println(
            "當玩家選擇Stay時,發牌者低於16,發牌者成功完成。"
          );
        }
      }
      (totP = GetTotalCardValue("Player")),
        (totD = GetTotalCardValue("Dealer"));
      if (totP > 21 && totD > 21) {
        Finish("DoubleBust");
      } else if (totP > 21) {
        Finish("PlayerBust");
      } else if (totD > 21) {
        Finish("DealerBust");
      } else if (totP == totD) {
        Finish("Tie");
      } else if (totP == 21 && CardsInPlayerHand[0].length == 2) {
        Finish("Player21");
      } else if (totD == 21 && CardsInDealerHand[0].length == 2) {
        Finish("Dealer21");
      } else if (totP > totD) {
        Finish("Win");
      } else if (totP < totD) {
        Finish("Lose");
      }
    } else if (playingAction["Hit"]) {
      AddCardToHand();
      (totP = GetTotalCardValue("Player")),
        (totD = GetTotalCardValue("Dealer"));
      if (
        totD == 21 &&
        CardsInDealerHand[0].length == 2 &&
        totP == 21 &&
        CardsInPlayerHand[0].length == 2
      ) {
        Finish("Tie");
      } else if (totP == 21 && CardsInPlayerHand[0].length == 2) {
        Finish("Player21");
      } else if (totD == 21 && CardsInDealerHand[0].length == 2) {
        Finish("Dealer21");
      } else if (totP > 21) {
        Finish("PlayerBust");
      } else if (totD > 21) {
        Finish("DealerBust");
      } else {
        DisplayHandWithProgression();
      }
    }
  }
}

function InitializeGame() {
  var cardNames = [
    "One",
    "Two",
    "Three",
    "Four",
    "Five",
    "Six",
    "Seven",
    "Eight",
    "Nine",
    "Ten",
    "Jack",
    "Queen",
    "King",
    "Ace",
  ];
  var cardSuits = ["Hearts", "Spades", "Diamonds", "Clubs"];
  for (var i = 0, a = 0; a < cardNames.length; i++) {
    if (i == 4) {
      a++;
      i = 0;
    }
    CardsInDeck[0].push(cardNames[a]);
    CardsInDeck[1].push(cardSuits[i]);
  }
  CardsInDeck[0].splice(-1);
  CardsInDeck[1].splice(-1);
  if (debug) {
    for (var i = 0; i < CardsInDeck[0].length; i++) {
      System.out.println(
        CardsInDeck[0][i] + " of " + CardsInDeck[1][i] + " pushed."
      );
    }
  }
  AddCardToHand();
  AddCardToHand();
}

function AddCardToHand() {
  var random;
  random = Math.floor(Math.random() * CardsInDeck[0].length);
  CardsInPlayerHand[0].push(CardsInDeck[0][random]);
  CardsInPlayerHand[1].push(CardsInDeck[1][random]);
  if (debug) {
    System.out.println(
      "Card Added to Player Hand: " +
        CardsInDeck[0][random] +
        " of " +
        CardsInDeck[1][random]
    );
  }
  for (var i = 0; i < CardsInDeck[0].length; i++) {
    if (
      CardsInPlayerHand[0][CardsInPlayerHand[0].length - 1] ==
        CardsInDeck[0][i] &&
      CardsInPlayerHand[1][CardsInPlayerHand[1].length - 1] == CardsInDeck[1][i]
    ) {
      if (debug) {
        System.out.println(
          "Card Spliced from Deck: " +
            CardsInDeck[0][i] +
            " of " +
            CardsInDeck[1][i]
        );
      }
      CardsInDeck[0].splice(i, 1);
      CardsInDeck[1].splice(i, 1);
      break;
    }
  }
  if (GetTotalCardValue("Dealer") < 16) {
    random = Math.floor(Math.random() * CardsInDeck[0].length);
    CardsInDealerHand[0].push(CardsInDeck[0][random]);
    CardsInDealerHand[1].push(CardsInDeck[1][random]);
    if (debug) {
      System.out.println(
        "Card Added to Dealer Hand: " +
          CardsInDeck[0][random] +
          " of " +
          CardsInDeck[1][random]
      );
    }
    for (var i = 0; i < CardsInDeck[0].length; i++) {
      if (
        CardsInDealerHand[0][CardsInDealerHand[0].length - 1] ==
          CardsInDeck[0][i] &&
        CardsInDealerHand[1][CardsInDealerHand[1].length - 1] ==
          CardsInDeck[1][i]
      ) {
        if (debug) {
          System.out.println(
            "Card Spliced from Deck: " +
              CardsInDeck[0][i] +
              " of " +
              CardsInDeck[1][i]
          );
        }
        CardsInDeck[0].splice(i, 1);
        CardsInDeck[1].splice(i, 1);
        break;
      }
    }
  }
}

function CompleteDealer() {
  while (GetTotalCardValue("Dealer") < 16) {
    var random = Math.floor(Math.random() * CardsInDeck[0].length);
    CardsInDealerHand[0].push(CardsInDeck[0][random]);
    CardsInDealerHand[1].push(CardsInDeck[1][random]);
    if (debug) {
      System.out.println(
        "Card Added to Dealer Hand: " +
          CardsInDeck[0][random] +
          " of " +
          CardsInDeck[1][random]
      );
    }
    for (var i = 0; i < CardsInDeck[0].length; i++) {
      if (
        CardsInDealerHand[0][CardsInDealerHand[0].length - 1] ==
          CardsInDeck[0][i] &&
        CardsInDealerHand[1][CardsInDealerHand.length - 1] == CardsInDeck[1][i]
      ) {
        if (debug) {
          System.out.println(
            "Card Spliced from Deck: " +
              CardsInDeck[0][i] +
              " of " +
              CardsInDeck[1][i]
          );
        }
        CardsInDeck[0].splice(i, 1);
        CardsInDeck[1].splice(i, 1);
        break;
      }
    }
  }
}

function GetValueByName(name) {
  var nums = [
    [
      "One",
      "Two",
      "Three",
      "Four",
      "Five",
      "Six",
      "Seven",
      "Eight",
      "Nine",
      "Ten",
      "Jack",
      "Queen",
      "King",
      "Ace",
    ],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11],
  ];
  for (var i = 0; i < nums[0].length; i++) {
    if (name == nums[0][i]) {
      return nums[1][i];
    }
  }
}

function DisplayHandWithProgression() {
  var text = "#d#n你手中的牌:#k#e";
  if (CardsInPlayerHand[0].length > 1) {
    text +=
      "\r\n#e第一張牌:#n #d" +
      CardsInPlayerHand[0][0] +
      "#k of " +
      (CardsInPlayerHand[1][0].equals("Hearts") ||
      CardsInPlayerHand[1][0].equals("Diamonds")
        ? "#r" + CardsInPlayerHand[1][0] + "#k"
        : CardsInPlayerHand[1][0]) +
      "\r\n#e第二張牌:#n #d" +
      CardsInPlayerHand[0][1] +
      "#k of " +
      (CardsInPlayerHand[1][1].equals("Hearts") ||
      CardsInPlayerHand[1][1].equals("Diamonds")
        ? "#r" + CardsInPlayerHand[1][1] + "#k"
        : CardsInPlayerHand[1][1]);
  }
  if (CardsInPlayerHand[0].length > 2) {
    text +=
      "\r\n#e第三張牌:#n #d" +
      CardsInPlayerHand[0][2] +
      "#k of " +
      (CardsInPlayerHand[1][2].equals("Hearts") ||
      CardsInPlayerHand[1][2].equals("Diamonds")
        ? "#r" + CardsInPlayerHand[1][2] + "#k"
        : CardsInPlayerHand[1][2]);
  }
  if (CardsInPlayerHand[0].length > 3) {
    text +=
      "\r\n#e第四張牌:#n #d" +
      CardsInPlayerHand[0][3] +
      "#k of " +
      (CardsInPlayerHand[1][3].equals("Hearts") ||
      CardsInPlayerHand[1][3].equals("Diamonds")
        ? "#r" + CardsInPlayerHand[1][3] + "#k"
        : CardsInPlayerHand[1][3]);
  }
  if (CardsInPlayerHand[0].length > 4) {
    text +=
      "\r\n#e第五張牌:#n #d" +
      CardsInPlayerHand[0][4] +
      "#k of " +
      (CardsInPlayerHand[1][4].equals("Hearts") ||
      CardsInPlayerHand[1][4].equals("Diamonds")
        ? "#r" + CardsInPlayerHand[1][4] + "#k"
        : CardsInPlayerHand[1][4]);
  }
  text += "\r\n\r\n#e總額: #b" + GetTotalCardValue("Player");
  if (CardsInPlayerHand[0].length > 4) {
    text += "\r\n看來您必須#bstay#k看看結果如何。#b \r\n\t#L1#Stay#l";
  } else {
    text += "\r\n你想選擇什麼?#b \r\n\t#L0#Hit#l \r\n\t#L1#Stay#l";
  }
  cm.sendSimple(text);
}

function GetTotalCardValue(name) {
  var sum = 0;
  if (name.equals("Player")) {
    for (var i = 0; i < CardsInPlayerHand[0].length; i++) {
      sum += GetValueByName(CardsInPlayerHand[0][i]);
    }
  } else if (name.equals("Dealer")) {
    for (var i = 0; i < CardsInDealerHand[0].length; i++) {
      sum += GetValueByName(CardsInDealerHand[0][i]);
    }
  }
  return sum;
}

function Finish(action) {
  if (action.equals("DoubleBust")) {
    cm.sendOk(
      "看起來我們倆都#r爆#k了! \r\n#b你手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Player") +
        " \r\n#r我手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Dealer") +
        " \r\n我想沒人勝利."
    );
  } else if (action.equals("PlayerBust")) {
    cm.sendOk(
      "哈哈,你看起來 #r爆#k了! \r\n#b你手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Player") +
        " \r\n#r我手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Dealer") +
        " \r\n抱歉,但是我好像要把你的 #b籌碼#k拿走!"
    );
    cm.getPlayer().gainBlackJackChips(-bet);
  } else if (action.equals("DealerBust")) {
    cm.sendOk(
      "我不能相信! 我 #r爆#k了! \r\n#b你手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Player") +
        " \r\n#r我手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Dealer") +
        " \r\n看來您已經贏得了一些#b籌碼#k!"
    );
    cm.getPlayer().gainBlackJackChips(bet);
  } else if (action.equals("Tie")) {
    cm.sendOk(
      "We tied! \r\n#b你手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Player") +
        "\r\n#r我手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Dealer") +
        " \r\n看來我們倆都沒贏..."
    );
  } else if (action.equals("Player21")) {
    cm.sendOk(
      "Wow! 你有 #rBlackJack#k! 這代表你用兩張牌得到 #r21 #k! \r\n#b你手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Player") +
        " \r\n你贏得了 #b2.5x 的籌碼#k"
    );
    cm.getPlayer().gainBlackJackChips(
      bet + (bet % 2 != 0 ? (bet + 1) / 2 : bet / 2)
    );
  } else if (action.equals("Dealer21")) {
    cm.sendOk(
      "#rBLACKJACK!#k 哈哈,看來我以兩張牌贏了#r21#k! \r\n#r我手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Dealer") +
        " \r\n抱歉,看來我必須帶走您的#b籌碼#k! 下次好運!"
    );
    cm.getPlayer().gainBlackJackChips(-bet);
  } else if (action.equals("Win")) {
    cm.sendOk(
      "我不敢相信,你贏了! \r\n#b你手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Player") +
        "\r\n#r我手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Dealer") +
        " \r\n看來您贏了一些 #b籌碼#k!"
    );
    cm.getPlayer().gainBlackJackChips(bet);
  } else if (action.equals("Lose")) {
    cm.sendOk(
      "哈哈,看起來你輸了! \r\n#b你手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Player") +
        "\r\n#r我手中的牌:#k \r\n\t" +
        DisplayCardsInHand("Dealer") +
        " \r\n抱歉,看來我必須帶走您的#b籌碼#k! 下次好運!"
    );
    cm.getPlayer().gainBlackJackChips(-bet);
  }
  if (debug) {
    System.out.println("Finish() carried out with a " + action);
  }
  cm.dispose();
}

function DisplayCardsInHand(name) {
  var cards = "";
  if (name.equals("Player")) {
    for (var i = 0; i < CardsInPlayerHand[0].length; i++) {
      cards +=
        "#d" +
        CardsInPlayerHand[0][i] +
        "#k of " +
        (CardsInPlayerHand[1][i].equals("Hearts") ||
        CardsInPlayerHand[1][i].equals("Diamonds")
          ? "#r" + CardsInPlayerHand[1][i] + "#k"
          : CardsInPlayerHand[1][i]) +
        (i != CardsInPlayerHand[0].length - 1 ? ", " : ".");
    }
  } else if (name.equals("Dealer")) {
    for (var i = 0; i < CardsInDealerHand[0].length; i++) {
      cards +=
        "#d" +
        CardsInDealerHand[0][i] +
        "#k of " +
        (CardsInDealerHand[1][i].equals("Hearts") ||
        CardsInDealerHand[1][i].equals("Diamonds")
          ? "#r" + CardsInDealerHand[1][i] + "#k"
          : CardsInDealerHand[1][i]) +
        (i != CardsInDealerHand[0].length - 1 ? ", " : ".");
    }
  }
  return cards;
}

function SpendChips(s) {
  if (status == 1) {
    if (debug) {
      System.out.println("SpendChips() progressing...");
    }
    var text =
      "你目前有 #b" +
      cm.getPlayer().getBlackJackChips() +
      " Black Jack 籌碼#k. 你想要換取什麼?";
    for (
      var i = 0;
      i < prizes[0].length;
      text +=
        "\r\n#L" +
        i +
        "##i" +
        prizes[0][i] +
        "##b ~ #t" +
        prizes[0][i] +
        "##k for #r" +
        prizes[1][i] +
        " Black Jack 籌碼#k#l",
        i++
    );
    cm.sendSimple(text);
  } else if (status == 2) {
    selected = s;
    cm.sendYesNo(
      "您確定要購買 #i" +
        prizes[0][s] +
        "##b ~ #t" +
        prizes[0][s] +
        "##k 以 #r" +
        prizes[1][s] +
        " Black Jack 籌碼#k?"
    );
  } else if (status == 3) {
    if (cm.getPlayer().getBlackJackChips() >= prizes[1][selected]) {
      cm.gainItem(prizes[0][selected]);
      cm.getPlayer().gainBlackJackChips(-prizes[1][selected]);
      cm.sendOk("感謝參與!享受您的獎品,然後再來!");
      cm.dispose();
    } else {
      cm.sendOk(
        "你還需要 #r" +
          (prizes[1][selected] - cm.getPlayer().getBlackJackChips()) +
          "  Black Jack 籌碼#k 才能兌換這個獎品!"
      );
      cm.dispose();
    }
    cm.dispose();
  }
}

GEMZ

來源:Emilyx3

[楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
/**
 * Gems Game NPC
 * Author: Emilyx3 of UMaple
 */

var Board;
var Score = 0;
var GameType = -1, BoardSize = -1, NumCols = -1, NumRows = -1,
NumGemTypes = -1, Difficulty = -1, NextComboMultiplier = 0,
GameTime = 180000 /*3 Minutes in Milliseconds*/, GameStart = -1;
var GameTypes = [
["普通模式", "玩一場常規遊戲"],
["時間攻擊模式", "3分鐘內盡可能獲取多的積分"]
//, ["Puzzle", "Solve puzzles"] // May add Puzzle mode later.
];
var BoardSizes = [
//Board Size Name, Number of Rows, Number of Cols
["Small", 6, 5],
["Medium", 7, 6],
["Large", 8, 7],
["Huge", 10, 7]
];
var Difficulties = [
//Difficulty Name, Number of Gem Types
["Very Easy", 4],
["Easy", 5],
["Medium", 6],
["Hard", 7]
];
var GameDropProbability = 0.01 / 100;
var GameDrops = [
//ItemID, Drop Probability (Should add Up to 1)
[5200000, 0.25],[5200001, 0.20],[5200002, 0.15],
[4031701, 0.05],[4001013, 0.10],[2340000, 0.25]
];
var Gems = [
//Ore, Gem
[4020000, 4021000], [4020001, 4021001], [4020002, 4021002], [4020003, 4021003],
[4020004, 4021004], [4020005, 4021005], [4020006, 4021006], [4020007, 4021007],
[4020008, 4021008]
];
var Neighbors4 = [[0,1],[0,-1],[-1,0],[1,0]];
var Neighbors8 = [[-1, -1],[-1, 0],[-1, 1],[0, -1],[0, 1],[1, -1],[1, 0], [1, 1]];
var status;

function Header() {
    return "#b============#kGEMZ#b============#k\r\n\r\n";
}
function MenuScreen() {
    return Header() +
    "請選擇:\r\n\r\n" +
    "#L0#開始遊戲#l\r\n" +
    "#L1#遊戲介紹#l";
}
function SendGameInstructions() {
    cm.sendNext(Header() +
        "#e遊戲介紹:#n\r\n" +
        "1. 通過單擊3塊連線或更多塊連線的礦石/寶石以獲得分數。\r\n" +
        "2. 您每破壞一組連線可獲得5點,如果消除高於3塊的連線可以+5%的分數。\r\n" +
        "3. 當您破壞礦石有機會(1次)將變成一個寶石。" +
        "如果寶石連線可得到30分。您也可以直接單擊寶石,但只能獲得20分。\r\n" +
        "4. 過程中可能出現特殊道具。它們有各自的功用:\r\n" +
        "    #v5021013# - 銷毀周圍的8格礦石/寶石(不影響特殊物品)。\r\n" +
        "    #v4031051# - 將周圍的8格變成寶石。\r\n" +
        "    #v4031311# - 將計時器增加2秒。(僅時間攻擊模式)\r\n" +
        "    #v3990021##v3990023# - 紅色(水平銷毀所有礦石/寶石),綠色(水平銷毀所有礦石為寶石)\r\n" +
        "    #v3991008##v3991034# - 紅色(垂直銷毀所有礦石/寶石),綠色(垂直銷毀所有礦石)\r\n" +
        "    #v3990020# - 垂直和水平存儲所有礦石/寶石\r\n" +
        "    每個被銷毀的礦石價值10分。被摧毀的寶石各值25分。特殊物品和掉落物品不會被銷毀。\r\n" +
        "5. 過程中可能會出現獎勵道具。單擊它們可以獲取它們:\r\n" +
        "    #v5200000##v5200001##v5200002##v4031701##v4001013##v2340000#\r\n" +
        "\r\nGood Luck ~"
        );
}
function SendGameTypeSelectionMenu() {
    var SendStr = Header();
    for (var i = 0;i < GameTypes.length;i++) {
        SendStr += "#L"+i+"#"+GameTypes[i][0]+" - "+GameTypes[i][1]+"#l\r\n";
    }
    cm.sendSimple(SendStr);
}
function SendBoardSizeSelectionMenu() {
    var SendStr = Header();
    for (var i = 0;i < BoardSizes.length;i++) {
        SendStr += "#L"+i+"#"+BoardSizes[i][0]+" - "+BoardSizes[i][1]+"x"+BoardSizes[i][2]+"#l\r\n";
    }
    cm.sendSimple(SendStr);
}
function SendDifficultySelectionMenu() {
    var SendStr = Header();
    for (var i = 0;i < Difficulties.length;i++) {
        SendStr += "#L"+i+"#"+Difficulties[i][0]+" - "+Difficulties[i][1]+" Gem Types#l\r\n";
    }
    cm.sendSimple(SendStr);
}
function InitializeBoard() {
    NumRows = BoardSizes[BoardSize][1];
    NumCols = BoardSizes[BoardSize][2];
    Board = new Array(NumRows);
    for (var i = 0;i < Board.length;i++) {
        Board[i] = new Array(NumCols);
    }
}
function InitializeGems() {
    NumGemTypes = Difficulties[Difficulty][1];
    Shuffle(Gems);
    for (var r = 0;r < Board.length;r++) {
        for (var c = 0;c < Board[r].length;c++) {
            Board[r][c] = GetRandomOre();
        }
    }
}
function GetRandomOre() {
    return Gems[Math.floor(Math.random() * NumGemTypes)][0];
}
function GetScoreString() {
    return "#b#e遊戲分數:#n#k " + AddCommas(Score.toFixed(2)) + "\r\n";
}
function GetGameSettingsDescriptionString() {
    return "#d#e遊戲設定:#n#k " + GameTypes[GameType][0]
    + " - 大小: " + BoardSizes[BoardSize][0] + "("+ BoardSizes[BoardSize][1]+"x" + BoardSizes[BoardSize][2]
    + ") - 難易度: " + Difficulties[Difficulty][0] + "\r\n";
}
function AddCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
function GetGameEndBoardString() {
    var SendStr = "";
    for (var r = 0;r < Board.length;r++) {
        for (var c = 0;c < Board[r].length;c++) {
            SendStr += "#v"+Board[r][c]+"#";
        }
        SendStr += "\r\n";
    }
    SendStr += "\r\n";
    return SendStr;
}
function GetBoardSelectionString() {
    var SendStr = "";
    var Count = 0;
    for (var r = 0;r < Board.length;r++) {
        for (var c = 0;c < Board[r].length;c++) {
            SendStr += "#L"+(Count++)+"##v"+Board[r][c]+"##l";
        }
        SendStr += "\r\n";
    }
    SendStr += "\r\n";
    return SendStr;
}
function ContainsGem(Combo, GemGroup) {
    for (var i = 0;i < Combo.length;i++) {
        if (Board[Combo[i][0]][Combo[i][1]] == GemGroup[1]) {
            return true;
        }
    }
    return false;
}
function DoMove(Move) {
    var ItemID = Board[Move[0]][Move[1]];
    var GemGroup = GetGemGroup(ItemID);
    var Combo = new Array();
    var IsSpecialItem = false;
    Combo.push([Move[0],Move[1]]);
    if (GemGroup != null) {
        //Normal Gem
        GetNeighborGems(Move[0],Move[1],GemGroup,Combo);
        if (Combo.length < 3) {
            if (!ContainsGem(Combo, GemGroup)) {
                return false;
            }
        }
        AddScoreForCombination(Combo, GemGroup);
    } else if (IsDropItem(ItemID)) {
        cm.gainItem(ItemID, 1);
    } else {
        //Special Item
        UseSpecialItem(ItemID, Move[0], Move[1], Combo);
        IsSpecialItem = true;
    }
    RemoveAndGenerateNewGems(Combo, Move[0], Move[1], GemGroup, IsSpecialItem);
    return true;
}

function RemoveGemAndShiftDown(r, c) {
    for (var i = r;i > 0;i--) {
        Board[i][c] = Board[i-1][c];
    }
    return [0, c];
}
function RemoveAndGenerateNewGems(Combo, r, c, GemGroup, IsSpecialItem) {
    var DropItemProb = (IsSpecialItem ? 0 : GameDropProbability + ((Combo.length > 5) ? (GameDropProbability / 10) * (Combo.length - 5) : 0));
    var SpecialItemProb = (IsSpecialItem ? 0 : 0.005 + (Combo.length > 3 ? 0.01 * (Combo.length - 3) : 0.0) + DropItemProb);
    for (var i = 0;i < Combo.length;i++) {
        var TurnToGemProb = 0.20 + ((Combo.length > 3) ? 0.2 * (Combo.length - 3) : 0.0);
        if (Combo[i][0] == r && Combo[i][1] == c && GemGroup != null
            && GemGroup[0] == Board[r][c] && Math.random() < TurnToGemProb) {
            Board[r][c] = GemGroup[1];
        } else {
            var NewGemLocation = RemoveGemAndShiftDown(Combo[i][0],Combo[i][1]);
            for (var j = i + 1; j < Combo.length;j++) {
                if (Combo[j][1] == Combo[i][1] && Combo[j][0] < Combo[i][0]) {
                    Combo[j][0]++;
                }
            }
            if (c == Combo[i][1] && r < Combo[i][0]) {
                r++;
            }
            var Ret = RandomOreGemDrop(NewGemLocation[0], NewGemLocation[1], DropItemProb, SpecialItemProb);
            //Only allow 1 item drop and 1 Special Item drop per combo.
            if (Ret == 1) {
                SpecialItemProb = 0;
            } else if (Ret == 2) {
                DropItemProb = 0; 
            }
        }
    }
}
function UseSpecialItem(ItemID, r, c, ToBeRemoved) {
    Score += 10;
    var Targets = new Array();
    switch (ItemID) {
        case 5021013:
        case 4031051:
            for (var i = 0;i < Neighbors8.length;i++) {
                var NewR = r + Neighbors8[i][0];
                var NewC = c + Neighbors8[i][1];
                Targets.push([NewR, NewC]);
            }
            break;
        case 3990021:
        case 3990023:
            for (i = 0;i < Board[r].length;i++) {
                if (i != c) {
                    Targets.push([r, i]);
                }
            }
            break;
        case 3991008:
        case 3991034:
            for (i = 0;i < Board.length;i++) {
                if (i != r) {
                    Targets.push([i, c]);
                }
            }
            break;
        case 3990020:
            for (i = 0;i < Board[r].length;i++) {
                if (i != c) {
                    Targets.push([r, i]);
                }
            }
            for (i = 0;i < Board.length;i++) {
                if (i != r) {
                    Targets.push([i, c]);
                }
            }
            break;
    }
    if (ItemID == 4031311) {
        GameTime += 2000;
    }
    switch (ItemID) {
        case 4031051:
        case 3990023:
        case 3991034:
            for (i = 0;i < Targets.length;i++) {
                if (ValidPosition(Targets[i][0], Targets[i][1])) {
                    var GemGroup = GetGemGroup(Board[Targets[i][0]][Targets[i][1]]);
                    if (GemGroup != null) {
                        if (GemGroup[0] == Board[Targets[i][0]][Targets[i][1]]) {
                            Board[Targets[i][0]][Targets[i][1]] = GemGroup[1];
                        }
                    }
                }
            }
            break;
        case 5021013:
        case 3990021:
        case 3991008:
        case 3990020:
            for (i = 0;i < Targets.length;i++) {
                if (ValidPosition(Targets[i][0], Targets[i][1])) {
                    GemGroup = GetGemGroup(Board[Targets[i][0]][Targets[i][1]]);
                    if (GemGroup != null) {
                        if (GemGroup[0] == Board[Targets[i][0]][Targets[i][1]]) {
                            Score += 10;
                        } else if (GemGroup[1] == Board[Targets[i][0]][Targets[i][1]]) {
                            Score += 25;
                        }
                        ToBeRemoved.push([Targets[i][0], Targets[i][1]]);
                    }
                }
            }
            break;
    }
}
function IsDropItem(ItemID) {
    var Len = GameDrops.length;
    while (Len--) {
        if (GameDrops[Len][0] == ItemID) {
            return true;
        }
    }
    return false;
}

function IsOreOrGem(ItemID) {
    for (var i = 0;i < Gems.length;i++) {
        for (var j = 0;j < Gems[i].length;i++) {
            if (Gems[i][j] == ItemID)
                return true;
        }
    }
    return false;
}

function GetDropItem() {
    var R = Math.random();
    var tot = 0;
    for (var i = 0;i < GameDrops.length;i++) {
        tot += GameDrops[i][1];
        if (R < tot) {
            return GameDrops[i][0];
        }
    }
    return 5200000; //Just in case.
}
function RandomOreGemDrop(r, c, DropItemProb, SpecialItemProb) {
    var R = Math.random();
    if (R < DropItemProb) {
        //Generate a Drop
        Board[r][c] = GetDropItem();
        return 2;
    } else if (R < SpecialItemProb) {
        //Generate Special Item
        Board[r][c] = GetRandomSpecialItem();
        return 1;
    } else {
        //Normal Ore
        Board[r][c] = GetRandomOre();
        return 0;
    }
}

/*
#v5021013# - Destroy surrounding 8 Ores/Gems (Does not affect special items). (25 points / destroyed item)\r\n" +
#v4031051# - Turns the surrounding 8 Ores into Gems.\r\n" +
#v4031311# - Add 2 seconds to timer. (Only Time Attack mode)\r\n" +
#v3990021##v3990023# - Red(Destroy all Ores/Gems Horizontally - 25 points / destroyed item), Green(Turn all Ores into Gems Horizontally)\r\n" +
#v3991008##v3991034# - Red(Destroy all Ores/Gems Vertically - 25 points / destroyed item), Green(Turn all Ores into Gems Vertically)\r\n" +
#v3990020# - Destory all Ores/Gems Vertically and Horizontally (25 points / destroyed item)\r\n" +
 */
function GetRandomSpecialItem() {
    var Prob = Math.random();
    if (GameType == 1 && Math.random() < 0.25) {
        return 4031311;
    }
    if (Prob < 0.10) {
        return 3990020;
    } else if (Prob < 0.25) {
        return 3991008;
    } else if (Prob < 0.35) {
        return 3991034;
    } else if (Prob < 0.50) {
        return 3990021;
    } else if (Prob < 0.60) {
        return 3990023;
    } else if (Prob < 0.70) {
        return 4031051;
    } else {
        return 5021013;
    }
}
function AddScoreForCombination(Combo, GemGroup) {
    var Multiplier = 1.0 + (Combo.length > 3 ? 0 : 0.05 * (Combo.length - 3))
    + NextComboMultiplier;
    if (Combo.length == 3) {
        NextComboMultiplier = 0;
    } else if (Combo.length == 4) {
        NextComboMultiplier += 0.01;
    } else if (Combo.length == 5) {
        NextComboMultiplier += 0.015;
    } else if (Combo.length > 5) {
        NextComboMultiplier += 0.02;
    }
    var Points = 0;
    for (var i = 0;i < Combo.length;i++) {
        var ItemID = Board[Combo[i][0]][Combo[i][1]];
        if (GemGroup[0] == ItemID) {
            Points += 5;
        } else if (GemGroup[1] == ItemID) {
            if (Combo.length >= 3) {
                Points += 30;
            } else {
                Points += 20;
            }
        }
    }
    Score += (Points * Multiplier);
}
function NoMoreMoves() {
    for (var i = 0;i < Board.length;i++) {
        for (var j = 0;j < Board[i].length;j++) {
            var GemGroup = GetGemGroup(Board[i][j]);
            if (GemGroup == null) { //Special Item
                return false;
            } else {
                var GetCount = new Array();
                GetCount.push([i,j]);
                GetNeighborGems(i, j, GemGroup, GetCount);
                if (GetCount.length > 2) {
                    return false;
                }
            }
        }
    }
    return true;
}
function GetNeighborGems(r, c, GemGroup, ToBeRemoved) {
    var CheckNeighbors = new Array();
    for (var i = 0;i < Neighbors4.length;i++) {
        var NewR = r + Neighbors4[i][0];
        var NewC = c + Neighbors4[i][1]
        if (ValidPosition(NewR, NewC) && Contains(Board[NewR][NewC], GemGroup)) {
            if (PushIfNotPresent([NewR, NewC], ToBeRemoved)) {
                CheckNeighbors.push([NewR, NewC]);
            }
        }
    }
    for (i = 0;i < CheckNeighbors.length;i++) {
        GetNeighborGems(CheckNeighbors[i][0], CheckNeighbors[i][1], GemGroup, ToBeRemoved);
    }
}
function ValidPosition(r, c) {
    return r >= 0 && r < NumRows && c >= 0 && c < NumCols;
}
function Contains(ItemID, GemGroup) {
    var i = GemGroup.length;
    while (i--) {
        if (GemGroup[i] == ItemID) {
            return true;
        }
    }
    return false;
}
function PushIfNotPresent(Move, Combo) {
    var i = Combo.length;
    while (i--) {
        var Comp = Combo[i];
        if (Comp[0] == Move[0] && Comp[1] == Move[1]) {
            return false;
        }
    }
    Combo.push(Move);
    return true;
}
function GetPositionFromSelection(sel) {
    return [(sel / NumCols) | 0, (sel % NumCols) | 0];
}
function GetGemGroup(ItemID) {
    for (var i = 0; i < Gems.length;i++) {
        for (var j = 0;j < Gems[i].length;j++) {
            if (Gems[i][j] == ItemID) {
                return Gems[i];
            }
        }
    }
    return null;
}
function Shuffle(arr) {
    var i = arr.length;
    if (i == 0)
        return;
    while (--i) {
        var j = Math.floor( Math.random() * ( i + 1 ) );
        var tempi = arr[i];
        var tempj = arr[j];
        arr[i] = tempj;
        arr[j] = tempi;
    }
}
function GetTimeLeftString() {
    var Ret = "Time Left: ";
    var Now = (new Date()).getTime();
    var Diff = GameTime + GameStart - Now;
    var MilliSec = (Diff % 1000) | 0;
    Diff /= 1000;
    var Min = (Diff / 60) | 0;
    var Sec = (Diff % 60) | 0;
    Ret += ((Min < 10) ? "0" + Min : Min) + ":" + ((Sec < 10) ? "0" + Sec : Sec)
    + "." + ((MilliSec < 100) ? "0" + ((MilliSec < 10) ? "0" + MilliSec : MilliSec) : MilliSec);
    Ret += " seconds\r\n";
    return Ret;
}
function GetGameTime(End) {
    var Diff = End - GameStart;
    var MilliSec = (Diff % 1000) | 0;
    Diff /= 1000;
    var Min = (Diff / 60) | 0;
    var Sec = (Diff % 60) | 0;
    return ((Min < 10) ? "0" + Min : Min) + ":" + ((Sec < 10) ? "0" + Sec : Sec)
    + "." + ((MilliSec < 100) ? "0" + ((MilliSec < 10) ? "0" + MilliSec : MilliSec) : MilliSec);

}
function start() {
    status = -1;
    action(1, 0, 0);
}
function CheckSelection(s, low, high) {
    return s < low && s > high;
}
function action(mode, type, selection) {
    if (mode == -1 || mode == 0)
        cm.dispose();
    else {

        if (mode == 1) {
            if (status != 6) {
                status++;
            }
        } else {
            status--;
        }

        if (status == 0) {
            cm.sendSimple(MenuScreen());
        } else if (status == 1) {
            if (selection == 0) {
                action(1, 0, 0); //Move to Next
            } else if (selection == 1) {
                SendGameInstructions();
            } else { //Packet Edits
                cm.dispose();
            }
        } else if (status == 2) {
            //Choose Game Type
            SendGameTypeSelectionMenu();
        } else if (status == 3) {
            if (CheckSelection(selection, 0, GameTypes.length - 1)) {
                cm.dispose();
                return;
            }
            GameType = selection;
            //Choose Board Size
            SendBoardSizeSelectionMenu();
        } else if (status == 4) {
            if (CheckSelection(selection, 0, BoardSizes.length - 1)) {
                cm.dispose();
                return;
            }
            BoardSize = selection;
            //Choose Difficulty
            SendDifficultySelectionMenu();
        } else if (status == 5) {
            if (CheckSelection(selection, 0, Difficulties.length - 1)) {
                cm.dispose();
                return;
            }
            Difficulty = selection;
            InitializeBoard();
            InitializeGems();
            //No Header while in game, it's just annoying
            cm.sendSimple(GetScoreString() + GetBoardSelectionString() + GetGameSettingsDescriptionString());
        } else if (status == 6) {
            if (CheckSelection(selection, 0, NumCols * NumRows - 1)) {
                cm.dispose();
                return;
            }
            if (GameStart == -1) {
                GameStart = (new Date()).getTime();
            }
            if (GameType == 1 && GameStart + GameTime < (new Date()).getTime()) {
                //Time Over
                status++;
                action(1, 0, 0);
                return;
            }
            var Move = GetPositionFromSelection(selection);
            var ValidMove = DoMove(Move);
            if (NoMoreMoves()) {
                status++;
                action(1, 0, 0);
            } else {
                cm.sendSimple(
                    (ValidMove ? "" : "無效的移動,您必須單擊一組3個或更多的礦石/寶石。\r\n")
                    + GetScoreString()+ (GameType == 1 ? GetTimeLeftString() : "")
                    + GetBoardSelectionString() + GetGameSettingsDescriptionString());
            }
        } else if (status == 8) {
            var SendStr = Header();
            SendStr += "#e#rGame Over#k#n\r\n";
            SendStr += GetScoreString();
            SendStr += GetGameSettingsDescriptionString();
            SendStr += "遊戲時間: " + GetGameTime((new Date()).getTime()) + "\r\n";
            SendStr += GetGameEndBoardString();
            cm.sendOk(SendStr);
        } else {
            cm.dispose();
        }
    }
}

OX遊戲

來源:XxОsirisxX

242KHBd [楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
status = -1;
playerFlag = 0;
computerFlag = 0;
var playerColor;
var computerColor;

function start() {
    cm.sendYesNo("嗨 #h #.\r\n想與無腦的電腦玩OX遊戲嗎?勝利的話可以贏得#b#t5220000##k1張");
}

function action (mode, type, selection) {
    if (mode != 1) {
        if (type == 1 && mode == 0) {
            cm.sendNext("膽小鬼。");
        }
        cm.dispose();
        return;
    }
    status++;
    if (status == 0) {
        cm.sendSimple("你想做?\r\n\r\n#b#L0#先手.\r\n#L1#後手.");
    } else if (status == 1) {
        playerColor = selection == 0 ? 3991040 : 3991049;
        computerColor = selection == 0 ? 3991023 : 3991014;
        if (selection == 1) {
            computerMoves();
        }
        play();
    } else {
        if ((playerFlag & (1 << selection)) == (1 << selection) || (computerFlag & (1 << selection)) == (1 << selection) || selection < 0 || selection > 9) {
            cm.sendNext("禁止作弊。");
            cm.dispose();
            return;
        }
        playerFlag |= 1 << selection;
        if (((playerFlag | computerFlag) ^ 511) == 0) {
            tie();
            return;
        }
        for (var i = 0; i < 3; ) {
            if (isWinner(playerFlag, i++)) {
                win();
                return;
            }
        }
        computerMoves();
        for (var i = 0; i < 3; ) {
            if (isWinner(computerFlag, i++)) {
                lose();
                return;
            }
        }
        play();
    }
}

function play() {
    cm.sendSimple(loadText(true));
}

function win() {
    if (cm.canHold(5220000)) {
        cm.gainItem(5220000, 1);
        cm.sendNext(loadText(false) + "\r\n\r\n哇!你做到了!恭喜你!您贏得了#b#t5220000##k.");
    } else {
        cm.sendNext("哇!你做到了!恭喜你!但很抱歉告訴您,我不能給您 #b#t5220000##k 因為您的背包似乎已滿。稍後再試。");
    }
    cm.dispose();
}

function isWinner(player, val) {
    return (player & (7 << (val * 3))) == 7 << (val * 3) || (player & (73 << val)) == (73 << val) || (((player & (1 << 4)) == 1 << 4) && (player & ((1 << 6) << (val * 2))) && ((player & (1 << 2) >> (val * 2))));
}

function lose() {
    cm.sendNext(loadText(false) + "\r\n\r\n您輕視了這台電腦。您應該再試一次,您可能只是累了。");
    cm.dispose();
}

function computerMoves() {
    if (playerFlag == 0) {
        var rand = Math.random() * 9 | 0;
        computerFlag |= 1 << (rand + 1 & 1 == 1 ? rand : 4);
    } else {
        if (computerFlag == 0) {
            for (var i = 0; i < 4; i++) {
                if (playerFlag & (2 << i) == 2 << i) {
                    computerFlag |= 1 << 4;
                    return;
                }
            }
            var rand = Math.random() * 9 | 0;
            if (playerFlag == 1 << 4) {
                computerFlag |= 1 << (rand + 1 & 1 == 1 ? rand : 0);
            } else {
                if ((playerFlag & 1) << (rand + 1 & 1 == 1 ? rand : 4) != 1 << (rand + 1 & 1 == 1 ? rand : 4)) {
                    computerFlag |= 1 << (rand + 1 & 1 == 1 ? rand : 4);
                } else {
                    computerFlag |= 1 << 4;
                }
            }
        } else {
            for (var i = 0; i < 9; i++) {
                if (((computerFlag | playerFlag) & (1 << i)) != 0) {
                    continue;
                }
                var nextMove = (computerFlag | (1 << i));
                for (var x = 0; x < 3; x++) {
                    if (isWinner(nextMove, x)) {
                        computerFlag |= 1 << i;
                        return;
                    }
                }
                var playerPossibleMove = (playerFlag | (1 << i));
                for (var x = 0; x < 3; x++) {
                    if (isWinner(playerPossibleMove, x)) {
                        computerFlag |= 1 << i;
                        return;
                    }
                }
            }
            for (var i = 0; i < 9; i++) {
                if (((computerFlag | playerFlag) & (1 << i)) != 0) {
                    continue;
                }
                computerFlag |= 1 << i;
                break;
            }
        }
    }
}

function loadText(sendSimple) {
    var text = (sendSimple ? "選擇你的位置." : "比賽結果.") + "\r\n\r\n";
    for (var i = 0; i < 9; i++) {
        text += ((playerFlag & (1 << i)) == (1 << i) ? "#i" + playerColor + "#" : (computerFlag & (1 << i)) == (1 << i) ? "#i" + computerColor + "#" : ((sendSimple ? "#L" + i + "#" : "") + "___")) + "\t\t" + (((i + 1) % 3 == 0 ? "\r\n" : ""));
    }
    return text;
}

function tie() {
    cm.sendNext(loadText(false) + "\r\n\r\n哦,看起來您與電腦平手了。你需要更加努力。");
    cm.dispose();
}

Mastermind

來源:Emilyx3

GZ7nKyN [楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
jBXDoTS [楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
/**
 * Mastermind NPC
 * Author: Emilyx3 of UMaple
 */
var status;
var CorrectCombo;
var CurrentCombo;
var PreviousGuesses = new Array();
var NumSpaces, NumColors;
var SpaceDiffSelected, PieceDiffSelected;
var Pieces = [4030002,4030003,4030004,4030005,4030006,4030007,4030008];
var SpaceDifficulties = [["Beginner(3)", 3], ["Easy(4)", 4], ["Intermediate(5)", 5], ["Hard(6)", 6], ["Expert(7)", 7]];
var PieceDifficulties = [["Beginner", 3], ["Easy", 4], ["Intermediate", 5], ["Hard", 6], ["Expert", 7]];
var MaxGuesses = 25;
//var CorrectPiece = 4031140, CorrectPieceAndSpace = 4005004;
var CorrectPiece = 4001007, CorrectPieceAndSpace = 4001008, Blank = 4031325, DeletePrevious =3991002;
var mount = Math.random()*50; //獎勵獲得的數量

function Header() {
    return "#d============#k Mastermind #d============#k\r\n\r\n";
}
function SendMainMenu() {
    cm.sendSimple(
        Header() +
        "#L0#開始遊戲#l\r\n" +
        "#L1#遊戲介紹#l"
    );
}
function SendGameInstructions() {
    cm.sendNext(Header() +
        "#b#e歡迎來到 game Mastermind.#k#n\r\n" +
        "#e遊戲介紹:#n\r\n" +
        "1)遊戲目標是猜測正確的方塊組合。\r\n" +
        "2)每次猜測之後,您將收到反饋,以了解如何正確推論。\r\n" +
        "    #v"+CorrectPieceAndSpace+"# - 表示#b正確#k零件在#b正確#k位置\r\n" +
        "    #v"+CorrectPiece+"# - 表示#b正確#k零件在#b不正確#k位置\r\n\r\n" +
        "    Example: " + GetFeedback([1,2]) + " - 1個正確零件在正確位置,以及2個正確零件在錯誤位置。\r\n" +
        "3)您擁有最多#r" + MaxGuesses + "#k次機會來嘗試獲得正確的組合。\r\n"
    );
}
function SendNumSpacesSelectionMenu() {
    var SendStr = Header();
    SendStr += "#e請選擇您要遊戲的猜測數:#n\r\n"
    for (var i = 0;i < SpaceDifficulties.length;i++) {
        SendStr += "#L" + i + "#" + SpaceDifficulties[i][0] + " - 猜測數:" + SpaceDifficulties[i][1] + "\r\n";
    }
    cm.sendSimple(SendStr);
}
function SendNumPiecesSelectionMenu() {
    var SendStr = Header();
    SendStr += "#e請選擇您想玩的零件數:#n\r\n"
    for (var i = 0;i < PieceDifficulties.length;i++) {
        SendStr += "#L" + i + "#" + PieceDifficulties[i][0] + " - 零件數:" + PieceDifficulties[i][1] + "\r\n";
    }
    cm.sendSimple(SendStr);
}
function GetGameSettingsDescription() {
    return "\r\n#d#e遊戲設定:#n#k\r\n" +
        SpaceDifficulties[SpaceDiffSelected][0] + " - 猜測數:" + SpaceDifficulties[SpaceDiffSelected][0] + "\r\n" +
        PieceDifficulties[PieceDiffSelected][0] + " - 零件數:" +  PieceDifficulties[PieceDiffSelected][1] + "\r\n";
}
function InitializeCorrectSequence() {
    Shuffle(Pieces);
    CorrectCombo = new Array(NumSpaces);
    for (var i = 0;i < CorrectCombo.length;i++) {
        CorrectCombo[i] = Math.floor(Math.random() * NumColors);
    }
}
function GetSequenceString(Sequence) {
    var Ret = "";
    for (var i = 0;i < Sequence.length;i++) {
        Ret += "#v" + Pieces[Sequence[i]] + "#";
    }
    return Ret;
}
function GetCurrentCombo() {
    var Ret = "你目前的猜測: ";
    for (var i = 0;i < CurrentCombo.length;i++) {
        Ret += "#v" + Pieces[CurrentCombo[i]] + "#";
    }
    for (var j = 0;j < NumSpaces - CurrentCombo.length;j++) {
        Ret += "#v" + Blank + "#";
    }
    return Ret + "\r\n";
}
function GetPieceSelectionString() {
    var Ret = "請選擇一個方塊:\r\n";
    for (var i = 0;i < NumColors;i++) {
        Ret += "#L" + i + "##v" + Pieces[i] + "##l ";
    }
    if (CurrentCombo.length > 0) {
        Ret += "#L" + NumColors + "##v" + DeletePrevious + "##l";
    }
    return Ret + "\r\n";

}
function CalculateMatch() {
    var Correct = new Array();
    for (var i = 0;i < NumSpaces;i++) {
        if (CorrectCombo[i] == CurrentCombo[i]) {
            Correct.push(i);
        }
    }
    var Ret1 = Correct.length;
    for (var j = 0;j < NumSpaces;j++) {
        if (!Contains(j, Correct)) {
            for (var k = 0;k < NumSpaces;k++) {
                if (!Contains(k, Correct)) {
                    if (CurrentCombo[j] == CorrectCombo[k]) {
                        Correct.push(k);
                        break;
                    }
                }
            }
        }
    }
    return [Ret1, Correct.length - Ret1];
}
function Contains(i, arr) {
    for (var j = 0;j < arr.length;j++) {
        if (arr[j] == i) {
            return true;
        }
    }
    return false;
}
function GetFeedback(CorrectNumbers) {
    var Ret = "";
    for (var i = 0;i < CorrectNumbers[0];i++) {
        Ret += "#v" + CorrectPieceAndSpace + "#";
    }
    for (var j = 0;j < CorrectNumbers[1];j++) {
        Ret += "#v" + CorrectPiece + "#";
    }
    return Ret;
}
function GetPreviousCombos() {
    if (PreviousGuesses.length == 0) {
        return "";
    }
    var Ret = "\r\n=== #e上一輪猜測#n ===\r\n";
    for (var i = PreviousGuesses.length - 1;i >= 0;i--) {
        var PreviousGuess = PreviousGuesses[i];
        Ret += ((i + 1) + ". ") + GetSequenceString(PreviousGuess[0]) + "   " + GetFeedback(PreviousGuess[1]) + "\r\n";
    }
    return Ret + "\r\n";
}
function Shuffle(arr) {
    var i = arr.length;
    if (i == 0)
        return;
    while (--i) {
        var j = Math.floor( Math.random() * ( i + 1 ) );
        var tempi = arr[i];
        var tempj = arr[j];
        arr[i] = tempj;
        arr[j] = tempi;
    }
}
function CheckSelection(s, low, high) {
    return s < low && s > high;
}
function start() {
    status = -1;
    action(1, 0, 0);
}
function action(mode, type, selection) {
    if (mode == -1 || (mode == 0 && (status != 5 || CurrentCombo == null || CurrentCombo.length != NumSpaces))) {
        cm.dispose();
        return;
    }
    if (status != 5)
        status++;

    if (status == 0) {
        SendMainMenu();
    } else if (status == 1) {
        if (selection == 0) {
            action(1, 0, 0);
        } else if (selection == 1) {
            SendGameInstructions();
        } else {
            cm.dispose();
        }
    } else if (status == 2) {
        SendNumSpacesSelectionMenu();
    } else if (status == 3) {
        if (CheckSelection(selection, 0, SpaceDifficulties.length - 1)) {
            cm.dispose();
            return;
        }
        SpaceDiffSelected = selection;
        NumSpaces = SpaceDifficulties[selection][1];
        SendNumPiecesSelectionMenu();
    } else if (status == 4) {
        if (CheckSelection(selection, 0, PieceDifficulties.length - 1)) {
            cm.dispose();
            return;
        }
        PieceDiffSelected = selection;
        NumColors = PieceDifficulties[selection][1];
        InitializeCorrectSequence();
        CurrentCombo = new Array();
        cm.sendSimple(Header() + GetCurrentCombo() + GetPieceSelectionString()
            + GetPreviousCombos() + GetGameSettingsDescription());
    } else if (status == 5) {
        if (CurrentCombo.length == NumSpaces) {
            if (mode == 0) { //No
                CurrentCombo.pop();
                cm.sendSimple(Header() + GetCurrentCombo() + GetPieceSelectionString()
                    + GetPreviousCombos() + GetGameSettingsDescription());
            } else if (mode == 1) { //Yes
                var Match = CalculateMatch();
                if (Match[0] == NumSpaces) {
                    //Win
                    cm.sendOk(Header() + "#b#e恭喜你,你只花了 "+(PreviousGuesses.length + 1)+" 次猜測就贏了!#n#k\r\n"
                        + GetSequenceString(CurrentCombo) + "    " + GetFeedback(Match) + "\r\n"
                        + GetPreviousCombos() + GetGameSettingsDescription());
                    cm.gainItem(4001126,mount); //贏的獎勵
                    cm.dispose();
                } else if (PreviousGuesses.length + 1 == MaxGuesses) {
                    //Lose
                    PreviousGuesses.push([CurrentCombo, Match]);
                    cm.sendOk(Header() + "#b#e抱歉,你輸了!您超過了最大猜測數 ("+MaxGuesses+")!#n#k\r\n"
                        + "正確的組合是:" +  GetSequenceString(CorrectCombo) + "\r\n"
                        + GetPreviousCombos() + GetGameSettingsDescription());
                    cm.dispose();
                } else {
                    //Continue
                    PreviousGuesses.push([CurrentCombo, Match]);
                    CurrentCombo = new Array();
                    cm.sendSimple(Header() +
                        "#r抱歉,序列不正確。#k\r\n"
                        + GetCurrentCombo() + GetPieceSelectionString()
                        + GetPreviousCombos() + GetGameSettingsDescription());
                }
            }
        } else {
            if (CheckSelection(selection, 0, (CurrentCombo.length > 0 ? NumColors : NumColors - 1))) {
                cm.dispose();
                return;
            }
            if (selection == NumColors) {
                CurrentCombo.pop();
            } else {
                CurrentCombo.push(selection);
            }
            if (CurrentCombo.length == NumSpaces) {
                cm.sendYesNo(Header() + "#r請確認您的選擇:#k\r\n"
                    + GetCurrentCombo() + GetPreviousCombos() + GetGameSettingsDescription()
                );
            } else {
                cm.sendSimple(Header() + GetCurrentCombo() + GetPieceSelectionString()
                    + GetPreviousCombos() + GetGameSettingsDescription());
            }
        }
    } else {
        cm.dispose();
    }
}

數字猜猜猜

[楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
zqE2ibk [楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
tscO2P4 [楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
6ojTdCd [楓之谷私服]小遊戲系列NPC腳本(2) - BJ、GEMS、OX遊戲、Mastermind、猜數字
/*數字猜猜猜

如果需要檢測背包容量功能,請在MapleCharacter加上

    public short getSpace(int type) {
        return getInventory(MapleInventoryType.getByType((byte) type)).getNumFreeSlot();
    }

    public boolean haveSpace(int type) {
        short slot = getInventory(MapleInventoryType.getByType((byte) type)).getNextFreeSlot();
        return (slot != -1);
    }

    public boolean haveSpaceForId(int itemid) {
        short slot = getInventory(GameConstants.getInventoryType(itemid)).getNextFreeSlot();
        return (slot != -1);
    }

PS.我的bosslog是getBossLog2和setBossLog2,看你自己的寫法,別照我的打。

*/

var a = 0;
var randomNumber = Array();
var n1;
var n2;
var n3;
var itemData = Array(
        1102275, 
        1042187, 
        1042174, 
        1042149, 
        1072437, 
        1072448, 
        1022005,
        1022006,
        1022007,
        1022009,
        1112414,
        1022097,
        1702256,
        1702257,
        1702258,
        1702259,
        1702260,
        1102040,
        1102041,
        1102042,
        1102043,
        1482073,
        1492073,
        1122075
        );
var pass = true;
var correct = 0
var NumberPosition = Array();


function start() {
    a = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
    } else {
        if (mode == 1)
            a++;
        else
            cm.dispose();
        if (a == -1) {
            cm.dispose();
        } else if (a == 0) {
            /* 沒有檢測背包容量功能這邊就不需要,有就可以把注釋消掉。
            for (var i = 1; i < 5; i++) {
                if (cm.getSpace(i) < 5) {
                    pass = false;
                }
            }*/
            if (cm.getBossLog2("數字猜猜猜") >= 5) {
                cm.sendOk("對不起,數字猜猜猜活動一天只能進行五次。");
                cm.dispose();
            } else {
                cm.sendSimple("萬眾期待的HOTTIME時間又到了!親愛的冒險家,請問你要做什麼呢? \r\n#b#L0# 我要玩數字猜猜猜活動! \r\n#L1# 介紹一下此活動。");
            }
        } else if (a == 1) {
            if (selection == 0) {//我要玩數字猜猜猜活動!
                if (pass) {
                    cm.sendNextS("系統將隨機產生10個數字,請您做好準備記錄下這10個數字。 \r\n#r -- 點擊下一步開始產生。 \r\n #r-- 如果切斷對話,參加活動所需的物品不歸還。", 3);
                } else {
                    cm.sendOk("請讓你的所有背包欄空出5個格子。");
                    cm.dispose();
                }
            } else if (selection == 1) {//介紹一下此活動。
                a = -1;
                cm.sendNext("#e數字猜猜猜活動遊戲規則:#n#d\r\n\r\n1)系統會隨機給出10個數字,並且公示。 \r\n2)這10個數字將被打亂,且隱藏。 \r\n3)玩家會被隨機提問第N個數字是什麼\r\n如果回答正確即可得到獎勵! \r\n4)玩家一共有3次提問的機會。 #e\r\n\r\n5)獎池中有隨機5個道具 \r\n - 回答正確1次,隨機從裡面得到1個道具。 \r\n - 回答正確2次,隨機從裡面得到3個道具。 \r\n - 回答正確3次,獎池內所有道具都帶走! #n\r\n\r\n 參加活動的時候切記您的所有背包空格都有5格以上的空間。");
            }//selection
        } else if (a == 2) {
            var temp;
            var i = 0;
            while (i < 10) {
                temp = Math.floor(Math.random() * 40);
                if (checkid(temp)) {
                    randomNumber.push(temp);//隨機0~39
                    i++;
                }
            }
            var text = "這10個隨機數字依次為:\r\n #r- 請拿起您的筆記下這隨機的數字!\r\n\r\n#d";
            for (var i = 0; i < randomNumber.length; i++) {
                text += "第" + (i + 1) + "個數字為: - " + randomNumber[i] + "\r\n";
            }
            cm.sendNextS(text, 3);
        } else if (a == 3) {
            cm.sendNextS("正在打亂這10個數字,請點擊下一步……。", 3)
        } else if (a == 4) {
            randomNumber.sort(function() {
                return 0.5 - Math.random();
            })//隨機打亂
            var temp = Math.floor(Math.random() * 10) + 1;
            var i = 0;
            while (i < 3) {
                temp = Math.floor(Math.random() * 10) + 1;
                //cm.sendY(temp)
                if (checkNumberPosition(temp)) {
                    NumberPosition.push(temp);//隨機1~10
                    i++;
                }
            }//隨機位數的數字猜
            cm.sendGetNumber("現在請您輸入第" + NumberPosition[0] + "個數字:\r\n #r-- 如果切斷對話,參加活動所需的物品不歸還。\r\n", 0, 0, 999);
        } else if (a == 5) {
            n1 = selection; //記錄玩家第一次輸入
            cm.sendGetNumber("現在請您輸入第" + NumberPosition[1] + "個數字:\r\n #r-- 如果切斷對話,參加活動所需的物品不歸還。\r\n", 0, 0, 999);
        } else if (a == 6) {
            n2 = selection;
            cm.sendGetNumber("現在請您輸入第" + NumberPosition[2] + "個數字:\r\n #r-- 如果切斷對話,參加活動所需的物品不歸還。\r\n", 0, 0, 999);
        } else if (a == 7) {
            n3 = selection;
            cm.sendNextS("你所輸入的數字為:\r\n\r\n 第" + NumberPosition[0] + "個數字 -- " + n1 + "\r\n 第" + NumberPosition[1] + "個數字 -- " + n2 + "\r\n 第" + NumberPosition[2] + "個數字 -- " + n3 + ".", 3);
        } else if (a == 8) {
            var text = "現在我來公佈結果:\r\n 經過打亂後的10個數字為:\r\n\r\n#b";
            for (var i = 0; i < randomNumber.length; i++) {
                if (i == (NumberPosition[0] - 1)) {
                    text += "第" + (i + 1) + "個數字為: - " + randomNumber[i] + " #r( 您的答案為:" + n1 + ")#b\r\n";
                } else if (i == (NumberPosition[1] - 1)) {
                    text += "第" + (i + 1) + "個數字為: - " + randomNumber[i] + " #r( 您的答案為:" + n2 + ")#b\r\n";
                } else if (i == (NumberPosition[2] - 1)) {
                    text += "第" + (i + 1) + "個數字為: - " + randomNumber[i] + " #r( 您的答案為:" + n3 + ")#b\r\n";
                } else {
                    text += "第" + (i + 1) + "個數字為: - " + randomNumber[i] + "\r\n";
                }
            }
            cm.sendNextS(text, 3);
        } else if (a == 9) {//判斷是否答對部分
            if (randomNumber[NumberPosition[0] - 1] == n1) {
                correct += 1;
            }
            if (randomNumber[NumberPosition[1] - 1] == n2) {
                correct += 1;
            }
            if (randomNumber[NumberPosition[2] - 1] == n3) {
                correct += 1;
            }
            var text = "系統判斷您一共答對了" + correct + "次。\r\n\r\n現在獎池裡面有下列的道具(隨機5個):\r\n\r\n#b";

            itemData.sort(function(){return Math.random()>0.5?-1:1;});
            //隨機打亂獎池
            for (var i = 0; i < 5; i++) {//拿前5个
                text += "#i" + itemData[i] + "#   #z" + itemData[i] + "#\r\n\r\n";
            } 
            cm.sendNextS(text + "#d\r\n\r\n - 回答正確1次,隨機從裡面得到1個道具。 \r\n - 回答正確2次,隨機從裡面得到3個道具。 \r\n - 回答正確3次,獎池內所有道具都帶走!", 3);
        } else if (a == 10) {
            if (correct == 0) {//沒回答正確
                cm.sendOk("對不起,你沒有回答正確。 \r\n領取物品的必要條件是必須至少回答一個正確。");
                cm.dispose();
            } else if (correct == 3) {//全部回答正確
                var text = "恭喜你!回答3個全部正確!你將獲取獎池內的所有物品!\r\n\r\n#b";
                for (var i = 0; i < 5; i++) {//拿前5个
                    text += "#i" + itemData[i] + "#   #z" + itemData[i] + "#\r\n\r\n";
                }
                cm.sendNextS(text + "#d\r\n\r\n請妥善保管哦!", 3);
            } else if (correct == 1) {//正確1個
                cm.gainItem(itemData[0], 1);
                cm.sendOk("贈送成功!喜歡獎池裡面的道具嗎?");
                cm.dispose();
            } else if (correct == 2) {//2個正確
                var text = "恭喜你!回答正確" + correct + "次,按照規則,你將獲取獎池內的:\r\n\r\n#b";
                for (var i = 0; i < 3; i++) {
                    text += "#i" + itemData[i] + "#   #z" + itemData[i] + "#\r\n\r\n";
                }
                a = 11;
                cm.sendNextS(text + "#d\r\n\r\n請妥善保管哦!", 3);
            } else {
                cm.sendOk("錯誤!請和管理員聯繫。\r\n錯誤代碼:" + correct);
            }
            cm.setBossLog2("數字猜猜猜");
        } else if (a == 11) {//全部正確
            for (var i = 0; i < 5; i++) {//拿前5个
                cm.gainItem(itemData[i], 1);
            }
            cm.sendOk("贈送成功!喜歡獎池裡面的道具嗎?");
            cm.dispose();
        } else if (a == 12) {//2題
            for (var i = 0; i < 3; i++) {
                cm.gainItem(itemData[i], 1);
            }
            cm.sendOk("贈送成功!喜歡獎池裡面的道具嗎?");
            cm.dispose();
        }//a
    }//mode
}//f


function checkid(number) {//檢查是否重複
    var i = 0;
    while (randomNumber.length >= i) {
        if (randomNumber[i] == number) {
            return false;
        }
        i++;
    }
    return true;
}

function checkNumberPosition(number) {//檢查是否重複
    var i = 0;
    while (NumberPosition.length >= i) {
        if (NumberPosition[i] == number) {
            return false;
        }
        i++;
    }
    return true;
}
0 0 評分數
Article Rating
訂閱
通知
guest

0 Comments
在線反饋
查看所有評論