You are on page 1of 11

you are required

# to write a standalone text-based Python program that allows players


# to play a CROSSWORD game.

Requirements:
# Your program is required to do the following:
# 1. Generate 2-dimensional game boards (minimum 10x10 and maximum 35x35).
# 2. Insert text strings within the 2D array (hidden from view).
# 3. Validate game coordinates within the 2D array.
# 4. Uncover individual characters at a specific location in the 2D array.
# 5. Keep track of the player's score, last selection, and update the game
board.

# Sample game boards (15x20 and 35x35) are displayed below

Python Crossword Puzzle...


123456789ABCDEFGHIJK
1|....................|
2|....................|
3|....................|
4|....................|
5|....................|
6|....................|
7|....................|
8|....................|
9|....................|
A|....................|
B|....................|
C|....................|
D|....................|
E|....................|
F|....................|
Current Score:0000 Last Move: N/A

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
1|...................................|
2|...................................|
3|...................................|
4|...................................|
5|...................................|
6|...................................|
7|...................................|
8|...................................|
9|...................................|
A|...................................|
B|...................................|
C|...................................|
D|...................................|
E|...................................|
F|...................................|
G|...................................|
H|...................................|
I|...................................|
J|...................................|
K|...................................|
L|...................................|
M|...................................|
N|...................................|
O|...................................|
P|...................................|
Q|...................................|
R|...................................|
S|...................................|
T|...................................|
U|...................................|
V|...................................|
W|...................................|
X|...................................|
Y|...................................|
Z|...................................|
Current Score:0000 Last Move: N/A

Specifications:
Basic game-play overview:
The game board dimensions will be supplied (minimum of
10 rows x 10 columns and maximum of 35 rows x 35 columns) such that
the rows and columns are to be labeled 1 to 9 and A to up to Z. The rows
are
labeled on the left and the columns at the top.
The game board's left and right boundaries are to be drawn using the '|'
character and the board contents itself drawn using only the '.' character.
Your program will insert words into the game board (2D array) starting at
specific locations and directions (hidden from view).
The words themselves will not be displayed (of course), but must be tracked
internally. The program will then uncover the contents of the board one
coordinate at a time.
Points (5 points for each letter uncovered) are awarded if the
coordinate uncovers one or more of the word's characters.
When this happens, that specific character and ALL characters in the
same row or column that it is connected to are then uncovered (replacing
the '.' with the actual letter in the character).
If a coordinate supplied does NOT uncover a letter in a word, then the '.'
is replaced with '~' character.
After each coordinate that is sent, your program updates the
current score (0000) and last move (with a "FOUND 'X' at '[X,Y]'" or
"NO letter FOUND at '[X,Y]'" message).
This is done by "redrawing" the entire screen.

This assignment MUST code the following functions as specified and will
called appropriately from the main( ) function that will be supplied.

Processing:
You MUST code the following Python functions as specified.

def generateGameBoard(nRows, nCols) :


+ This function accepts 2 integers ('nRows' and 'nCols') and creates a 2D
array of
'nRows' rows and 'nCols' columns and inserts the "." character into each
element of
the 2D array. You may assume that 'nRows' and 'nCols' will be always be
numbers
from 10 to 35.
This function returns the completed 2D array.

def loadWord(boardData, nRows, nCols, coord, word, dir) :


+ This function accepts an initialized 2D array 'boardData' containing
'nRows'
rows and 'nCols' columns and attempts to insert the supplied string
'word' into
'boardData' starting at cordinate 'coord' horizontally if 'dir' is 'H' or
verticallly
if dir is 'V'.
You may assume that 'coord' will ALWAYS be a 2 character string
containing a
ccoordinate, that the supplied string 'word' will only contain printable
characters
and will fit within the 'boardData' 2D array given the starting
coordinate 'coord', and
that 'dir' will always be either an "H" or a "V".
This function inserts the 'word' string (character by character) into the
2D array
(overwriting any characters already present) starting at the coordinate
'coord' from
left to right if 'dir' is "H" and from top to bottom if 'dir' is "V".
The function returns the 'boardData' 2D array containing the newly
inserted 'word'.

def checkCoord(nRows, nCols, coord) :


