mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-24 20:10:15 -08:00
show terminal header with link to sources
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>ROCK, SCISSORS, PAPER</title>
|
||||
<link rel="stylesheet" href="../../00_Utilities/javascript/style_terminal.css" />
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output"></pre>
|
||||
<script src="rockscissors.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,107 +0,0 @@
|
||||
// ROCK, SCISSORS, PAPER
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Main control section
|
||||
async function main()
|
||||
{
|
||||
print(tab(21) + "GAME OF ROCK, SCISSORS, PAPER\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("HOW MANY GAMES");
|
||||
q = parseInt(await input());
|
||||
if (q >= 11)
|
||||
print("SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY.\n");
|
||||
else
|
||||
break;
|
||||
}
|
||||
h = 0; // Human
|
||||
c = 0; // Computer
|
||||
for (g = 1; g <= q; g++ ) {
|
||||
print("\n");
|
||||
print("GAME NUMBER " + g + "\n");
|
||||
x = Math.floor(Math.random() * 3 + 1);
|
||||
while (1) {
|
||||
print("3=ROCK...2=SCISSORS...1=PAPER\n");
|
||||
print("1...2...3...WHAT'S YOUR CHOICE");
|
||||
k = parseInt(await input());
|
||||
if (k != 1 && k != 2 && k != 3)
|
||||
print("INVALID.\n");
|
||||
else
|
||||
break;
|
||||
}
|
||||
print("THIS IS MY CHOICE...");
|
||||
switch (x) {
|
||||
case 1:
|
||||
print("...PAPER\n");
|
||||
break;
|
||||
case 2:
|
||||
print("...SCISSORS\n");
|
||||
break;
|
||||
case 3:
|
||||
print("...ROCK\n");
|
||||
break;
|
||||
}
|
||||
if (x == k) {
|
||||
print("TIE GAME. NO WINNER.\n");
|
||||
} else if ((x > k && (k != 1 || x != 3)) || (x == 1 && k == 3)) {
|
||||
print("WOW! I WIN!!!\n");
|
||||
c++;
|
||||
} else {
|
||||
print("YOU WIN!!!\n");
|
||||
h++;
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("HERE IS THE FINAL GAME SCORE:\n");
|
||||
print("I HAVE WON " + c + " GAME(S).\n");
|
||||
print("YOU HAVE WON " + h + " GAME(S).\n");
|
||||
print("AND " + (q - (c + h)) + " GAME(S) ENDED IN A TIE.\n");
|
||||
print("\n");
|
||||
print("THANKS FOR PLAYING!!\n");
|
||||
}
|
||||
|
||||
main();
|
||||
117
74_Rock_Scissors_Paper/javascript/rockscissors.mjs
Normal file
117
74_Rock_Scissors_Paper/javascript/rockscissors.mjs
Normal file
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env node
|
||||
// ROCK, SCISSORS, PAPER
|
||||
//
|
||||
// Converted from BASIC to Javascript by Alexander Wunschik (mojoaxel)
|
||||
|
||||
import { println, tab, input } from '../../00_Common/javascript/common.mjs';
|
||||
|
||||
let userWins = 0;
|
||||
let computerWins = 0;
|
||||
let ties = 0;
|
||||
|
||||
// 30 INPUT "HOW MANY GAMES";Q
|
||||
// 40 IF Q<11 THEN 60
|
||||
// 50 PRINT "SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY.": GOTO 30
|
||||
// 60 FOR G=1 TO Q
|
||||
async function getGameCount() {
|
||||
let gameCount = await input("HOW MANY GAMES");
|
||||
if (gameCount > 10) {
|
||||
println("SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY.");
|
||||
return await getGameCount();
|
||||
}
|
||||
return gameCount;
|
||||
}
|
||||
|
||||
// #90 PRINT "3=ROCK...2=SCISSORS...1=PAPER"
|
||||
// #100 INPUT "1...2...3...WHAT'S YOUR CHOICE";K
|
||||
// #110 IF (K-1)*(K-2)*(K-3)<>0 THEN PRINT "INVALID.": GOTO 90
|
||||
async function getUserInput() {
|
||||
println("3=ROCK...2=SCISSORS...1=PAPER");
|
||||
const userChoice = await input("1...2...3...WHAT'S YOUR CHOICE");
|
||||
if (userChoice < 1 || userChoice > 3) {
|
||||
println("INVALID.");
|
||||
return await getUserInput();
|
||||
}
|
||||
return userChoice;
|
||||
}
|
||||
|
||||
async function game() {
|
||||
// 10 PRINT TAB(21);"GAME OF ROCK, SCISSORS, PAPER"
|
||||
// 20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
// 25 PRINT:PRINT:PRINT
|
||||
println(tab(21), 'GAME OF ROCK, SCISSORS, PAPER');
|
||||
println(tab(15), 'CREATIVE COMPUTING MORRISTOWN, NEW JERSEY');
|
||||
println('\n\n');
|
||||
|
||||
let gameCount = await getGameCount();
|
||||
|
||||
async function playGame(gameNumber) {
|
||||
// 70 PRINT: PRINT "GAME NUMBER";G
|
||||
println("\nGAME NUMBER ", gameNumber);
|
||||
|
||||
const ROCK = 3;
|
||||
const SCISSORS = 2;
|
||||
const PAPER = 1;
|
||||
|
||||
const usersChoice = await getUserInput();
|
||||
|
||||
// 80 X=INT(RND(1)*3+1)
|
||||
const computersChoice = Math.floor(Math.random()*3) + 1;
|
||||
|
||||
// 120 PRINT "THIS IS MY CHOICE..."
|
||||
// 130 ON X GOTO 140,150,160
|
||||
// 140 PRINT "...PAPER": GOTO 170
|
||||
// 150 PRINT "...SCISSORS": GOTO 170
|
||||
// 160 PRINT "...ROCK"
|
||||
println("THIS IS MY CHOICE...",
|
||||
computersChoice === PAPER ? "...PAPER" :
|
||||
computersChoice === SCISSORS ? "...SCISSORS" :
|
||||
"...ROCK");
|
||||
|
||||
|
||||
// 170 IF X=K THEN 250
|
||||
// 180 IF X>K THEN 230
|
||||
// 190 IF X=1 THEN 210
|
||||
// 200 PRINT "YOU WIN!!!":H=H+1: GOTO 260
|
||||
// 210 IF K<>3 THEN 200
|
||||
// 220 PRINT "WOW! I WIN!!!":C=C+1:GOTO 260
|
||||
// 230 IF K<>1 OR X<>3 THEN 220
|
||||
// 240 GOTO 200
|
||||
// 250 PRINT "TIE GAME. NO WINNER."
|
||||
if (computersChoice == usersChoice) {
|
||||
println("TIE GAME. NO WINNER.");
|
||||
ties++;
|
||||
} else if (
|
||||
(computersChoice == ROCK && usersChoice == SCISSORS) ||
|
||||
(computersChoice == PAPER && usersChoice == ROCK) ||
|
||||
(computersChoice == SCISSORS && usersChoice == PAPER)
|
||||
) {
|
||||
println("WOW! I WIN!!!");
|
||||
computerWins++;
|
||||
} else {
|
||||
println("YOU WIN!!!");
|
||||
userWins++;
|
||||
}
|
||||
}
|
||||
|
||||
for (let gameNumber = 1; gameNumber <= gameCount; gameNumber++) {
|
||||
await playGame(gameNumber);
|
||||
// 260 NEXT G
|
||||
}
|
||||
|
||||
// 270 PRINT: PRINT "HERE IS THE FINAL GAME SCORE:"
|
||||
// 280 PRINT "I HAVE WON";C;"GAME(S)."
|
||||
// 290 PRINT "YOU HAVE WON";H;"GAME(S)."
|
||||
// 300 PRINT "AND";Q-(C+H20);"GAME(S) ENDED IN A TIE."
|
||||
println("\nHERE IS THE FINAL GAME SCORE:");
|
||||
println(`I HAVE WON ${computerWins} GAME(S).`);
|
||||
println(`YOU HAVE WON ${userWins} GAME(S).`);
|
||||
println(`AND ${ties} GAME(S) ENDED IN A TIE.`);
|
||||
|
||||
// 310 PRINT: PRINT "THANKS FOR PLAYING!!"
|
||||
println("\nTHANKS FOR PLAYING!!");
|
||||
|
||||
// 320 END
|
||||
process.exit(0);
|
||||
}
|
||||
game();
|
||||
Reference in New Issue
Block a user