Вы находитесь на странице: 1из 6

Apple AiC Summer 2020 Workshop

Tic Tac Toe

The Game

Tic Tac Toe is a two-player game where one player uses “x” and the other uses “o”.
Each turn, a player puts their letter on one of the spaces in the board. The winner is the
player that can get three of their letter in a row. If the board fills up and no player gets
three in a row, it’s a tie. Here’s an example:

Beginning of the game:


_____________
____|____|____
____|____|____
____|____|____

Player 1’s first turn:


_____________
____|____|____
____|_O _|____
____|____|____

Player 2’s first turn:


_____________
____|____|_X__
____|_O _|____
____|____|____

Player 1’s second turn:


_____________
____|____|_X__
____|_O _|_O__
____|____|____

Player 2’s second turn:


_____________
____|____|_X__
____|_O _|_O__
____|____|_X__

Player 1’s third turn:


_____________
____|____|_X__
_O _|_O _|_O__
____|____|_X__
Player 1 wins!

Your task
Your task is to build a command line version of tic tac toe. That means that the user will
run your game from the command line and type commands to play the game. For
example, your program might print something like this to start:

> Hello, and welcome to tic tac toe! Player one, you're X.
Player two will be O. Player one, it's your turn. Enter a number
from 1 to 9 to choose which spot on the board you'd like to
place your X:
______________
_1__|__2_|_3__
_4__|__5_|_6__
_7__|__8_|_9__

> 5

> Player two's turn. Enter a number from 1 to 9 to choose which


spot on the board you'd like to place your O
______________
_1__|__2_|_3__
_4__|__X_|_6__
_7__|__8_|_9__

> 3

In this example, the > signifies the start of a command or printed text in Terminal. You
get to choose how the players should interact with your game and what the board
should look like, numbers 1-9 is just one example. You're welcome to make your game
as simple or elaborate as you'd like. You might choose to make a more simple game
and spend more time practicing branches and pull requests with Git instead. Work with
your team to divide up the work and help each other with Git and other things you'd like
to learn.

Most importantly, have fun with it! We hope this provides a space to get to know other
amazing NCWITers while working on something fun and gaining practice with new tools.
We will not be evaluating you on this project, it's really for you! We will have casual
presentations towards the end of the workshop where you can share what you've
learned with your peers and with women from Apple.

Programming Tic Tac Toe

Python (macOS, Windows)


Option 1
Create a new .py file (Python file) in Sublime Text and save it to your Desktop. Press
Ctrl-B to run the code in Sublime's command line.

Option 2
Create a new TicTacToe.py file (Python file) in Sublime Text and save it to your Desktop.
To run it on the command line, open Terminal or Command Line, navigate to your
Desktop
(cd ~/Desktop), and then type:

python TicTacToe.py

to run the file. Make sure you have Python installed on your computer. Message us on
Slack if you need help with this!

Java (macOS, Windows)

Create a TicTacToe.java file in Sublime and save it to your Desktop. To run it on the
command line, open Terminal or Command Line, navigate to your Desktop
(cd ~/Desktop), and then double check that the Java environment file is pointing to the
directory where you installed the JDK (Java Development Kit):

export JAVA_HOME=/Library/Java/Home

Then to build and run your file type:


javac TicTacToe.java
java TicTacToe

Swift (macOS)

Open Xcode and create a new playground. File > New Playground >iOS Blank. Add the
boilerplate code, and your code will run in real-time, without you having to build
anything!
All Options
Please reach out to us on Slack if you need help with any of these steps! We want you
to be up and running ASAP so you can work on your games :)

Breaking up the work

Together: how will you represent the board in code? An array? A text file? Try to get on
the same page about this early, so that you can use that representation to create
functions for the following things:

1. How will you print out the board so that the players can see the current state?
2. How will you accept user input on the command line? How will the user specify
which space on the board to place their letter?
3. How will you place an X or O on the board and make sure that space is valid and
empty?
4. How will you detect when someone has won, or there's a tie, and end the game?

Possible extensions

There are lots of possibilities for getting creative and taking your code a step further!

1. Can you extend the board to different sizes? Can one of the players specify the size
they want?
2. Can you keep a running score so that players can play against each other multiple
times and see who’s won the most games? How will you store the data? Maybe a
text file?
3. Can you add a user interface so that players can interact with the game in UI
instead of on the command line?
4. Can you get the computer to play against you? How will it decide what moves to
make? In the most basic version, it could choose a random available spot.
5. How accessible is your game? If you added UI (or not), what's the experience like
for someone who is blind and uses VoiceOver or another screen reader? Can you
improve the experience?

Resources

Reading console input:

Python
https://www.geeksforgeeks.org/taking-input-in-python/

Java
https://www.codejava.net/java-se/file-io/3-ways-for-reading-input-from-the-user-in-the-
console
Swift
https://developer.apple.com/documentation/swift/swift_standard_library/
input_and_output

Вам также может понравиться