mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-28 13:46:06 -08:00
Merge pull request #1 from coding-horror/main
Sync upstream with my fork
This commit is contained in:
59
18 Bullseye/perl/bullseye.pl
Normal file
59
18 Bullseye/perl/bullseye.pl
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
|
||||
|
||||
print ' 'x 32 . "BULLSEYE\n";
|
||||
print ' 'x 15 . "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n";
|
||||
print "\n\n\n";
|
||||
|
||||
print "IN THIS GAME, UP TO 20 PLAYERS THROW DARTS AT A TARGET\n";
|
||||
print "WITH 10, 20, 30, AND 40 POINT ZONES. THE OBJECTIVE IS\n";
|
||||
print "TO GET 200 POINTS.\n\n";
|
||||
|
||||
print "THROW\t\tDESCRIPTION\t\tPROBABLE SCORE\n";
|
||||
print" 1\t\tFAST OVERARM\t\tBULLSEYE OR COMPLETE MISS\n";
|
||||
print" 2\t\tCONTROLLED OVERARM\t10, 20 OR 30 POINTS\n";
|
||||
print" 3\t\tUNDERARM\t\tANYTHING\n\n";
|
||||
|
||||
my @A; my $M=0; my $R=0; my @S; my @W; for (my $I=1; $I<=20; $I++) { $S[$I]= 0; }
|
||||
print "HOW MANY PLAYERS? "; chomp(my $N = <STDIN>); print "\n";
|
||||
for (my $I=1; $I<=$N; $I++) {
|
||||
print "NAME OF PLAYER #$I? "; chomp($A[$I] = <STDIN>);
|
||||
}
|
||||
|
||||
do {
|
||||
$R= $R+1; print "\n"; print "ROUND $R---------\n";
|
||||
for (my $I=1; $I<=$N; $I++) {
|
||||
my $Flag=0;
|
||||
my $T;
|
||||
do {
|
||||
print "\n"; print "$A[$I]'S THROW? "; chomp($T = <STDIN>);
|
||||
if ($T<1 || $T>3) { print "INPUT 1, 2, OR 3!\n"; $Flag=0; }
|
||||
else { $Flag=1; }
|
||||
} until ($Flag==1);
|
||||
|
||||
my $P1; my $P2; my $P3; my $P4;
|
||||
if ($T==1) { $P1=.65; $P2=.55; $P3=.5; $P4=.5; }
|
||||
if ($T==2) { $P1=.99; $P2=.77; $P3=.43; $P4=.01; }
|
||||
if ($T==3) { $P1=.95; $P2=.75; $P3=.45; $P4=.05; }
|
||||
|
||||
my $B=0;
|
||||
my $U= rand(1);
|
||||
if ($U>= $P1) { print "BULLSEYE!! 40 POINTS!\n"; $B=40; }
|
||||
elsif ($U>= $P2) { print "30-POINT ZONE!\n"; $B=30; }
|
||||
elsif ($U>= $P3) { print "20-POINT ZONE\n"; $B=20; }
|
||||
elsif ($U>= $P4) { print "WHEW! 10 POINTS.\n"; $B=10; }
|
||||
else { print "MISSED THE TARGET! TOO BAD.\n"; $B=0; }
|
||||
$S[$I]= $S[$I]+$B; print "TOTAL SCORE =$S[$I]";
|
||||
}
|
||||
|
||||
for (my $I=1; $I<=$N; $I++) {
|
||||
if ($S[$I]>=200) { $M= $M+1; $W[$M]= $I; }
|
||||
}
|
||||
} until ($M!=0);
|
||||
|
||||
print "\n"; print "WE HAVE A WINNER!!\n\n";
|
||||
for (my $I=1; $I<=$M; $I++) { print $A[$W[$I]]." SCORED ".$S[$W[$I]]." POINTS.\n"; }
|
||||
print "\n"; print "THANKS FOR THE GAME.\n"; exit;
|
||||
|
||||
|
||||
8
43 Hammurabi/csharp/Game.csproj
Normal file
8
43 Hammurabi/csharp/Game.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
25
43 Hammurabi/csharp/Hammurabi.sln
Normal file
25
43 Hammurabi/csharp/Hammurabi.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.31129.286
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Game", "Game.csproj", "{20599300-7C6E-48A2-AB24-EC7CCF224A5C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{20599300-7C6E-48A2-AB24-EC7CCF224A5C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {89DBE213-C0F0-4ABA-BB2D-5D9AAED41FF6}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
37
43 Hammurabi/csharp/src/ActionResult.cs
Normal file
37
43 Hammurabi/csharp/src/ActionResult.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace Hammurabi
|
||||
{
|
||||
/// <summary>
|
||||
/// Enumerates the different possible outcomes of attempting the various
|
||||
/// actions in the game.
|
||||
/// </summary>
|
||||
public enum ActionResult
|
||||
{
|
||||
/// <summary>
|
||||
/// The action was a success.
|
||||
/// </summary>
|
||||
Success,
|
||||
|
||||
/// <summary>
|
||||
/// The action could not be completed because the city does not have
|
||||
/// enough bushels of grain.
|
||||
/// </summary>
|
||||
InsufficientStores,
|
||||
|
||||
/// <summary>
|
||||
/// The action could not be completed because the city does not have
|
||||
/// sufficient acreage.
|
||||
/// </summary>
|
||||
InsufficientLand,
|
||||
|
||||
/// <summary>
|
||||
/// The action could not be completed because the city does not have
|
||||
/// sufficient population.
|
||||
/// </summary>
|
||||
InsufficientPopulation,
|
||||
|
||||
/// <summary>
|
||||
/// The requested action offended the city steward.
|
||||
/// </summary>
|
||||
Offense
|
||||
}
|
||||
}
|
||||
65
43 Hammurabi/csharp/src/Controller.cs
Normal file
65
43 Hammurabi/csharp/src/Controller.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
|
||||
namespace Hammurabi
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides methods for reading input from the user.
|
||||
/// </summary>
|
||||
public static class Controller
|
||||
{
|
||||
/// <summary>
|
||||
/// Continuously prompts the user to enter a number until he or she
|
||||
/// enters a valid number and updates the game state.
|
||||
/// </summary>
|
||||
/// <param name="state">
|
||||
/// The current game state.
|
||||
/// </param>
|
||||
/// <param name="prompt">
|
||||
/// Action that will display the prompt to the user.
|
||||
/// </param>
|
||||
/// <param name="rule">
|
||||
/// The rule to invoke once input is retrieved.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The updated game state.
|
||||
/// </returns>
|
||||
public static GameState TryUntilSuccess(
|
||||
GameState state,
|
||||
Action prompt,
|
||||
Func<GameState, int, (GameState newState, ActionResult result)> rule)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
prompt();
|
||||
|
||||
if (!Int32.TryParse(Console.ReadLine(), out var amount))
|
||||
{
|
||||
View.ShowInvalidNumber();
|
||||
}
|
||||
else
|
||||
{
|
||||
var (newState, result) = rule(state, amount);
|
||||
|
||||
switch (result)
|
||||
{
|
||||
case ActionResult.InsufficientLand:
|
||||
View.ShowInsufficientLand(state);
|
||||
break;
|
||||
case ActionResult.InsufficientPopulation:
|
||||
View.ShowInsufficientPopulation(state);
|
||||
break;
|
||||
case ActionResult.InsufficientStores:
|
||||
View.ShowInsufficientStores(state);
|
||||
break;
|
||||
case ActionResult.Offense:
|
||||
// Not sure why we have to blow up the game here...
|
||||
// Maybe this made sense in the 70's.
|
||||
throw new GreatOffence();
|
||||
default:
|
||||
return newState;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
45
43 Hammurabi/csharp/src/GameResult.cs
Normal file
45
43 Hammurabi/csharp/src/GameResult.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace Hammurabi
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores the final game result.
|
||||
/// </summary>
|
||||
public record GameResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the player's performance rating.
|
||||
/// </summary>
|
||||
public PerformanceRating Rating { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of acres in the city per person.
|
||||
/// </summary>
|
||||
public int AcresPerPerson { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of people who starved the final year in office.
|
||||
/// </summary>
|
||||
public int FinalStarvation { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of people who starved.
|
||||
/// </summary>
|
||||
public int TotalStarvation { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the average starvation rate per year (as a percentage
|
||||
/// of population).
|
||||
/// </summary>
|
||||
public int AverageStarvationRate { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of people who want to assassinate the player.
|
||||
/// </summary>
|
||||
public int Assassins { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a flag indicating whether the player was impeached for
|
||||
/// starving too many people.
|
||||
/// </summary>
|
||||
public bool WasImpeached { get; init; }
|
||||
}
|
||||
}
|
||||
73
43 Hammurabi/csharp/src/GameState.cs
Normal file
73
43 Hammurabi/csharp/src/GameState.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
namespace Hammurabi
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores the state of the game.
|
||||
/// </summary>
|
||||
public record GameState
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the current game year.
|
||||
/// </summary>
|
||||
public int Year { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the city's population.
|
||||
/// </summary>
|
||||
public int Population { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the population increase this year.
|
||||
/// </summary>
|
||||
public int PopulationIncrease { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of people who starved.
|
||||
/// </summary>
|
||||
public int Starvation { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the city's size in acres.
|
||||
/// </summary>
|
||||
public int Acres { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the price for an acre of land (in bushels).
|
||||
/// </summary>
|
||||
public int LandPrice { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of bushels of grain in the city stores.
|
||||
/// </summary>
|
||||
public int Stores { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the amount of food distributed to the people.
|
||||
/// </summary>
|
||||
public int FoodDistributed { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of acres that were planted.
|
||||
/// </summary>
|
||||
public int AcresPlanted { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of bushels produced per acre.
|
||||
/// </summary>
|
||||
public int Productivity { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the amount of food lost to rats.
|
||||
/// </summary>
|
||||
public int Spoilage { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a flag indicating whether the current year is a plague year.
|
||||
/// </summary>
|
||||
public bool IsPlagueYear { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a flag indicating whether the player has been impeached.
|
||||
/// </summary>
|
||||
public bool IsImpeached { get; init; }
|
||||
}
|
||||
}
|
||||
12
43 Hammurabi/csharp/src/GreatOffence.cs
Normal file
12
43 Hammurabi/csharp/src/GreatOffence.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Hammurabi
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that the game cannot continue due to the player's extreme
|
||||
/// incompetance and/or unserious attitude!
|
||||
/// </summary>
|
||||
public class GreatOffence : InvalidOperationException
|
||||
{
|
||||
}
|
||||
}
|
||||
14
43 Hammurabi/csharp/src/PerformanceRating.cs
Normal file
14
43 Hammurabi/csharp/src/PerformanceRating.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Hammurabi
|
||||
{
|
||||
/// <summary>
|
||||
/// Enumerates the different performance ratings that the player can
|
||||
/// achieve.
|
||||
/// </summary>
|
||||
public enum PerformanceRating
|
||||
{
|
||||
Disgraceful,
|
||||
Bad,
|
||||
Ok,
|
||||
Terrific
|
||||
}
|
||||
}
|
||||
51
43 Hammurabi/csharp/src/Program.cs
Normal file
51
43 Hammurabi/csharp/src/Program.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Hammurabi
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public const int GameLength = 10;
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var random = new Random ((int) (DateTime.UtcNow.Ticks / 10000)) ;
|
||||
var state = Rules.BeginGame();
|
||||
var history = ImmutableList<GameState>.Empty;
|
||||
|
||||
View.ShowBanner();
|
||||
|
||||
try
|
||||
{
|
||||
while (state.Year <= GameLength && !state.IsImpeached)
|
||||
{
|
||||
state = Rules.BeginTurn(state, random);
|
||||
View.ShowCitySummary(state);
|
||||
|
||||
View.ShowLandPrice(state);
|
||||
var newState = Controller.TryUntilSuccess(state, View.PromptBuyLand, Rules.BuyLand);
|
||||
state = newState.Acres != state.Acres ?
|
||||
newState : Controller.TryUntilSuccess(state, View.PromptSellLand, Rules.SellLand);
|
||||
|
||||
View.ShowSeparator();
|
||||
state = Controller.TryUntilSuccess(state, View.PromptFeedPeople, Rules.FeedPeople);
|
||||
|
||||
View.ShowSeparator();
|
||||
state = Controller.TryUntilSuccess(state, View.PromptPlantCrops, Rules.PlantCrops);
|
||||
|
||||
state = Rules.EndTurn(state, random);
|
||||
history = history.Add(state);
|
||||
}
|
||||
|
||||
var result = Rules.GetGameResult(history, random);
|
||||
View.ShowGameResult(result);
|
||||
}
|
||||
catch (GreatOffence)
|
||||
{
|
||||
View.ShowGreatOffence();
|
||||
}
|
||||
|
||||
View.ShowFarewell();
|
||||
}
|
||||
}
|
||||
}
|
||||
207
43 Hammurabi/csharp/src/Rules.cs
Normal file
207
43 Hammurabi/csharp/src/Rules.cs
Normal file
@@ -0,0 +1,207 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hammurabi
|
||||
{
|
||||
public static class Rules
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates the initial state for a new game.
|
||||
/// </summary>
|
||||
public static GameState BeginGame() =>
|
||||
new GameState
|
||||
{
|
||||
Year = 1,
|
||||
Population = 95,
|
||||
PopulationIncrease = 5,
|
||||
Starvation = 0,
|
||||
Acres = 1000,
|
||||
Stores = 0,
|
||||
AcresPlanted = 1000,
|
||||
Productivity = 3,
|
||||
Spoilage = 200,
|
||||
IsPlagueYear = false,
|
||||
IsImpeached = false
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Updates the game state to start a new turn.
|
||||
/// </summary>
|
||||
public static GameState BeginTurn(GameState state, Random random) =>
|
||||
state with
|
||||
{
|
||||
Population = (state.Population + state.PopulationIncrease - state.Starvation) / (state.IsPlagueYear ? 2 : 1),
|
||||
LandPrice = random.Next(10) + 17,
|
||||
Stores = state.Stores + (state.AcresPlanted * state.Productivity) - state.Spoilage,
|
||||
AcresPlanted = 0,
|
||||
FoodDistributed = 0
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to purchase the given number of acres.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The updated game state and action result.
|
||||
/// </returns>
|
||||
public static (GameState newState, ActionResult result) BuyLand(GameState state, int amount)
|
||||
{
|
||||
var price = state.LandPrice * amount;
|
||||
|
||||
if (price < 0)
|
||||
return (state, ActionResult.Offense);
|
||||
else
|
||||
if (price > state.Stores)
|
||||
return (state, ActionResult.InsufficientStores);
|
||||
else
|
||||
return (state with { Acres = state.Acres + amount, Stores = state.Stores - price }, ActionResult.Success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to sell the given number of acres.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The updated game state and action result.
|
||||
/// </returns>
|
||||
public static (GameState newState, ActionResult result) SellLand(GameState state, int amount)
|
||||
{
|
||||
var price = state.LandPrice * amount;
|
||||
|
||||
if (price < 0)
|
||||
return (state, ActionResult.Offense);
|
||||
else
|
||||
if (amount >= state.Acres)
|
||||
return (state, ActionResult.InsufficientLand);
|
||||
else
|
||||
return (state with { Acres = state.Acres - amount, Stores = state.Stores + price }, ActionResult.Success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to feed the people the given number of buschels.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// <returns>
|
||||
/// The updated game state and action result.
|
||||
/// </returns>
|
||||
public static (GameState newState, ActionResult result) FeedPeople(GameState state, int amount)
|
||||
{
|
||||
if (amount < 0)
|
||||
return (state, ActionResult.Offense);
|
||||
else
|
||||
if (amount > state.Stores)
|
||||
return (state, ActionResult.InsufficientStores);
|
||||
else
|
||||
return (state with { Stores = state.Stores - amount, FoodDistributed = state.FoodDistributed + amount }, ActionResult.Success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to plant crops on the given number of acres.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The updated game state and action result.
|
||||
/// </returns>
|
||||
public static (GameState newState, ActionResult result) PlantCrops(GameState state, int amount)
|
||||
{
|
||||
var storesRequired = amount / 2;
|
||||
var maxAcres = state.Population * 10;
|
||||
|
||||
if (amount < 0)
|
||||
return (state, ActionResult.Offense);
|
||||
else
|
||||
if (amount > state.Acres)
|
||||
return (state, ActionResult.InsufficientLand);
|
||||
else
|
||||
if (storesRequired > state.Stores)
|
||||
return (state, ActionResult.InsufficientStores);
|
||||
else
|
||||
if ((state.AcresPlanted + amount) > maxAcres)
|
||||
return (state, ActionResult.InsufficientPopulation);
|
||||
else
|
||||
return (state with
|
||||
{
|
||||
AcresPlanted = state.AcresPlanted + amount,
|
||||
Stores = state.Stores - storesRequired,
|
||||
}, ActionResult.Success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends the current turn and returns the updated game state.
|
||||
/// </summary>
|
||||
public static GameState EndTurn(GameState state, Random random)
|
||||
{
|
||||
var productivity = random.Next(1, 6);
|
||||
var harvest = productivity * state.AcresPlanted;
|
||||
|
||||
var spoilage = random.Next(1, 6) switch
|
||||
{
|
||||
2 => state.Stores / 2,
|
||||
4 => state.Stores / 4,
|
||||
_ => 0
|
||||
};
|
||||
|
||||
var populationIncrease= (int)((double) random.Next(1, 6) * (20 * state.Acres + state.Stores + harvest - spoilage) / state.Population / 100 + 1);
|
||||
|
||||
var plagueYear = random.Next(20) < 3;
|
||||
|
||||
var peopleFed = state.FoodDistributed / 20;
|
||||
var starvation = peopleFed < state.Population ? state.Population - peopleFed : 0;
|
||||
var impeached = starvation > state.Population * 0.45;
|
||||
|
||||
return state with
|
||||
{
|
||||
Year = state.Year + 1,
|
||||
Productivity = productivity,
|
||||
Spoilage = spoilage,
|
||||
PopulationIncrease = populationIncrease,
|
||||
Starvation = starvation,
|
||||
IsPlagueYear = plagueYear,
|
||||
IsImpeached = impeached
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Examines the game's history to arrive at the final result.
|
||||
/// </summary>
|
||||
public static GameResult GetGameResult(IEnumerable<GameState> history, Random random)
|
||||
{
|
||||
var (_, averageStarvationRate, totalStarvation, finalState) = history.Aggregate(
|
||||
(count: 0, starvationRate: 0, totalStarvation: 0, finalState: default(GameState)),
|
||||
(stats, state) =>
|
||||
(
|
||||
stats.count + 1,
|
||||
((stats.starvationRate * stats.count) + (state.Starvation * 100 / state.Population)) / (stats.count + 1),
|
||||
stats.totalStarvation + state.Starvation,
|
||||
state
|
||||
));
|
||||
|
||||
var acresPerPerson = finalState.Acres / finalState.Population;
|
||||
|
||||
var rating = finalState.IsImpeached ?
|
||||
PerformanceRating.Disgraceful :
|
||||
(averageStarvationRate, acresPerPerson) switch
|
||||
{
|
||||
(> 33, _) => PerformanceRating.Disgraceful,
|
||||
(_, < 7) => PerformanceRating.Disgraceful,
|
||||
(> 10, _) => PerformanceRating.Bad,
|
||||
(_, < 9) => PerformanceRating.Bad,
|
||||
(> 3, _) => PerformanceRating.Ok,
|
||||
(_, < 10) => PerformanceRating.Ok,
|
||||
_ => PerformanceRating.Terrific
|
||||
};
|
||||
|
||||
var assassins = rating == PerformanceRating.Ok ?
|
||||
random.Next(0, (int)(finalState.Population * 0.8)) : 0;
|
||||
|
||||
return new GameResult
|
||||
{
|
||||
Rating = rating,
|
||||
AcresPerPerson = acresPerPerson,
|
||||
FinalStarvation = finalState.Starvation,
|
||||
TotalStarvation = totalStarvation,
|
||||
AverageStarvationRate = averageStarvationRate,
|
||||
Assassins = assassins,
|
||||
WasImpeached = finalState.IsImpeached
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
193
43 Hammurabi/csharp/src/View.cs
Normal file
193
43 Hammurabi/csharp/src/View.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
|
||||
namespace Hammurabi
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides various methods for presenting information to the user.
|
||||
/// </summary>
|
||||
public static class View
|
||||
{
|
||||
/// <summary>
|
||||
/// Shows the introductory banner to the player.
|
||||
/// </summary>
|
||||
public static void ShowBanner()
|
||||
{
|
||||
Console.WriteLine(" HAMURABI");
|
||||
Console.WriteLine(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA");
|
||||
Console.WriteLine("FOR A TEN-YEAR TERM OF OFFICE.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows a summary of the current state of the city.
|
||||
/// </summary>
|
||||
public static void ShowCitySummary(GameState state)
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("HAMURABI: I BEG TO REPORT TO YOU,");
|
||||
Console.WriteLine($"IN YEAR {state.Year}, {state.Starvation} PEOPLE STARVED, {state.PopulationIncrease} CAME TO THE CITY,");
|
||||
|
||||
if (state.IsPlagueYear)
|
||||
{
|
||||
Console.WriteLine("A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED.");
|
||||
}
|
||||
|
||||
Console.WriteLine($"POPULATION IS NOW {state.Population}");
|
||||
Console.WriteLine($"THE CITY NOW OWNS {state.Acres} ACRES.");
|
||||
Console.WriteLine($"YOU HARVESTED {state.Productivity} BUSHELS PER ACRE.");
|
||||
Console.WriteLine($"THE RATS ATE {state.Spoilage} BUSHELS.");
|
||||
Console.WriteLine($"YOU NOW HAVE {state.Stores} BUSHELS IN STORE.");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the current cost of land.
|
||||
/// </summary>
|
||||
/// <param name="state"></param>
|
||||
public static void ShowLandPrice(GameState state)
|
||||
{
|
||||
Console.WriteLine ($"LAND IS TRADING AT {state.LandPrice} BUSHELS PER ACRE.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays a section separator.
|
||||
/// </summary>
|
||||
public static void ShowSeparator()
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inform the player that he or she has entered an invalid number.
|
||||
/// </summary>
|
||||
public static void ShowInvalidNumber()
|
||||
{
|
||||
Console.WriteLine("PLEASE ENTER A VALID NUMBER");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inform the player that he or she has insufficient acreage.
|
||||
/// </summary>
|
||||
public static void ShowInsufficientLand(GameState state)
|
||||
{
|
||||
Console.WriteLine($"HAMURABI: THINK AGAIN. YOU OWN ONLY {state.Acres} ACRES. NOW THEN,");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inform the player that he or she has insufficient population.
|
||||
/// </summary>
|
||||
public static void ShowInsufficientPopulation(GameState state)
|
||||
{
|
||||
Console.WriteLine($"BUT YOU HAVE ONLY {state.Population} PEOPLE TO TEND THE FIELDS! NOW THEN,");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inform the player that he or she has insufficient grain stores.
|
||||
/// </summary>
|
||||
public static void ShowInsufficientStores(GameState state)
|
||||
{
|
||||
Console.WriteLine("HAMURABI: THINK AGAIN. YOU HAVE ONLY");
|
||||
Console.WriteLine($"{state.Stores} BUSHELS OF GRAIN. NOW THEN,");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show the player that he or she has caused great offence.
|
||||
/// </summary>
|
||||
public static void ShowGreatOffence()
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("HAMURABI: I CANNOT DO WHAT YOU WISH.");
|
||||
Console.WriteLine("GET YOURSELF ANOTHER STEWARD!!!!!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the game's final result to the user.
|
||||
/// </summary>
|
||||
public static void ShowGameResult(GameResult result)
|
||||
{
|
||||
if (!result.WasImpeached)
|
||||
{
|
||||
Console.WriteLine($"IN YOUR 10-YEAR TERM OF OFFICE, {result.AverageStarvationRate} PERCENT OF THE");
|
||||
Console.WriteLine("POPULATION STARVED PER YEAR ON THE AVERAGE, I.E. A TOTAL OF");
|
||||
Console.WriteLine($"{result.TotalStarvation} PEOPLE DIED!!");
|
||||
|
||||
Console.WriteLine("YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH");
|
||||
Console.WriteLine($"{result.AcresPerPerson} ACRES PER PERSON.");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
switch (result.Rating)
|
||||
{
|
||||
case PerformanceRating.Disgraceful:
|
||||
if (result.WasImpeached)
|
||||
Console.WriteLine($"YOU STARVED {result.FinalStarvation} PEOPLE IN ONE YEAR!!!");
|
||||
|
||||
Console.WriteLine("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY");
|
||||
Console.WriteLine("BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE");
|
||||
Console.WriteLine("ALSO BEEN DECLARED NATIONAL FINK!!!!");
|
||||
break;
|
||||
case PerformanceRating.Bad:
|
||||
Console.WriteLine("YOUR HEAVY-HANDED PERFORMANCE SMACKS OF NERO AND IVAN IV.");
|
||||
Console.WriteLine("THE PEOPLE (REMIANING) FIND YOU AN UNPLEASANT RULER, AND,");
|
||||
Console.WriteLine("FRANKLY, HATE YOUR GUTS!!");
|
||||
break;
|
||||
case PerformanceRating.Ok:
|
||||
Console.WriteLine("YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT");
|
||||
Console.WriteLine($"REALLY WASN'T TOO BAD AT ALL. {result.Assassins} PEOPLE");
|
||||
Console.WriteLine("WOULD DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR");
|
||||
Console.WriteLine("TRIVIAL PROBLEMS.");
|
||||
break;
|
||||
case PerformanceRating.Terrific:
|
||||
Console.WriteLine("A FANTASTIC PERFORMANCE!!! CHARLEMANGE, DISRAELI, AND");
|
||||
Console.WriteLine("JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows a farewell message to the user.
|
||||
/// </summary>
|
||||
public static void ShowFarewell()
|
||||
{
|
||||
Console.WriteLine("SO LONG FOR NOW.");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prompts the user to buy land.
|
||||
/// </summary>
|
||||
public static void PromptBuyLand()
|
||||
{
|
||||
Console.WriteLine ("HOW MANY ACRES DO YOU WISH TO BUY");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prompts the user to sell land.
|
||||
/// </summary>
|
||||
public static void PromptSellLand()
|
||||
{
|
||||
Console.WriteLine("HOW MANY ACRES DO YOU WISH TO SELL");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prompts the user to feed the people.
|
||||
/// </summary>
|
||||
public static void PromptFeedPeople()
|
||||
{
|
||||
Console.WriteLine("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prompts the user to plant crops.
|
||||
/// </summary>
|
||||
public static void PromptPlantCrops()
|
||||
{
|
||||
Console.WriteLine("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED");
|
||||
}
|
||||
}
|
||||
}
|
||||
46
64 Nicomachus/perl/nicomachus.pl
Normal file
46
64 Nicomachus/perl/nicomachus.pl
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
|
||||
|
||||
print ' 'x 33 . "NICOMA\n";
|
||||
print ' 'x 15 . "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n";
|
||||
print "\n\n\n";
|
||||
print "BOOMERANG PUZZLE FROM ARITHMETICA OF NICOMACHUS -- A.D. 90!\n";
|
||||
|
||||
|
||||
while (1) {
|
||||
print "\n";
|
||||
print "PLEASE THINK OF A NUMBER BETWEEN 1 AND 100.\n";
|
||||
print "YOUR NUMBER DIVIDED BY 3 HAS A REMAINDER OF";
|
||||
print "? "; chomp(my $A = <STDIN>);
|
||||
print "YOUR NUMBER DIVIDED BY 5 HAS A REMAINDER OF";
|
||||
print "? "; chomp(my $B = <STDIN>);
|
||||
print "YOUR NUMBER DIVIDED BY 7 HAS A REMAINDER OF";
|
||||
print "? "; chomp(my $C = <STDIN>);
|
||||
print "\n";
|
||||
print "LET ME THINK A MOMENT...\n";
|
||||
print "\n";
|
||||
for (my $I=1; $I<=1500; $I++) { }
|
||||
my $D= 70*$A+21*$B+15*$C;
|
||||
|
||||
while ($D>105) {
|
||||
$D= $D-105;
|
||||
}
|
||||
|
||||
print "YOUR NUMBER WAS $D, RIGHT";
|
||||
|
||||
my $Flag=0;
|
||||
do {
|
||||
print "? "; chomp($A = uc(<STDIN>));
|
||||
print "\n";
|
||||
if ($A eq "YES") { print "HOW ABOUT THAT!!\n"; $Flag=1; }
|
||||
if ($A eq "NO") { print "I FEEL YOUR ARITHMETIC IS IN ERROR.\n"; $Flag=1; }
|
||||
if ($Flag==0) { print "EH? I DON'T UNDERSTAND '$A' TRY 'YES' OR 'NO'.\n"; }
|
||||
} until ($Flag==1);
|
||||
|
||||
print "\n";
|
||||
print "LET'S TRY ANOTHER.\n";
|
||||
} #goto Line20;
|
||||
exit;
|
||||
|
||||
|
||||
39
66 Number/perl/number.pl
Normal file
39
66 Number/perl/number.pl
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
|
||||
print ' 'x 33 . "NUMBER\n";
|
||||
print ' 'x 15 . "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n";
|
||||
print "\n\n\n";
|
||||
print "YOU HAVE 100 POINTS. BY GUESSING NUMBERS FROM 1 TO 5, YOU\n";
|
||||
print "CAN GAIN OR LOSE POINTS DEPENDING UPON HOW CLOSE YOU GET TO\n";
|
||||
print "A RANDOM NUMBER SELECTED BY THE COMPUTER.\n"; print "\n";
|
||||
print "YOU OCCASIONALLY WILL GET A JACKPOT WHICH WILL DOUBLE(!)\n";
|
||||
print "YOUR POINT COUNT. YOU WIN WHEN YOU GET 500 POINTS.\n";
|
||||
print "\n"; my $P=100;
|
||||
|
||||
Line12:
|
||||
while ($P<500) {
|
||||
print "GUESS A NUMBER FROM 1 TO 5? "; chomp(my $G = <STDIN>);
|
||||
my $R= &FNR(1);
|
||||
my $S= &FNR(1);
|
||||
my $T= &FNR(1);
|
||||
my $U= &FNR(1);
|
||||
my $V= &FNR(1);
|
||||
if ($G eq $R) { $P=$P-5; }
|
||||
if ($G eq $S) { $P=$P+5; }
|
||||
if ($G eq $T) { $P=$P+$P; print "YOU HIT THE JACKPOT!!!\n"; }
|
||||
if ($G eq $U) { $P=$P+1; }
|
||||
if ($G eq $V) { $P=$P-($P*.5); }
|
||||
if ($G<1 || $G>5) { redo; }
|
||||
print "YOU HAVE $P POINTS.\n"; print "\n";
|
||||
}
|
||||
print "!!!!YOU WIN!!!! WITH $P POINTS.\n";
|
||||
exit;
|
||||
|
||||
|
||||
sub FNR {
|
||||
my ($X)= @_; #Useless...
|
||||
return int(5*rand(1)+1);
|
||||
}
|
||||
|
||||
|
||||
192
68 Orbit/java/Orbit.java
Normal file
192
68 Orbit/java/Orbit.java
Normal file
@@ -0,0 +1,192 @@
|
||||
import java.lang.Integer;
|
||||
import java.lang.Math;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Orbit
|
||||
* <p>
|
||||
* Based on the BASIC game of Orbit here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/68%20Orbit/orbit.bas
|
||||
* <p>
|
||||
* Note: The idea was to create a version of the 1970's BASIC game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*
|
||||
* Converted from BASIC to Java by Darren Cardenas.
|
||||
*/
|
||||
|
||||
public class Orbit {
|
||||
|
||||
private final Scanner scan; // For user input
|
||||
|
||||
public Orbit() {
|
||||
|
||||
scan = new Scanner(System.in);
|
||||
|
||||
} // End of constructor Orbit
|
||||
|
||||
public void play() {
|
||||
|
||||
showIntro();
|
||||
startGame();
|
||||
|
||||
} // End of method play
|
||||
|
||||
private static void showIntro() {
|
||||
|
||||
System.out.println(" ".repeat(32) + "ORBIT");
|
||||
System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n");
|
||||
|
||||
System.out.println("SOMEWHERE ABOVE YOUR PLANET IS A ROMULAN SHIP.");
|
||||
System.out.println("");
|
||||
System.out.println("THE SHIP IS IN A CONSTANT POLAR ORBIT. ITS");
|
||||
System.out.println("DISTANCE FROM THE CENTER OF YOUR PLANET IS FROM");
|
||||
System.out.println("10,000 TO 30,000 MILES AND AT ITS PRESENT VELOCITY CAN");
|
||||
System.out.println("CIRCLE YOUR PLANET ONCE EVERY 12 TO 36 HOURS.");
|
||||
System.out.println("");
|
||||
System.out.println("UNFORTUNATELY, THEY ARE USING A CLOAKING DEVICE SO");
|
||||
System.out.println("YOU ARE UNABLE TO SEE THEM, BUT WITH A SPECIAL");
|
||||
System.out.println("INSTRUMENT YOU CAN TELL HOW NEAR THEIR SHIP YOUR");
|
||||
System.out.println("PHOTON BOMB EXPLODED. YOU HAVE SEVEN HOURS UNTIL THEY");
|
||||
System.out.println("HAVE BUILT UP SUFFICIENT POWER IN ORDER TO ESCAPE");
|
||||
System.out.println("YOUR PLANET'S GRAVITY.");
|
||||
System.out.println("");
|
||||
System.out.println("YOUR PLANET HAS ENOUGH POWER TO FIRE ONE BOMB AN HOUR.");
|
||||
System.out.println("");
|
||||
System.out.println("AT THE BEGINNING OF EACH HOUR YOU WILL BE ASKED TO GIVE AN");
|
||||
System.out.println("ANGLE (BETWEEN 0 AND 360) AND A DISTANCE IN UNITS OF");
|
||||
System.out.println("100 MILES (BETWEEN 100 AND 300), AFTER WHICH YOUR BOMB'S");
|
||||
System.out.println("DISTANCE FROM THE ENEMY SHIP WILL BE GIVEN.");
|
||||
System.out.println("");
|
||||
System.out.println("AN EXPLOSION WITHIN 5,000 MILES OF THE ROMULAN SHIP");
|
||||
System.out.println("WILL DESTROY IT.");
|
||||
System.out.println("");
|
||||
System.out.println("BELOW IS A DIAGRAM TO HELP YOU VISUALIZE YOUR PLIGHT.");
|
||||
System.out.println("");
|
||||
System.out.println("");
|
||||
System.out.println(" 90");
|
||||
System.out.println(" 0000000000000");
|
||||
System.out.println(" 0000000000000000000");
|
||||
System.out.println(" 000000 000000");
|
||||
System.out.println(" 00000 00000");
|
||||
System.out.println(" 00000 XXXXXXXXXXX 00000");
|
||||
System.out.println(" 00000 XXXXXXXXXXXXX 00000");
|
||||
System.out.println(" 0000 XXXXXXXXXXXXXXX 0000");
|
||||
System.out.println(" 0000 XXXXXXXXXXXXXXXXX 0000");
|
||||
System.out.println(" 0000 XXXXXXXXXXXXXXXXXXX 0000");
|
||||
System.out.println("180<== 00000 XXXXXXXXXXXXXXXXXXX 00000 ==>0");
|
||||
System.out.println(" 0000 XXXXXXXXXXXXXXXXXXX 0000");
|
||||
System.out.println(" 0000 XXXXXXXXXXXXXXXXX 0000");
|
||||
System.out.println(" 0000 XXXXXXXXXXXXXXX 0000");
|
||||
System.out.println(" 00000 XXXXXXXXXXXXX 00000");
|
||||
System.out.println(" 00000 XXXXXXXXXXX 00000");
|
||||
System.out.println(" 00000 00000");
|
||||
System.out.println(" 000000 000000");
|
||||
System.out.println(" 0000000000000000000");
|
||||
System.out.println(" 0000000000000");
|
||||
System.out.println(" 270");
|
||||
System.out.println("");
|
||||
System.out.println("X - YOUR PLANET");
|
||||
System.out.println("O - THE ORBIT OF THE ROMULAN SHIP");
|
||||
System.out.println("");
|
||||
System.out.println("ON THE ABOVE DIAGRAM, THE ROMULAN SHIP IS CIRCLING");
|
||||
System.out.println("COUNTERCLOCKWISE AROUND YOUR PLANET. DON'T FORGET THAT");
|
||||
System.out.println("WITHOUT SUFFICIENT POWER THE ROMULAN SHIP'S ALTITUDE");
|
||||
System.out.println("AND ORBITAL RATE WILL REMAIN CONSTANT.");
|
||||
System.out.println("");
|
||||
System.out.println("GOOD LUCK. THE FEDERATION IS COUNTING ON YOU.");
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
private void startGame() {
|
||||
|
||||
double bombDistance = 0;
|
||||
int bombAltitude = 0;
|
||||
int bombAngle = 0;
|
||||
int deltaAngle = 0;
|
||||
int hour = 0;
|
||||
int shipAltitude = 0;
|
||||
int shipAngle = 0;
|
||||
int shipRate = 0;
|
||||
String userResponse = "";
|
||||
|
||||
// Begin outer while loop
|
||||
while (true) {
|
||||
shipAngle = (int) (361 * Math.random());
|
||||
shipAltitude = (int) (201 * Math.random() + 200);
|
||||
shipRate = (int) (21 * Math.random() + 10);
|
||||
|
||||
hour = 0;
|
||||
|
||||
// Begin time limit loop
|
||||
while (hour < 7) {
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("");
|
||||
System.out.println("THIS IS HOUR " + (hour + 1) + ", AT WHAT ANGLE DO YOU WISH TO SEND");
|
||||
System.out.print("YOUR PHOTON BOMB? ");
|
||||
bombAngle = Integer.parseInt(scan.nextLine());
|
||||
|
||||
System.out.print("HOW FAR OUT DO YOU WISH TO DETONATE IT? ");
|
||||
bombAltitude = Integer.parseInt(scan.nextLine());
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("");
|
||||
|
||||
// Update ship position
|
||||
shipAngle += shipRate;
|
||||
|
||||
// Handle full revolutions
|
||||
if (shipAngle >= 360) {
|
||||
shipAngle -= 360;
|
||||
}
|
||||
|
||||
deltaAngle = Math.abs(shipAngle - bombAngle);
|
||||
|
||||
// Keep angle in upper quadrants
|
||||
if (deltaAngle >= 180) {
|
||||
deltaAngle = 360 - deltaAngle;
|
||||
}
|
||||
|
||||
bombDistance = Math.sqrt(shipAltitude * shipAltitude + bombAltitude * bombAltitude - 2 * shipAltitude
|
||||
* bombAltitude * Math.cos(deltaAngle * Math.PI / 180));
|
||||
|
||||
System.out.format("YOUR PHOTON BOMB EXPLODED " + "%.5f" + "*10^2 MILES FROM THE\n", bombDistance);
|
||||
System.out.println("ROMULAN SHIP.");
|
||||
|
||||
// Win condition
|
||||
if (bombDistance <= 50) {
|
||||
System.out.println("YOU HAVE SUCCESSFULLY COMPLETED YOUR MISSION.");
|
||||
break;
|
||||
}
|
||||
|
||||
hour++;
|
||||
|
||||
} // End time limit loop
|
||||
|
||||
// Lose condition
|
||||
if (hour == 7) {
|
||||
System.out.println("YOU HAVE ALLOWED THE ROMULANS TO ESCAPE.");
|
||||
}
|
||||
|
||||
System.out.println("ANOTHER ROMULAN SHIP HAS GONE INTO ORBIT.");
|
||||
System.out.print("DO YOU WISH TO TRY TO DESTROY IT? ");
|
||||
userResponse = scan.nextLine();
|
||||
|
||||
if (!userResponse.toUpperCase().equals("YES")) {
|
||||
System.out.println("GOOD BYE.");
|
||||
break;
|
||||
}
|
||||
|
||||
} // End outer while loop
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Orbit game = new Orbit();
|
||||
game.play();
|
||||
|
||||
} // End of method main
|
||||
|
||||
} // End of class Orbit
|
||||
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();
|
||||
163
93 23 Matches/java/TwentyThreeMatches.java
Normal file
163
93 23 Matches/java/TwentyThreeMatches.java
Normal file
@@ -0,0 +1,163 @@
|
||||
import java.util.Scanner;
|
||||
import java.lang.Math;
|
||||
|
||||
/**
|
||||
* Game of 23 Matches
|
||||
* <p>
|
||||
* Based on the BASIC game of 23 Matches here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/93%2023%20Matches/23matches.bas
|
||||
* <p>
|
||||
* Note: The idea was to create a version of the 1970's BASIC game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*
|
||||
* Converted from BASIC to Java by Darren Cardenas.
|
||||
*/
|
||||
|
||||
public class TwentyThreeMatches {
|
||||
|
||||
private static final int MATCH_COUNT_START = 23;
|
||||
|
||||
private static final int HEADS = 1;
|
||||
|
||||
private final Scanner scan; // For user input
|
||||
|
||||
public TwentyThreeMatches() {
|
||||
|
||||
scan = new Scanner(System.in);
|
||||
|
||||
} // End of constructor TwentyThreeMatches
|
||||
|
||||
public void play() {
|
||||
|
||||
showIntro();
|
||||
startGame();
|
||||
|
||||
} // End of method play
|
||||
|
||||
private static void showIntro() {
|
||||
|
||||
System.out.println(" ".repeat(30) + "23 MATCHES");
|
||||
System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n");
|
||||
|
||||
System.out.println(" THIS IS A GAME CALLED '23 MATCHES'.");
|
||||
System.out.println("");
|
||||
System.out.println("WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE");
|
||||
System.out.println("MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE");
|
||||
System.out.println("THE LAST MATCH.");
|
||||
System.out.println("");
|
||||
System.out.println("LET'S FLIP A COIN TO SEE WHO GOES FIRST.");
|
||||
System.out.println("IF IT COMES UP HEADS, I WILL WIN THE TOSS.");
|
||||
System.out.println("");
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
private void startGame() {
|
||||
|
||||
int coinSide = (int) (3 * Math.random());
|
||||
int cpuRemoves = 0;
|
||||
int matchesLeft = MATCH_COUNT_START;
|
||||
int playerRemoves = 0;
|
||||
|
||||
if (coinSide == HEADS) {
|
||||
|
||||
System.out.println("HEADS! I WIN! HA! HA!");
|
||||
System.out.println("PREPARE TO LOSE, MEATBALL-NOSE!!");
|
||||
System.out.println("");
|
||||
System.out.println("I TAKE 2 MATCHES");
|
||||
|
||||
matchesLeft -= 2;
|
||||
|
||||
} else {
|
||||
|
||||
System.out.println("TAILS! YOU GO FIRST. ");
|
||||
System.out.println("");
|
||||
|
||||
}
|
||||
|
||||
// Begin outer while loop
|
||||
while (true) {
|
||||
|
||||
if (coinSide == HEADS) {
|
||||
|
||||
System.out.println("THE NUMBER OF MATCHES IS NOW " + matchesLeft);
|
||||
System.out.println("");
|
||||
System.out.println("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES.");
|
||||
|
||||
}
|
||||
|
||||
coinSide = HEADS;
|
||||
|
||||
System.out.print("HOW MANY DO YOU WISH TO REMOVE? ");
|
||||
|
||||
// Begin match removal while loop
|
||||
while (true) {
|
||||
|
||||
playerRemoves = scan.nextInt();
|
||||
|
||||
// Handle invalid entries
|
||||
if ((playerRemoves > 3) || (playerRemoves <= 0)) {
|
||||
|
||||
System.out.println("VERY FUNNY! DUMMY!");
|
||||
System.out.println("DO YOU WANT TO PLAY OR GOOF AROUND?");
|
||||
System.out.print("NOW, HOW MANY MATCHES DO YOU WANT? ");
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
} // End match removal while loop
|
||||
|
||||
matchesLeft -= playerRemoves;
|
||||
|
||||
System.out.println("THERE ARE NOW " + matchesLeft + " MATCHES REMAINING.");
|
||||
|
||||
// Win condition
|
||||
if (matchesLeft <= 1) {
|
||||
|
||||
// Win condition
|
||||
System.out.println("YOU WON, FLOPPY EARS !");
|
||||
System.out.println("THINK YOU'RE PRETTY SMART !");
|
||||
System.out.println("LETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !!");
|
||||
System.out.println("");
|
||||
return;
|
||||
|
||||
} else if ((matchesLeft >= 2) && (matchesLeft <= 4)) {
|
||||
|
||||
cpuRemoves = matchesLeft - 1;
|
||||
|
||||
} else {
|
||||
|
||||
cpuRemoves = 4 - playerRemoves;
|
||||
|
||||
}
|
||||
|
||||
System.out.println("MY TURN ! I REMOVE " + cpuRemoves + " MATCHES");
|
||||
|
||||
matchesLeft -= cpuRemoves;
|
||||
|
||||
// Lose condition
|
||||
if (matchesLeft <= 1) {
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("YOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!");
|
||||
System.out.println("HA ! HA ! I BEAT YOU !!!");
|
||||
System.out.println("");
|
||||
System.out.println("GOOD BYE LOSER!");
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
} // End outer while loop
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
TwentyThreeMatches game = new TwentyThreeMatches();
|
||||
game.play();
|
||||
|
||||
} // End of method main
|
||||
|
||||
} // End of class TwentyThreeMatches
|
||||
223
96 Word/java/Word.java
Normal file
223
96 Word/java/Word.java
Normal file
@@ -0,0 +1,223 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Word
|
||||
* <p>
|
||||
* Based on the BASIC game of Word here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/96%20Word/word.bas
|
||||
* <p>
|
||||
* Note: The idea was to create a version of the 1970's BASIC game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*
|
||||
* Converted from BASIC to Java by Darren Cardenas.
|
||||
*/
|
||||
|
||||
public class Word {
|
||||
|
||||
private final static String[] WORDS = {
|
||||
|
||||
"DINKY", "SMOKE", "WATER", "GRASS", "TRAIN", "MIGHT",
|
||||
"FIRST", "CANDY", "CHAMP", "WOULD", "CLUMP", "DOPEY"
|
||||
|
||||
};
|
||||
|
||||
private final Scanner scan; // For user input
|
||||
|
||||
private enum Step {
|
||||
INITIALIZE, MAKE_GUESS, USER_WINS
|
||||
}
|
||||
|
||||
public Word() {
|
||||
|
||||
scan = new Scanner(System.in);
|
||||
|
||||
} // End of constructor Word
|
||||
|
||||
public void play() {
|
||||
|
||||
showIntro();
|
||||
startGame();
|
||||
|
||||
} // End of method play
|
||||
|
||||
private void showIntro() {
|
||||
|
||||
System.out.println(" ".repeat(32) + "WORD");
|
||||
System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n");
|
||||
|
||||
System.out.println("I AM THINKING OF A WORD -- YOU GUESS IT. I WILL GIVE YOU");
|
||||
System.out.println("CLUES TO HELP YOU GET IT. GOOD LUCK!!");
|
||||
System.out.println("\n");
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
private void startGame() {
|
||||
|
||||
char[] commonLetters = new char[8];
|
||||
char[] exactLetters = new char[8];
|
||||
|
||||
int commonIndex = 0;
|
||||
int ii = 0; // Loop iterator
|
||||
int jj = 0; // Loop iterator
|
||||
int numGuesses = 0;
|
||||
int numMatches = 0;
|
||||
int wordIndex = 0;
|
||||
|
||||
Step nextStep = Step.INITIALIZE;
|
||||
|
||||
String commonString = "";
|
||||
String exactString = "";
|
||||
String guessWord = "";
|
||||
String secretWord = "";
|
||||
String userResponse = "";
|
||||
|
||||
// Begin outer while loop
|
||||
while (true) {
|
||||
|
||||
switch (nextStep) {
|
||||
|
||||
case INITIALIZE:
|
||||
|
||||
System.out.println("\n");
|
||||
System.out.println("YOU ARE STARTING A NEW GAME...");
|
||||
|
||||
// Select a secret word from the list
|
||||
wordIndex = (int) (Math.random() * WORDS.length);
|
||||
secretWord = WORDS[wordIndex];
|
||||
|
||||
numGuesses = 0;
|
||||
|
||||
Arrays.fill(exactLetters, 1, 6, '-');
|
||||
Arrays.fill(commonLetters, 1, 6, '\0');
|
||||
|
||||
nextStep = Step.MAKE_GUESS;
|
||||
break;
|
||||
|
||||
case MAKE_GUESS:
|
||||
|
||||
System.out.print("GUESS A FIVE LETTER WORD? ");
|
||||
guessWord = scan.nextLine().toUpperCase();
|
||||
|
||||
numGuesses++;
|
||||
|
||||
// Win condition
|
||||
if (guessWord.equals(secretWord)) {
|
||||
nextStep = Step.USER_WINS;
|
||||
continue;
|
||||
}
|
||||
|
||||
Arrays.fill(commonLetters, 1, 8, '\0');
|
||||
|
||||
// Surrender condition
|
||||
if (guessWord.equals("?")) {
|
||||
System.out.println("THE SECRET WORD IS " + secretWord);
|
||||
System.out.println("");
|
||||
nextStep = Step.INITIALIZE; // Play again
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for valid input
|
||||
if (guessWord.length() != 5) {
|
||||
System.out.println("YOU MUST GUESS A 5 LETTER WORD. START AGAIN.");
|
||||
numGuesses--;
|
||||
nextStep = Step.MAKE_GUESS; // Guess again
|
||||
continue;
|
||||
}
|
||||
|
||||
numMatches = 0;
|
||||
commonIndex = 1;
|
||||
|
||||
for (ii = 1; ii <= 5; ii++) {
|
||||
|
||||
for (jj = 1; jj <= 5; jj++) {
|
||||
|
||||
if (secretWord.charAt(ii - 1) != guessWord.charAt(jj - 1)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Avoid out of bounds errors
|
||||
if (commonIndex <= 5) {
|
||||
commonLetters[commonIndex] = guessWord.charAt(jj - 1);
|
||||
commonIndex++;
|
||||
}
|
||||
|
||||
if (ii == jj) {
|
||||
exactLetters[jj] = guessWord.charAt(jj - 1);
|
||||
}
|
||||
|
||||
// Avoid out of bounds errors
|
||||
if (numMatches < 5) {
|
||||
numMatches++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exactString = "";
|
||||
commonString = "";
|
||||
|
||||
// Build the exact letters string
|
||||
for (ii = 1; ii <= 5; ii++) {
|
||||
exactString += exactLetters[ii];
|
||||
}
|
||||
|
||||
// Build the common letters string
|
||||
for (ii = 1; ii <= numMatches; ii++) {
|
||||
commonString += commonLetters[ii];
|
||||
}
|
||||
|
||||
System.out.println("THERE WERE " + numMatches + " MATCHES AND THE COMMON LETTERS WERE..."
|
||||
+ commonString);
|
||||
|
||||
System.out.println("FROM THE EXACT LETTER MATCHES, YOU KNOW................" + exactString);
|
||||
|
||||
// Win condition
|
||||
if (exactString.equals(secretWord)) {
|
||||
nextStep = Step.USER_WINS;
|
||||
continue;
|
||||
}
|
||||
|
||||
// No matches
|
||||
if (numMatches <= 1) {
|
||||
System.out.println("");
|
||||
System.out.println("IF YOU GIVE UP, TYPE '?' FOR YOUR NEXT GUESS.");
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
nextStep = Step.MAKE_GUESS;
|
||||
break;
|
||||
|
||||
case USER_WINS:
|
||||
|
||||
System.out.println("YOU HAVE GUESSED THE WORD. IT TOOK " + numGuesses + " GUESSES!");
|
||||
System.out.println("");
|
||||
|
||||
System.out.print("WANT TO PLAY AGAIN? ");
|
||||
userResponse = scan.nextLine();
|
||||
|
||||
if (userResponse.toUpperCase().equals("YES")) {
|
||||
nextStep = Step.INITIALIZE; // Play again
|
||||
} else {
|
||||
return; // Quit game
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("INVALID STEP");
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
} // End outer while loop
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Word word = new Word();
|
||||
word.play();
|
||||
|
||||
} // End of method main
|
||||
|
||||
} // End of class Word
|
||||
Reference in New Issue
Block a user