93_23_Matches\csharp

The program is available in two versions, a "goto" version that mimics the original program and an "object-oriented" version.
This commit is contained in:
王振耀
2022-01-09 23:54:29 +08:00
parent 3dacdcc878
commit d2e2b920f1
5 changed files with 326 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_23matches</RootNamespace>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32002.261
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "23matches", "23matches.csproj", "{9DBE7354-0749-4750-9224-5F9F95C64905}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9DBE7354-0749-4750-9224-5F9F95C64905}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9DBE7354-0749-4750-9224-5F9F95C64905}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DBE7354-0749-4750-9224-5F9F95C64905}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9DBE7354-0749-4750-9224-5F9F95C64905}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0A87AE2F-68AC-4354-9C8D-578209D41174}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,129 @@
using System;
using System.Threading;
namespace _23matches
{
class Program
{
/// <summary>
/// Mimics the "goto" version of the original program
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Random random = new Random();
StartNewGame:
Console.WriteLine("23 MATCHES".PadLeft(31));
Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".PadLeft(15));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("THIS IS A GAME CALLED '23 MATCHES'.");
Console.WriteLine();
Console.WriteLine("WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE");
Console.WriteLine("MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE");
Console.WriteLine("THE LAST MATCH.");
Console.WriteLine();
Console.WriteLine("Input exit to close the program.");
Console.WriteLine("Input cls Screen Clear.");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("LET'S FLIP A COIN TO SEE WHO GOES FIRST.");
Console.WriteLine("IF IT COMES UP HEADS, I WILL WIN THE TOSS.");
Console.WriteLine();
StartTheGame:
string command;
int N = 23;
int K = 0;
int Q = random.Next(2);
if (Q == 1)
goto ComputerFirst;
else
goto PlayerFirst;
ComputerFirst:
Console.WriteLine("HEADS! I WIN! HA! HA!");
Console.WriteLine("PREPARE TO LOSE, MEATBALL-NOSE!!");
Console.WriteLine();
int ain = random.Next(1, 3);
Console.WriteLine($"I TAKE {ain} MATCHES");
N = N - ain;
goto PlayersProceed;
PlayerFirst:
Console.WriteLine("TAILS! YOU GO FIRST. ");
Console.WriteLine();
goto PlayersSpeak;
PlayersProceed:
Console.WriteLine($"THE NUMBER OF MATCHES IS NOW {N}");
Console.WriteLine();
Console.WriteLine("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES.");
Console.WriteLine("HOW MANY DO YOU WISH TO REMOVE ");
goto PlayersSpeak;
PlayersSpeak:
command = Console.ReadLine().ToLower();
if (command.Equals("exit"))
{
System.Diagnostics.Process tt = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
tt.Kill();
}
if (command.Equals("cls"))
{
Console.Clear();
goto PlayersProceed;
}
try
{
K = Convert.ToInt32(command);
}
catch (System.Exception)
{
goto PlayerInputError;
}
if (K > 3 || K <= 0)
goto PlayerInputError;
N = N - K;
Console.WriteLine($"THERE ARE NOW {N} MATCHES REMAINING.");
if (N == 4 || N == 3 || N == 2)
goto TheComputerSpeaks;
else if (N <= 1)
goto ThePlayerWins;
else
goto TheComputerSpeaks;
TheComputerSpeaks:
int Z = 4 - K;
Console.WriteLine($"MY TURN ! I REMOVE {Z} MATCHES");
N = N - Z;
if (N <= 1)
goto TheComputerWins;
else
goto PlayersProceed;
PlayerInputError:
Console.WriteLine("VERY FUNNY! DUMMY!");
Console.WriteLine("DO YOU WANT TO PLAY OR GOOF AROUND?");
Console.WriteLine("NOW, HOW MANY MATCHES DO YOU WANT ");
goto PlayersSpeak;
ThePlayerWins:
Console.WriteLine("YOU WON, FLOPPY EARS !");
Console.WriteLine("THINK YOU'RE PRETTY SMART !");
Console.WriteLine("LETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !!");
Console.WriteLine();
Console.WriteLine();
goto StartTheGame;
TheComputerWins:
Console.WriteLine();
Console.WriteLine("YOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!");
Console.WriteLine("HA ! HA ! I BEAT YOU !!!");
Console.WriteLine();
Console.WriteLine("GOOD BYE LOSER!");
Console.WriteLine();
Console.WriteLine();
goto StartNewGame;
}
}
}

