Note: Each new deal starts with the same cards. My sheet named cards just has numbers from 1 to 52 in the first 52 rows of ColumnA
function dealNCards(n) {
var n=n||6;
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Cards');
const rg=sh.getRange(1,1,sh.getLastRow(),1);
const cards=rg.getDisplayValues().map(function(r){return r[0];});
var deal=[];
do{
let idx=Math.floor(Math.random()*cards.length);
deal.push(cards[idx]);
cards.splice(idx,1);
}while(deal.length<n);
var s=Utilities.formatString('<br /><input type="button" value="Deal %s Cards " onClick="google.script.run.dealNCards(%s)" />',n+1,n+1)
var html=deal.join(',')+s;
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'The Deal')
return deal;
}
Here's another version:
function dealNCards(n) {
var n=n||24;
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Cards');
const rg=sh.getRange(1,1,sh.getLastRow(),1);
const cards=rg.getDisplayValues().map(function(r){return r[0];});
var deal=[];
do{
let idx=Math.floor(Math.random()*cards.length);
deal.push(cards[idx]);
cards.splice(idx,1);
}while(deal.length<n && cards.length>0);
var s=Utilities.formatString('<br /><input type="button" value="Deal %s Cards " onClick="google.script.run.dealNCards(%s)" />',n+1,n+1);
var p=Utilities.formatString('Dealt %s cards.<br />',deal.length)
var html=p+ deal.join(',')+s;
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'The Deal')
return deal;
}
Here's a much more fun version. It will deal n cards into m hands until it runs out of cards.
function dealnCardsmHands(n,m) {
var n=n||5;
var m=m||4;
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Cards');
const rg=sh.getRange(1,1,sh.getLastRow(),1);
const cards=rg.getDisplayValues().map(function(r){return r[0];});
var deal=[];
var hands={};
for(let i=0;i<m;i++) {
hands[i+1]=[];
}
do{
for(let i=0;i<m;i++) {
let idx=Math.floor(Math.random()*cards.length);
hands[i+1].push(cards[idx]);
cards.splice(idx,1);
}
}while(hands[m].length<n && cards.length>=m);
var html=Utilities.formatString('<br />Hands: %s Cards: %s',m,hands[m].length);
for(let i=0;i<m;i++) {
html+=Utilities.formatString('<br />Hand%s: %s',i+1,hands[i+1].join(','))
}
html+=Utilities.formatString('<br /><input type="text" id="cards" value="%s" />Cards',hands[m].length);
html+=Utilities.formatString('<br /><input type="text" id="hands" value="%s" />Hands',m);
html+='<br /><input type="button" value="Deal" onclick="deal();" />';
html+='<script>function deal(){google.script.run.dealnCardsmHands(document.getElementById("cards").value,document.getElementById("hands").value);}</script>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'The Deal')
return deal;
}
This deals the suits out without using a spreadsheet.
function dealnCardsmHands(n,m) {
var cA=['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
var sA=['s','d','h','c'];
cards=[];
sA.forEach(function(s,i){
cA.forEach(function(c,j){
cards.push(String(c+s));
});
});
var n=n||5;
var m=m||4;
//const ss=SpreadsheetApp.getActive();
//const sh=ss.getSheetByName('Cards');
//const rg=sh.getRange(1,1,sh.getLastRow(),1);
//const cards=rg.getDisplayValues().map(function(r){return r[0];});
var deal=[];
var hands={};
for(let i=0;i<m;i++) {
hands[i+1]=[];
}
do{
for(let i=0;i<m;i++) {
let idx=Math.floor(Math.random()*cards.length);
hands[i+1].push(cards[idx]);
cards.splice(idx,1);
}
}while(hands[m].length<n && cards.length>=m);
var html=Utilities.formatString('<br />Hands: %s Cards: %s',m,hands[m].length);
for(let i=0;i<m;i++) {
html+=Utilities.formatString('<br />Hand%s: %s',i+1,hands[i+1].join(','))
}
html+=Utilities.formatString('<br /><input type="text" id="cards" value="%s" />Cards',hands[m].length);
html+=Utilities.formatString('<br /><input type="text" id="hands" value="%s" />Hands',m);
html+='<br /><input type="button" value="Deal" onclick="deal();" />';
html+='<script>function deal(){google.script.run.dealnCardsmHands(document.getElementById("cards").value,document.getElementById("hands").value);}</script>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'The Deal')
return deal;
}
This version deals the hands and also sorts them by suit and rank.
function dealnCardsmHands(n,m) {
var cA=['A','K','Q','J','10','9','8','7','6','5','4','3','2']
var sA=['s','d','h','c'];
var cards=[];
sA.forEach(function(s,i){
cA.forEach(function(c,j){
cards.push(String(c+s));
});
});
var n=n||12;
var m=m||4;
//const ss=SpreadsheetApp.getActive();
//const sh=ss.getSheetByName('Cards');
//const rg=sh.getRange(1,1,sh.getLastRow(),1);
//const cards=rg.getDisplayValues().map(function(r){return r[0];});
var deal=[];
var hands={};
for(let i=0;i<m;i++) {
hands[i+1]=[];
}
do{
for(let i=0;i<m;i++) {
let idx=Math.floor(Math.random()*cards.length);
hands[i+1].push(cards[idx]);
cards.splice(idx,1);
}
}while(hands[m].length<n && cards.length>=m);
var html=Utilities.formatString('<br />Hands: %s Cards: %s',m,hands[m].length);
for(let i=0;i<m;i++) {
hands[i+1].sort(function(a,b){
var aidx=sA.indexOf(a.slice(-1));
var bidx=sA.indexOf(b.slice(-1));
if(aidx!=bidx) {
return aidx-bidx;
}else{
var A=cA.indexOf(a.slice(0,-1));
var B=cA.indexOf(b.slice(0,-1));
return A-B;
}
});
html+=Utilities.formatString('<br />Hand%s: %s',i+1,hands[i+1].join(','))
}
html+=Utilities.formatString('<br /><input type="text" id="cards" value="%s" />Cards',hands[m].length);
html+=Utilities.formatString('<br /><input type="text" id="hands" value="%s" />Hands',m);
html+='<br /><input type="button" value="Deal" onclick="deal();" />';
html+='<script>function deal(){google.script.run.dealnCardsmHands(document.getElementById("cards").value,document.getElementById("hands").value);}</script>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'The Deal')
return deal;
}