mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-26 20:54:07 -08:00
Merge pull request #258 from nanochess/main
Ported STOCKMARKET to Javascript
This commit is contained in:
9
71 Poker/javascript/poker.html
Normal file
9
71 Poker/javascript/poker.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>POKER</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="poker.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
751
71 Poker/javascript/poker.js
Normal file
751
71 Poker/javascript/poker.js
Normal file
@@ -0,0 +1,751 @@
|
||||
// POKER
|
||||
//
|
||||
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
|
||||
//
|
||||
|
||||
function print(str)
|
||||
{
|
||||
document.getElementById("output").appendChild(document.createTextNode(str));
|
||||
}
|
||||
|
||||
function input()
|
||||
{
|
||||
var input_element;
|
||||
var input_str;
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
input_element = document.createElement("INPUT");
|
||||
|
||||
print("? ");
|
||||
input_element.setAttribute("type", "text");
|
||||
input_element.setAttribute("length", "50");
|
||||
document.getElementById("output").appendChild(input_element);
|
||||
input_element.focus();
|
||||
input_str = undefined;
|
||||
input_element.addEventListener("keydown", function (event) {
|
||||
if (event.keyCode == 13) {
|
||||
input_str = input_element.value;
|
||||
document.getElementById("output").removeChild(input_element);
|
||||
print(input_str);
|
||||
print("\n");
|
||||
resolve(input_str);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tab(space)
|
||||
{
|
||||
var str = "";
|
||||
while (space-- > 0)
|
||||
str += " ";
|
||||
return str;
|
||||
}
|
||||
|
||||
var aa = [];
|
||||
var ba = [];
|
||||
var b;
|
||||
var c;
|
||||
var d;
|
||||
var g;
|
||||
var i;
|
||||
var k;
|
||||
var m;
|
||||
var n;
|
||||
var p;
|
||||
var s;
|
||||
var u;
|
||||
var v;
|
||||
var x;
|
||||
var z;
|
||||
var hs;
|
||||
var is;
|
||||
var js;
|
||||
var ks;
|
||||
|
||||
function fna(x)
|
||||
{
|
||||
return Math.floor(10 * Math.random());
|
||||
}
|
||||
|
||||
function fnb(x)
|
||||
{
|
||||
return x % 100;
|
||||
}
|
||||
|
||||
function im_busted()
|
||||
{
|
||||
print("I'M BUSTED. CONGRATULATIONS!\n");
|
||||
}
|
||||
|
||||
// 1740
|
||||
function deal_card()
|
||||
{
|
||||
while (1) {
|
||||
aa[z] = 100 * Math.floor(4 * Math.random()) + Math.floor(100 * Math.random());
|
||||
if (Math.floor(aa[z] / 100) > 3) // Invalid suit
|
||||
continue;
|
||||
if (aa[z] % 100 > 12) // Invalid number
|
||||
continue;
|
||||
if (z != 1) {
|
||||
for (k = 1; k <= z - 1; k++) {
|
||||
if (aa[z] == aa[k])
|
||||
break;
|
||||
}
|
||||
if (k <= z - 1) // Repeated card
|
||||
continue;
|
||||
if (z > 10) {
|
||||
n = aa[u];
|
||||
aa[u] = aa[z];
|
||||
aa[z] = n;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 1850
|
||||
function show_cards()
|
||||
{
|
||||
for (z = n; z <= n + 4; z++) {
|
||||
print(" " + z + "-- ");
|
||||
k = fnb(aa[z]);
|
||||
show_number();
|
||||
print(" OF");
|
||||
k = Math.floor(aa[z] / 100);
|
||||
show_suit();
|
||||
if (z % 2 == 0)
|
||||
print("\n");
|
||||
}
|
||||
print("\n");
|
||||
}
|
||||
|
||||
// 1950
|
||||
function show_number()
|
||||
{
|
||||
if (k == 9)
|
||||
print("JACK");
|
||||
if (k == 10)
|
||||
print("QUEEN");
|
||||
if (k == 11)
|
||||
print("KING");
|
||||
if (k == 12)
|
||||
print("ACE");
|
||||
if (k < 9)
|
||||
print(" " + (k + 2));
|
||||
}
|
||||
|
||||
// 2070
|
||||
function show_suit()
|
||||
{
|
||||
if (k == 0)
|
||||
print(" CLUBS\t");
|
||||
if (k == 1)
|
||||
print(" DIAMONDS\t");
|
||||
if (k == 2)
|
||||
print(" HEARTS\t");
|
||||
if (k == 3)
|
||||
print(" SPADES\t");
|
||||
}
|
||||
|
||||
// 2170
|
||||
function evaluate_hand()
|
||||
{
|
||||
u = 0;
|
||||
for (z = n; z <= n + 4; z++) {
|
||||
ba[z] = fnb(aa[z]);
|
||||
if (z != n + 4) {
|
||||
if (Math.floor(aa[z] / 100) == Math.floor(aa[z + 1] / 100))
|
||||
u++;
|
||||
}
|
||||
}
|
||||
if (u == 4) {
|
||||
x = 11111;
|
||||
d = aa[n];
|
||||
hs = "A FLUS";
|
||||
is = "H IN";
|
||||
u = 15;
|
||||
return;
|
||||
}
|
||||
for (z = n; z <= n + 3; z++) {
|
||||
for (k = z + 1; k <= n + 4; k++) {
|
||||
if (ba[z] > ba[k]) {
|
||||
x = aa[z];
|
||||
aa[z] = aa[k];
|
||||
ba[z] = ba[k];
|
||||
aa[k] = x;
|
||||
ba[k] = aa[k] - 100 * Math.floor(aa[k] / 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
x = 0;
|
||||
for (z = n; z <= n + 3; z++) {
|
||||
if (ba[z] == ba[z + 1]) {
|
||||
x = x + 11 * Math.pow(10, z - n);
|
||||
d = aa[z];
|
||||
if (u < 11) {
|
||||
u = 11;
|
||||
hs = "A PAIR";
|
||||
is = " OF ";
|
||||
} else if (u == 11) {
|
||||
if (ba[z] == ba[z - 1]) {
|
||||
hs = "THREE";
|
||||
is = " ";
|
||||
u = 13;
|
||||
} else {
|
||||
hs = "TWO P";
|
||||
is = "AIR, ";
|
||||
u = 12;
|
||||
}
|
||||
} else if (u == 12) {
|
||||
u = 16;
|
||||
hs = "FULL H";
|
||||
is = "OUSE, ";
|
||||
} else if (ba[z] == ba[z - 1]) {
|
||||
u = 17;
|
||||
hs = "FOUR";
|
||||
is = " ";
|
||||
} else {
|
||||
u = 16;
|
||||
hs = "FULL H";
|
||||
is = "OUSE. ";
|
||||
}
|
||||
}
|
||||
}
|
||||
if (x == 0) {
|
||||
if (ba[n] + 3 == ba[n + 3]) {
|
||||
x = 1111;
|
||||
u = 10;
|
||||
}
|
||||
if (ba[n + 1] + 3 == ba[n + 4]) {
|
||||
if (u == 10) {
|
||||
u = 14;
|
||||
hs = "STRAIG";
|
||||
is = "HT";
|
||||
x = 11111;
|
||||
d = aa[n + 4];
|
||||
return;
|
||||
}
|
||||
u = 10;
|
||||
x = 11110;
|
||||
}
|
||||
}
|
||||
if (u < 10) {
|
||||
d = aa[n + 4];
|
||||
hs = "SCHMAL";
|
||||
is = "TZ, ";
|
||||
u = 9;
|
||||
x = 11000;
|
||||
i = 6;
|
||||
return;
|
||||
}
|
||||
if (u == 10) {
|
||||
if (i == 1)
|
||||
i = 6;
|
||||
return;
|
||||
}
|
||||
if (u > 12)
|
||||
return;
|
||||
if (fnb(d) > 6)
|
||||
return;
|
||||
i = 6;
|
||||
}
|
||||
|
||||
function get_prompt(question, def)
|
||||
{
|
||||
var str;
|
||||
|
||||
str = window.prompt(question, def);
|
||||
print(question + "? " + str + "\n");
|
||||
return str;
|
||||
}
|
||||
|
||||
function player_low_in_money()
|
||||
{
|
||||
print("\n");
|
||||
print("YOU CAN'T BET WITH WHAT YOU HAVEN'T GOT.\n");
|
||||
str = "N";
|
||||
if (o % 2 != 0) {
|
||||
str = get_prompt("WOULD YOU LIKE TO SELL YOUR WATCH", "YES");
|
||||
if (str.substr(0, 1) != "N") {
|
||||
if (fna(0) < 7) {
|
||||
print("I'LL GIVE YOU $75 FOR IT.\n");
|
||||
s += 75;
|
||||
} else {
|
||||
print("THAT'S A PRETTY CRUMMY WATCH - I'LL GIVE YOU $25.\n");
|
||||
s += 25;
|
||||
}
|
||||
o *= 2;
|
||||
}
|
||||
}
|
||||
if (o % 3 == 0 && str.substr(0, 1) == "N") {
|
||||
str = get_prompt("WILL YOU PART WITH THAT DIAMOND TIE TACK", "YES");
|
||||
if (str.substr(0, 1) != "N") {
|
||||
if (fna(0) < 6) {
|
||||
print("YOU ARE NOW $100 RICHER.\n");
|
||||
s += 100;
|
||||
} else {
|
||||
print("IT'S PASTE. $25.\n");
|
||||
s += 25;
|
||||
}
|
||||
o *= 3;
|
||||
}
|
||||
}
|
||||
if (str.substr(0,1) == "N") {
|
||||
print("YOUR WAD IS SHOT. SO LONG, SUCKER!\n");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function computer_low_in_money()
|
||||
{
|
||||
if (c - g - v >= 0)
|
||||
return false;
|
||||
if (g == 0) {
|
||||
v = c;
|
||||
return false;
|
||||
}
|
||||
if (c - g < 0) {
|
||||
print("I'LL SEE YOU.\n");
|
||||
k = g;
|
||||
s = s - g;
|
||||
c = c - k;
|
||||
p = p + g + k;
|
||||
return false;
|
||||
}
|
||||
js = "N";
|
||||
if (o % 2 == 0) {
|
||||
js = get_prompt("WOULD YOU LIKE TO BUY BACK YOUR WATCH FOR $50", "YES");
|
||||
if (js.substr(0, 1) != "N") {
|
||||
c += 50;
|
||||
o /= 2;
|
||||
}
|
||||
}
|
||||
if (js.substr(0, 1) == "N" && o % 3 == 0) {
|
||||
js = get_prompt("WOULD YOU LIKE TO BUY BACK YOUR TIE TACK FOR $50", "YES");
|
||||
if (js.substr(0, 1) != "N") {
|
||||
c += 50;
|
||||
o /= 3;
|
||||
}
|
||||
}
|
||||
if (js.substr(0, 1) == "N") {
|
||||
print("I'M BUSTED. CONGRATULATIONS!\n");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function ask_for_bet()
|
||||
{
|
||||
var forced;
|
||||
|
||||
if (t != Math.floor(t)) {
|
||||
if (k != 0 || g != 0 || t != 0.5) {
|
||||
print("NO SMALL CHANGE, PLEASE.\n");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (s - g - t < 0) {
|
||||
if (player_low_in_money())
|
||||
return 2;
|
||||
return 0;
|
||||
}
|
||||
if (t == 0) {
|
||||
i = 3;
|
||||
} else if (g + t < k) {
|
||||
print("IF YOU CAN'T SEE MY BET, THEN FOLD.\n");
|
||||
return 0;
|
||||
} else {
|
||||
g += t;
|
||||
if (g != k) {
|
||||
forced = false;
|
||||
if (z != 1) {
|
||||
if (g <= 3 * z)
|
||||
forced = true;
|
||||
} else {
|
||||
if (g <= 5) {
|
||||
if (z < 2) {
|
||||
v = 5;
|
||||
if (g <= 3 * z)
|
||||
forced = true;
|
||||
}
|
||||
} else {
|
||||
if (z == 1 || t > 25) {
|
||||
i = 4;
|
||||
print("I FOLD.\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (forced || z == 2) {
|
||||
v = g - k + fna(0);
|
||||
if (computer_low_in_money())
|
||||
return 2;
|
||||
print("I'LL SEE YOU, AND RAISE YOU " + v + "\n");
|
||||
k = g + v;
|
||||
return 0;
|
||||
}
|
||||
print("I'LL SEE YOU.\n");
|
||||
k = g;
|
||||
}
|
||||
}
|
||||
s -= g;
|
||||
c -= k;
|
||||
p += g + k;
|
||||
return 1;
|
||||
}
|
||||
|
||||
function check_for_win(type)
|
||||
{
|
||||
if (type == 0 && i == 3 || type == 1) {
|
||||
print("\n");
|
||||
print("I WIN.\n");
|
||||
c += p;
|
||||
} else if (type == 0 && i == 4 || type == 2) {
|
||||
print("\n");
|
||||
print("YOU WIN.\n");
|
||||
s += p;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
print("NOW I HAVE $" + c + " AND YOU HAVE $" + s + "\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
function show_hand()
|
||||
{
|
||||
print(hs + is);
|
||||
if (hs == "A FLUS") {
|
||||
k = Math.floor(k / 100);
|
||||
print("\n");
|
||||
show_suit();
|
||||
print("\n");
|
||||
} else {
|
||||
k = fnb(k);
|
||||
show_number();
|
||||
if (hs == "SCHMAL" || hs == "STRAIG")
|
||||
print(" HIGH\n");
|
||||
else
|
||||
print("'S\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(33) + "POKER\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("WELCOME TO THE CASINO. WE EACH HAVE $200.\n");
|
||||
print("I WILL OPEN THE BETTING BEFORE THE DRAW; YOU OPEN AFTER.\n");
|
||||
print("TO FOLD BET 0; TO CHECK BET .5.\n");
|
||||
print("ENOUGH TALK -- LET'S GET DOWN TO BUSINESS.\n");
|
||||
print("\n");
|
||||
o = 1;
|
||||
c = 200;
|
||||
s = 200;
|
||||
z = 0;
|
||||
while (1) {
|
||||
p = 0;
|
||||
//
|
||||
print("\n");
|
||||
if (c <= 5) {
|
||||
im_busted();
|
||||
return;
|
||||
}
|
||||
print("THE ANTE IS $5, I WILL DEAL:\n");
|
||||
print("\n");
|
||||
if (s <= 5) {
|
||||
if (player_low_in_money())
|
||||
return;
|
||||
}
|
||||
p += 10;
|
||||
s -= 5;
|
||||
c -= 5;
|
||||
for (z = 1; z <= 10; z++)
|
||||
deal_card();
|
||||
print("YOUR HAND:\n");
|
||||
n = 1;
|
||||
show_cards();
|
||||
n = 6;
|
||||
i = 2;
|
||||
evaluate_hand();
|
||||
print("\n");
|
||||
first = true;
|
||||
if (i == 6) {
|
||||
if (fna(0) > 7) {
|
||||
x = 11100;
|
||||
i = 7;
|
||||
z = 23;
|
||||
} else if (fna(0) > 7) {
|
||||
x = 11110;
|
||||
i = 7;
|
||||
z = 23;
|
||||
} else if (fna(0) < 2) {
|
||||
x = 11111;
|
||||
i = 7;
|
||||
z = 23;
|
||||
} else {
|
||||
z = 1;
|
||||
k = 0;
|
||||
print("I CHECK.\n");
|
||||
first = false;
|
||||
}
|
||||
} else {
|
||||
if (u < 13) {
|
||||
if (fna(0) < 2) {
|
||||
i = 7;
|
||||
z = 23;
|
||||
} else {
|
||||
z = 0;
|
||||
k = 0;
|
||||
print("I CHECK.\n");
|
||||
first = false;
|
||||
}
|
||||
} else if (u > 16) {
|
||||
z = 2;
|
||||
if (fna(0) < 1)
|
||||
z = 35;
|
||||
} else {
|
||||
z = 35;
|
||||
}
|
||||
}
|
||||
if (first) {
|
||||
v = z + fna(0);
|
||||
g = 0;
|
||||
if (computer_low_in_money())
|
||||
return;
|
||||
print("I'LL OPEN WITH $" + v + "\n");
|
||||
k = v;
|
||||
}
|
||||
g = 0;
|
||||
do {
|
||||
print("\nWHAT IS YOUR BET");
|
||||
t = parseFloat(await input());
|
||||
status = ask_for_bet();
|
||||
} while (status == 0) ;
|
||||
if (status == 2)
|
||||
return;
|
||||
status = check_for_win(0);
|
||||
if (status == 1) {
|
||||
while (1) {
|
||||
print("DO YOU WISH TO CONTINUE");
|
||||
hs = await input();
|
||||
if (hs == "YES") {
|
||||
status = 1;
|
||||
break;
|
||||
}
|
||||
if (hs == "NO") {
|
||||
status = 2;
|
||||
break;
|
||||
}
|
||||
print("ANSWER YES OR NO, PLEASE.\n");
|
||||
}
|
||||
}
|
||||
if (status == 2)
|
||||
return;
|
||||
if (status == 1) {
|
||||
p = 0;
|
||||
continue;
|
||||
}
|
||||
print("\n");
|
||||
print("NOW WE DRAW -- HOW MANY CARDS DO YOU WANT");
|
||||
while (1) {
|
||||
t = parseInt(await input());
|
||||
if (t != 0) {
|
||||
z = 10;
|
||||
if (t >= 4) {
|
||||
print("YOU CAN'T DRAW MORE THAN THREE CARDS.\n");
|
||||
continue;
|
||||
}
|
||||
print("WHAT ARE THEIR NUMBERS:\n");
|
||||
for (q = 1; q <= t; q++) {
|
||||
u = parseInt(await input());
|
||||
z++;
|
||||
deal_card();
|
||||
}
|
||||
print("YOUR NEW HAND:\n");
|
||||
n = 1;
|
||||
show_cards();
|
||||
}
|
||||
break;
|
||||
}
|
||||
z = 10 + t;
|
||||
for (u = 6; u <= 10; u++) {
|
||||
if (Math.floor(x / Math.pow(10, u - 6)) != 10 * Math.floor(x / Math.pow(10, u - 5)))
|
||||
break;
|
||||
z++;
|
||||
deal_card();
|
||||
}
|
||||
print("\n");
|
||||
print("I AM TAKING " + (z - 10 - t) + " CARD");
|
||||
if (z != 11 + t) {
|
||||
print("S");
|
||||
}
|
||||
print("\n");
|
||||
n = 6;
|
||||
v = i;
|
||||
i = 1;
|
||||
evaluate_hand();
|
||||
b = u;
|
||||
m = d;
|
||||
if (v == 7) {
|
||||
z = 28;
|
||||
} else if (i == 6) {
|
||||
z = 1;
|
||||
} else {
|
||||
if (u < 13) {
|
||||
z = 2;
|
||||
if (fna(0) == 6)
|
||||
z = 19;
|
||||
} else if (u < 16) {
|
||||
z = 19;
|
||||
if (fna(0) == 8)
|
||||
z = 11;
|
||||
} else {
|
||||
z = 2;
|
||||
}
|
||||
}
|
||||
k = 0;
|
||||
g = 0;
|
||||
do {
|
||||
print("\nWHAT IS YOUR BET");
|
||||
t = parseFloat(await input());
|
||||
status = ask_for_bet();
|
||||
} while (status == 0) ;
|
||||
if (status == 2)
|
||||
return;
|
||||
if (t == 0.5) {
|
||||
if (v != 7 && i == 6) {
|
||||
print("I'LL CHECK\n");
|
||||
} else {
|
||||
v = z + fna(0);
|
||||
if (computer_low_in_money())
|
||||
return;
|
||||
print("I'LL BET $" + v + "\n");
|
||||
k = v;
|
||||
do {
|
||||
print("\nWHAT IS YOUR BET");
|
||||
t = parseFloat(await input());
|
||||
status = ask_for_bet();
|
||||
} while (status == 0) ;
|
||||
if (status == 2)
|
||||
return;
|
||||
status = check_for_win(0);
|
||||
if (status == 1) {
|
||||
while (1) {
|
||||
print("DO YOU WISH TO CONTINUE");
|
||||
hs = await input();
|
||||
if (hs == "YES") {
|
||||
status = 1;
|
||||
break;
|
||||
}
|
||||
if (hs == "NO") {
|
||||
status = 2;
|
||||
break;
|
||||
}
|
||||
print("ANSWER YES OR NO, PLEASE.\n");
|
||||
}
|
||||
}
|
||||
if (status == 2)
|
||||
return;
|
||||
if (status == 1) {
|
||||
p = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
status = check_for_win(0);
|
||||
if (status == 1) {
|
||||
while (1) {
|
||||
print("DO YOU WISH TO CONTINUE");
|
||||
hs = await input();
|
||||
if (hs == "YES") {
|
||||
status = 1;
|
||||
break;
|
||||
}
|
||||
if (hs == "NO") {
|
||||
status = 2;
|
||||
break;
|
||||
}
|
||||
print("ANSWER YES OR NO, PLEASE.\n");
|
||||
}
|
||||
}
|
||||
if (status == 2)
|
||||
return;
|
||||
if (status == 1) {
|
||||
p = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("NOW WE COMPARE HANDS:\n");
|
||||
js = hs;
|
||||
ks = is;
|
||||
print("MY HAND:\n");
|
||||
n = 6;
|
||||
show_cards();
|
||||
n = 1;
|
||||
evaluate_hand();
|
||||
print("\n");
|
||||
print("YOU HAVE ");
|
||||
k = d;
|
||||
show_hand();
|
||||
hs = js;
|
||||
is = ks;
|
||||
k = m;
|
||||
print("AND I HAVE ");
|
||||
show_hand();
|
||||
status = 0;
|
||||
if (b > u) {
|
||||
status = 1;
|
||||
} else if (u > b) {
|
||||
status = 2;
|
||||
} else {
|
||||
if (hs != "A FLUS") {
|
||||
if (fnb(m) < fnb(d))
|
||||
status = 2;
|
||||
else if (fnb(m) > fnb(d))
|
||||
status = 1;
|
||||
} else {
|
||||
if (fnb(m) > fnb(d))
|
||||
status = 1;
|
||||
else if (fnb(d) > fnb(m))
|
||||
status = 2;
|
||||
}
|
||||
if (status == 0) {
|
||||
print("THE HAND IS DRAWN.\n");
|
||||
print("ALL $" + p + " REMAINS IN THE POT.\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
status = check_for_win(status);
|
||||
if (status == 1) {
|
||||
while (1) {
|
||||
print("DO YOU WISH TO CONTINUE");
|
||||
hs = await input();
|
||||
if (hs == "YES") {
|
||||
status = 1;
|
||||
break;
|
||||
}
|
||||
if (hs == "NO") {
|
||||
status = 2;
|
||||
break;
|
||||
}
|
||||
print("ANSWER YES OR NO, PLEASE.\n");
|
||||
}
|
||||
}
|
||||
if (status == 2)
|
||||
return;
|
||||
if (status == 1) {
|
||||
p = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
83 Stock Market/javascript/stockmarket.html
Normal file
9
83 Stock Market/javascript/stockmarket.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>STOCKMARKET</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="stockmarket.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
317
83 Stock Market/javascript/stockmarket.js
Normal file
317
83 Stock Market/javascript/stockmarket.js
Normal file
@@ -0,0 +1,317 @@
|
||||
// STOCKMARKET
|
||||
//
|
||||
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
|
||||
//
|
||||
|
||||
function print(str)
|
||||
{
|
||||
document.getElementById("output").appendChild(document.createTextNode(str));
|
||||
}
|
||||
|
||||
function input()
|
||||
{
|
||||
var input_element;
|
||||
var input_str;
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
input_element = document.createElement("INPUT");
|
||||
|
||||
print("? ");
|
||||
input_element.setAttribute("type", "text");
|
||||
input_element.setAttribute("length", "50");
|
||||
document.getElementById("output").appendChild(input_element);
|
||||
input_element.focus();
|
||||
input_str = undefined;
|
||||
input_element.addEventListener("keydown", function (event) {
|
||||
if (event.keyCode == 13) {
|
||||
input_str = input_element.value;
|
||||
document.getElementById("output").removeChild(input_element);
|
||||
print(input_str);
|
||||
print("\n");
|
||||
resolve(input_str);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tab(space)
|
||||
{
|
||||
var str = "";
|
||||
while (space-- > 0)
|
||||
str += " ";
|
||||
return str;
|
||||
}
|
||||
|
||||
var sa = [];
|
||||
var pa = [];
|
||||
var za = [];
|
||||
var ca = [];
|
||||
var i1;
|
||||
var n1;
|
||||
var e1;
|
||||
var i2;
|
||||
var n2;
|
||||
var e2;
|
||||
var x1;
|
||||
var w3;
|
||||
var t8;
|
||||
var a;
|
||||
var s4;
|
||||
|
||||
// New stock values - subroutine
|
||||
function randomize_initial()
|
||||
{
|
||||
// RANDOMLY PRODUCE NEW STOCK VALUES BASED ON PREVIOUS
|
||||
// DAY'S VALUES
|
||||
// N1,N2 ARE RANDOM NUMBERS OF DAYS WHICH RESPECTIVELY
|
||||
// DETERMINE WHEN STOCK I1 WILL INCREASE 10 PTS. AND STOCK
|
||||
// I2 WILL DECREASE 10 PTS.
|
||||
// IF N1 DAYS HAVE PASSED, PICK AN I1, SET E1, DETERMINE NEW N1
|
||||
if (n1 <= 0) {
|
||||
i1 = Math.floor(4.99 * Math.random() + 1);
|
||||
n1 = Math.floor(4.99 * Math.random() + 1);
|
||||
e1 = 1;
|
||||
}
|
||||
// IF N2 DAYS HAVE PASSED, PICK AN I2, SET E2, DETERMINE NEW N2
|
||||
if (n2 <= 0) {
|
||||
i2 = Math.floor(4.99 * Math.random() + 1);
|
||||
n2 = Math.floor(4.99 * Math.random() + 1);
|
||||
e2 = 1;
|
||||
}
|
||||
// DEDUCT ONE DAY FROM N1 AND N2
|
||||
n1--;
|
||||
n2--;
|
||||
// LOOP THROUGH ALL STOCKS
|
||||
for (i = 1; i <= 5; i++) {
|
||||
x1 = Math.random();
|
||||
if (x1 < 0.25) {
|
||||
x1 = 0.25;
|
||||
} else if (x1 < 0.5) {
|
||||
x1 = 0.5;
|
||||
} else if (x1 < 0.75) {
|
||||
x1 = 0.75;
|
||||
} else {
|
||||
x1 = 0.0;
|
||||
}
|
||||
// BIG CHANGE CONSTANT:W3 (SET TO ZERO INITIALLY)
|
||||
w3 = 0;
|
||||
if (e1 >= 1 && Math.floor(i1 + 0.5) == Math.floor(i + 0.5)) {
|
||||
// ADD 10 PTS. TO THIS STOCK; RESET E1
|
||||
w3 = 10;
|
||||
e1 = 0;
|
||||
}
|
||||
if (e2 >= 1 && Math.floor(i2 + 0.5) == Math.floor(i + 0.5)) {
|
||||
// SUBTRACT 10 PTS. FROM THIS STOCK; RESET E2
|
||||
w3 -= 10;
|
||||
e2 = 0;
|
||||
}
|
||||
// C(I) IS CHANGE IN STOCK VALUE
|
||||
ca[i] = Math.floor(a * sa[i]) + x1 + Math.floor(3 - 6 * Math.random() + 0.5) + w3;
|
||||
ca[i] = Math.floor(100 * ca[i] + 0.5) / 100;
|
||||
sa[i] += ca[i];
|
||||
if (sa[i] <= 0) {
|
||||
ca[i] = 0;
|
||||
sa[i] = 0;
|
||||
} else {
|
||||
sa[i] = Math.floor(100 * sa[i] + 0.5) / 100;
|
||||
}
|
||||
}
|
||||
// AFTER T8 DAYS RANDOMLY CHANGE TREND SIGN AND SLOPE
|
||||
if (--t8 < 1) {
|
||||
// RANDOMLY CHANGE TREND SIGN AND SLOPE (A), AND DURATION
|
||||
// OF TREND (T8)
|
||||
t8 = Math.floor(4.99 * Math.random() + 1);
|
||||
a = Math.floor((Math.random() / 10) * 100 + 0.5) / 100;
|
||||
s4 = Math.random();
|
||||
if (s4 > 0.5)
|
||||
a = -a;
|
||||
}
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(30) + "STOCK MARKET\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
// STOCK MARKET SIMULATION -STOCK-
|
||||
// REVISED 8/18/70 (D. PESSEL, L. BRAUN, C. LOSIK)
|
||||
// IMP VRBLS: A-MRKT TRND SLP; B5-BRKRGE FEE; C-TTL CSH ASSTS;
|
||||
// C5-TTL CSH ASSTS (TEMP); C(I)-CHNG IN STK VAL; D-TTL ASSTS;
|
||||
// E1,E2-LRG CHNG MISC; I-STCK #; I1,I2-STCKS W LRG CHNG;
|
||||
// N1,N2-LRG CHNG DAY CNTS; P5-TTL DAYS PRCHSS; P(I)-PRTFL CNTNTS;
|
||||
// Q9-NEW CYCL?; S4-SGN OF A; S5-TTL DYS SLS; S(I)-VALUE/SHR;
|
||||
// T-TTL STCK ASSTS; T5-TTL VAL OF TRNSCTNS;
|
||||
// W3-LRG CHNG; X1-SMLL CHNG(<$1); Z4,Z5,Z6-NYSE AVE.; Z(I)-TRNSCT
|
||||
// SLOPE OF MARKET TREND:A (SAME FOR ALL STOCKS)
|
||||
x = 1;
|
||||
a = Math.floor(Math.random() / 10 * 100 + 0.5) / 100;
|
||||
t5 = 0;
|
||||
x9 = 0;
|
||||
n1 = 0;
|
||||
n2 = 0;
|
||||
e1 = 0;
|
||||
e2 = 0;
|
||||
// INTRODUCTION
|
||||
print("DO YOU WANT THE INSTRUCTIONS (YES-TYPE 1, NO-TYPE 0)");
|
||||
z9 = parseInt(await input());
|
||||
print("\n");
|
||||
print("\n");
|
||||
if (z9 >= 1) {
|
||||
print("THIS PROGRAM PLAYS THE STOCK MARKET. YOU WILL BE GIVEN\n");
|
||||
print("$10,000 AND MAY BUY OR SELL STOCKS. THE STOCK PRICES WILL\n");
|
||||
print("BE GENERATED RANDOMLY AND THEREFORE THIS MODEL DOES NOT\n");
|
||||
print("REPRESENT EXACTLY WHAT HAPPENS ON THE EXCHANGE. A TABLE\n");
|
||||
print("OF AVAILABLE STOCKS, THEIR PRICES, AND THE NUMBER OF SHARES\n");
|
||||
print("IN YOUR PORTFOLIO WILL BE PRINTED. FOLLOWING THIS, THE\n");
|
||||
print("INITIALS OF EACH STOCK WILL BE PRINTED WITH A QUESTION\n");
|
||||
print("MARK. HERE YOU INDICATE A TRANSACTION. TO BUY A STOCK\n");
|
||||
print("TYPE +NNN, TO SELL A STOCK TYPE -NNN, WHERE NNN IS THE\n");
|
||||
print("NUMBER OF SHARES. A BROKERAGE FEE OF 1% WILL BE CHARGED\n");
|
||||
print("ON ALL TRANSACTIONS. NOTE THAT IF A STOCK'S VALUE DROPS\n");
|
||||
print("TO ZERO IT MAY REBOUND TO A POSITIVE VALUE AGAIN. YOU\n");
|
||||
print("HAVE $10,000 TO INVEST. USE INTEGERS FOR ALL YOUR INPUTS.\n");
|
||||
print("(NOTE: TO GET A 'FEEL' FOR THE MARKET RUN FOR AT LEAST\n");
|
||||
print("10 DAYS)\n");
|
||||
print("-----GOOD LUCK!-----\n");
|
||||
}
|
||||
// GENERATION OF STOCK TABLE: INPUT REQUESTS
|
||||
// INITIAL STOCK VALUES
|
||||
sa[1] = 100;
|
||||
sa[2] = 85;
|
||||
sa[3] = 150;
|
||||
sa[4] = 140;
|
||||
sa[5] = 110;
|
||||
// INITIAL T8 - # DAYS FOR FIRST TREND SLOPE (A)
|
||||
t8 = Math.floor(4.99 * Math.random() + 1);
|
||||
// RANDOMIZE SIGN OF FIRST TREND SLOPE (A)
|
||||
if (Math.random() <= 0.5)
|
||||
a -= a;
|
||||
// RANDOMIZE INITIAL VALUES
|
||||
randomize_initial();
|
||||
// INITIAL PORTFOLIO CONTENTS
|
||||
for (i = 1; i <= 5; i++) {
|
||||
pa[i] = 0;
|
||||
za[i] = 0;
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
// INITIALIZE CASH ASSETS:C
|
||||
c = 10000;
|
||||
z5 = 0;
|
||||
// PRINT INITIAL PORTFOLIO
|
||||
print("STOCK\t \t\t\tINITIALS\tPRICE/SHARE\n");
|
||||
print("INT. BALLISTIC MISSILES\t\t IBM\t\t" + sa[1] + "\n");
|
||||
print("RED CROSS OF AMERICA\t\t RCA\t\t" + sa[2] + "\n");
|
||||
print("LICHTENSTEIN, BUMRAP & JOKE\t LBJ\t\t" + sa[3] + "\n");
|
||||
print("AMERICAN BANKRUPT CO.\t\t ABC\t\t" + sa[4] + "\n");
|
||||
print("CENSURED BOOKS STORE\t\t CBS\t\t" + sa[5] + "\n");
|
||||
while (1) {
|
||||
print("\n");
|
||||
// NYSE AVERAGE:Z5; TEMP. VALUE:Z4; NET CHANGE:Z6
|
||||
z4 = z5;
|
||||
z5 = 0;
|
||||
t = 0;
|
||||
for (i = 1; i <= 5; i++) {
|
||||
z5 += sa[i];
|
||||
t += sa[i] * pa[i];
|
||||
}
|
||||
z5 = Math.floor(100 * (z5 / 5) + 0.5) / 100;
|
||||
z6 = Math.floor((z5 - z4) * 100 + 0.5) / 100;
|
||||
// TOTAL ASSETS:D
|
||||
d = t + c;
|
||||
if (x9 <= 0) {
|
||||
print("NEW YORK STOCK EXCHANGE AVERAGE: " + z5 + "\n");
|
||||
} else {
|
||||
print("NEW YORK STOCK EXCHANGE AVERAGE: " + z5 + " NET CHANGE " + z6 + "\n");
|
||||
}
|
||||
print("\n");
|
||||
t = Math.floor(100 * t + 0.5) / 100;
|
||||
print("TOTAL STOCK ASSETS ARE $" + t + "\n");
|
||||
c = Math.floor(100 * c + 0.5) / 100;
|
||||
print("TOTAL CASH ASSETS ARE $" + c + "\n");
|
||||
d = Math.floor(100 * d + 0.5) / 100;
|
||||
print("TOTAL ASSETS ARE $" + d + "\n");
|
||||
print("\n");
|
||||
if (x9 != 0) {
|
||||
print("DO YOU WISH TO CONTINUE (YES-TYPE 1, NO-TYPE 0)");
|
||||
q9 = parseInt(await input());
|
||||
if (q9 < 1) {
|
||||
print("HOPE YOU HAD FUN!!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// INPUT TRANSACTIONS
|
||||
while (1) {
|
||||
print("WHAT IS YOUR TRANSACTION IN\n");
|
||||
print("IBM");
|
||||
za[1] = parseInt(await input());
|
||||
print("RCA");
|
||||
za[2] = parseInt(await input());
|
||||
print("LBJ");
|
||||
za[3] = parseInt(await input());
|
||||
print("ABC");
|
||||
za[4] = parseInt(await input());
|
||||
print("CBS");
|
||||
za[5] = parseInt(await input());
|
||||
print("\n");
|
||||
// TOTAL DAY'S PURCHASES IN $:P5
|
||||
p5 = 0;
|
||||
// TOTAL DAY'S SALES IN $:S5
|
||||
s5 = 0;
|
||||
for (i = 1; i <= 5; i++) {
|
||||
za[i] = Math.floor(za[i] + 0.5);
|
||||
if (za[i] > 0) {
|
||||
p5 += za[i] * sa[i];
|
||||
} else {
|
||||
s5 -= za[i] * sa[i];
|
||||
if (-za[i] > pa[i]) {
|
||||
print("YOU HAVE OVERSOLD A STOCK; TRY AGAIN.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i <= 5)
|
||||
contine;
|
||||
// TOTAL VALUE OF TRANSACTIONS:T5
|
||||
t5 = p5 + s5;
|
||||
// BROKERAGE FEE:B5
|
||||
b5 = Math.floor(0.01 * t5 * 100 + 0.5) / 100;
|
||||
// CASH ASSETS=OLD CASH ASSETS-TOTAL PURCHASES
|
||||
// -BROKERAGE FEES+TOTAL SALES:C5
|
||||
c5 = c - p5 - b5 + s5;
|
||||
if (c5 < 0) {
|
||||
print("YOU HAVE USED $" + (-c5) + " MORE THAN YOU HAVE.\n");
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
c = c5;
|
||||
// CALCULATE NEW PORTFOLIO
|
||||
for (i = 1; i <= 5; i++) {
|
||||
pa[i] += za[i];
|
||||
}
|
||||
// CALCULATE NEW STOCK VALUES
|
||||
randomize_initial();
|
||||
// PRINT PORTFOLIO
|
||||
// BELL RINGING-DIFFERENT ON MANY COMPUTERS
|
||||
print("\n");
|
||||
print("********** END OF DAY'S TRADING **********\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
if (x9 >= 1) ;
|
||||
print("STOCK\tPRICE/SHARE\tHOLDINGS\tVALUE\tNET PRICE CHANGE\n");
|
||||
print("IBM\t" + sa[1] + "\t\t" + pa[1] + "\t\t" + sa[1] * pa[1] + "\t" + ca[1] + "\n");
|
||||
print("RCA\t" + sa[2] + "\t\t" + pa[2] + "\t\t" + sa[2] * pa[2] + "\t" + ca[2] + "\n");
|
||||
print("LBJ\t" + sa[3] + "\t\t" + pa[3] + "\t\t" + sa[3] * pa[3] + "\t" + ca[3] + "\n");
|
||||
print("ABC\t" + sa[4] + "\t\t" + pa[4] + "\t\t" + sa[4] * pa[4] + "\t" + ca[4] + "\n");
|
||||
print("CBS\t" + sa[5] + "\t\t" + pa[5] + "\t\t" + sa[5] * pa[5] + "\t" + ca[5] + "\n");
|
||||
x9 = 1;
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user