Be sure to read this entire write-up before starting!
Do not use string methods or functions, lists, or any topics not
covered in Week2 or Hw2.
Treblecross!
Your main task today is to write the function treblecross() so that your app closely (if perhaps not perfectly)
matches the app in this video:
Also, once you fairly closely match the video, you may
go further (time permitting),
adding more features or more well-chosen complexity.
However, if you have some time, you should first do the
"Time Permitting" exercise described below.
Starter Code:
Here is some suggested starter code for you.
While there
are quite a few functions, most of them are very short
and straightforward.
Be sure to carefully read the starter code before
you start. Look especially closely at the
treblecross() function!
# Be sure to first read the writeup and carefully watch the video!
import random
'''
This represents a 1xn board as an integer,
where 1 is occupied and 0 is empty.
Actually, we do this right-to-left rather than left-to-right,
so this board:
x x - - x
is represented by this integer:
10011
'''
def treblecross():
# play an 1xn game of Treblecross (1d tic-tac-toe)
printIntroMessage()
n = getBoardSize()
# initialize the board
board = 0 # think of this as an n-digit number of all 0's to start
# play the game
player = 1
while True:
# Draw the board:
drawBoard(board, n)
# Check if the game is over:
if isGameOver(board, n):
print(f'Game over! Player {player} wins!')
break
# Get the the move:
move = getMove(player, board, n)
if move == 'c':
move = getComputerMove(board, n)
# Make the move:
board = makeMove(player, board, move)
# Switch current player:
player = getOtherPlayer(player)
def printIntroMessage():
# Print a short introduction, a couple of lines that
# say what the app is (Treblecross) and very briefly
# describe how to play the game:
return 42 # Replace this with your code
def getBoardSize():
# Use input() to get and return n, the size of the 1xn board,
# where 3 <= n <= 9. It is ok to crash if the user enters
# a non-integer, but for integers out of range, just adjust
# them to be in range.
return 42 # Replace this with your code
def drawBoard(board, n):
# Draw the board. Remember to use end='' in your print
# statements if it helps (which we found it did!).
return 42 # Replace this with your code
def isGameOver(board, n):
# Return True if the 1xn board contains three consecutive non-empty
# cells (1's), and False otherwise.
# You might find getKthDigit() to be handy here.
return 42 # Replace this with your code
def getMove(player, board, n):
# Using input(), get the next move from the player. Do this in a loop,
# and keep asking until you get a legal value.
# If the user enters 'c', return that, as the user is asking
# for a computer-generated move.
# Otherwise, return the integer of the legal move so long
# as the board is empty at that cell.
# Otherwise, print an appropriate message and keep looping.
return 42 # Replace this with your code
def getComputerMove(board, n):
# Get the computer-generated move for this 1xn board.
# For now, we'll just move randomly. We could of course do
# much better (interesting food for thought).
return 42 # Replace this with your code
def makeMove(player, board, move):
# Return the new board (an integer) resulting from making the
# given legal move (an integer) on the given board.
return 42 # Replace this with your code
def getOtherPlayer(player):
# If player is 1, return 2.
# If player is 2, return 1.
return 42 # Replace this with your code
def getKthDigit(n, k):
# This is from the guided exercises.
return 42 # Replace this with your code
def setKthDigit(n, k, d):
# This is from the guided exercises.
return 42 # Replace this with your code
treblecross()
Design Requirements:
You must follow these design requirements:
Store the board in a variable named
board of type integer.
If the game board has n cells in it, then the variable
board should be an integer with n digits in it.
The digits in board will all be 0 or 1,
where 0 means the cell is empty, and a 1 means the cell
has an 'x' in it.
To start, set board = 0, since that
will represent an entirely empty board regardless of
the value of n.
Important Hints:
Again, do not use string methods or functions,
lists, or any topics not covered in or
before Week 2 / Hw2.
Yes, this would be easier
and more natural to code
with strings and lists. But we are learning just how
powerful integers alone can be. Amazing!
Store the board as a single integer. It will be a
bit easier if you do this right-to-left rather
than left-to-right, so this board:
x x - - x
is represented by this integer:
10011
This way, the the kth digit from the right of the integer
corresponds to the kth cell from the left on the board.
Use getKthDigit(board, k) to see if the kth
cell is empty or not -- that is, if the kth digit
in board is 0 or 1.
Use setKthDigit(board, k) to set the kth
cell of the board to occupied (that is, to 1).
For the computer-generated move, it's ok at first to
just make a random legal move. For that, you might
want to use random.randrange to start at
a random cell. Keep searching from that random cell
(perhaps using % to wrap around the end of the board)
until you find an empty cell and move there.
Once you are done, and have completed the
"Time Permitting" code tracing task below, then
you may want to think about better and more interesting
ways to generate computer moves. Fun!
To print the board, it is helpful to print each cell
in a loop,
but stay on the same line using end='',
like so:
print('This all ', end='')
print('prints on ', end='')
print('one line!', end='')
print() # this just prints a newline
We also found it helpful to print using f-strings,
like so:
x = 42
print(f'x = {x}') # prints: x = 42
As usual, get the game working without worrying too much
about the output formatting. Only then get the formatting
right.
Also, the output strings in the video work well in a console
outside of CS Academy, but are a bit long inside the console.
Feel free to shorten them or split them across multiple
lines. For example, instead of this prompt:
Enter the board size (an integer between 3 and 9) -->
You might use something more like this prompt:
Enter board size (3-9) -->
Time permitting:
If you finish the creative coding task above, you and your
partner(s) should work together on
Additional Code Tracing Exercises from 2.2.10.