Noughts and Crosses. Step 1: Drawing the grid. Introduction

Similar documents
UNIT 10A Visualizing Data: Graphics in Python. Drawing using Python. We will be using Python's interface to Tcl/Tk, a cross-plaoorm graphics library.

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

Tic Tac Toe Game! Day 8

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

Functionally Modular. Self-Review Questions

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

Mill Hill School. 13+ Entrance Examination Mathematics. January Time: 1 hour. School: Mark: Materials required for examination

CAS London CPD Day February 16

Draw beautiful and intricate patterns with Python Turtle, while learning how to code with Python.

Drawing Shapes on a Coordinate Grid

1 Some easy lines (2, 17) (10, 17) (18, 2) (18, 14) (1, 5) (8, 12) Check with a ruler. Are your lines straight?

Mid Unit Review. Of the four learning outcomes for this unit, we have covered the first two. 1.1 LO1 2.1 LO2 LO2

PLC Papers Created For:

Generating Vectors Overview

Level 3 Computing Year 2 Lecturer: Phil Smith

Load your files from the end of Lab A, since these will be your starting point.

DRAFT CHAPTER. Surface Area GET READY. xxx. Math Link. 5.1 Warm Up xxx. 5.1 Views of Three-Dimensional Objects xxx. 5.

Self-Teach Exercises: Getting Started Turtle Python

Section A Solids Grade E

CISC 1600, Lab 3.1: Processing

LO: To recreate Matisse s Snail in Python

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

CSE120 Wi18 Final Review

Introduction to Flash - Creating a Motion Tween

Tic-Tac-Toe. By the time you are done with this activity, you and your team should be able to:

Tkinter Part II: Buttons, Lambda & Dynamic Content

Graphical User Interfaces

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Question 1 [20 points]

