mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
Use docstrings
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from typing import Tuple, Any
|
from typing import Any, Tuple
|
||||||
|
|
||||||
|
|
||||||
class WinOptions(IntEnum):
|
class WinOptions(IntEnum):
|
||||||
@@ -41,7 +41,7 @@ class StartOptions(IntEnum):
|
|||||||
|
|
||||||
|
|
||||||
def print_intro() -> None:
|
def print_intro() -> None:
|
||||||
"""Prints out the introduction and rules for the game."""
|
"""Print out the introduction and rules for the game."""
|
||||||
print("BATNUM".rjust(33, " "))
|
print("BATNUM".rjust(33, " "))
|
||||||
print("CREATIVE COMPUTING MORRISSTOWN, NEW JERSEY".rjust(15, " "))
|
print("CREATIVE COMPUTING MORRISSTOWN, NEW JERSEY".rjust(15, " "))
|
||||||
print()
|
print()
|
||||||
|
|||||||
@@ -1,23 +1,21 @@
|
|||||||
######################################################################
|
"""
|
||||||
#
|
Buzzword Generator
|
||||||
# Buzzword Generator
|
|
||||||
#
|
From: BASIC Computer Games (1978)
|
||||||
# From: BASIC Computer Games (1978)
|
Edited by David H. Ahl
|
||||||
# Edited by David H. Ahl
|
|
||||||
#
|
"This program is an invaluable aid for preparing speeches and
|
||||||
# "This program is an invaluable aid for preparing speeches and
|
briefings about education technology. This buzzword generator
|
||||||
# briefings about education technology. This buzzword generator
|
provides sets of three highly-acceptable words to work into your
|
||||||
# provides sets of three highly-acceptable words to work into your
|
material. Your audience will never know that the phrases don't
|
||||||
# material. Your audience will never know that the phrases don't
|
really mean much of anything because they sound so great! Full
|
||||||
# really mean much of anything because they sound so great! Full
|
instructions for running are given in the program.
|
||||||
# instructions for running are given in the program.
|
|
||||||
#
|
"This version of Buzzword was written by David Ahl."
|
||||||
# "This version of Buzzword was written by David Ahl."
|
|
||||||
#
|
|
||||||
#
|
Python port by Jeff Jetton, 2019
|
||||||
# Python port by Jeff Jetton, 2019
|
"""
|
||||||
#
|
|
||||||
######################################################################
|
|
||||||
|
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|||||||
@@ -1,24 +1,23 @@
|
|||||||
########################################################
|
"""
|
||||||
# Calendar
|
Calendar
|
||||||
#
|
|
||||||
# From: BASIC Computer Games (1978)
|
From: BASIC Computer Games (1978)
|
||||||
# Edited by David Ahl#
|
Edited by David Ahl#
|
||||||
#
|
|
||||||
# This program prints out a calendar
|
This program prints out a calendar
|
||||||
# for any year. You must specify the
|
for any year. You must specify the
|
||||||
# starting day of the week of the year in
|
starting day of the week of the year in
|
||||||
# statement 130. (Sunday(0), Monday
|
statement 130. (Sunday(0), Monday
|
||||||
# (-1), Tuesday(-2), etc.) You can determine
|
(-1), Tuesday(-2), etc.) You can determine
|
||||||
# this by using the program WEEKDAY.
|
this by using the program WEEKDAY.
|
||||||
# You must also make two changes
|
You must also make two changes
|
||||||
# for leap years in statement 360 and 620.
|
for leap years in statement 360 and 620.
|
||||||
# The program listing describes the necessary
|
The program listing describes the necessary
|
||||||
# changes. Running the program produces a
|
changes. Running the program produces a
|
||||||
# nice 12-month calendar.
|
nice 12-month calendar.
|
||||||
# The program was written by Geofrey
|
The program was written by Geofrey
|
||||||
# Chase of the Abbey, Portsmouth, Rhode Island.
|
Chase of the Abbey, Portsmouth, Rhode Island.
|
||||||
#
|
"""
|
||||||
########################################################
|
|
||||||
|
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
@@ -150,8 +149,6 @@ def main() -> None:
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
########################################################
|
|
||||||
#
|
|
||||||
########################################################
|
########################################################
|
||||||
#
|
#
|
||||||
# Porting notes:
|
# Porting notes:
|
||||||
|
|||||||
@@ -1,30 +1,27 @@
|
|||||||
########################################################
|
"""
|
||||||
#
|
Dice
|
||||||
# Dice
|
|
||||||
#
|
|
||||||
# From: BASIC Computer Games (1978)
|
|
||||||
# Edited by David H. Ahl
|
|
||||||
#
|
|
||||||
# "Not exactly a game, this program simulates rolling
|
|
||||||
# a pair of dice a large number of times and prints out
|
|
||||||
# the frequency distribution. You simply input the
|
|
||||||
# number of rolls. It is interesting to see how many
|
|
||||||
# rolls are necessary to approach the theoretical
|
|
||||||
# distribution:
|
|
||||||
#
|
|
||||||
# 2 1/36 2.7777...%
|
|
||||||
# 3 2/36 5.5555...%
|
|
||||||
# 4 3/36 8.3333...%
|
|
||||||
# etc.
|
|
||||||
#
|
|
||||||
# "Daniel Freidus wrote this program while in the
|
|
||||||
# seventh grade at Harrison Jr-Sr High School,
|
|
||||||
# Harrison, New York."
|
|
||||||
#
|
|
||||||
# Python port by Jeff Jetton, 2019
|
|
||||||
#
|
|
||||||
########################################################
|
|
||||||
|
|
||||||
|
From: BASIC Computer Games (1978)
|
||||||
|
Edited by David H. Ahl
|
||||||
|
|
||||||
|
"Not exactly a game, this program simulates rolling
|
||||||
|
a pair of dice a large number of times and prints out
|
||||||
|
the frequency distribution. You simply input the
|
||||||
|
number of rolls. It is interesting to see how many
|
||||||
|
rolls are necessary to approach the theoretical
|
||||||
|
distribution:
|
||||||
|
|
||||||
|
2 1/36 2.7777...%
|
||||||
|
3 2/36 5.5555...%
|
||||||
|
4 3/36 8.3333...%
|
||||||
|
etc.
|
||||||
|
|
||||||
|
"Daniel Freidus wrote this program while in the
|
||||||
|
seventh grade at Harrison Jr-Sr High School,
|
||||||
|
Harrison, New York."
|
||||||
|
|
||||||
|
Python port by Jeff Jetton, 2019
|
||||||
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,22 @@
|
|||||||
########################################################
|
"""
|
||||||
#
|
Guess
|
||||||
# Guess
|
|
||||||
#
|
From: Basic Computer Games (1978)
|
||||||
# From: Basic Computer Games (1978)
|
|
||||||
#
|
"In program Guess, the computer chooses a random
|
||||||
# "In program Guess, the computer chooses a random
|
integer between 0 and any limit and any limit you
|
||||||
# integer between 0 and any limit and any limit you
|
set. You must then try to guess the number the
|
||||||
# set. You must then try to guess the number the
|
computer has choosen using the clues provideed by
|
||||||
# computer has choosen using the clues provideed by
|
the computer.
|
||||||
# the computer.
|
You should be able to guess the number in one less
|
||||||
# You should be able to guess the number in one less
|
than the number of digits needed to represent the
|
||||||
# than the number of digits needed to represent the
|
number in binary notation - i.e. in base 2. This ought
|
||||||
# number in binary notation - i.e. in base 2. This ought
|
to give you a clue as to the optimum search technique.
|
||||||
# to give you a clue as to the optimum search technique.
|
Guess converted from the original program in FOCAL
|
||||||
# Guess converted from the original program in FOCAL
|
which appeared in the book "Computers in the Classroom"
|
||||||
# which appeared in the book "Computers in the Classroom"
|
by Walt Koetke of Lexington High School, Lexington,
|
||||||
# by Walt Koetke of Lexington High School, Lexington,
|
Massaschusetts.
|
||||||
# Massaschusetts.
|
"""
|
||||||
#
|
|
||||||
########################################################
|
|
||||||
|
|
||||||
# Altough the introduction says that the computer chooses
|
# Altough the introduction says that the computer chooses
|
||||||
# a number between 0 and any limit, it actually chooses
|
# a number between 0 and any limit, it actually chooses
|
||||||
|
|||||||
@@ -1,43 +1,46 @@
|
|||||||
def new_board():
|
from typing import Dict
|
||||||
# Using a dictionary in python to store the board, since we are not including all numbers within a given range.
|
|
||||||
board = {}
|
|
||||||
for i in [
|
def new_board() -> Dict[int, str]:
|
||||||
13,
|
"""
|
||||||
14,
|
Using a dictionary in python to store the board,
|
||||||
15,
|
since we are not including all numbers within a given range.
|
||||||
22,
|
"""
|
||||||
23,
|
return {
|
||||||
24,
|
13: "!",
|
||||||
29,
|
14: "!",
|
||||||
30,
|
15: "!",
|
||||||
31,
|
22: "!",
|
||||||
32,
|
23: "!",
|
||||||
33,
|
24: "!",
|
||||||
34,
|
29: "!",
|
||||||
35,
|
30: "!",
|
||||||
38,
|
31: "!",
|
||||||
39,
|
32: "!",
|
||||||
40,
|
33: "!",
|
||||||
42,
|
34: "!",
|
||||||
43,
|
35: "!",
|
||||||
44,
|
38: "!",
|
||||||
47,
|
39: "!",
|
||||||
48,
|
40: "!",
|
||||||
49,
|
42: "!",
|
||||||
50,
|
43: "!",
|
||||||
51,
|
44: "!",
|
||||||
52,
|
47: "!",
|
||||||
53,
|
48: "!",
|
||||||
58,
|
49: "!",
|
||||||
59,
|
50: "!",
|
||||||
60,
|
51: "!",
|
||||||
67,
|
52: "!",
|
||||||
68,
|
53: "!",
|
||||||
69,
|
58: "!",
|
||||||
]:
|
59: "!",
|
||||||
board[i] = "!"
|
60: "!",
|
||||||
board[41] = "O"
|
67: "!",
|
||||||
return board
|
68: "!",
|
||||||
|
69: "!",
|
||||||
|
41: "O",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def print_instructions() -> None:
|
def print_instructions() -> None:
|
||||||
@@ -73,7 +76,7 @@ NUMBERS. OK, LET'S BEGIN.
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def print_board(board) -> None:
|
def print_board(board: Dict[int, str]) -> None:
|
||||||
"""Prints the boards using indexes in the passed parameter"""
|
"""Prints the boards using indexes in the passed parameter"""
|
||||||
print(" " * 2 + board[13] + board[14] + board[15])
|
print(" " * 2 + board[13] + board[14] + board[15])
|
||||||
print(" " * 2 + board[22] + board[23] + board[24])
|
print(" " * 2 + board[22] + board[23] + board[24])
|
||||||
@@ -131,7 +134,7 @@ def play_game() -> None:
|
|||||||
print("SAVE THIS PAPER AS A RECORD OF YOUR ACCOMPLISHMENT!")
|
print("SAVE THIS PAPER AS A RECORD OF YOUR ACCOMPLISHMENT!")
|
||||||
|
|
||||||
|
|
||||||
def move(board):
|
def move(board: Dict[int, str]) -> bool:
|
||||||
"""Queries the user to move. Returns false if the user puts in an invalid input or move, returns true if the move was successful"""
|
"""Queries the user to move. Returns false if the user puts in an invalid input or move, returns true if the move was successful"""
|
||||||
start_input = input("MOVE WHICH PIECE? ")
|
start_input = input("MOVE WHICH PIECE? ")
|
||||||
|
|
||||||
@@ -154,7 +157,7 @@ def move(board):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
difference = abs(start - end)
|
difference = abs(start - end)
|
||||||
center = (end + start) / 2
|
center = int((end + start) / 2)
|
||||||
if (
|
if (
|
||||||
(difference == 2 or difference == 18)
|
(difference == 2 or difference == 18)
|
||||||
and board[end] == "O"
|
and board[end] == "O"
|
||||||
@@ -175,20 +178,20 @@ def main() -> None:
|
|||||||
play_game()
|
play_game()
|
||||||
|
|
||||||
|
|
||||||
def is_game_finished(board):
|
def is_game_finished(board) -> bool:
|
||||||
# Checks all locations and whether or not a move is possible at that location.
|
"""Check all locations and whether or not a move is possible at that location."""
|
||||||
for pos in board.keys():
|
for pos in board.keys():
|
||||||
if board[pos] == "!":
|
if board[pos] == "!":
|
||||||
for space in [1, 9]:
|
for space in [1, 9]:
|
||||||
# Checks if the next location has a peg
|
# Checks if the next location has a peg
|
||||||
nextToPeg = ((pos + space) in board) and board[pos + space] == "!"
|
next_to_peg = ((pos + space) in board) and board[pos + space] == "!"
|
||||||
# Checks both going forward (+ location) or backwards (-location)
|
# Checks both going forward (+ location) or backwards (-location)
|
||||||
hasMovableSpace = (
|
has_movable_space = (
|
||||||
not ((pos - space) in board and board[pos - space] == "!")
|
not ((pos - space) in board and board[pos - space] == "!")
|
||||||
) or (
|
) or (
|
||||||
not ((pos + space * 2) in board and board[pos + space * 2] == "!")
|
not ((pos + space * 2) in board and board[pos + space * 2] == "!")
|
||||||
)
|
)
|
||||||
if nextToPeg and hasMovableSpace:
|
if next_to_peg and has_movable_space:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from random import random
|
|||||||
|
|
||||||
|
|
||||||
def direction(A, B, X, Y) -> None:
|
def direction(A, B, X, Y) -> None:
|
||||||
"""Prints the direction hint for finding the hurkle."""
|
"""Print the direction hint for finding the hurkle."""
|
||||||
|
|
||||||
print("GO ", end="")
|
print("GO ", end="")
|
||||||
if Y < B:
|
if Y < B:
|
||||||
|
|||||||
@@ -1,24 +1,22 @@
|
|||||||
######################################################################
|
"""
|
||||||
#
|
LOVE
|
||||||
# LOVE
|
|
||||||
#
|
From: BASIC Computer Games (1978)
|
||||||
# From: BASIC Computer Games (1978)
|
Edited by David H. Ahl
|
||||||
# Edited by David H. Ahl
|
|
||||||
#
|
"This program is designed to reproduce Robert Indiana's great art
|
||||||
# "This program is designed to reproduce Robert Indiana's great art
|
work 'Love' with a message of your choice up to 60 characters long.
|
||||||
# work 'Love' with a message of your choice up to 60 characters long.
|
|
||||||
#
|
"The [DATA variable is] an alternating count of the number
|
||||||
# "The [DATA variable is] an alternating count of the number
|
of characters and blanks which form the design. These data give
|
||||||
# of characters and blanks which form the design. These data give
|
the correct proportions for a standard 10 character per inch
|
||||||
# the correct proportions for a standard 10 character per inch
|
Teletype or line printer.
|
||||||
# Teletype or line printer.
|
|
||||||
#
|
"The LOVE program was created by David Ahl."
|
||||||
# "The LOVE program was created by David Ahl."
|
|
||||||
#
|
|
||||||
#
|
Python port by Jeff Jetton, 2019
|
||||||
# Python port by Jeff Jetton, 2019
|
"""
|
||||||
#
|
|
||||||
######################################################################
|
|
||||||
|
|
||||||
|
|
||||||
# Image data. Each top-level element is a row. Each row element
|
# Image data. Each top-level element is a row. Each row element
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ def get_invalid_letters(user_command):
|
|||||||
|
|
||||||
# 2000
|
# 2000
|
||||||
def print_board(guesses) -> None:
|
def print_board(guesses) -> None:
|
||||||
"""Prints previous guesses within the round."""
|
"""Print previous guesses within the round."""
|
||||||
print("Board")
|
print("Board")
|
||||||
print("Move\tGuess\tBlack White")
|
print("Move\tGuess\tBlack White")
|
||||||
for idx, guess in enumerate(guesses):
|
for idx, guess in enumerate(guesses):
|
||||||
@@ -248,7 +248,7 @@ def compare_two_positions(guess: str, answer: str) -> List[Union[str, int]]:
|
|||||||
|
|
||||||
# 5000 + logic from 1160
|
# 5000 + logic from 1160
|
||||||
def print_score(computer_score, human_score, is_final_score: bool = False) -> None:
|
def print_score(computer_score, human_score, is_final_score: bool = False) -> None:
|
||||||
"""Prints score after each turn ends, including final score at end of game."""
|
"""Print score after each turn ends, including final score at end of game."""
|
||||||
if is_final_score:
|
if is_final_score:
|
||||||
print("GAME OVER")
|
print("GAME OVER")
|
||||||
print("FINAL SCORE:")
|
print("FINAL SCORE:")
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ def bet_results(bet_ids: List[int], bet_values: List[int], result):
|
|||||||
|
|
||||||
|
|
||||||
def print_check(amount: int) -> None:
|
def print_check(amount: int) -> None:
|
||||||
"""Prints a check of a given amount"""
|
"""Print a check of a given amount"""
|
||||||
name = input("TO WHOM SHALL I MAKE THE CHECK? ")
|
name = input("TO WHOM SHALL I MAKE THE CHECK? ")
|
||||||
|
|
||||||
print("-" * 72)
|
print("-" * 72)
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
########################################################
|
"""
|
||||||
#
|
Russian Roulette
|
||||||
# Russian Roulette
|
|
||||||
#
|
From Basic Computer Games (1978)
|
||||||
# From Basic Computer Games (1978)
|
|
||||||
#
|
In this game, you are given by the computer a
|
||||||
# In this game, you are given by the computer a
|
revolver loaded with one bullet and five empty
|
||||||
# revolver loaded with one bullet and five empty
|
chambers. You spin the chamber and pull the trigger
|
||||||
# chambers. You spin the chamber and pull the trigger
|
by inputting a "1", or, if you want to quit, input
|
||||||
# by inputting a "1", or, if you want to quit, input
|
a "2". You win if you play ten times and are still
|
||||||
# a "2". You win if you play ten times and are still
|
alive.
|
||||||
# alive.
|
Tom Adametx wrote this program while a student at
|
||||||
# Tom Adametx wrote this program while a student at
|
Curtis Jr. High School in Sudbury, Massachusetts.
|
||||||
# Curtis Jr. High School in Sudbury, Massachusetts.
|
"""
|
||||||
#
|
|
||||||
########################################################
|
|
||||||
|
|
||||||
|
|
||||||
from random import random
|
from random import random
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
###################
|
BoardType = List[List[Optional[int]]]
|
||||||
#
|
CoordinateType = Tuple[int, int]
|
||||||
# static variables
|
|
||||||
#
|
|
||||||
###################
|
|
||||||
|
|
||||||
BOARD_WIDTH = 10
|
BOARD_WIDTH = 10
|
||||||
BOARD_HEIGHT = 10
|
BOARD_HEIGHT = 10
|
||||||
|
|
||||||
# game ships
|
|
||||||
#
|
|
||||||
# data structure keeping track of information
|
# data structure keeping track of information
|
||||||
# about the ships in the game. for each ship,
|
# about the ships in the game. for each ship,
|
||||||
# the following information is provided:
|
# the following information is provided:
|
||||||
@@ -35,26 +32,21 @@ VALID_MOVES = [
|
|||||||
[1, 0], # South
|
[1, 0], # South
|
||||||
[1, -1], # South West
|
[1, -1], # South West
|
||||||
[0, -1], # West
|
[0, -1], # West
|
||||||
[-1, -1],
|
[-1, -1], # North West
|
||||||
] # North West
|
]
|
||||||
|
|
||||||
COORD_REGEX = "[ \t]{0,}(-?[0-9]{1,3})[ \t]{0,},[ \t]{0,}(-?[0-9]{1,2})"
|
COORD_REGEX = "[ \t]{0,}(-?[0-9]{1,3})[ \t]{0,},[ \t]{0,}(-?[0-9]{1,2})"
|
||||||
|
|
||||||
####################
|
|
||||||
#
|
|
||||||
# global variables
|
|
||||||
#
|
|
||||||
####################
|
|
||||||
|
|
||||||
# array of BOARD_HEIGHT arrays, BOARD_WIDTH in length,
|
# array of BOARD_HEIGHT arrays, BOARD_WIDTH in length,
|
||||||
# representing the human player and computer
|
# representing the human player and computer
|
||||||
player_board = []
|
player_board: BoardType = []
|
||||||
computer_board = []
|
computer_board: BoardType = []
|
||||||
|
|
||||||
# array representing the coordinates
|
# array representing the coordinates
|
||||||
# for each ship for player and computer
|
# for each ship for player and computer
|
||||||
# array is in the same order as SHIPS
|
# array is in the same order as SHIPS
|
||||||
computer_ship_coords = []
|
computer_ship_coords: List[List[CoordinateType]] = []
|
||||||
|
|
||||||
|
|
||||||
####################################
|
####################################
|
||||||
@@ -88,10 +80,9 @@ num_player_shots = 7
|
|||||||
#
|
#
|
||||||
####################################
|
####################################
|
||||||
|
|
||||||
# flag indicating whose turn
|
# flag indicating whose turn it currently is
|
||||||
# it currently is
|
COMPUTER = False
|
||||||
COMPUTER = 0
|
PLAYER = True
|
||||||
PLAYER = 1
|
|
||||||
active_turn = COMPUTER
|
active_turn = COMPUTER
|
||||||
|
|
||||||
####################
|
####################
|
||||||
@@ -108,26 +99,27 @@ random.seed()
|
|||||||
|
|
||||||
# random_x_y
|
# random_x_y
|
||||||
#
|
#
|
||||||
# generate a valid x,y coordinate on the board
|
|
||||||
# returns: x,y
|
|
||||||
# x: integer between 1 and BOARD_HEIGHT
|
def random_x_y() -> CoordinateType:
|
||||||
# y: integer between 1 and BOARD WIDTH
|
"""Generate a valid x,y coordinate on the board"""
|
||||||
def random_x_y():
|
|
||||||
x = random.randrange(1, BOARD_WIDTH + 1)
|
x = random.randrange(1, BOARD_WIDTH + 1)
|
||||||
y = random.randrange(1, BOARD_HEIGHT + 1)
|
y = random.randrange(1, BOARD_HEIGHT + 1)
|
||||||
return (x, y)
|
return (x, y)
|
||||||
|
|
||||||
|
|
||||||
# input_coord
|
def input_coord() -> CoordinateType:
|
||||||
#
|
"""
|
||||||
# ask user for single (x,y) coordinate
|
Ask user for single (x,y) coordinate
|
||||||
# validate the coordinates are within the bounds
|
|
||||||
# of the board width and height. mimic the behavior
|
validate the coordinates are within the bounds
|
||||||
# of the original program which exited with error
|
of the board width and height. mimic the behavior
|
||||||
# messages if coordinates where outside of array bounds.
|
of the original program which exited with error
|
||||||
# if input is not numeric, print error out to user and
|
messages if coordinates where outside of array bounds.
|
||||||
# let them try again.
|
if input is not numeric, print error out to user and
|
||||||
def input_coord():
|
let them try again.
|
||||||
|
"""
|
||||||
match = None
|
match = None
|
||||||
while not match:
|
while not match:
|
||||||
coords = input("? ")
|
coords = input("? ")
|
||||||
@@ -148,25 +140,25 @@ def input_coord():
|
|||||||
return x, y
|
return x, y
|
||||||
|
|
||||||
|
|
||||||
# generate_ship_coordinates
|
def generate_ship_coordinates(ship: int) -> List[CoordinateType]:
|
||||||
#
|
"""
|
||||||
# given a ship from the SHIPS array, generate
|
given a ship from the SHIPS array, generate
|
||||||
# the coordinates of the ship. the starting point
|
the coordinates of the ship. the starting point
|
||||||
# of the ship's first coordinate is generated randomly.
|
of the ship's first coordinate is generated randomly.
|
||||||
# once the starting coordinates are determined, the
|
once the starting coordinates are determined, the
|
||||||
# possible directions of the ship, accounting for the
|
possible directions of the ship, accounting for the
|
||||||
# edges of the board, are determined. once possible
|
edges of the board, are determined. once possible
|
||||||
# directions are found, a direction is randomly
|
directions are found, a direction is randomly
|
||||||
# determined and the remaining coordinates are
|
determined and the remaining coordinates are
|
||||||
# generated by adding or substraction from the starting
|
generated by adding or substraction from the starting
|
||||||
# coordinates as determined by direction.
|
coordinates as determined by direction.
|
||||||
#
|
|
||||||
# arguments:
|
arguments:
|
||||||
# ship - index into the SHIPS array
|
ship - index into the SHIPS array
|
||||||
#
|
|
||||||
# returns:
|
returns:
|
||||||
# array of sets of coordinates (x,y)
|
array of sets of coordinates (x,y)
|
||||||
def generate_ship_coordinates(ship):
|
"""
|
||||||
# randomly generate starting x,y coordinates
|
# randomly generate starting x,y coordinates
|
||||||
start_x, start_y = random_x_y()
|
start_x, start_y = random_x_y()
|
||||||
|
|
||||||
@@ -213,20 +205,13 @@ def generate_ship_coordinates(ship):
|
|||||||
return coords
|
return coords
|
||||||
|
|
||||||
|
|
||||||
# create_blank_board
|
def create_blank_board() -> BoardType:
|
||||||
#
|
"""Create a blank game board"""
|
||||||
# helper function to create a game board
|
return [[None for _y in range(BOARD_WIDTH)] for _x in range(BOARD_HEIGHT)]
|
||||||
# that is blank
|
|
||||||
def create_blank_board():
|
|
||||||
return [[None for y in range(BOARD_WIDTH)] for x in range(BOARD_HEIGHT)]
|
|
||||||
|
|
||||||
|
|
||||||
# print_board
|
def print_board(board: BoardType) -> None:
|
||||||
#
|
"""Print out the game board for testing purposes"""
|
||||||
# print out the game board for testing
|
|
||||||
# purposes
|
|
||||||
def print_board(board) -> None:
|
|
||||||
|
|
||||||
# print board header (column numbers)
|
# print board header (column numbers)
|
||||||
print(" ", end="")
|
print(" ", end="")
|
||||||
for z in range(BOARD_WIDTH):
|
for z in range(BOARD_WIDTH):
|
||||||
@@ -243,27 +228,31 @@ def print_board(board) -> None:
|
|||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
# place_ship
|
def place_ship(board: BoardType, coords: List[CoordinateType], ship: int) -> None:
|
||||||
#
|
"""
|
||||||
# place a ship on a given board. updates
|
Place a ship on a given board.
|
||||||
# the board's row,column value at the given
|
|
||||||
# coordinates to indicate where a ship is
|
updates
|
||||||
# on the board.
|
the board's row,column value at the given
|
||||||
#
|
coordinates to indicate where a ship is
|
||||||
# inputs: board - array of BOARD_HEIGHT by BOARD_WIDTH
|
on the board.
|
||||||
# coords - array of sets of (x,y) coordinates of each
|
|
||||||
# part of the given ship
|
inputs: board - array of BOARD_HEIGHT by BOARD_WIDTH
|
||||||
# ship - integer repreesnting the type of ship (given in SHIPS)
|
coords - array of sets of (x,y) coordinates of each
|
||||||
def place_ship(board, coords, ship):
|
part of the given ship
|
||||||
|
ship - integer representing the type of ship (given in SHIPS)
|
||||||
|
"""
|
||||||
for coord in coords:
|
for coord in coords:
|
||||||
board[coord[0] - 1][coord[1] - 1] = ship
|
board[coord[0] - 1][coord[1] - 1] = ship
|
||||||
|
|
||||||
|
|
||||||
# NOTE: A little quirk that exists here and in the orginal
|
def generate_board() -> Tuple[BoardType, List[List[CoordinateType]]]:
|
||||||
# game: Ships are allowed to cross each other!
|
"""
|
||||||
# For example: 2 destroyers, length 2, one at
|
NOTE: A little quirk that exists here and in the orginal
|
||||||
# [(1,1),(2,2)] and other at [(2,1),(1,2)]
|
game: Ships are allowed to cross each other!
|
||||||
def generate_board():
|
For example: 2 destroyers, length 2, one at
|
||||||
|
[(1,1),(2,2)] and other at [(2,1),(1,2)]
|
||||||
|
"""
|
||||||
board = create_blank_board()
|
board = create_blank_board()
|
||||||
|
|
||||||
ship_coords = []
|
ship_coords = []
|
||||||
@@ -284,7 +273,9 @@ def generate_board():
|
|||||||
return board, ship_coords
|
return board, ship_coords
|
||||||
|
|
||||||
|
|
||||||
def execute_shot(turn, board, x, y, current_turn):
|
def execute_shot(
|
||||||
|
turn: bool, board: BoardType, x: int, y: int, current_turn: int
|
||||||
|
) -> int:
|
||||||
"""
|
"""
|
||||||
given a board and x, y coordinates,
|
given a board and x, y coordinates,
|
||||||
execute a shot. returns True if the shot
|
execute a shot. returns True if the shot
|
||||||
@@ -298,12 +289,8 @@ def execute_shot(turn, board, x, y, current_turn):
|
|||||||
return ship_hit
|
return ship_hit
|
||||||
|
|
||||||
|
|
||||||
# calculate_shots
|
def calculate_shots(board: BoardType) -> int:
|
||||||
#
|
"""Examine each board and determine how many shots remaining"""
|
||||||
# function to examine each board
|
|
||||||
# and determine how many shots remaining
|
|
||||||
def calculate_shots(board):
|
|
||||||
|
|
||||||
ships_found = [0 for x in range(len(SHIPS))]
|
ships_found = [0 for x in range(len(SHIPS))]
|
||||||
for x in range(BOARD_HEIGHT):
|
for x in range(BOARD_HEIGHT):
|
||||||
for y in range(BOARD_WIDTH):
|
for y in range(BOARD_WIDTH):
|
||||||
@@ -318,19 +305,12 @@ def calculate_shots(board):
|
|||||||
return shots
|
return shots
|
||||||
|
|
||||||
|
|
||||||
# initialize
|
def initialize_game() -> None:
|
||||||
#
|
# initialize the global player and computer boards
|
||||||
# function to initialize global variables used
|
|
||||||
# during game play.
|
|
||||||
def initialize_game():
|
|
||||||
|
|
||||||
# initialize the global player and computer
|
|
||||||
# boards
|
|
||||||
global player_board
|
global player_board
|
||||||
player_board = create_blank_board()
|
player_board = create_blank_board()
|
||||||
|
|
||||||
# generate the ships for the computer's
|
# generate the ships for the computer's board
|
||||||
# board
|
|
||||||
global computer_board
|
global computer_board
|
||||||
global computer_ship_coords
|
global computer_ship_coords
|
||||||
computer_board, computer_ship_coords = generate_board()
|
computer_board, computer_ship_coords = generate_board()
|
||||||
@@ -355,8 +335,8 @@ def initialize_game():
|
|||||||
ship_coords.append(list)
|
ship_coords.append(list)
|
||||||
|
|
||||||
# add ships to the user's board
|
# add ships to the user's board
|
||||||
for ship in range(len(SHIPS)):
|
for ship_index in range(len(SHIPS)):
|
||||||
place_ship(player_board, ship_coords[ship], ship)
|
place_ship(player_board, ship_coords[ship_index], ship_index)
|
||||||
|
|
||||||
# see if the player wants the computer's ship
|
# see if the player wants the computer's ship
|
||||||
# locations printed out and if the player wants to
|
# locations printed out and if the player wants to
|
||||||
@@ -366,9 +346,9 @@ def initialize_game():
|
|||||||
while input_loop:
|
while input_loop:
|
||||||
player_start = input("DO YOU WANT TO START? ")
|
player_start = input("DO YOU WANT TO START? ")
|
||||||
if player_start == "WHERE ARE YOUR SHIPS?":
|
if player_start == "WHERE ARE YOUR SHIPS?":
|
||||||
for ship in range(len(SHIPS)):
|
for ship_index in range(len(SHIPS)):
|
||||||
print(SHIPS[ship][0])
|
print(SHIPS[ship_index][0])
|
||||||
coords = computer_ship_coords[ship]
|
coords = computer_ship_coords[ship_index]
|
||||||
for coord in coords:
|
for coord in coords:
|
||||||
x = coord[0]
|
x = coord[0]
|
||||||
y = coord[1]
|
y = coord[1]
|
||||||
@@ -384,14 +364,11 @@ def initialize_game():
|
|||||||
print_computer_shots = True
|
print_computer_shots = True
|
||||||
|
|
||||||
global first_turn
|
global first_turn
|
||||||
global second_turn
|
|
||||||
if player_start.lower() != "yes":
|
if player_start.lower() != "yes":
|
||||||
first_turn = COMPUTER
|
first_turn = COMPUTER
|
||||||
second_turn = PLAYER
|
|
||||||
|
|
||||||
# calculate the initial number of shots for each
|
# calculate the initial number of shots for each
|
||||||
global num_computer_shots
|
global num_computer_shots, num_player_shots
|
||||||
global num_player_shots
|
|
||||||
num_player_shots = calculate_shots(player_board)
|
num_player_shots = calculate_shots(player_board)
|
||||||
num_computer_shots = calculate_shots(computer_board)
|
num_computer_shots = calculate_shots(computer_board)
|
||||||
|
|
||||||
@@ -407,31 +384,22 @@ def initialize_game():
|
|||||||
# forth, replicating the gotos in the original game
|
# forth, replicating the gotos in the original game
|
||||||
|
|
||||||
|
|
||||||
# initialize the first_turn function to the
|
# initialize the first_turn function to the player's turn
|
||||||
# player's turn
|
|
||||||
first_turn = PLAYER
|
first_turn = PLAYER
|
||||||
|
|
||||||
|
|
||||||
# initialize the second_turn to the computer's
|
def execute_turn(turn: bool, current_turn: int) -> int:
|
||||||
# turn
|
global num_computer_shots, num_player_shots
|
||||||
second_turn = COMPUTER
|
|
||||||
|
|
||||||
|
# print out the number of shots the current player has
|
||||||
def execute_turn(turn, current_turn):
|
|
||||||
|
|
||||||
global num_computer_shots
|
|
||||||
global num_player_shots
|
|
||||||
|
|
||||||
# print out the number of shots the current
|
|
||||||
# player has
|
|
||||||
board = None
|
board = None
|
||||||
num_shots = 0
|
num_shots = 0
|
||||||
if turn == COMPUTER:
|
if turn == COMPUTER:
|
||||||
print("I HAVE", num_computer_shots, "SHOTS.")
|
print(f"I HAVE {num_computer_shots} SHOTS.")
|
||||||
board = player_board
|
board = player_board
|
||||||
num_shots = num_computer_shots
|
num_shots = num_computer_shots
|
||||||
else:
|
else:
|
||||||
print("YOU HAVE", num_player_shots, "SHOTS.")
|
print(f"YOU HAVE {num_player_shots} SHOTS.")
|
||||||
board = computer_board
|
board = computer_board
|
||||||
num_shots = num_player_shots
|
num_shots = num_player_shots
|
||||||
|
|
||||||
@@ -486,34 +454,22 @@ def execute_turn(turn, current_turn):
|
|||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
# keep track of the turn
|
|
||||||
current_turn = 0
|
current_turn = 0
|
||||||
|
|
||||||
# initialize the player and computer
|
|
||||||
# boards
|
|
||||||
initialize_game()
|
initialize_game()
|
||||||
|
|
||||||
# execute turns until someone wins or we run
|
# execute turns until someone wins or we run
|
||||||
# out of squares to shoot
|
# out of squares to shoot
|
||||||
|
|
||||||
game_over = False
|
game_over = False
|
||||||
while not game_over:
|
while not game_over:
|
||||||
|
current_turn += 1
|
||||||
# increment the turn
|
|
||||||
current_turn = current_turn + 1
|
|
||||||
|
|
||||||
print("\n")
|
print("\n")
|
||||||
print("TURN", current_turn)
|
print("TURN", current_turn)
|
||||||
|
|
||||||
# print("computer")
|
if (
|
||||||
# print_board(computer_board)
|
execute_turn(first_turn, current_turn) == 0
|
||||||
# print("player")
|
or execute_turn(not first_turn, current_turn) == 0
|
||||||
# print_board(player_board)
|
):
|
||||||
|
|
||||||
if execute_turn(first_turn, current_turn) == 0:
|
|
||||||
game_over = True
|
|
||||||
continue
|
|
||||||
if execute_turn(second_turn, current_turn) == 0:
|
|
||||||
game_over = True
|
game_over = True
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user