+ This function accepts the total number of rows and columns (as integers)
in
the game board and the coordinate (as a string) and uses regular
expressions ONLY to validate the coordinate as follows:
+ Must be exactly 2 characters in length, and may not be blank (not
whitespace)
+ May only consist of the characters (uppercase only) A-Z or digits 1-9
+ The first character represents the rows and the second character
represents columns, so the coordinate supplied MUST be valid given the
rows and columns supplied as parameters (i.e. if there are only 10 rows,
then a
coordinate of "C5" would not be valid given that "C" would represent the
12th row).
If all of these criteria are met, your function returns a True value
or False otherwise.

def updateGameBoard(boardData, boardMask, nRows, nCols, coord, score,


lastMove) :
+ This function attempts to uncover a word in the 2D game board at a
specific location.
The supplied coordinate 'coord' may or may not be valid.
+ If 'coord' is valid, then it determines if element at that location in
the 2D array
'boardData' contains a valid word character (a character other than '.'
or '~') and if
it does, writes that character to the exact same position in the 2D array
'boardMask'.
In addition, this function must also check if the character in any
adjoining location
(i.e. left, right, up, or down) also contains a word character and if it
does, writes
that character to 'boardMask', but ONLY for letters joined to that
specific coordinate
and NOT other words that letters may be connected to!
5 points are awarded for each character that is uncovered and the
"Current Score NNN"
message is updated at the bottom of the screen (see below for examples).

CAUTION: You must take care to not attempt to access an element in the 2D
array that
is out of bounds (i.e. beyond the left/right or top/bottom
indexes of the array).

Also, if the coordinate supplied uncovers a word character, the "Last


Move:" message is
updated to add the text: "FOUND '?' at [X,Y]" (where '?' is the letter,
'X' is the row,
and 'Y' is the column) is written at the bottom of the game board next to
the
"Current Score: NNNN" message (see below for examples).

+ If the coordinate is valid but does not find a word character at that
location, then
the function places the '~' character at that location in the 2D array
'boardMask', and
the "Last Move:" message is updated to add the text: "NO letter FOUND at
'[X,Y]'"
('X' is the row, and 'Y' is the column) is written at the bottom of the
game board next
to the "Current Score: NNNN" message (see below for examples).

+ If the coordinate is not valid, then 'boardMask' is not updated, the


Current Score:
message does not change, and the Last Move: message is updated to "[X,Y]
is an INVALID COORDINATE"
(see examples below).

UPDATED: DO NOT CLEAR THE SCREEN


+ This function simply prints the entire game board including
the game title, coordinate values, boundaries, and messages, BUT
DOES NOT clear the screen!

+ This function returns the following updated varaiables:


(boardData, boardMask, score, and lastMove).

Assuming a starting game board data of, with board dimensions below of:
12x26
and coordinates supplies as: "55", "1D", "X2", "B4", "6J"

............N.............
........HELLO WORLD.......
.........X................
.........A................
.........M........A.......
.........I......NORTH.....
.........N........E.......
.........E........A.......
..PYTHON..................
............PUZZLE........
...7-SEAS.....O...........
..............O...........

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|..........................|
2|..........................|
3|..........................|
4|..........................|
5|....~.....................|
6|..........................|
7|..........................|
8|..........................|
9|..........................|
A|..........................|
B|..........................|
C|..........................|
Current Score:0000 Last Move: NO letter FOUND at [5,5]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|............O.............|
3|..........................|
4|..........................|
5|....~.....................|
6|..........................|
7|..........................|
8|..........................|
9|..........................|
A|..........................|
B|..........................|
C|..........................|
Current Score:0010 Last Move: FOUND 'N' at [1,D]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|............O.............|
3|..........................|
4|..........................|
5|....~.....................|
6|..........................|
7|..........................|
8|..........................|
9|..........................|
A|..........................|
B|..........................|
C|..........................|
Current Score:0010 Last Move: [X,2] is an INVALID COORDINATE

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|............O.............|
3|..........................|
4|..........................|
5|....~.....................|
6|..........................|
7|..........................|
8|..........................|
9|..........................|
A|..........................|
B|...7-SEAS.................|
C|..........................|
Current Score:0040 Last Move: FOUND '7' at [B,4]
Python Crossword Puzzle...
123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|............O.............|
3|..........................|
4|..........................|
5|....~.............A.......|
6|................NORTH.....|
7|..................E.......|
8|..................A.......|
9|..........................|
A|..........................|
B|...7-SEAS.................|
C|..........................|
Current Score:0080 Last Move: FOUND 'R' at [6,J]

