xxxxxxxxxx
722
// A Game of Chance
// Setting:
// You just woke up after getting kidnapped. You are tied up in a chair. The man standing in the dark in front of you asks you to play a game of chance. Do you accept your fate or will you fight your way back?
// How to Play:
// In front of you are 9 cards. There are 8 numbered cards (2-9) and 1 death card.
// Every time you opt to turn over a card, you wager 1 gold.
// If you hit a number card, continue! If you hit the death card, you will lose what you wagered + 2 gold.
// If you uncover everything, you win everything you wagered. If you pass the game, you will gain want you wagered - 2 gold.
// In addition, there are power ups to help you! If you get 3 in a row or column, you can use a power up to help you.
// + powerup allows you to select any card that hasn't been turned over and you will get a hint as to what the card could be above.
// > powerup allows you to select any uncovered card and reveals whether neighbor cards are higher or lower than the chosen card.
// In addition, you can guess where the death card is at any point by hitting the "GUESS D" button. If you successfully guess, you will gain, in addition to what you wagered, 3 times the number of remaining cards. If you fail, you will lose what you wagered + 10 gold.
// Can you win the game of chance?
let diff = 'e';
let cardList;
let powList;
let rows;
let cols;
let count = 0;
let maxcount;
let cards = [];
let cardPositions = [
[],
[],
[],
[]
];
let powerups = [];
let powerupPositions = [
[],
[],
[],
[]
];
let gameState = 0;
let gameEvents = []
currentPool = 0;
Winnings = 0;
TheShot = 0;
let passButton;
let guessDeathButton;
class Card {
constructor(val, flipped) {
this.val = val;
this.flipped = flipped;
}
}
class PowerUp {
constructor(val, enabled, used, selected) {
this.val = val;
this.enabled = enabled;
this.used = used;
this.selected = selected
}
}
class GameEvent {
constructor(eventType, info) {
this.type = eventType;
this.info = info;
}
}
function initialize(diff) {
cards = [];
cardPositions = [
[],
[],
[],
[]
];
powerups = [];
powerupPositions = [
[],
[],
[],
[]
];
gameState = 0;
gameEvents = [];
if (diff == 'e') {
cardList = [2, 3, 4, 5, 6, 7, 8, 9, 'D']; //9 cards = rows * cols
powList = ['V', 'C']; //Equal Probability of V,C,S
rows = 3;
cols = 3;
maxcount = 7;
} else if (diff == 'm') {
cardList = [2, 3, 4, 5, 6, 7, 8, 9, 'D', 2, 3, 4];
powList = ['V', 'C'];
rows = 3;
cols = 4;
maxcount = 10;
} else if (diff == 'h') {
cardList = [2, 3, 4, 5, 6, 7, 8, 9, 'D'];
powList = [1, 2, 3];
rows = 4;
cols = 4;
maxcount = 14;
}
}
function setup() {
createCanvas(720, 900);
initialize(diff);
setUpGame();
passButton = createButton('PASS');
passButton.position(140, 720);
passButton.mousePressed(passGame);
drawDeathButton = createButton('GUESS D');
drawDeathButton.position(30, 800);
drawDeathButton.mousePressed(guessDeath);
}
function guessDeath() {
if (gameState == 2) {
gameState = 0;
} else if (gameState == 0) {
gameState = 2;
}
}
function passGame() {
print("===== RESULTS =====");
print("You have hit pass.");
print("You had " + currentPool + " gold invested in the game. Because you passed, you lose 2 gold");
print("This game's winnings are " + (currentPool - 2) + " gold.");
Winnings = Winnings + currentPool - 2;
currentPool = 0;
print("Total Winnings are " + Winnings + " gold.")
print("Click anywhere to go on to the next round!");
print("===== RESULTS =====")
gameState = 1;
count = 0;
}
function resetGame() {
initialize(diff);
setUpGame();
}
function setUpGame() {
// Card Generation Method
for (var i = 0; i < rows; i++) {
cards.push([])
cardPositions[0].push([]);
cardPositions[1].push([]);
cardPositions[2].push([]);
cardPositions[3].push([]);
for (var j = 0; j < cols; j++) {
cards[i].push(new Card(cardList.splice(Math.floor(Math.random() * cardList.length), 1)[0], false));
cardPositions[0][i].push('x');
cardPositions[1][i].push('x');
cardPositions[2][i].push('x');
cardPositions[3][i].push('x');
}
}
//print(cards)
//Power Up Generation Method
for (i = 0; i < rows + cols; i++) {
var randomN = Math.floor(Math.random() * powList.length);
powerups.push(new PowerUp(powList[randomN], false, false, false));
powerupPositions[0].push('x');
powerupPositions[1].push('x');
powerupPositions[2].push('x');
powerupPositions[3].push('x');
}
//print(powerups)
//Set a random, non Death card to be up
while (true) {
var rr = Math.floor(Math.random() * rows);
var rc = Math.floor(Math.random() * cols);
if (cards[rr][rc].val != 'D') {
cards[rr][rc].flipped = true;
break;
}
}
//Score Stuff
currentPool = 1;
count = 0;
}
function drawBoard(nr, nc) {
//Draw Board
fill(255);
startW = 140;
startH = 100;
sizeW = 480;
sizeH = 600;
rect(startW, startH, sizeW, sizeH);
//Draw Cards
rectW = (sizeW / nc);
rectH = (sizeH / nr);
rectScale = 0.8;
for (var i = 0; i < nr; i++) {
for (var j = 0; j < nc; j++) {
var x = startW + (rectW * j) + (rectW * (1 - rectScale) / 2);
var y = startH + (rectH * i) + (rectH * (1 - rectScale) / 2);
rect(x, y, rectW * rectScale, rectH * rectScale);
// Add Card Stuff based on state
if (cards[i][j].flipped) {
textSize(this.rectW / 2);
textAlign(CENTER, CENTER);
fill(0);
text(" " + cards[i][j].val, x, y, rectW * rectScale, rectH * rectScale);
fill(255);
}
//Populate Card Knowledge Graph (One Time)!
if (cardPositions[0][i][j] == 'x') {
cardPositions[0][i][j] = x;
cardPositions[1][i][j] = y;
cardPositions[2][i][j] = rectW * rectScale;
cardPositions[3][i][j] = rectH * rectScale;
}
}
}
//Draw Powerups!
textSize(startH / 3);
textAlign(CENTER, CENTER);
for (i = 1; i < cols * 2; i = i + 2) {
var index = (i - 1) / 2;
let s = '';
if (powerups[index].used) {
fill(50);
} else if (powerups[index].selected) {
fill(0, 255, 0);
} else if (powerups[index].enabled) {
fill(255);
} else {
fill(150);
}
square((startW + (i * rectW / 2) - (startH / 4)), startH / 4, startH / 2);
if (powerups[index].val == 'V') {
s = ' +';
} else if (powerups[index].val == 'C') {
s = ' >'
}
fill(0);
text(s, (startW + (i * rectW / 2) - (startH / 4)), startH / 4, startH / 2, startH / 2);
if (powerupPositions[0][index] == 'x') {
powerupPositions[0][index] = (startW + (i * rectW / 2) - (startH / 4));
powerupPositions[1][index] = startH / 4;
powerupPositions[2][index] = startH / 2;
powerupPositions[3][index] = startH / 2;
}
}
for (i = 1; i < rows * 2; i = i + 2) {
var index = (i - 1) / 2 + cols;
let s = '';
if (powerups[index].used) {
fill(50);
} else if (powerups[index].selected) {
fill(0, 255, 0);
} else if (powerups[index].enabled) {
fill(255);
} else {
fill(150);
}
square(startW + sizeW + ((width - startW - sizeW) / 4), startH + (i * rectH / 2) - (startW / 4), startH / 2)
if (powerups[index].val == 'V') {
s = ' +';
} else if (powerups[index].val == 'C') {
s = ' >'
}
fill(0);
text(s, startW + sizeW + ((width - startW - sizeW) / 4), startH + (i * rectH / 2) - (startW / 4), startH / 2, startH / 2);
if (powerupPositions[0][index] == 'x') {
powerupPositions[0][index] = startW + sizeW + ((width - startW - sizeW) / 4);
powerupPositions[1][index] = startH + (i * rectH / 2) - (startW / 4);
powerupPositions[2][index] = startH / 2;
powerupPositions[3][index] = startH / 2;
}
}
fill(0);
//Draw Game Events
textSize(startH / 6);
textAlign(CENTER, CENTER);
for (var i = 0; i < gameEvents.length; i++) {
if (gameEvents[i].type == 10) {
if (!cards[gameEvents[i].info[0]][gameEvents[i].info[1]].flipped) {
fill(255)
circle(startW + (rectW * gameEvents[i].info[1]) + (rectW / 2),
startH + (rectH * gameEvents[i].info[0]) + (rectH / 2),
rectH/3);
fill(0)
text(" " + gameEvents[i].info[2] + "+",
startW + (rectW * gameEvents[i].info[1]) + (rectW * (1 - rectScale) / 2),
startH + (rectH * gameEvents[i].info[0]) + (rectH * (1 - rectScale) / 2),
rectW * rectScale,
rectH * rectScale);
}
}
else if (gameEvents[i].type == 11) {
//Left, Then Top, Then Right, Then Bot
if (gameEvents[i].info[2][0] != '-') {
fill(255);
square(startW + (rectW * gameEvents[i].info[1]) - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) + (rectH / 2) - (rectH/12),
rectH/6);
fill(0);
text(" " + gameEvents[i].info[2][0],
startW + (rectW * gameEvents[i].info[1]) - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) + (rectH / 2) - (rectH/12),
rectH/6, rectH/6);
}
if (gameEvents[i].info[2][1] != '-') {
fill(255)
square(startW + (rectW * gameEvents[i].info[1]) + (rectW / 2) - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) - (rectH/12),
rectH/6);
fill(0)
text(" " + gameEvents[i].info[2][1],
startW + (rectW * gameEvents[i].info[1]) + (rectW / 2) - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) - (rectH/12),
rectH/6, rectH/6)
}
if (gameEvents[i].info[2][2] != '-') {
fill(255)
square(startW + (rectW * gameEvents[i].info[1]) + rectW - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) + (rectH / 2) - (rectH/12),
rectH/6);
fill(0)
text(" " + gameEvents[i].info[2][2],
startW + (rectW * gameEvents[i].info[1]) + rectW - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) + (rectH / 2) - (rectH/12),
rectH/6, rectH/6)
}
if (gameEvents[i].info[2][3] != '-') {
fill(255)
square(startW + (rectW * gameEvents[i].info[1]) + (rectW / 2) - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) + rectH - (rectH/12),
rectH/6);
fill(0)
text(" " + gameEvents[i].info[2][3],
startW + (rectW * gameEvents[i].info[1]) + (rectW / 2) - (rectH/12),
startH + (rectH * gameEvents[i].info[0]) + rectH - (rectH/12),
rectH/6, rectH/6)
}
}
}
}
function mousePressed() {
if (mouseX > width || mouseY > height) return;
if (gameState == 1) {
resetGame();
}
var coords = getMouseClick();
// Check what was pressed
if (coords[0] == 'c') {
handleCardClick(coords[1], coords[2]);
print('cardPressed', coords);
} else if (coords[0] == 'p') {
handlePowerUpClick(coords[1]);
print('powerUpPressed', coords);
} else if (gameState != 2) {
gameState = 0;
for (var i = 0; i < powerups.length; i++) {
powerups[i].selected = false;
}
}
}
function getMouseClick() {
var what;
var x;
var y;
// Check if Card got pressed!
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
if ((mouseX > cardPositions[0][i][j]) &&
(mouseX < cardPositions[0][i][j] + cardPositions[2][i][j]) &&
(mouseY > cardPositions[1][i][j]) &&
(mouseY < cardPositions[1][i][j] + cardPositions[3][i][j])) {
what = 'c';
x = i;
y = j;
break;
}
if (what != null) break;
}
if (what != null) break;
}
// Check if powerup got pressed!
for (var i = 0; i < powerups.length; i++) {
if ((mouseX > powerupPositions[0][i]) &&
(mouseX < powerupPositions[0][i] + powerupPositions[2][i]) &&
(mouseY > powerupPositions[1][i]) &&
(mouseY < powerupPositions[1][i] + powerupPositions[3][i])) {
what = 'p';
x = i;
break;
}
if (what != null) break;
}
// Return
if (what != null) {
return [what, x, y];
} else {
return ['n', -1, -1];
}
}
function handleCardClick(cr, cc) {
if (gameState == 0) {
if (!cards[cr][cc].flipped) {
cards[cr][cc].flipped = true;
count += 1;
currentPool++;
//Win check
if (cards[cr][cc].val == 'D') {
print("===== RESULTS =====");
print("You have hit death.");
print("You had " + currentPool + " gold invested in the game. Because you hit the Death card, you lose 2 gold, including what you had in the game.");
print("This game's winnings are " + (-currentPool - 2) + " gold.");
Winnings = Winnings - currentPool - 2;
currentPool = 0;
print("Total Winnings are " + Winnings + " gold.");
print("Click anywhere to go on to the next round!");
print("===== RESULTS =====")
gameState = 1;
} else if (count == maxcount) {
print("===== RESULTS =====");
print("You have uncovered all the cards!");
print("You had " + currentPool + " gold invested in the game. Because you avoided Death, you get 2 more gold.");
print("This game's winnings are " + (currentPool + 2) + " gold.");
Winnings = Winnings + currentPool + 2;
currentPool = 0;
print("Total game's winnings are " + Winnings + " gold.");
print("Click anywhere to go on to the next round!");
print("===== RESULTS =====");
gameState = 1;
count = 0;
}
}
} else if (gameState == 2) { // GUESS D
if (!cards[cr][cc].flipped) {
cards[cr][cc].flipped = true;
count += 1;
currentPool++;
//Win check
if (cards[cr][cc].val == 'D') {
print("===== RESULTS =====");
print("You correctly cheated death.");
print("You had " + currentPool + " gold invested in the game. Because you correctly guessed the Death card, you gain " + 3*(9 - count) + " gold, including what you had in the game.");
print("This game's winnings are " + (currentPool + 2 + 3*(9 - count)) + " gold.");
Winnings = Winnings + currentPool + 2 + 3*(9 - count);
currentPool = 0;
print("Total Winnings are " + Winnings + " gold.");
print("Click anywhere to go on to the next round!");
print("===== RESULTS =====")
gameState = 1;
count = 0;
} else {
print("===== RESULTS =====");
print("You incorrectly guessed the death card.");
print("You had " + currentPool + " gold invested in the game. Because you incorrectly guessed the Death card, you lose 10 gold, including what you had in the game.");
print("This game's winnings are " + Winnings + " gold.");
Winnings = Winnings - currentPool - 10;
currentPool = 0;
print("Total Winnings are " + Winnings + " gold.");
print("Click anywhere to go on to the next round!");
print("===== RESULTS =====")
gameState = 1;
count = 0;
}
}
} else if (gameState == 10) { //i.e using powerup V
if (!cards[cr][cc].flipped) {
//Generate a number
var gen = cards[cr][cc].val;
while(true){
if (gen == 'D') {
gen = 10;
}
if (gen == 2) {
break;
}
var flip = (Math.floor(Math.random() * 5));
//print(flip);
if (flip == 0) {
break;
} else {
gen = gen - 1;
}
}
gameEvents.push(new GameEvent(10, [cr, cc, gen]));
//print(gameEvents)
gameState = 0;
for (var i = 0; i < powerups.length; i++) {
if (powerups[i].selected) {
powerups[i].used = true;
}
powerups[i].selected = false;
}
} else {
print('You have to click on a unrevealed card to use this!');
}
} else if (gameState == 11) { // C
if (cards[cr][cc].flipped) {
var cardVals = [];
for (var i = 0; i < rows; i++) {
cardVals.push([]);
for (var j = 0; j < cols; j++) {
if (cards[i][j].val == 'D') {
cardVals[i].push(10);
} else {
cardVals[i].push(cards[i][j].val);
}
}
}
//Decide how many and what directions
// cr = 0 means no top
// cr = row - 1 means to bot
// cc = 0 means no left
// c = cols - 1 means no right
var comparisons = [] // Going left, top, right, bot
if (cc > 0) {
if (cardVals[cr][cc-1] > cardVals[cr][cc]) {
comparisons.push(">");
}
else if (cardVals[cr][cc-1] < cardVals[cr][cc]) {
comparisons.push("<");
}
else {
comparisons.push("=");
}
} else {
comparisons.push("-")
}
if (cr > 0) {
if (cardVals[cr][cc] > cardVals[cr-1][cc]) {
comparisons.push("/\\");
}
else if (cardVals[cr][cc] < cardVals[cr-1][cc]) {
comparisons.push("\\/");
}
else {
comparisons.push("||");
}
}else {
comparisons.push("-")
}
if (cc < cols - 1) {
if (cardVals[cr][cc] > cardVals[cr][cc+1]) {
comparisons.push(">");
}
else if (cardVals[cr][cc] < cardVals[cr][cc+1]) {
comparisons.push("<");
}
else {
comparisons.push("=");
}
} else {
comparisons.push("-")
}
if (cr < rows - 1) {
if (cardVals[cr][cc] > cardVals[cr+1][cc]) {
comparisons.push("\\/");
}
else if (cardVals[cr][cc] < cardVals[cr+1][cc]) {
comparisons.push("/\\");
}
else {
comparisons.push("||");
}
}else {
comparisons.push("-")
}
gameEvents.push(new GameEvent(11, [cr,cc, comparisons]));
gameState = 0;
for (var i = 0; i < powerups.length; i++) {
if (powerups[i].selected) {
powerups[i].used = true;
}
powerups[i].selected = false;
}
} else {
print('You have to click on a revealed card to use this!');
}
}
//Check if a powerup needs to be enabled!
//rows
for (var i = 0; i < cols; i++) {
allFlipped = true;
for (var j = 0; j < rows; j++) {
if (!cards[j][i].flipped) {
allFlipped = false;
break;
}
}
if (allFlipped) {
powerups[i].enabled = true;
}
}
//cols
for (var i = 0; i < rows; i++) {
allFlipped = true;
for (var j = 0; j < cols; j++) {
if (!cards[i][j].flipped) {
allFlipped = false;
break;
}
}
if (allFlipped) {
powerups[i + cols].enabled = true;
}
}
}
function handlePowerUpClick(pi) {
if (powerups[pi].used || !powerups[pi].enabled || gameState != 0) {
gameState = 0;
for (var i = 0; i < powerups.length; i++) {
powerups[i].selected = false;
}
return;
}
if (gameState == 0) {
powerups[pi].selected = true;
if (powerups[pi].val == 'V') {
gameState = 10;
} else if (powerups[pi].val == 'C') {
gameState = 11;
}
} else if (gameState >= 10) {
gameState = 0;
for (var i = 0; i < powerups.length; i++) {
powerups[i].selected = false;
}
}
}
function drawDeath() {
fill(255);
rect(20, 100, 100, 600);
}
function draw() {
background(220);
text(gameState, 10,10,100,100);
drawBoard(rows, cols);
drawDeath();
textSize(startH/2);
textAlign(CENTER, CENTER);
fill(0);
text(" Current", 0, 720, width, height/15);
text(" " + currentPool, 0, 720, width, height/5);
text(" Winnings", width*11/20, 720, width/2, height/15);
text(" " + Winnings, width*11/20, 720, width/2, height/5);
}