mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-20 00:31:18 -08:00
remove copy/pasted ports from alternate languages
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<LangVersion>10</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneCheck", "OneCheck.csproj", "{3964167F-17D8-44FB-A9F7-EA2DB1C5DA3F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{3964167F-17D8-44FB-A9F7-EA2DB1C5DA3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{3964167F-17D8-44FB-A9F7-EA2DB1C5DA3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3964167F-17D8-44FB-A9F7-EA2DB1C5DA3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3964167F-17D8-44FB-A9F7-EA2DB1C5DA3F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -1,3 +0,0 @@
|
||||
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/)
|
||||
@@ -1,252 +0,0 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of One Check
|
||||
* <p>
|
||||
* Based on the BASIC game of One Check here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/67%20One%20Check/onecheck.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 OneCheck {
|
||||
|
||||
private final Scanner scan; // For user input
|
||||
|
||||
private enum Step {
|
||||
SHOW_INSTRUCTIONS, SHOW_BOARD, GET_MOVE, GET_SUMMARY, QUERY_RETRY
|
||||
}
|
||||
|
||||
public OneCheck() {
|
||||
|
||||
scan = new Scanner(System.in);
|
||||
|
||||
} // End of constructor OneCheck
|
||||
|
||||
public void play() {
|
||||
|
||||
showIntro();
|
||||
startGame();
|
||||
|
||||
} // End of method play
|
||||
|
||||
private static void showIntro() {
|
||||
|
||||
System.out.println(" ".repeat(29) + "ONE CHECK");
|
||||
System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n");
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
private void startGame() {
|
||||
|
||||
int fromSquare = 0;
|
||||
int numJumps = 0;
|
||||
int numPieces = 0;
|
||||
int square = 0;
|
||||
int startPosition = 0;
|
||||
int toSquare = 0;
|
||||
|
||||
// Move legality test variables
|
||||
int fromTest1 = 0;
|
||||
int fromTest2 = 0;
|
||||
int toTest1 = 0;
|
||||
int toTest2 = 0;
|
||||
|
||||
int[] positions = new int[65];
|
||||
|
||||
Step nextStep = Step.SHOW_INSTRUCTIONS;
|
||||
|
||||
String lineContent = "";
|
||||
String userResponse = "";
|
||||
|
||||
// Begin outer while loop
|
||||
while (true) {
|
||||
|
||||
// Begin switch
|
||||
switch (nextStep) {
|
||||
|
||||
case SHOW_INSTRUCTIONS:
|
||||
|
||||
System.out.println("SOLITAIRE CHECKER PUZZLE BY DAVID AHL\n");
|
||||
System.out.println("48 CHECKERS ARE PLACED ON THE 2 OUTSIDE SPACES OF A");
|
||||
System.out.println("STANDARD 64-SQUARE CHECKERBOARD. THE OBJECT IS TO");
|
||||
System.out.println("REMOVE AS MANY CHECKERS AS POSSIBLE BY DIAGONAL JUMPS");
|
||||
System.out.println("(AS IN STANDARD CHECKERS). USE THE NUMBERED BOARD TO");
|
||||
System.out.println("INDICATE THE SQUARE YOU WISH TO JUMP FROM AND TO. ON");
|
||||
System.out.println("THE BOARD PRINTED OUT ON EACH TURN '1' INDICATES A");
|
||||
System.out.println("CHECKER AND '0' AN EMPTY SQUARE. WHEN YOU HAVE NO");
|
||||
System.out.println("POSSIBLE JUMPS REMAINING, INPUT A '0' IN RESPONSE TO");
|
||||
System.out.println("QUESTION 'JUMP FROM ?'\n");
|
||||
System.out.println("HERE IS THE NUMERICAL BOARD:\n");
|
||||
|
||||
nextStep = Step.SHOW_BOARD;
|
||||
break;
|
||||
|
||||
case SHOW_BOARD:
|
||||
|
||||
// Begin loop through all squares
|
||||
for (square = 1; square <= 57; square += 8) {
|
||||
|
||||
lineContent = String.format("% -4d%-4d%-4d%-4d%-4d%-4d%-4d%-4d", square, square + 1, square + 2,
|
||||
square + 3, square + 4, square + 5, square + 6, square + 7);
|
||||
System.out.println(lineContent);
|
||||
|
||||
} // End loop through all squares
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("AND HERE IS THE OPENING POSITION OF THE CHECKERS.");
|
||||
System.out.println("");
|
||||
|
||||
Arrays.fill(positions, 1);
|
||||
|
||||
// Begin generating start positions
|
||||
for (square = 19; square <= 43; square += 8) {
|
||||
|
||||
for (startPosition = square; startPosition <= square + 3; startPosition++) {
|
||||
|
||||
positions[startPosition] = 0;
|
||||
|
||||
}
|
||||
} // End generating start positions
|
||||
|
||||
numJumps = 0;
|
||||
|
||||
printBoard(positions);
|
||||
|
||||
nextStep = Step.GET_MOVE;
|
||||
break;
|
||||
|
||||
case GET_MOVE:
|
||||
|
||||
System.out.print("JUMP FROM? ");
|
||||
fromSquare = scan.nextInt();
|
||||
scan.nextLine(); // Discard newline
|
||||
|
||||
// User requested summary
|
||||
if (fromSquare == 0) {
|
||||
nextStep = Step.GET_SUMMARY;
|
||||
break;
|
||||
}
|
||||
|
||||
System.out.print("TO? ");
|
||||
toSquare = scan.nextInt();
|
||||
scan.nextLine(); // Discard newline
|
||||
System.out.println("");
|
||||
|
||||
// Check legality of move
|
||||
fromTest1 = (int) Math.floor((fromSquare - 1.0) / 8.0);
|
||||
fromTest2 = fromSquare - 8 * fromTest1;
|
||||
toTest1 = (int) Math.floor((toSquare - 1.0) / 8.0);
|
||||
toTest2 = toSquare - 8 * toTest1;
|
||||
|
||||
if ((fromTest1 > 7) ||
|
||||
(toTest1 > 7) ||
|
||||
(fromTest2 > 8) ||
|
||||
(toTest2 > 8) ||
|
||||
(Math.abs(fromTest1 - toTest1) != 2) ||
|
||||
(Math.abs(fromTest2 - toTest2) != 2) ||
|
||||
(positions[(toSquare + fromSquare) / 2] == 0) ||
|
||||
(positions[fromSquare] == 0) ||
|
||||
(positions[toSquare] == 1)) {
|
||||
|
||||
System.out.println("ILLEGAL MOVE. TRY AGAIN...");
|
||||
nextStep = Step.GET_MOVE;
|
||||
break;
|
||||
}
|
||||
|
||||
positions[toSquare] = 1;
|
||||
positions[fromSquare] = 0;
|
||||
positions[(toSquare + fromSquare) / 2] = 0;
|
||||
numJumps++;
|
||||
|
||||
printBoard(positions);
|
||||
|
||||
nextStep = Step.GET_MOVE;
|
||||
break;
|
||||
|
||||
case GET_SUMMARY:
|
||||
|
||||
numPieces = 0;
|
||||
|
||||
// Count remaining pieces
|
||||
for (square = 1; square <= 64; square++) {
|
||||
numPieces += positions[square];
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("YOU MADE " + numJumps + " JUMPS AND HAD " + numPieces + " PIECES");
|
||||
System.out.println("REMAINING ON THE BOARD.\n");
|
||||
|
||||
nextStep = Step.QUERY_RETRY;
|
||||
break;
|
||||
|
||||
case QUERY_RETRY:
|
||||
|
||||
while (true) {
|
||||
System.out.print("TRY AGAIN? ");
|
||||
userResponse = scan.nextLine();
|
||||
System.out.println("");
|
||||
|
||||
if (userResponse.toUpperCase().equals("YES")) {
|
||||
nextStep = Step.SHOW_BOARD;
|
||||
break;
|
||||
}
|
||||
else if (userResponse.toUpperCase().equals("NO")) {
|
||||
System.out.println("O.K. HOPE YOU HAD FUN!!");
|
||||
return;
|
||||
}
|
||||
else {
|
||||
System.out.println("PLEASE ANSWER 'YES' OR 'NO'.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("INVALID STEP");
|
||||
nextStep = Step.QUERY_RETRY;
|
||||
break;
|
||||
|
||||
} // End of switch
|
||||
|
||||
} // End outer while loop
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
public void printBoard(int[] positions) {
|
||||
|
||||
int column = 0;
|
||||
int row = 0;
|
||||
String lineContent = "";
|
||||
|
||||
// Begin loop through all rows
|
||||
for (row = 1; row <= 57; row += 8) {
|
||||
|
||||
// Begin loop through all columns
|
||||
for (column = row; column <= row + 7; column++) {
|
||||
|
||||
lineContent += " " + positions[column];
|
||||
|
||||
} // End loop through all columns
|
||||
|
||||
System.out.println(lineContent);
|
||||
lineContent = "";
|
||||
|
||||
} // End loop through all rows
|
||||
|
||||
System.out.println("");
|
||||
|
||||
} // End of method printBoard
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
OneCheck game = new OneCheck();
|
||||
game.play();
|
||||
|
||||
} // End of method main
|
||||
|
||||
} // End of class OneCheck
|
||||
@@ -1,3 +0,0 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Oracle Java](https://openjdk.java.net/)
|
||||
@@ -1,3 +0,0 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells)
|
||||
@@ -1,9 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>ONE CHECK</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="onecheck.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,151 +0,0 @@
|
||||
// ONE CHECK
|
||||
//
|
||||
// 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 a = [];
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(30) + "ONE CHECK\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
for (i = 0; i <= 64; i++)
|
||||
a[i] = 0;
|
||||
print("SOLITAIRE CHECKER PUZZLE BY DAVID AHL\n");
|
||||
print("\n");
|
||||
print("48 CHECKERS ARE PLACED ON THE 2 OUTSIDE SPACES OF A\n");
|
||||
print("STANDARD 64-SQUARE CHECKERBOARD. THE OBJECT IS TO\n");
|
||||
print("REMOVE AS MANY CHECKERS AS POSSIBLE BY DIAGONAL JUMPS\n");
|
||||
print("(AS IN STANDARD CHECKERS). USE THE NUMBERED BOARD TO\n");
|
||||
print("INDICATE THE SQUARE YOU WISH TO JUMP FROM AND TO. ON\n");
|
||||
print("THE BOARD PRINTED OUT ON EACH TURN '1' INDICATES A\n");
|
||||
print("CHECKER AND '0' AN EMPTY SQUARE. WHEN YOU HAVE NO\n");
|
||||
print("POSSIBLE JUMPS REMAINING, INPUT A '0' IN RESPONSE TO\n");
|
||||
print("QUESTION 'JUMP FROM ?'\n");
|
||||
print("\n");
|
||||
print("HERE IS THE NUMERICAL BOARD:\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
for (j = 1; j <= 57; j += 8) {
|
||||
str = "";
|
||||
for (i = 0; i <= 7; i++) {
|
||||
while (str.length < 4 * i)
|
||||
str += " ";
|
||||
str += " " + (j + i);
|
||||
}
|
||||
print(str + "\n");
|
||||
}
|
||||
print("\n");
|
||||
print("AND HERE IS THE OPENING POSITION OF THE CHECKERS.\n");
|
||||
print("\n");
|
||||
for (j = 1; j <= 64; j++)
|
||||
a[j] = 1;
|
||||
for (j = 19; j <= 43; j += 8)
|
||||
for (i = j; i <= j + 3; i++)
|
||||
a[i] = 0;
|
||||
m = 0;
|
||||
while (1) {
|
||||
// Print board
|
||||
for (j = 1; j <= 57; j += 8) {
|
||||
str = "";
|
||||
for (i = j; i <= j + 7; i++) {
|
||||
str += " " + a[i] + " ";
|
||||
}
|
||||
print(str + "\n");
|
||||
}
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("JUMP FROM");
|
||||
f = parseInt(await input());
|
||||
if (f == 0)
|
||||
break;
|
||||
print("TO");
|
||||
t = parseInt(await input());
|
||||
print("\n");
|
||||
// Check legality of move
|
||||
f1 = Math.floor((f - 1) / 8);
|
||||
f2 = f - 8 * f1;
|
||||
t1 = Math.floor((t - 1) / 8);
|
||||
t2 = t - 8 * t1;
|
||||
if (f1 > 7 || t1 > 7 || f2 > 8 || t2 > 8 || Math.abs(f1 - t1) != 2 || Math.abs(f2 - t2) != 2 || a[(t + f) / 2] == 0 || a[f] == 0 || a[t] == 1) {
|
||||
print("ILLEGAL MOVE. TRY AGAIN...\n");
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (f == 0)
|
||||
break;
|
||||
// Update board
|
||||
a[t] = 1;
|
||||
a[f] = 0;
|
||||
a[(t + f) / 2] = 0;
|
||||
m++;
|
||||
}
|
||||
// End game summary
|
||||
s = 0;
|
||||
for (i = 1; i <= 64; i++)
|
||||
s += a[i];
|
||||
print("\n");
|
||||
print("YOU MADE " + m + " JUMPS AND HAD " + s + " PIECES\n");
|
||||
print("REMAINING ON THE BOARD.\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("TRY AGAIN");
|
||||
str = await input();
|
||||
if (str == "YES")
|
||||
break;
|
||||
if (str == "NO")
|
||||
break;
|
||||
print("PLEASE ANSWER 'YES' OR 'NO'.\n");
|
||||
}
|
||||
if (str == "NO")
|
||||
break;
|
||||
}
|
||||
print("\n");
|
||||
print("O.K. HOPE YOU HAD FUN!!\n");
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -1,3 +0,0 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Pascal](https://en.wikipedia.org/wiki/Pascal_(programming_language))
|
||||
@@ -1,3 +0,0 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Perl](https://www.perl.org/)
|
||||
@@ -1,252 +0,0 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
use 5.010; # To get 'state' and 'say'
|
||||
|
||||
use strict; # Require explicit declaration of variables
|
||||
use warnings; # Enable optional compiler warnings
|
||||
|
||||
use English; # Use more friendly names for Perl's magic variables
|
||||
use List::Util qw{ sum }; # Add all its arguments
|
||||
use Term::ReadLine; # Prompt and return user input
|
||||
|
||||
our $VERSION = '0.000_01';
|
||||
|
||||
print <<'EOD';
|
||||
ONE CHECK
|
||||
Creative Computing Morristown, New Jersey
|
||||
|
||||
|
||||
|
||||
Solitaire checker puzzle by David Ahl
|
||||
|
||||
48 checkers are placed on the 2 outside spaces of a
|
||||
standard 64-square checkerboard. The object is to
|
||||
remove as many checkers as possible by diagonal jumps
|
||||
(as in standard checkers). Use the numbered board to
|
||||
indicate the square you wish to jump from and to. On
|
||||
the board printed out on each turn '1' indicates a
|
||||
checker and '0' an empty square. When you have no
|
||||
possible jumps remaining, input a '0' in response to
|
||||
question 'Jump from?'
|
||||
EOD
|
||||
|
||||
while ( 1 ) { # Iterate indefinitely
|
||||
|
||||
board_num(); # Display the numerical board.
|
||||
|
||||
# Initialize the board, which is a two-dimensional array.
|
||||
my @board = map { [ ( 1 ) x 8 ] } 0 .. 7; # Initialize to all 1.
|
||||
for my $row ( 2 .. 5 ) { # Set the center section to 0
|
||||
for my $col ( 2 .. 5 ) {
|
||||
$board[$row][$col] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
print <<'EOD';
|
||||
And here is the opening position of the checkers.
|
||||
|
||||
EOD
|
||||
board_pos( \@board );
|
||||
|
||||
my $moves = 0; # Number of moves made.
|
||||
|
||||
# A game proceeds while 'Jump from' is a true value. We make use of
|
||||
# the fact that of the possible returns, only 0 evaluates false.
|
||||
while ( my $jump_from = get_input(
|
||||
'Jump from? ',
|
||||
sub {
|
||||
$ARG = lc; # The caller sees this.
|
||||
return 1 if $ARG eq 'b';
|
||||
return unless m/ \A [0-9]+ \z /smx;
|
||||
$ARG += 0; # Numify, because string '00' is true.
|
||||
return $ARG < 65;
|
||||
},
|
||||
"Please enter a number from 0 to 64, or 'b' to re-display the numeric board\n"
|
||||
)
|
||||
) {
|
||||
if ( $jump_from eq 'b' ) {
|
||||
board_num();
|
||||
board_pos( \@board );
|
||||
next;
|
||||
}
|
||||
|
||||
my $jump_to = get_input(
|
||||
' to? ',
|
||||
sub { m/ \A [0-9]+ \z /smx },
|
||||
"Please enter a number from 1 to 64\n",
|
||||
);
|
||||
|
||||
if ( make_move( \@board, $jump_from, $jump_to ) ) {
|
||||
$moves++;
|
||||
board_pos( \@board );
|
||||
} else {
|
||||
say 'Illegal move. Try again.';
|
||||
}
|
||||
}
|
||||
|
||||
my $checkers_left = sum( map { sum( @{ $board[$_] } ) } 0 .. 7 );
|
||||
print <<"EOD";
|
||||
|
||||
You made $moves jumps and had $checkers_left pieces
|
||||
remaining on the board.
|
||||
|
||||
EOD
|
||||
|
||||
last unless get_yes_no( 'Try again' );
|
||||
|
||||
}
|
||||
|
||||
print <<'EOD';
|
||||
|
||||
O.K. Hope you had fun!!
|
||||
EOD
|
||||
|
||||
# Print the numerical board
|
||||
sub board_num {
|
||||
print <<'EOD';
|
||||
|
||||
Here is the numerical board:
|
||||
|
||||
EOD
|
||||
foreach my $row ( 0 .. 7 ) {
|
||||
state $tplt = ( '%3d' x 8 ) . "\n";
|
||||
my $inx = $row * 8;
|
||||
printf $tplt, map { $inx + $_ } 1 .. 8;
|
||||
}
|
||||
say '';
|
||||
return;
|
||||
}
|
||||
|
||||
# Print the board position
|
||||
sub board_pos {
|
||||
my ( $board ) = @_;
|
||||
for my $row ( 0 .. 7 ) {
|
||||
state $tplt = ( '%2d' x 8 ) . "\n";
|
||||
printf $tplt, @{ $board->[$row] };
|
||||
}
|
||||
say '';
|
||||
return;
|
||||
}
|
||||
|
||||
# Make the move. This is a subroutine for convenience in control flow.
|
||||
# We return a true value for success, and false for failure.
|
||||
sub make_move {
|
||||
my ( $board, $jump_from, $jump_to ) = @_;
|
||||
$jump_from -= 1;
|
||||
$jump_to -= 1;
|
||||
my $from_row = int( $jump_from / 8 ); # Truncates toward 0
|
||||
my $from_col = $jump_from % 8;
|
||||
my $to_row = int( $jump_to / 8 ); # Truncates toward 0
|
||||
my $to_col = $jump_to % 8;
|
||||
return unless $board->[$from_row][$from_col]; # From must be occupied
|
||||
return if $board->[$to_row][$to_col]; # To must be vacant
|
||||
return unless abs( $from_row - $to_row ) == 2; # Must cross two rows
|
||||
return unless abs( $from_col - $to_col ) == 2; # Must cross two cols
|
||||
my $over_row = ( $from_row + $to_row ) / 2; # The row jumped over
|
||||
my $over_col = ( $from_col + $to_col ) / 2; # The col jumped over
|
||||
$board->[$from_row][$from_col] = # Clear the from cell
|
||||
$board->[$over_row][$over_col] = 0; # and the jumped cell
|
||||
$board->[$to_row][$to_col] = 1; # Occupy the to cell
|
||||
return 1;
|
||||
}
|
||||
|
||||
# Get input from the user. The arguments are:
|
||||
# * The prompt
|
||||
# * A reference to validation code. This code receives the response in
|
||||
# $ARG and returns true for a valid response.
|
||||
# * A warning to print if the response is not valid. This must end in a
|
||||
# return.
|
||||
# The first valid response is returned. An end-of-file terminates the
|
||||
# script.
|
||||
sub get_input {
|
||||
my ( $prompt, $validate, $warning ) = @ARG;
|
||||
|
||||
# If no validator is passed, default to one that always returns
|
||||
# true.
|
||||
$validate ||= sub { 1 };
|
||||
|
||||
# Create the readline object. The 'state' causes the variable to be
|
||||
# initialized only once, no matter how many times this subroutine is
|
||||
# called. The do { ... } is a compound statement used because we
|
||||
# need to tweak the created object before we store it.
|
||||
state $term = do {
|
||||
my $obj = Term::ReadLine->new( 'reverse' );
|
||||
$obj->ornaments( 0 );
|
||||
$obj;
|
||||
};
|
||||
|
||||
while ( 1 ) { # Iterate indefinitely
|
||||
|
||||
# Read the input into the topic variable, localized to prevent
|
||||
# Spooky Action at a Distance. We exit on undef, which signals
|
||||
# end-of-file.
|
||||
exit unless defined( local $ARG = $term->readline( $prompt ) );
|
||||
|
||||
# Return the input if it is valid.
|
||||
return $ARG if $validate->();
|
||||
|
||||
# Issue the warning, and go around the merry-go-round again.
|
||||
warn $warning;
|
||||
}
|
||||
}
|
||||
|
||||
# Get a yes-or-no answer. The argument is the prompt, which will have
|
||||
# '? [y/n]: ' appended. The donkey work is done by get_input(), which is
|
||||
# requested to validate the response as beginning with 'y' or 'n',
|
||||
# case-insensitive. The return is a true value for 'y' and a false value
|
||||
# for 'n'.
|
||||
sub get_yes_no {
|
||||
my ( $prompt ) = @ARG;
|
||||
state $map_answer = {
|
||||
n => 0,
|
||||
y => 1,
|
||||
};
|
||||
my $resp = lc get_input(
|
||||
"$prompt? [y/n]: ",
|
||||
sub { m/ \A [yn] /smxi },
|
||||
"Please respond 'y' or 'n'\n",
|
||||
);
|
||||
return $map_answer->{ substr $resp, 0, 1 };
|
||||
}
|
||||
|
||||
__END__
|
||||
|
||||
=head1 TITLE
|
||||
|
||||
one check - Play the game 'One Check' from Basic Computer Games
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
one check.pl
|
||||
|
||||
=head1 DETAILS
|
||||
|
||||
This Perl script is a port of onecheck.
|
||||
|
||||
This is a solitaire game played on a checker board, where the object is
|
||||
to eliminate as many checkers as possible by making diagonal jumps and
|
||||
removing the jumped checkers.
|
||||
|
||||
It is pretty much a straight port of the BASIC original.
|
||||
|
||||
=head1 PORTED BY
|
||||
|
||||
Thomas R. Wyant, III F<wyant at cpan dot org>
|
||||
|
||||
=head1 COPYRIGHT AND LICENSE
|
||||
|
||||
Copyright (C) 2022 by Thomas R. Wyant, III
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the same terms as Perl 5.10.0. For more details, see the Artistic
|
||||
License 1.0 at
|
||||
L<https://www.perlfoundation.org/artistic-license-10.html>, and/or the
|
||||
Gnu GPL at L<http://www.gnu.org/licenses/old-licenses/gpl-1.0.txt>.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but
|
||||
without any warranty; without even the implied warranty of
|
||||
merchantability or fitness for a particular purpose.
|
||||
|
||||
=cut
|
||||
|
||||
# ex: set expandtab tabstop=4 textwidth=72 :
|
||||
@@ -1,3 +0,0 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Python](https://www.python.org/about/)
|
||||
@@ -1,128 +0,0 @@
|
||||
# ONE CHECK
|
||||
|
||||
# Port to python by imiro
|
||||
|
||||
|
||||
def tab(x):
|
||||
return " " * x
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
# Initial instructions
|
||||
print(tab(30) + "ONE CHECK")
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print("SOLITAIRE CHECKER PUZZLE BY DAVID AHL")
|
||||
print()
|
||||
print("48 CHECKERS ARE PLACED ON THE 2 OUTSIDE SPACES OF A")
|
||||
print("STANDARD 64-SQUARE CHECKERBOARD. THE OBJECT IS TO")
|
||||
print("REMOVE AS MANY CHECKERS AS POSSIBLE BY DIAGONAL JUMPS")
|
||||
print("(AS IN STANDARD CHECKERS). USE THE NUMBERED BOARD TO")
|
||||
print("INDICATE THE SQUARE YOU WISH TO JUMP FROM AND TO. ON")
|
||||
print("THE BOARD PRINTED OUT ON EACH TURN '1' INDICATES A")
|
||||
print("CHECKER AND '0' AN EMPTY SQUARE. WHEN YOU HAVE NO")
|
||||
print("POSSIBLE JUMPS REMAINING, INPUT A '0' IN RESPONSE TO")
|
||||
print("QUESTION 'JUMP FROM ?'")
|
||||
print()
|
||||
print("HERE IS THE NUMERICAL BOARD:")
|
||||
print()
|
||||
|
||||
while True:
|
||||
for j in range(1, 64, 8):
|
||||
for i in range(j, j + 7):
|
||||
print(i, end=(" " * (3 if i < 10 else 2)))
|
||||
print(j + 7)
|
||||
print()
|
||||
print("AND HERE IS THE OPENING POSITION OF THE CHECKERS.")
|
||||
print()
|
||||
|
||||
(jumps, left) = play_game()
|
||||
|
||||
print()
|
||||
print("YOU MADE " + jumps + " JUMPS AND HAD " + left + " PIECES")
|
||||
print("REMAINING ON THE BOARD.")
|
||||
print()
|
||||
|
||||
if not (try_again()):
|
||||
break
|
||||
|
||||
print()
|
||||
print("O.K. HOPE YOU HAD FUN!!")
|
||||
|
||||
|
||||
def play_game():
|
||||
# Initialize board
|
||||
# Give more than 64 elements to accomodate 1-based indexing
|
||||
board = [1] * 70
|
||||
for j in range(19, 44, 8):
|
||||
for i in range(j, j + 4):
|
||||
board[i] = 0
|
||||
jumps = 0
|
||||
while True:
|
||||
# print board
|
||||
for j in range(1, 64, 8):
|
||||
for i in range(j, j + 7):
|
||||
print(board[i], end=" ")
|
||||
print(board[j + 7])
|
||||
print()
|
||||
|
||||
while True:
|
||||
print("JUMP FROM", end=" ")
|
||||
f = input()
|
||||
f = int(f)
|
||||
if f == 0:
|
||||
break
|
||||
print("TO", end=" ")
|
||||
t = input()
|
||||
t = int(t)
|
||||
print()
|
||||
|
||||
# Check legality of move
|
||||
f1 = (f - 1) // 8
|
||||
f2 = f - 8 * f1
|
||||
t1 = (t - 1) // 8
|
||||
t2 = t - 8 * t1
|
||||
if (
|
||||
f1 > 7
|
||||
or t1 > 7
|
||||
or f2 > 8
|
||||
or t2 > 8
|
||||
or abs(f1 - t1) != 2
|
||||
or abs(f2 - t2) != 2
|
||||
or board[(t + f) // 2] == 0
|
||||
or board[f] == 0
|
||||
or board[t] == 1
|
||||
):
|
||||
print("ILLEGAL MOVE. TRY AGAIN...")
|
||||
continue
|
||||
break
|
||||
|
||||
if f == 0:
|
||||
break
|
||||
board[t] = 1
|
||||
board[f] = 0
|
||||
board[(t + f) // 2] = 0
|
||||
jumps = jumps + 1
|
||||
|
||||
left = 0
|
||||
for i in range(1, 64 + 1):
|
||||
left = left + board[i]
|
||||
return (str(jumps), str(left))
|
||||
|
||||
|
||||
def try_again():
|
||||
print("TRY AGAIN", end=" ")
|
||||
answer = input()
|
||||
if answer.upper() == "YES":
|
||||
return True
|
||||
elif answer.upper() == "NO":
|
||||
return False
|
||||
print("PLEASE ANSWER 'YES' OR 'NO'.")
|
||||
try_again()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,3 +0,0 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Ruby](https://www.ruby-lang.org/en/)
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "OneCheck", "OneCheck.vbproj", "{E19E8AE0-06FE-4EB5-9F24-7FF74D65F55A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{E19E8AE0-06FE-4EB5-9F24-7FF74D65F55A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E19E8AE0-06FE-4EB5-9F24-7FF74D65F55A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E19E8AE0-06FE-4EB5-9F24-7FF74D65F55A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E19E8AE0-06FE-4EB5-9F24-7FF74D65F55A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -1,8 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>OneCheck</RootNamespace>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<LangVersion>16.9</LangVersion>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,3 +0,0 @@
|
||||
Original BASIC source [downloaded from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Visual Basic .NET](https://en.wikipedia.org/wiki/Visual_Basic_.NET)
|
||||
Reference in New Issue
Block a user