0% found this document useful (0 votes)
136 views44 pages

Work Guide: Project Python

The document describes a project to code a simplified Scrabble game in Python. It outlines 5 key classes needed: SakClass to represent the letter bag, Player as the base class with subclasses for Human and Computer players, and a Game class. It provides details on class methods and the game scenario, which involves drawing letters from the bag for players and removing them, allowing players to take turns making words, and scoring words based on letters. The game would be played through the command line interface of IDLE.

Uploaded by

Muhammad Bassam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views44 pages

Work Guide: Project Python

The document describes a project to code a simplified Scrabble game in Python. It outlines 5 key classes needed: SakClass to represent the letter bag, Player as the base class with subclasses for Human and Computer players, and a Game class. It provides details on class methods and the game scenario, which involves drawing letters from the bag for players and removing them, allowing players to take turns making words, and scoring words based on letters. The game would be played through the command line interface of IDLE.

Uploaded by

Muhammad Bassam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Work Guide

Project Python
Scrabble

ΕΡΓΑΣΙΑΘ2

THEORETICS & EDUCATIONAL2020

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Contents
• (1) General description
• (2) Classes
• (3) Game Script
• (4) Data structure for acceptable words
• (5) Play algorithm
• (6) Library & import
• (7) Documentation & docstring
• (8) Evaluation
• (9) Deliverable
• (10) Reminder for the exams of the course Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(1) General description

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(1) What will you need for your work

• To complete your work properly you will need:


• (a) This one Work Guide 2019 ( which you obviously already have)
• (b) The Slide packages for Python
• (c) The file greek7.txt which contains the words of the Greek language that have up to
7 letters.
• All of the above are on the course page

• Online resources:
• Scrabble - Wikipedia : What is Scrabble and general rules of the game

• Python itertools ( built-in library) - especially the function


itertools.permutations ()
• Built in types (especially string methods: strip , split )
Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(2) General job description 1/2

• The overall goal of the job is to develop an application in Python


code that implements a simplified version of the Scrabble
game on the computer

• In this version a player plays Scrabble against the computer trying to


create a word with a high score based on the letters available to him in
every move.

• The specific purpose of the work is to an algorithm is written to


guide the computer (or even the player) to play the game

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(2) General job description 2/2
• Your application should generally follow the analysis described in the following slides

• You can choose whether to work individually or in groups ( Ν max = 2, ie up to 2 people in the
group)

• In case of teamwork then each team member:


• A) Writes his / her own algorithm integrated into the game as a game option
(ie can be selected from 'Settings')

• B) You should be aware of the documentation of the entire code that makes up the
job

• Each improvement or expansion of the Game Scenario / Algorithm (in relation to what
I suggest in this Guide) is perfectly acceptable as long as they are satisfactory documented
and to me notified in a timely manner from the team to get my approval. Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Environment

• The application will run at IDLE environment of basic Python and


will be based on Python code version 3.6 or higher

• All information as the game progresses will be displayed on the


command line '>>>' ( character mode) of
IDLE environment ( ie the application will not be windowed and will not use graphics)

• E.g. a possible display of messages from the game could be as


follows:

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(2) Classes

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


The 5 classes of the game

• Programmatically the application should:


• 1) To use at least the following 5 Classes:
• → SakClass: class that describes objects that act as a "toy letter bag"

• → Player: basic class from which Human and Computer are derived

• → Human: Player derivative class that describes how the human player plays

• → Computer: Player derivative class that describes how the player


computer plays
• → Game: class that describes how a game (game) evolves
• In addition to the above you can implement other classes if you want

• More information about the classes is given in the following slides Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Class: SakClass

sak
(object
SakClass type)
getletters ()
(pulls out of the bag for him
player N letters)

PLAYER

putbackletters ()
(returns player letters
in the bag)
randomize_sak ()
("Prepares" the bag
Pi
in letters)

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Classes: Player, Human & Computer

Player

Properties, Methods

Human Computer

Properties, Methods Properties, Methods

• Player: Basic
• Human, Computer: derivatives of Player
Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Class: Player

