Spring2015.CSCI220Homework5 History

Hide minor edits - Show changes to output

Changed line 2 from:
'''Due Date''': Wednesday, Apr. 29, 2015\\
to:
'''Due Date''': Friday, May 1, 2015\\
Changed line 45 from:
To simplify testing, for now, the '''computer should choose the next available (non-empty) square''' starting from 1. Also, '''print''' (via print statement) '''the player and move made, and the outcome of the game.'''
to:
To simplify testing, for now, the '''computer should choose the next available (non-empty) square''' starting from 1. Also, '''print''' (via print statement) '''each player and move made, and the outcome of the game.'''
Changed lines 45-47 from:
To simplify testing, for now, the '''computer should choose the next available (non-empty) square''' starting from 1.

Also, '''print (via
print statement) the player and move made, and the outcome of the game.'''
to:
To simplify testing, for now, the '''computer should choose the next available (non-empty) square''' starting from 1. Also, '''print''' (via print statement) '''the player and move made, and the outcome of the game.'''
Added lines 46-47:

Also, '''print (via print statement) the player and move made, and the outcome of the game.'''
Changed line 2 from:
'''Due Date''': Monday, Apr. 27, 2015\\
to:
'''Due Date''': Wednesday, Apr. 29, 2015\\
Changed line 57 from:
** @@getSquare(position)@@, where @@position@@ is an integer
to:
** @@getSquare(position)@@, where @@position@@ is an integer - returns the corresponding square's value as a string (i.e., "X", "O", or " ")
Changed lines 72-73 from:
** @@getMove()@@ - returns the move selected by the human - for now, in order to simplify things, use @@input()@@ to get the move (1-9). If the user makes a mistake, they should be prompted again (and again) until they get it right.
to:
** @@getMove(board)@@ - returns the move selected by the human - for now, in order to simplify things, use @@input()@@ to get the move (1-9). If the user makes a mistake, they should be prompted again (and again) until they get it right.
Changed line 76 from:
** @@getMove()@@ - returns the move selected by the computer - for now, in order to simplify things, pick the next empty square (starting from 1).
to:
** @@getMove(board)@@ - returns the move selected by the computer - for now, in order to simplify things, pick the next empty square (starting from 1).
Deleted line 61:
** @@__repr__()@@ - returns a string representation of the board
Deleted lines 14-15:
The user should be able to click on the square they wish to play. (''Hint:'' There are different ways to make this happen - think before you code!)
Added lines 82-83:

The user should be able to click on the square they wish to play. (''Hint:'' There are different ways to make this happen - think before you code!)
Changed lines 35-36 from:
The input to the program is the human player's moves - one at a time - capturing a mouse click on the board. If the user makes mistakes (i.e., clicks outside of a free/available square), you should ignore it. Every proper human move you should update the board (by placing an 'X' at the corresponding square).
to:
Use @@input()@@ to get the human move (1-9). If the user makes a mistake (i.e., selects a non-empty or an out-of-range (1-9) square), they should be prompted again (and again) until they get it right. Every proper human move you should update the board (by placing an 'X' at the corresponding square).
Changed lines 64-65 from:
to:
** @@__repr__()@@ - returns a string representation of the board
Changed lines 71-72 from:
to:
** @@__repr__()@@ - returns a string representation of a square (its contents as a single character - a string)
Changed lines 74-75 from:
** additional details will be provided soon
to:
** @@__init__()@@ - initializes a human player
** @@getMove()@@ - returns the move selected by the human - for now, in order to simplify things, use @@input()@@ to get the move (1-9). If the user makes a mistake, they should be prompted again (and again) until they get it right.
Changed lines 78-79 from:
** additional details will be provided soon
to:
** @@__init__()@@ - initializes a computer player
** @@getMove()@@ - returns the move selected by the computer - for now, in order to simplify things, pick the next empty square (starting from 1).
Added lines 82-85:

!!Bonus

Modify the above design (maintaining classes @@Board@@ and @@Square@@) so that the input to the program is the human player's moves - one at a time - capturing a mouse click on the board. If the user makes mistakes (i.e., clicks outside of a free/available square), you should ignore it. Every proper human move you should update the board (by placing an 'X' at the corresponding square).
Changed lines 61-63 from:
** @@isEmpty()@@, - returns True or False indicating if the board is empty or not
** @@isFull()@@, - returns True or False indicating if the board is full or not
** @@isWin()@@, - returns True or False indicating if the board contains a wining configuration (either player)
to:
** @@isEmpty()@@ - returns True or False indicating if the board is empty or not
** @@isFull()@@ - returns True or False indicating if the board is full or not
** @@isWin()@@ - returns True or False indicating if the board contains a wining configuration (either player)
Added lines 51-78:
!!Design

Your program should consist of the following classes:

* @@Board@@ this class encapsulates the game board

** @@__init__()@@ - builds a board of nine squares
** @@clear()@@ initializes the board to empty squares
** @@getSquare(position)@@, where @@position@@ is an integer
** @@setSquare(position, value)@@, where @@position@@ is an integer and @@value@@ is a string
** @@isEmpty()@@, - returns True or False indicating if the board is empty or not
** @@isFull()@@, - returns True or False indicating if the board is full or not
** @@isWin()@@, - returns True or False indicating if the board contains a wining configuration (either player)