MAIN PROGRAM:

# Your solution may ONLY use the python modules listed below
# program: A1main.py
# author: danny abesdris
# date: march 12, 2021
# purpose: python main( ) program for PRG550 Winter 2021 Assignment #1

import math
import random
import string
import collections
import datetime
import re
import time
import copy

# YOUR CODE BELOW...

def generateGameBoard(nRows, nCols) :


... your code here
# end def

def loadWord(boardData, nRows, nCols, coord, word, dir) :


... your code here
# end def

def checkCoord(nRows, nCols, coord) :


... your code here
# end def

def updateGameBoard(boardData, boardMask, nRows, nCols, coord, score,


lastMove) :
... your code here
# end def

def main( ) :
score = 0
coords = [ "25", "2A", "XL", "9Q", "1D", "6J", "93", "B4", "AF" ]
lastMove = ""
board = generateGameBoard(20, 26)
mask = generateGameBoard(20, 26)

board = loadWord(board, 20, 26, "29", "HELLO", "H") # loading words onto
game board
board = loadWord(board, 20, 26, "1D", "NO", "V")
board = loadWord(board, 20, 26, "2A", "EXAMINE", "V")
board = loadWord(board, 20, 26, "5J", "AREA", "V")
board = loadWord(board, 20, 26, "6H", "NORTH", "H")
board = loadWord(board, 20, 26, "93", "PYTHON", "H")
board = loadWord(board, 20, 26, "AD", "PUZZLE", "H")
board = loadWord(board, 20, 26, "B4", "7-SEAS", "H")
board = loadWord(board, 20, 26, "AF", "ZOO", "V")

for coord in coords :


(board, mask, score, lastMove) = updateGameBoard(board, mask, 20, 26,
coord, score, lastMove)
print( )

if __name__ == "__main__" :
main( )

# The expected output is listed below. NOTE: You are NOT to clear the
screen
# after each call to
updateGameBoard( )

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|..........................|
2|....~.....................|
3|..........................|
4|..........................|
5|..........................|
6|..........................|
7|..........................|
8|..........................|
9|..........................|
A|..........................|
B|..........................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0000 Last Move: NO letter FOUND at [2,5]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|..........................|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M................|
6|.........I................|
7|.........N................|
8|.........E................|
9|..........................|
A|..........................|
B|..........................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0055 Last Move: FOUND 'E' at [2,A]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|..........................|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M................|
6|.........I................|
7|.........N................|
8|.........E................|
9|..........................|
A|..........................|
B|..........................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0055 Last Move: [X,L] is an INVALID COORDINATE

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|..........................|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M................|
6|.........I................|
7|.........N................|
8|.........E................|
9|.........................~|
A|..........................|
B|..........................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0055 Last Move: NO letter FOUND at [9,Q]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M................|
6|.........I................|
7|.........N................|
8|.........E................|
9|.........................~|
A|..........................|
B|..........................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0060 Last Move: FOUND 'N' at [1,D]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M........A.......|
6|.........I......NORTH.....|
7|.........N........E.......|
8|.........E........A.......|
9|.........................~|
A|..........................|
B|..........................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0100 Last Move: FOUND 'R' at [6,J]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M........A.......|
6|.........I......NORTH.....|
7|.........N........E.......|
8|.........E........A.......|
9|..PYTHON.................~|
A|..........................|
B|..........................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0130 Last Move: FOUND 'P' at [9,3]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M........A.......|
6|.........I......NORTH.....|
7|.........N........E.......|
8|.........E........A.......|
9|..PYTHON.................~|
A|..........................|
B|...7-SEAS.................|
C|..........................|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0160 Last Move: FOUND '7' at [B,4]

Python Crossword Puzzle...


123456789ABCDEFGHIJKLMNOPQ
1|............N.............|
2|....~...HELLO.............|
3|.........X................|
4|.........A................|
5|.........M........A.......|
6|.........I......NORTH.....|
7|.........N........E.......|
8|.........E........A.......|
9|..PYTHON.................~|
A|............PUZZLE........|
B|...7-SEAS.....O...........|
C|..............O...........|
D|..........................|
E|..........................|
F|..........................|
G|..........................|
H|..........................|
I|..........................|
J|..........................|
K|..........................|
Current Score:0200 Last Move: FOUND 'Z' at [A,F]

You might also like