View File

@@ -0,0 +1,161 @@
using System;
using System.Threading;
namespace _23matches
{
class ObjectOrientedVersion_Program
{
/// <summary>
/// Object-oriented version
/// </summary>
/// <param name="args"></param>
static void Main_Two(string[] args)
{
Game game = new Game();
game.GameRun();
}
}
public class Game
{
string command;
int N;
int K;
Random random = new Random();
public void GameRun()
{
StartNewGame();
StartTheGame();
}
void StartNewGame()
{
Console.WriteLine("23 MATCHES".PadLeft(31));
Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".PadLeft(15));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("THIS IS A GAME CALLED '23 MATCHES'.");
Console.WriteLine();
Console.WriteLine("WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE");
Console.WriteLine("MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE");
Console.WriteLine("THE LAST MATCH.");
Console.WriteLine();
Console.WriteLine("Input exit to close the program.");
Console.WriteLine("Input cls Screen Clear.");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("LET'S FLIP A COIN TO SEE WHO GOES FIRST.");
Console.WriteLine("IF IT COMES UP HEADS, I WILL WIN THE TOSS.");
Console.WriteLine();
}
void StartTheGame()
{
N = 23;
K = 0;
int Q = random.Next(2);
if (Q == 1)
ComputerFirst();
else
{
PlayerFirst();
}
}
void ComputerFirst()
{//210
Console.WriteLine("HEADS! I WIN! HA! HA!");
Console.WriteLine("PREPARE TO LOSE, MEATBALL-NOSE!!");
Console.WriteLine();
int ain = random.Next(1, 3);
Console.WriteLine($"I TAKE {ain} MATCHES");
N = N - ain;
PlayersProceed();
}
void PlayerFirst()
{
Console.WriteLine("TAILS! YOU GO FIRST. ");
Console.WriteLine();
PlayersSpeak();
}
void PlayersProceed()
{
Console.WriteLine($"THE NUMBER OF MATCHES IS NOW {N}");
Console.WriteLine();
PlayersSpeak();
}
void RemindsPlayersToEnter()
{
Console.WriteLine("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES.");
Console.WriteLine("HOW MANY DO YOU WISH TO REMOVE ");
}
void PlayersSpeak()
{
RemindsPlayersToEnter();
command = Console.ReadLine().ToLower();
if (command.Equals("exit"))
{
System.Diagnostics.Process tt = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
tt.Kill();
}
if (command.Equals("cls"))
{
Console.Clear();
PlayersSpeak();
}
try
{
K = Convert.ToInt32(command);
}
catch (System.Exception)
{
PlayerInputError();
}
if (K > 3 || K <= 0)
PlayerInputError();
N = N - K;
Console.WriteLine($"THERE ARE NOW {N} MATCHES REMAINING.");
if (N == 4 || N == 3 || N == 2)
TheComputerSpeaks(N);
else if (N <= 1)
ThePlayerWins();
else
TheComputerSpeaks(4 - K);
}
void PlayerInputError()
{
Console.WriteLine("VERY FUNNY! DUMMY!");
Console.WriteLine("DO YOU WANT TO PLAY OR GOOF AROUND?");
Console.WriteLine("NOW, HOW MANY MATCHES DO YOU WANT ");
PlayersSpeak();
}
void TheComputerSpeaks(int ain)
{
int Z = ain;
Console.WriteLine($"MY TURN ! I REMOVE {Z} MATCHES");//390
N = N - Z;
if (N <= 1)
TheComputerWins();
else
PlayersProceed();
}
void ThePlayerWins()
{
Console.WriteLine("YOU WON, FLOPPY EARS !");
Console.WriteLine("THINK YOU'RE PRETTY SMART !");
Console.WriteLine("LETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !!");
Console.WriteLine();
Console.WriteLine();
StartTheGame();
}
void TheComputerWins()
{
Console.WriteLine();
Console.WriteLine("YOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!");
Console.WriteLine("HA ! HA ! I BEAT YOU !!!");
Console.WriteLine();
Console.WriteLine("GOOD BYE LOSER!");
Console.WriteLine();
Console.WriteLine();
GameRun();
}
}
}

View File

@@ -1,3 +1,5 @@
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
Conversion to [Microsoft C#](https://docs.microsoft.com/en-us/dotnet/csharp/)
The program is available in two versions, a "goto" version that mimics the original program and an "object-oriented" version.