Player

__init__, __repr__
…… ..

• Player: Basic
• Methods: init, repr and any other you judge

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Class: Human

Human

__init__, __repr__,
play
… ..

• Human: Player production


• Methods: init, repr, play and whatever others you judge
• The method play will implement / call the algorithm according to which the player
will play
Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Class: Computer

Computer

__init__, __repr__,
play
… ..

• Computer: Player production


• Methods: init, repr, play and whatever others you judge
• The method play will implement / call the algorithm according to which the
computer will play
Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Class: Game

Game

__init__, __repr__,
setup, run, end,
… ..

• Game: Basic
• Methods: init, repr, setup, run, end and whatever others you judge
• The method setup take the necessary actions at the start of the game

• The method run the game is 'running'


• The method end takes the necessary actions when closing the game Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


How to save the code of the classes and the
master program
• The code of the classes will be in one separate file which you
will name classes.py
• The master program code will be located in a named file main-AEM.py

• Where AEM you will write your AEM number, eg main-1234.py

• If you work in a team the file name will include both AEMs one after
the other main-AEM1AEM2.py
• E.g. main-12343001.py

• The code of the classes will be entered in the main program with a command import

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(3) Game Scenario

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Game Scenario 1/7
• 1) The game can start with an introductory screen
showing a list of options to the player. An example of
such a screen can be seen next to:

• Also initially the program should create one object -


snapshot ( e.g.
sak) type SakClass

• The object will implement the " bag »Of the physical
game. That is, it will be a data structure that
contains the letters and their values ("points"
given by each letter) and the methods associated
with the "bag"

• Information on how many of these letters Pi


there are here

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Game Scenario 2/7
• 2) The evolution of the game is as follows:
• The program draws 7 letters from the "bag" for the player and 7 for the computer.
Shows the player his letters along with their value (the value can be an integer that
appears next to the letter). At the same time the program will remove from the "bag"
the letters that were drawn. The information on how many letters remain in the bag
should be known to the player.

• The program then waits for the player to enter a word with the letters it has (an
example screen you see below - it is absolutely indicative - I do not mean to display
exactly these messages and your application)

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


- - Game Scenario 3/7
• 3) When the player enters a word then your program must do at least the following
checks:
• 3-A) Checks if the word consists of letters that the player actually has. If no, displays
a relevant message and waits for a new word
• 3-B) If Yes, then checks if the given word is in the list of accepted words that have
emerged from the file greek7.txt.
• 3-C) Instead of a word the player if he wants can type 'p' ( ie pass) so the program
should: a) draw new letters for the player and b) then return the previous letters of
the player to the "bag".

• Then move on to step 5 described in the next slide.

• Where is the greek7.txt file located?


• It is available on the course page. We explain how it is made and what it contains in
the laboratory
Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


- - Game Scenario 4/7

• 4) If the word that typed the player is acceptable the program calculates the
points of the word and displays to the player his new score, along with a prompt
of the form ' Enter to continue '(eg see example of messages below)

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


- - Game Scenario 5/7
• 5) If 'Enter' (or 'p') is given by the player then the program:
• 5-1) Fills in new letters of the player's resources (so that it is always 7, remember that at
the same time he must remove them from the "bag").

• 5-2) Displays the letters of the computer and the word being played, along with the word score
and the overall score of the computer. He secretly fills in the letters of the PC and finally
returns to step 3.
• An example screen with these steps is shown below.

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


- - Game Scenario 6/7

• 6) The game continues like this until something like this happens:

• 6-1) The player wishes to stop ( the does not find an acceptable word
to play and there are no letters to change) so it introduces the
character 'q' when it is his turn to type a word.

• 6-2) There are no more letters in the "bag" to replace what is missing,
either the player or the PC

• 6-3) The computer does not find an acceptable word to play

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


- - Game Scenario 7/7

• 7) In any of the above cases the program stops and:

• 7-1) Announces the Player and PC scores announcing