Paint Tutorial (Project #14a)

Quick Reference Tables

Grade 6 Mathematics Item Specifications Florida Standards Assessments

Word 2003: Flowcharts Learning guide

Computer and Programming: Lab 1

In this project, you ll learn how to use a turtle to draw awesome shapes and patterns.

Adobe Photoshop How to Use the Marquee Selection Tools

Drawing Tools. Drawing a Rectangle

Understanding the Screen

Properties of a Circle Diagram Source:

PYTHON NOTES (drawing.py and drawstuff.py)

Intro to Python Programming

Total. Name: Signature: Circle course section: MW 9-11 MW 11-1 MW 6-8 TTh 9-11 TTh 1-3

Shape Cluster Photo Written by Steve Patterson

CISC 1600, Lab 2.1: Processing

Introduction to Google SketchUp

Introduction To Python

Haskell Refresher Informatics 2D

CMPSCI 119 Fall 2018 Wednesday, November 14, 2018 Midterm #2 Solution Key Professor William T. Verts

Total. Name: Signature: Circle course section: MW 9-11 MW 11-1 MW 6-8 TTh 9-11 TTh 1-3

A B C 1 Number of Squares Number of Nickels Total Number of Nickels =A2+1 =B2*2 =B3+C2

Learning outcomes. COMPSCI 101 Principles of Programming. Drawing 2D shapes using Characters. Printing a Row of characters

Chapter 10 Test Review

Programming Fundamentals

This document should only be used with the Apple Macintosh version of Splosh.

Here is a sample IDLE window illustrating the use of these two functions:

CPSC 217 Assignment 3

M8WSB-C07.qxd 4/4/08 7:00 PM Page NEL

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

# arrange Label in parent widget

UNIT 19 Similarity Lesson Plan 1

Measuring Triangles. 1 cm 2. 1 cm. 1 cm

CSci 1113 Lab Exercise 6 (Week 7): Arrays & Strings

Adobe Flash CS3 Reference Flash CS3 Application Window

Part 3. Useful Python

CISC 1600, Lab 3.2: Interactivity in Processing

Programming Project 1

CSC 108. Prof. Yue-Ling Wong. Lab 6: Tic-tac-toe Game


1 Undirected Vertex Geography UVG

The figures below are all prisms. The bases of these prisms are shaded, and the height (altitude) of each prism marked by a dashed line:

Mathematics Stage 5 PAS5.2.3 Coordinate geometry. Midpoint, distance and gradient

CS12020 for CGVG. Practical 2. Jim Finnis

CS1 Studio Project: Connect Four

Easy Graphical User Interfaces

CS159 Midterm #1 Review

Plot four points A, B, C and D on the grid to make a rectangle ABCD of length 6 cm and width 4 cm. (2)

IT115: Introduction to Java Programming 1. IT115: Introduction to Java Programming. Tic Tac Toe Application. Trina VanderLouw

Programming Training. This Week: Tkinter for GUI Interfaces. Some examples

Name 8-6A. 1. What type of quadrilateral is shown below? A Rectangle B Trapezoid C Rhombus D Square. 2. What is true about every rhombus?

CISC 1600 Lecture 3.1 Introduction to Processing

An Introduction to Processing

SHAPES & TRANSFORMS. Chapter 12 of Pro WPF : By Matthew MacDonald Assist Lect. Wadhah R. Baiee. College of IT Univ.

Grade 6 Math Circles October 16 & Non-Euclidean Geometry and the Globe

Lecture 5 2D Transformation

Session 8.2. Finding Winners Using Arrays

TSM Report Designer. Even Microsoft Excel s Data Import add-in can be used to extract TSM information into an Excel spread sheet for reporting.

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

Tutorial 3: Constructive Editing (2D-CAD)

Tkinter: Input and Output Bindings. Marquette University

Answer Key: CIS 166 Final Exam, Version 3, Spring 2015

Senet. Language Reference Manual. 26 th October Lilia Nikolova Maxim Sigalov Dhruvkumar Motwani Srihari Sridhar Richard Muñoz

The width is cm 2 marks. 2 a Measure the length of the line in millimetres. b Mark with an arrow ( ) the midpoint of the line.

2.6 Graphics SIMPLE DRAWINGS 9/9/16 74

1. Complete these exercises to practice creating user functions in small sketches.

Use lists. Use loops. Use conditionals. Define and use functions. Create and use code modules

Drawing a Circle. 78 Chapter 5. geometry.pyde. def setup(): size(600,600) def draw(): ellipse(200,100,20,20) Listing 5-1: Drawing a circle

Java GUI Test #1 Solutions 7/10/2015

Input/Output Machines

Assignment 6. INF109 Dataprogrammering for naturvitskap

Slide 1 / 96. Linear Relations and Functions

Transcription:

6 Noughts and Crosses These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs at www.codeclub.org.uk/. This coursework is developed in the open on GitHub, https://github.com/codeclub/ come and join us! Introduction Time for another game, and today is Noughts and Crosses, or Tic-Tac-Toe. Players take turns marking an X or an O until one player gets three in a row. Step 1: Drawing the grid We want to draw four lines, in a # pattern, like this: We could use the turtle commands to draw it, but today we're going to learn about the Tk Canvas.

Activity Checklist 1. Open up IDLE, create a New File, and save it as 'xox.py' 2. Write the following code from tkinter import * main = Tk( c = Canvas(main, width=600, height=600 c.pack( c.create_line(200, 0, 200, 600 c.create_line(400, 0, 400, 600 c.create_line(0, 200, 600, 200 c.create_line(0, 400, 600, 400 mainloop( 3. Save and Run your program, and it will draw a grid! The Canvas We make a 600 by 600 pixel window with c = Canvas(main, width=600, height=600, which looks like this to the computer:

0 200 400 600... 0+--------+--------+--------+-----> across 200 A B 400 C D 600... V down Here point A is at 200 across, 200 down. Point B is at 400 across, 200 down. Point C is at 200 across, 400 down. Point D is at 400 across, 400 down. Each of the commands c.create_line(across1,down1,across2,down2 draws a line across the screen, and the four numbers act as positions. If we wanted to draw a line from A to D, we would do c.create_line(200, 200, 400, 400.

0 200 400 600... 0 +--------A--------B--------+-----> across 200 M O 400 N P 600 C D... V down We want to draw lines from A to C, B to D, M to O, and N to P. c.create_line(200, 0, 200, 600 # A to C c.create_line(400, 0, 400, 600 # B to D c.create_line(0, 200, 600, 200 # M to O c.create_line(0, 400, 600, 400 # N to P In code, across is often called x, and down is often called y. This grid system is quite like the coordinates you've learned in mathematics, but we start from the top-left hand side. Step 2: Drawing a O

Activity Checklist 1. In the same file, let's add a new function to draw when you click the mouse! from tkinter import * main = Tk( c = Canvas(main, width=600, height=600 c.pack( c.create_line(200, 0, 200, 600 c.create_line(400, 0, 400, 600 c.create_line(0, 200, 600, 200 c.create_line(0, 400, 600, 400 def click(event: c.create_oval(200,200,400,400 c.bind("<button-1>", click mainloop( 2. Run your code, and click on the grid, what happens? You should see a circle in the centre of the grid.

3. Let's edit the code, so it will draw where you click For this we'll need to take the mouse position, and work out which grid square it is in, and change the click function from tkinter import * main = Tk( c = Canvas(main, width=600, height=600 c.pack( c.create_line(200, 0, 200, 600 c.create_line(400, 0, 400, 600 c.create_line(0, 200, 600, 200 c.create_line(0, 400, 600, 400 def click(event: across = int(c.canvasx(event.x/200 down = int(c.canvasy(event.y/200 c.create_oval( across*200,down*200, (across+1*200,(down+1*200 c.bind("<button-1>", click mainloop( int(c.canvasx(event.x/200 takes the mouse position event.x, turns it into the canvas position, c.canvas(event.x and then divides it by 200, and rounds it down, so we get a number 0, 1 or 2 to tell us how far across the mouse is. 4. Run the code, click in the grid squares, each one should fill in with a circle! The code c.create_oval(across*200,down*200,(across+1*200, (down+1*200 turns 'Along 1, Down 2' into positions on the grid, like Along 200, Down 400. Step 3: Drawing an X

Activity Checklist 1. In the same file, let's add some code to draw an X, then an O, then an X,...

from tkinter import * main = Tk( c = Canvas(main, width=600, height=600 c.pack( c.create_line(200, 0, 200, 600 c.create_line(400, 0, 400, 600 c.create_line(0, 200, 600, 200 c.create_line(0, 400, 600, 400 shape = "O" def click(event: global shape across = int(c.canvasx(event.x/200 down = int(c.canvasy(event.y/200 if shape == "O": c.create_oval( across*200,down*200, (across+1*200,(down+1*200 shape = "X" else: c.create_line( across*200, down*200, (across+1*200, (down+1*200 c.create_line( across*200, (down+1*200, (across+1*200, down*200 shape = "O" c.bind("<button-1>", click mainloop(

2. Run your program, try click on a grid, it should draw a O, click elsewhere it should draw an X We've used a new feature of python, global to let us change the variable shape in the function click. If you change variables defined outside of a function, then you have to use global. 3. What happens if you click on the same square twice? This is because our code doesn't keep track of what has been drawn, or where players have moved. We will have write some more code to fix this. Step 4: Keeping track Activity Checklist To stop players playing the same move twice, we'll have to keep track of the moves they make. To do this we'll introduce a list called grid 1. In the same file, from tkinter import * main = Tk( c = Canvas(main, width=600, height=600 c.pack( c.create_line(200, 0, 200, 600 c.create_line(400, 0, 400, 600 c.create_line(0, 200, 600, 200 c.create_line(0, 400, 600, 400 shape = "O" grid = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", ] def click(event:

global shape, grid across = int(c.canvasx(event.x/200 down = int(c.canvasy(event.y/200 square = across + (down*3 if grid[square] == "X" or grid[square] == "O": return if shape == "O": c.create_oval( cross*200,down*200, (across+1*200,(down+1*200 grid[square] = "O" shape = "X" else: c.create_line( across*200, down*200, (across+1*200, (down+1*200 c.create_line( across*200, (down+1*200, (across+1*200, down*200 grid[square] = "X" shape = "O" c.bind("<button-1>", click mainloop( 2. Run your program, and try and click in the same square twice? What happens? Step 5: Finding a winner Now we have got the game working, we need to find a winner! Activity Checklist

1. In the same file, we're going to introduce a new function winner, and call it to check if the game has been won The completed code looks like this! from tkinter import * main = Tk( c = Canvas(main, width=600, height=600 c.pack( c.create_line(200, 0, 200, 600 c.create_line(400, 0, 400, 600 c.create_line(0, 200, 600, 200 c.create_line(0, 400, 600, 400 shape = "O" grid = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", ] def click(event: global shape, grid across = int(c.canvasx(event.x/200 down = int(c.canvasy(event.y/200 square = across + (down*3 if grid[square] == "X" or grid[square] == "O": return if winner(: return if shape == "O": c.create_oval( across*200,down*200, (across+1*200,(down+1*200

grid[square] = "O" shape = "X" else: c.create_line( across*200, down*200, (across+1*200, (down+1*200 c.create_line( across*200, (down+1*200, (across+1*200, down*200 grid[square] = "X" shape = "O" def winner(: for across in range(3: row = across*3 line = grid[row] + grid[row+1] + grid[row+2] if line == "XXX" or line == "OOO": return True for down in range(3: line = grid[down] + grid[down+3] + grid[down+6] if line == "XXX" or line == "OOO": return True line = grid[0]+grid[4]+grid[8] if line == "XXX" or line == "OOO": return True line = grid[2]+grid[4]+grid[6] if line == "XXX" or line == "OOO": return True c.bind("<button-1>", click mainloop( 2. Try playing the game and winning, can you make any more moves?

We have four checks in winner 1. Check each row for three X's or O's 2. Check each column for three X's or O's 3. Check the diagonal from left to right 4. Check the diagonal from right to left. Try You're all done! Why not change the code to draw different shapes! These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs at www.codeclub.org.uk/. This coursework is developed in the open on GitHub, https://github.com/codeclub/ come and join us!