mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-27 07:14:48 -08:00
MAINT: Apply pre-commit
Remove byte-order-marker pre-commit check as there would be many adjustments necessary
This commit is contained in:
@@ -9,43 +9,43 @@ import java.lang.Math;
|
||||
* <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 Reverse {
|
||||
|
||||
|
||||
public class Reverse {
|
||||
|
||||
private final int NUMBER_COUNT = 9;
|
||||
|
||||
|
||||
private final Scanner scan; // For user input
|
||||
|
||||
|
||||
private enum Step {
|
||||
INITIALIZE, PERFORM_REVERSE, TRY_AGAIN, END_GAME
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Reverse() {
|
||||
|
||||
|
||||
scan = new Scanner(System.in);
|
||||
|
||||
} // End of constructor Reverse
|
||||
|
||||
} // End of constructor Reverse
|
||||
|
||||
public void play() {
|
||||
|
||||
|
||||
showIntro();
|
||||
startGame();
|
||||
|
||||
} // End of method play
|
||||
|
||||
|
||||
} // End of method play
|
||||
|
||||
private static void showIntro() {
|
||||
|
||||
|
||||
System.out.println(" ".repeat(31) + "REVERSE");
|
||||
System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n");
|
||||
System.out.println("REVERSE -- A GAME OF SKILL");
|
||||
System.out.println("");
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
private void startGame() {
|
||||
|
||||
int index = 0;
|
||||
@@ -53,162 +53,162 @@ public class Reverse {
|
||||
int numReverse = 0;
|
||||
int tempVal = 0;
|
||||
int[] numList = new int[NUMBER_COUNT + 1];
|
||||
|
||||
|
||||
Step nextStep = Step.INITIALIZE;
|
||||
|
||||
|
||||
String userResponse = "";
|
||||
|
||||
|
||||
System.out.print("DO YOU WANT THE RULES? ");
|
||||
userResponse = scan.nextLine();
|
||||
|
||||
|
||||
if (!userResponse.toUpperCase().equals("NO")) {
|
||||
|
||||
this.printRules();
|
||||
|
||||
this.printRules();
|
||||
}
|
||||
|
||||
|
||||
// Begin outer while loop
|
||||
while (true) {
|
||||
|
||||
|
||||
// Begin outer switch
|
||||
switch (nextStep) {
|
||||
|
||||
|
||||
case INITIALIZE:
|
||||
|
||||
// Make a random list of numbers
|
||||
|
||||
// Make a random list of numbers
|
||||
numList[1] = (int)((NUMBER_COUNT - 1) * Math.random() + 2);
|
||||
|
||||
|
||||
for (index = 2; index <= NUMBER_COUNT; index++) {
|
||||
|
||||
|
||||
// Keep generating lists if there are duplicates
|
||||
while (true) {
|
||||
|
||||
|
||||
numList[index] = (int)(NUMBER_COUNT * Math.random() + 1);
|
||||
|
||||
// Search for duplicates
|
||||
if (!this.findDuplicates(numList, index)) {
|
||||
if (!this.findDuplicates(numList, index)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("HERE WE GO ... THE LIST IS:");
|
||||
|
||||
|
||||
numMoves = 0;
|
||||
|
||||
|
||||
this.printBoard(numList);
|
||||
|
||||
|
||||
nextStep = Step.PERFORM_REVERSE;
|
||||
break;
|
||||
|
||||
|
||||
case PERFORM_REVERSE:
|
||||
|
||||
|
||||
System.out.print("HOW MANY SHALL I REVERSE? ");
|
||||
numReverse = Integer.parseInt(scan.nextLine());
|
||||
|
||||
if (numReverse == 0) {
|
||||
|
||||
nextStep = Step.TRY_AGAIN;
|
||||
|
||||
|
||||
if (numReverse == 0) {
|
||||
|
||||
nextStep = Step.TRY_AGAIN;
|
||||
|
||||
} else if (numReverse > NUMBER_COUNT) {
|
||||
|
||||
System.out.println("OOPS! TOO MANY! I CAN REVERSE AT MOST " + NUMBER_COUNT);
|
||||
nextStep = Step.PERFORM_REVERSE;
|
||||
|
||||
|
||||
System.out.println("OOPS! TOO MANY! I CAN REVERSE AT MOST " + NUMBER_COUNT);
|
||||
nextStep = Step.PERFORM_REVERSE;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
numMoves++;
|
||||
|
||||
|
||||
for (index = 1; index <= (int)(numReverse / 2.0); index++) {
|
||||
|
||||
|
||||
tempVal = numList[index];
|
||||
numList[index] = numList[numReverse - index + 1];
|
||||
numList[numReverse - index + 1] = tempVal;
|
||||
}
|
||||
|
||||
this.printBoard(numList);
|
||||
|
||||
|
||||
nextStep = Step.TRY_AGAIN;
|
||||
|
||||
// Check for a win
|
||||
|
||||
// Check for a win
|
||||
for (index = 1; index <= NUMBER_COUNT; index++) {
|
||||
|
||||
if (numList[index] != index) {
|
||||
nextStep = Step.PERFORM_REVERSE;
|
||||
}
|
||||
|
||||
if (numList[index] != index) {
|
||||
nextStep = Step.PERFORM_REVERSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (nextStep == Step.TRY_AGAIN) {
|
||||
System.out.println("YOU WON IT IN " + numMoves + " MOVES!!!");
|
||||
System.out.println("YOU WON IT IN " + numMoves + " MOVES!!!");
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case TRY_AGAIN:
|
||||
|
||||
|
||||
System.out.println("");
|
||||
System.out.print("TRY AGAIN (YES OR NO)? ");
|
||||
userResponse = scan.nextLine();
|
||||
|
||||
|
||||
if (userResponse.toUpperCase().equals("YES")) {
|
||||
nextStep = Step.INITIALIZE;
|
||||
} else {
|
||||
nextStep = Step.END_GAME;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case END_GAME:
|
||||
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("O.K. HOPE YOU HAD FUN!!");
|
||||
return;
|
||||
|
||||
return;
|
||||
|
||||
default:
|
||||
|
||||
System.out.println("INVALID STEP");
|
||||
|
||||
System.out.println("INVALID STEP");
|
||||
break;
|
||||
|
||||
} // End outer switch
|
||||
|
||||
} // End outer while loop
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
} // End outer switch
|
||||
|
||||
} // End outer while loop
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
public boolean findDuplicates(int[] board, int length) {
|
||||
|
||||
|
||||
int index = 0;
|
||||
|
||||
|
||||
for (index = 1; index <= length - 1; index++) {
|
||||
|
||||
|
||||
// Identify duplicates
|
||||
if (board[length] == board[index]) {
|
||||
|
||||
|
||||
return true; // Found a duplicate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false; // No duplicates found
|
||||
|
||||
|
||||
} // End of method findDuplicates
|
||||
|
||||
|
||||
public void printBoard(int[] board) {
|
||||
|
||||
|
||||
int index = 0;
|
||||
|
||||
|
||||
System.out.println("");
|
||||
|
||||
|
||||
for (index = 1; index <= NUMBER_COUNT; index++) {
|
||||
|
||||
System.out.format("%2d", board[index]);
|
||||
|
||||
System.out.format("%2d", board[index]);
|
||||
}
|
||||
|
||||
System.out.println("\n");
|
||||
|
||||
|
||||
System.out.println("\n");
|
||||
|
||||
} // End of method printBoard
|
||||
|
||||
|
||||
public void printRules() {
|
||||
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("THIS IS THE GAME OF 'REVERSE'. TO WIN, ALL YOU HAVE");
|
||||
System.out.println("TO DO IS ARRANGE A LIST OF NUMBERS (1 THROUGH " + NUMBER_COUNT + ")");
|
||||
@@ -229,14 +229,14 @@ public class Reverse {
|
||||
System.out.println("NO DOUBT YOU WILL LIKE THIS GAME, BUT");
|
||||
System.out.println("IF YOU WANT TO QUIT, REVERSE 0 (ZERO).");
|
||||
System.out.println("");
|
||||
|
||||
|
||||
} // End of method printRules
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
Reverse game = new Reverse();
|
||||
game.play();
|
||||
|
||||
|
||||
} // End of method main
|
||||
|
||||
|
||||
} // End of class Reverse
|
||||
|
||||
@@ -12,10 +12,10 @@ 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");
|
||||
|
||||
@@ -2,25 +2,24 @@
|
||||
import random
|
||||
import textwrap
|
||||
|
||||
|
||||
NUMCNT = 9 # How many numbers are we playing with?
|
||||
|
||||
|
||||
def play():
|
||||
print('REVERSE'.center(72))
|
||||
print('CREATIVE COMPUTING MORRISTOWN, NEW JERSEY'.center(72))
|
||||
print("REVERSE".center(72))
|
||||
print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".center(72))
|
||||
print()
|
||||
print()
|
||||
print('REVERSE -- A GAME OF SKILL')
|
||||
print("REVERSE -- A GAME OF SKILL")
|
||||
print()
|
||||
|
||||
if not input('DO YOU WANT THE RULES? (yes/no) ').lower().startswith('n'):
|
||||
if not input("DO YOU WANT THE RULES? (yes/no) ").lower().startswith("n"):
|
||||
rules()
|
||||
|
||||
while True:
|
||||
game_loop()
|
||||
|
||||
if not input('TRY AGAIN? (yes/no) ').lower().startswith('y'):
|
||||
if not input("TRY AGAIN? (yes/no) ").lower().startswith("y"):
|
||||
return
|
||||
|
||||
|
||||
@@ -32,13 +31,13 @@ def game_loop():
|
||||
|
||||
# Print original list and start the game
|
||||
print()
|
||||
print('HERE WE GO ... THE LIST IS:')
|
||||
print("HERE WE GO ... THE LIST IS:")
|
||||
print_list(numbers)
|
||||
|
||||
turns = 0
|
||||
while True:
|
||||
try:
|
||||
howmany = int(input('HOW MANY SHALL I REVERSE? '))
|
||||
howmany = int(input("HOW MANY SHALL I REVERSE? "))
|
||||
assert howmany >= 0
|
||||
except (ValueError, AssertionError):
|
||||
continue
|
||||
@@ -47,7 +46,7 @@ def game_loop():
|
||||
return
|
||||
|
||||
if howmany > NUMCNT:
|
||||
print('OOPS! WRONG! I CAN REVERSE AT MOST', NUMCNT)
|
||||
print("OOPS! WRONG! I CAN REVERSE AT MOST", NUMCNT)
|
||||
continue
|
||||
|
||||
turns += 1
|
||||
@@ -62,19 +61,20 @@ def game_loop():
|
||||
|
||||
# Check for a win
|
||||
if all(numbers[i] == i + 1 for i in range(NUMCNT)):
|
||||
print('YOU WON IT IN {} MOVES!'.format(turns))
|
||||
print(f"YOU WON IT IN {turns} MOVES!")
|
||||
print()
|
||||
return
|
||||
|
||||
|
||||
def print_list(numbers):
|
||||
"""Print out the list"""
|
||||
print(' '.join(map(str, numbers)))
|
||||
print(" ".join(map(str, numbers)))
|
||||
|
||||
|
||||
def rules():
|
||||
"""Print out the rules"""
|
||||
help = textwrap.dedent("""
|
||||
help = textwrap.dedent(
|
||||
"""
|
||||
THIS IS THE GAME OF "REVERSE". TO WIN, ALL YOU HAVE
|
||||
TO DO IS ARRANGE A LIST OF NUMBERS (1 THROUGH {})
|
||||
IN NUMERICAL ORDER FROM LEFT TO RIGHT. TO MOVE, YOU
|
||||
@@ -93,12 +93,15 @@ def rules():
|
||||
|
||||
NO DOUBT YOU WILL LIKE THIS GAME, BUT
|
||||
IF YOU WANT TO QUIT, REVERSE 0 (ZERO).
|
||||
""".format(NUMCNT))
|
||||
""".format(
|
||||
NUMCNT
|
||||
)
|
||||
)
|
||||
print(help)
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
play()
|
||||
except KeyboardInterrupt:
|
||||
|
||||
@@ -55,7 +55,7 @@ def reverseIt (howManyToReverse, triesSoFar)
|
||||
# extract and reverse the first howManyToReverse elements of the array
|
||||
subArray = $digitArray.take(howManyToReverse)
|
||||
subArray.reverse!
|
||||
|
||||
|
||||
# get the remaining elements of the original array
|
||||
endArray = $digitArray.slice(howManyToReverse, ARRAYSIZE)
|
||||
# append those elements to the reversed elements
|
||||
@@ -108,4 +108,3 @@ while r != 0 do # zero will end the game
|
||||
end
|
||||
r = askHowManyToReverse
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user