the winner
• 7-2) Enters in appropriate data structure stored in a file a new entry
with whichever elements of the game are considered important (eg
how many moves were played, what was the player score and PC).
The program "loads" this structure when the game starts and can give
the player relevant information.

• To save data to a file you will use pickle or json library (prefer json) which
you can read about in the 09-PythonFiles.pdf slide pack in the section 'Conserva
(pickle & json) Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(4) Data structure for
acceptable words

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Structures for words: in Dictionary
(dictionary) ήσεΛίστα (list);

• Your startup program should do one more important job:

• Upload the language words from the file greek7.txt


and transfer them to an appropriate structure in the code so that the
necessary checks can be made in the game
• You have to decide on data structure which will contain the words of
the language that your program will consult.

• The type of structure will depend on code efficiency your. We


discussed this in the workshop and we have given the answer.

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(5) Play algorithm for the
Computer class

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


How does the computer work? SCENARIO1

• The PC game algorithm is MIN-MAX-SMART


• That is, the player can have one of the three following formats:

• A) ΜΙΝ Letters : The program creates all possible permutations


( permutations) of the letters available on the PC starting from 2 and going up to 7
letters. For each permutation it checks if it is an acceptable word and plays the first
acceptable word it finds.
• B) MAX Letters : As in A but the program starts with the permutations of the letters
every 7 and coming down to 2. Plays the first accepted word again but now the one
with the most letters.
• C) SMART : As in A but runs out all permutations of 2 to 7 letters without stopping.
He finds the acceptable words and at the end he plays the word that gives the most
points.

• The permutations of a list of objects can easily be accessed in your code via the Pi
itertools.permutations () function

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


How does the computer work? SCENARIO2

• The PC game algorithm has the form: SMART-FAIL

• SMART: The SMART algorithm is called first (as explained in scenario 1) and
creates a list of possible words that can be played

• FAIL: Then it is called FAIL. As a person does not always find the optimal word, the
FAIL algorithm introduces a degree of failure to play the optimal word found by
SMART.
• The FAIL algorithm takes as input the list of possible words generated by
SMART and chooses to play eg 2 the better or the 3 the
best word in the list (instead of the best first) depending on how you spell it.

• You can decide for yourself the details of the FAIL algorithm
so that FAIL simulates the memory / ability of a person who does not know all the words
or can not always find the best possible word. Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


How does the computer work? SCENARIO3

• The PC game algorithm has the form: SMART-EXPERT

• SMART: The algorithm as explained in scenario 1


• EXPERT: simulates a "specialist" in a field who may know specific terms and therefore
more words than those contained in the greek7.txt file. E.g. the name 'Sphinx' does
not exist in greek7.txt but could be included in a dictionary of archaic word types

• In this scenario you should enrich (or replace) the greek7.txt file with another
that contains a larger or specialized wealth of words (remember: up to 7 letters).

• That is, it is essentially the SMART algorithm but which is applied to an


enriched word file.

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


How does the computer work? SCENARIO4

• The PC game algorithm has the form: SMART-LEARN

• SMART: The algorithm as explained in scenario 1


• LEARN: The computer "learns" new words, ie the algorithm simulates a
player who learns as he plays.
• That is, if the player-man in a move enters a word that does not exist in the
greek7.txt file (but it is up to 7 letters) then instead of being rejected the player is
asked if he wants to be included in the word file for the sequel.

• If the player chooses 'YES' the word is included in the word structure of the current
game to be recognized later.
• In this scenario at the end of the game you have to update the greek7.txt file with the
new words so that they are available in the next batch.

• You can design the technical-programming details of the scenario Pi


yourself

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


How does the computer work? SCENARIO5

• The PC game algorithm has the form: SMART-TEACH

• SMART: The algorithm as explained in scenario 1


• TEACH: The computer "teaches" the player, ie informs him what would be the best
word to play.
• When it is the player-human turn to play, the computer also executes the SMART
algorithm.
• After the player plays a word the computer informs the player if the word played is
the best possible. If not, then inform him which would be the best or even the 2nd the best
word he could play.

• You can design the technical-programming details of the scenario