* @@Square@@ this class encapsulates a single square

** @@__init__()@@ - builds an empty square
** @@getValue()@@ - returns the square value as a string (i.e., "X", "O", or " ")
** @@setValue(value)@@ - sets the square contents, where @@value@@ is a string (i.e., "X", "O", or " ")

* @@HumanPlayer@@ encapsulates the human player
** additional details will be provided soon

* @@ComputerPlayer@@ encapsulates the computer player
** additional details will be provided soon

* function @@playTicTacToe()@@ this function declares instances of above classes and uses their functions to implement the game
Changed lines 81-83 from:
In general, you should comment any variable, obscure statement, block of code, etc. you create.

Your comments
should reflect the "why" something is there (or being done), as opposed to the "how". The "how" is reflected in the code. The "why" usually comes from the thought you had, as to what you were trying to do, before you wrote the corresponding Python code.
to:
In general, you should comment any class, function, variable, obscure statement, block of code, etc. you create. Functions should be documented using Python [[http://epydoc.sourceforge.net/docstrings.html | docstrings]].

Your comments should reflect
the "what" something does, as opposed to the "how" it does it. The "how" is reflected in the code. The "what" usually comes from the thought you had, as to what you need to do, before you wrote the corresponding Python code.
Changed line 71 from:
# Your Python program. Call it, '''ticTacToe.py'''.
to:
# Your Python program. Call it, '''ticTacToeOO.py'''.
Added lines 1-83:
'''Assigned Date''': Friday, Apr. 17, 2015\\
'''Due Date''': Monday, Apr. 27, 2015\\
'''Due Time''': 12:20pm

Last modified on {$LastModified} [-(see [[http://www.cs.cofc.edu/~manaris/index.php/Spring2015.CSCI220Homework5?action=diff&source=n&minor=n | updates]]) -]

!!Assignment

Write a program that simulates a game of Tic-Tac-Toe. In our version of the game, two players, human and computer, play in turns, placing X's and O's, respectively, on the 3x3 square board. The human player always goes first. The game goes on until either there is a winner (3 X's or 3 O's in a vertical, horizontal, or diagonal line), or the game is a draw (all squares are occupied and no one has won).

The program should create a GUI display with the Tic-Tac-Toe board (via the [[http://www.cs.cofc.edu/~manaris/jythonmusic/?page_id=179 | GUI library]]) and update it after each move., e.g.,

http://www.cs.cofc.edu/~manaris/uploads/ticTacToe.png

The user should be able to click on the square they wish to play. (''Hint:'' There are different ways to make this happen - think before you code!)

The human player always goes first. The program should be as robust as possible. When the game is over it should output a message describing the outcome.

This is a '''solo''' assignment (you must work alone).

!!Learning Objectives

* To learn the fundamentals of object-oriented analysis and design.
* To learn the major features of object-oriented programming, including classes, objects, and methods.
* To learn the implementation of these features in the Python language.
* To design expressions using relational and logical operators
* To design selection statements
* To design repetition statements
* To design simple data structures using lists
* To design functions
* To apply object-oriented design in program development

!!Input and Output

The input to the program is the human player's moves - one at a time - capturing a mouse click on the board. If the user makes mistakes (i.e., clicks outside of a free/available square), you should ignore it. Every proper human move you should update the board (by placing an 'X' at the corresponding square).

Each proper human move should be followed by the computer move (e.g., "Computer moves to square 3!" - shown on a label on the display), which should also update the board (by placing an 'O' at the corresponding square).

When a player wins, a colored line (thick enough) should show the winning combination.

http://www.cs.cofc.edu/~manaris/uploads/ticTacToeWin.png

Also, when the game is over, a "Game Over" label (or icon) should appear.

!!!Note

To simplify testing, for now, the '''computer should choose the next available (non-empty) square''' starting from 1.

Soon we will use some Artificial Intelligence (AI) code to make the game more interesting.

!!Documentation

In general, you should comment any variable, obscure statement, block of code, etc. you create.

Your comments should reflect the "why" something is there (or being done), as opposed to the "how". The "how" is reflected in the code. The "why" usually comes from the thought you had, as to what you were trying to do, before you wrote the corresponding Python code.

So, since that info is in your head first (i.e., '''before''' you write the code), it is much easier to type it in your program ('''before''' you figure out the corresponding code that does what needs to be done).

Conversely, once you have already written the code, it is very hard to go back and recreate the original thought (thus comments written '''after''' the code tend to focus on the "how" something is being done, and not the "why" - the latter being what we need.)

The goal of comments is to gently introduce someone accurately into the big picture of our code - the forest view - while the code itself represents the tree view.

Additionally, your code should always have header documentation that explains what the program does, in general terms.

Follow the documentation instructions from [[Spring2015.CSCI220Homework1 | Homework 1]].

!!Submissions

You will submit your assignment via [[https://lms.cofc.edu/ | OAKS]]. Your submission consists of:

# Your Python program. Call it, '''ticTacToe.py'''.

# A printout of your program - to be submitted in class on the due date.

# '''Be ready to play (demonstrate) your game in class.'''

!!Grading

Your grade will be based on how well you followed the above instructions.

!!Credits

# Images come from [[http://mathworld.wolfram.com/Tic-Tac-Toe.html | http://mathworld.wolfram.com]].