function calcuate() { var roles = document.getElementById("roles").value.split("\n").removeAll(""); var players = document.getElementById("players").value.split("\n").removeAll(""); if (roles.length == players.length) { document.getElementById("result").value = ""; var count = roles.length; for (var i = 0; i < count; i++) { // Math.random range[0,1) // Math.random() * players.length range [0, length) // Math.floor(Math.random() * players.length) range [0, 1, 2, ... range-1] var player = players[Math.floor(Math.random() * players.length)]; var role = roles[i]; players.remove(player); document.getElementById("result").value += player + " ---------- " + role + " \n"; } } else { document.getElementById("result").value = "角色与身份数量不符。" }}
var a=0;var b=10000000;for (var i =0;i<b;i++) {a += Math.random()*7;}console.log(a/b)VM307:6 3.5001398964456847
enum CardType { Privilege = 1, Common}interface Card { name: string; type: CardType;}class Player { name: string; cards: Card[]; constructor(public playerName) { this.name = playerName; this.cards = []; }}let cards: Array<Card> = [ { name: "杀手牌", type: CardType.Privilege }, { name: "狼人牌", type: CardType.Privilege }, { name: "狙击手牌", type: CardType.Privilege }, { name: "狙击手牌", type: CardType.Privilege }, { name: "束魂牌", type: CardType.Privilege }, { name: "束魂牌", type: CardType.Privilege }, { name: "巫医牌", type: CardType.Privilege }, { name: "巫医牌", type: CardType.Privilege }, { name: "防弹衣/狼毒牌", type: CardType.Privilege }, { name: "防弹衣/狼毒牌", type: CardType.Privilege }, { name: "禁锢/反狙击牌", type: CardType.Privilege }, { name: "禁锢/反狙击牌", type: CardType.Privilege }, { name: "绝杀/特赦牌", type: CardType.Privilege }, { name: "绝杀/特赦牌", type: CardType.Privilege }, { name: "诅咒/庇佑牌", type: CardType.Privilege }, { name: "诅咒/庇佑牌", type: CardType.Privilege }, { name: "纵火/圣人牌", type: CardType.Privilege }, { name: "纵火/圣人牌", type: CardType.Privilege }, { name: "保镖牌", type: CardType.Privilege }, { name: "保镖牌", type: CardType.Privilege }, { name: "保镖牌", type: CardType.Privilege }, { name: "阿米巴变形牌", type: CardType.Privilege }, { name: "阿米巴变形牌", type: CardType.Privilege }, { name: "阿米巴变形牌", type: CardType.Privilege }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }, { name: "庶民牌", type: CardType.Common }];let players: Array<Player> = [ new Player("玩家1"), new Player("玩家2"), new Player("玩家3"), new Player("玩家4"), new Player("玩家5"), new Player("玩家6"), new Player("玩家7")];function hasCard(cardName: string, player: Player) { for (var i = player.cards.length - 1; i >= 0; i--) { if (player.cards[i].name == cardName) { return true; } } return false;}function hasCardCount(cardName: string, player: Player) { let result = 0; for (var i = player.cards.length - 1; i >= 0; i--) { if (player.cards[i].name == cardName) { result++; } } return result;}function doing(outCount: number) { let tempCards = []; let tempPlayers = []; for (var i = cards.length - 1; i >= 0; i--) { tempCards.push(cards[i]); } for (var i = players.length - 1; i >= 0; i--) { tempPlayers.push(players[i]); } //决定弃牌 let out = []; for (var i = outCount - 1; i >= 0; i--) { let card = undefined; let cardId = undefined; do { cardId = Math.floor(Math.random() * tempCards.length); card = tempCards[cardId]; } while (card.name == "杀手牌" || card.name == "狼人牌"); out.push(card); tempCards.splice(cardId, 1); } console.log("【弃牌如下】"); for (var i = out.length - 1; i >= 0; i--) { console.log(out[i].name); } //庶民牌 let waterCards = []; for (var i = tempCards.length - 1; i >= 0; i--) { if (tempCards[i].name == "庶民牌") { waterCards.push(tempCards[i]); tempCards.splice(i, 1); } } for (var i = players.length - 1; i >= 0; i--) { players[i].cards.push(waterCards.pop()); } while (waterCards.length > 0) { let playerId = Math.floor(Math.random() * tempPlayers.length); let player = tempPlayers[playerId]; if (hasCardCount("庶民牌", player) < 2) { player.cards.push(waterCards.pop()); } } let currentPID = 0; for (; tempCards.length > 0;) { let cardId = Math.floor(Math.random() * tempCards.length); let card = tempCards[cardId]; let player: Player = tempPlayers[currentPID]; if (player.cards.length == 5) { currentPID ++; continue; } if (card.name == "杀手牌") { if (hasCard("狼人牌", player)) { continue; } } if (card.name == "狼人牌") { if (hasCard("杀手牌", player)) { continue; } } if (card.name == "狙击手牌") { if (hasCard("狙击手牌", player)) { continue; } } if (card.name == "束魂牌") { if (hasCard("束魂牌", player)) { continue; } } if (card.name == "巫医牌") { if (hasCard("巫医牌", player)) { continue; } } if (card.name == "防弹衣/狼毒牌") { if (hasCard("防弹衣/狼毒牌", player)) { continue; } } if (card.name == "禁锢/反狙击牌") { if (hasCard("禁锢/反狙击牌", player)) { continue; } } if (card.name == "绝杀/特赦牌") { if (hasCard("绝杀/特赦牌", player)) { continue; } } if (card.name == "诅咒/庇佑牌") { if (hasCard("诅咒/庇佑牌", player)) { continue; } } if (card.name == "纵火/圣人牌") { if (hasCard("纵火/圣人牌", player)) { continue; } } if (card.name == "保镖牌") { if (hasCard("保镖牌", player)) { continue; } } if (card.name == "阿米巴变形牌") { if (hasCard("阿米巴变形牌", player)) { continue; } } player.cards.push(card); tempCards.splice(cardId, 1); currentPID++; if (currentPID == tempPlayers.length) { currentPID = 0; } } return { out: out, players: tempPlayers };}let result = doing(1);for (var i = result.players.length - 1; i >= 0; i--) { document.body.innerHTML += "<h2>【" + result.players[i].name + "】</h2>"; for (var j = result.players[i].cards.length - 1; j >= 0; j--) { document.body.innerHTML += "<h3>" + result.players[i].cards[j].name + "</h3>"; }}
【玩家1】束魂牌巫医牌保镖牌庶民牌庶民牌【玩家2】保镖牌纵火/圣人牌诅咒/庇佑牌阿米巴变形牌庶民牌【玩家3】阿米巴变形牌保镖牌禁锢/反狙击牌庶民牌庶民牌【玩家4】禁锢/反狙击牌狙击手牌防弹衣/狼毒牌绝杀/特赦牌庶民牌【玩家5】狼人牌束魂牌巫医牌庶民牌庶民牌【玩家6】杀手牌阿米巴变形牌诅咒/庇佑牌防弹衣/狼毒牌庶民牌【玩家7】纵火/圣人牌狙击手牌绝杀/特赦牌庶民牌庶民牌
enum CardType { 身份牌 = 1, 特权牌, 庶民牌}enum PlayerCamp { 杀手阵营, 狼人阵营, 平民阵营, 自选阵营, Unknow}interface Card { name: string; type: CardType;}interface CardTableItem { card: Card; count: number;}let players: Array<Player> = [];let cardTable: Array<CardTableItem> = [ { card: { name: "杀手牌", type: CardType.身份牌 }, count: 1 }, { card: { name: "狼人牌", type: CardType.身份牌 }, count: 1 }, { card: { name: "狙击手牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "束魂牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "巫医牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "防弹衣/狼毒牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "禁锢/反狙击牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "绝杀/特赦牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "诅咒/庇佑牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "纵火/圣人牌", type: CardType.特权牌 }, count: 2 }, { card: { name: "保镖牌", type: CardType.特权牌 }, count: 3 }, { card: { name: "阿米巴变形牌", type: CardType.特权牌 }, count: 3 }, { card: { name: "庶民牌", type: CardType.庶民牌 }, count: 12 }];let cards: Array<Card> = getCards(cardTable);class Player { name: string; cards: Card[]; camp: PlayerCamp; constructor(public playerName) { this.name = playerName; this.cards = []; this.camp = PlayerCamp.Unknow; }}function showCardTable(cardTable: Array<CardTableItem>) { for (var i = 0; i < cardTable.length; ++i) { document.getElementById("cardTable").innerHTML += "<tr>\ <td><input name='card_item_" + i + "' type='text' value='" + cardTable.card.name + "'/></td>\ <td>\ " + cardTypeSelect(cardTable.card) + "\ </td>\ <td><input name='' type='number' value='"+ cardTable.count + "'/></td>\ </tr>"; }}function cardTypeSelect(card: Card) { let result = "<select>"; for (var i = 1; i < 4; ++i) { let selected = (card.type == i) ? "selected" : ""; result += "<option value='" + i + "' " + selected + ">" + CardType + "</option>"; } result += "</select>"; return result;}function getCards(cardTable: Array<CardTableItem>) { let cards: Array<Card> = []; for (var i = 0; i < cardTable.length; ++i) { let cardTableItem = cardTable; for (var j = 0; j < cardTableItem.count; j++) { cards.push(cardTableItem.card); } } return cards;}function updatePlayers() { let playerstrings = (<HTMLInputElement>document.getElementById("players")).value; let playerarray = playerstrings.split("\n"); for (var i = playerarray.length - 1; i >= 0; i--) { if (playerarray == "") { playerarray.slice(i, 1); } } if (playerarray.length == 7) { players = []; for (var i = 0; i < playerarray.length; i++) { players.push(new Player(playerarray)); } }}function hasCard(cardName: string, player: Player) { for (var i = player.cards.length - 1; i >= 0; i--) { if (player.cards.name == cardName) { return true; } } return false;}function hasCardCount(cardName: string, player: Player) { let result = 0; for (var i = player.cards.length - 1; i >= 0; i--) { if (player.cards.name == cardName) { result++; } } return result;}function doing(outCount: number) { let tempCards = undefined; let tempPlayers = undefined; let blocktime = undefined; let out = undefined; let limits = getCardOfType(cards, CardType.特权牌); while (true) { out = []; blocktime = 0; tempPlayers = []; tempCards = []; for (var i = cards.length - 1; i >= 0; i--) { tempCards.push(cards); } for (var i = 0; i < players.length; i++) { tempPlayers.push(players); players.cards = []; } //决定弃牌 for (var i = outCount - 1; i >= 0; i--) { let card = undefined; let cardId = undefined; do { cardId = Math.floor(Math.random() * tempCards.length); card = tempCards[cardId]; } while (card.name == "杀手牌" || card.name == "狼人牌"); out.push(card); tempCards.splice(cardId, 1); } //庶民牌 let waterCards = []; for (var i = tempCards.length - 1; i >= 0; i--) { if (tempCards.name == "庶民牌") { waterCards.push(tempCards); tempCards.splice(i, 1); } } for (var i = players.length - 1; i >= 0; i--) { players.cards.push(waterCards.pop()); } while (waterCards.length > 0) { let playerId = Math.floor(Math.random() * tempPlayers.length); let player = tempPlayers[playerId]; if (hasCardCount("庶民牌", player) < 2) { player.cards.push(waterCards.pop()); } } // 身份牌和特权牌 let currentPID = 0; let blockCheck = false; while (tempCards.length > 0) { if (blocktime > 20) { blockCheck = true; console.log("发牌无解,重发"); break; } let cardId = Math.floor(Math.random() * tempCards.length); let card = tempCards[cardId]; let player: Player = tempPlayers[currentPID]; if (player.cards.length == 5) { currentPID++; if (currentPID == tempPlayers.length) { currentPID = 0; } continue; } if (card.name == "杀手牌") { if (hasCard("狼人牌", player)) { blocktime++; continue; } } if (card.name == "狼人牌") { if (hasCard("杀手牌", player)) { blocktime++; continue; } } let checkLimit = false; for (var i = limits.length - 1; i >= 0; i--) { let limit = limits; if (card.name == limit) { if (hasCard(limit, player)) { checkLimit = true; break; } } } if (checkLimit) { blocktime++; continue; } player.cards.push(card); tempCards.splice(cardId, 1); currentPID++; if (currentPID == tempPlayers.length) { currentPID = 0; } } if (blockCheck) { continue; } //阵营判断 let unknowPlay = []; for (var i = tempPlayers.length - 1; i >= 0; i--) { let player: Player = tempPlayers; if (hasCard("杀手牌", player)) { player.camp = PlayerCamp.杀手阵营; } else if (hasCard("狼人牌", player)) { player.camp = PlayerCamp.狼人阵营; } else { unknowPlay.push(player); } } let sel = Math.floor(Math.random() * unknowPlay.length); let selplayer: Player = unknowPlay[sel]; selplayer.camp = PlayerCamp.自选阵营; unknowPlay.splice(sel, 1); for (var i = unknowPlay.length - 1; i >= 0; i--) { unknowPlay.camp = PlayerCamp.平民阵营; } break; } return { out: out, players: tempPlayers };}function getCardOfType(cards: Array<Card>, cardType: CardType) { let result = []; for (var i = cards.length - 1; i >= 0; i--) { let card = cards; if (card.type == cardType) { if (result.indexOf(card.name) == -1) { result.push(card.name); } } } return result;}function showPlayerTable(result) { document.getElementById("playerTable").innerHTML = ""; for (var i = 0; i < result.players.length; ++i) { let player: Player = result.players; let cards: string = ""; for (var j = player.cards.length - 1; j >= 0; j--) { cards += player.cards[j].name + " "; } document.getElementById("playerTable").innerHTML += "<tr>\ <td>" + player.name + "</td>\ <td>" + PlayerCamp[player.camp] + "</td>\ <td>" + cards + "</td>\ </tr>"; } let outstring: string = "" for (var i = result.out.length - 1; i >= 0; i--) { outstring += result.out.name; } document.getElementById("playerTable").innerHTML += "<tr>\ <td></td>\ <td>弃牌</td>\ <td>" + outstring + "</td>\ </tr>";}function refresh() { updatePlayers(); let result = doing(1); showPlayerTable(result);}refresh();
引用第9楼K40-程心于2016-09-12 23:47发表的 :表示不是很确定.push的作用