yourself.

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


SCENARIOS AND SETTINGS

• In your work you should choose which scenario from the previous
ones you will implement in your code.
• If the work is done by group of 2 people should be implemented 2
different scenarios.

• When the player chooses' Settings at the beginning of the game your
schedule presents the possible scenarios and allows the player to
choose.

• If you are thinking of other interesting scenarios you can implement


them first let me know and get my approval
Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(6) Library & import

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Module (Library, module)
• A 'module' (' library 'or' module ') is a file
. py in which you have stored code that does specific tasks, eg classes
or even simple functions

• Such a library is linked to your master program by command import

• E.g. suppose you wrote the SakClass class and saved it in a


classes.py file
• Then in your main program you can write:
• import classes
• And create the SakClass sak object by writing:

• sak = classes.SakClass ()
Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(7) Documentation & docstring

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(7) Documentation (docstring format)

• Include documentation of your code in the form


docstring at the beginning of the main program embedded in a function
named ' guidelines '
• See the next section ('Evaluation' 'to know what to include in the
documentation
• Also include any useful information about how you run your
program and what someone who wants to play your game should
know.

• The documentation docstring should be displayed when typed:


>>> help (guidelines)
• Prove that the above works properly before submitting your work.

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(8) Job evaluation

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


• The evaluation of the work will be based on the
(8) Evaluation 1/2 following

JUDGE- CHARACTER- COMMENTS - EXPLANATIONS

RIO ΣΤΙΚΟ

A CLASSES If the classes have been implemented as described. What methods have you
implemented in each class and what do they do (2 lines for each). Also if you
have implemented other classes

- (explain all of the above in docstring)

LIST, DICTIONARY Explain what structure you used


Β for the Structure
- (explain it to docstring)
of Words

Classes to be in a separate library (separate


Γ LIBRARY file) as described.

Which or which scenarios for the play algorithm have been implemented
Δ ALGORITHM - ( explain it to docstring especially if you have implemented one
play
particular scenario not suggested here) Pi

• Continuation of evaluation criteria… →

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(8) Evaluation 2/2
JUDGE- CHARACTER- COMMENTS - EXPLANATIONS
RIO ΣΤΙΚΟ

It is generally evaluated correctness of the code so that the game is played


Ε CORRECTNESS correctly as well as the comprehensible appearance of messages and implementation
CODES -
of the envisaged functions.
MESSAGES
You can design the appearance of the messages as you wish and not necessarily
according to the previous examples I give
here.

Include in the docstring any information I request in this table or anything


F. DOCUMENTATION else that is important to understanding how your code works

ATTENTION: Lack of documentation in docstring reduces your


score.

If they have been submitted all files which are necessary for your password to
Ζ ARCHIVES be executed correctly.
- (explain to docstring what files your work consists of)

Pi
ATTENTION: Incorrect execution of the code due to lack of a
file reduces your rating.
ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS
(9) Deliverable

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


(9) Deliverable

• Deliverable of your work is 1 zip or rar file that includes:

• (a) The file classes.py which includes classes of your work


• (b) The file main-AEM.py which includes main program
• E.g. main1234.py or main12343005.py in case of 2 partners with AEM 1234 and
3005 (the AEM in a row)
• (c) Everything else required file as you see fit for code execution

• >>> The above files squeeze in zip or rar file


• >>>
areName the compressed file: PythonScrabbleAEM ( or you put AEM1AEM2 if you
2 partners)

• Submit via elearning until deadline which will be announced in the exam you want to
submit the assignment Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS


Reminder for course exams

• The lesson has 3 exams:


• Θ1: Python test (end of lab test) 20%

• Θ2: Project Python Scrabble 40%

• Θ3: Written 40%

• You can " share ”Exams as you want in the exams: June & September 2020 (and
February 2021 if you are a graduate)

• Caution: If you owe the course and it is taught again in the spring of 2021, you are
obliged to take a comprehensive review ( no points are held after February 2021)

Pi

ΤΜ. INFORMATICS AUTH ST. DIMITRIADIS

You might also like