Conditionals Lab Dan Maselko. Overview  Gain familiarity with working with conditionals  Become familiar with using the modulus operator  Understand.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Tic Tac Toe Game Design Using OOP
Tic Tac Toe Game playing strategies
Introduction to Programming Using simple games to convey introductory concepts MERLOT International Conference 2004 Tracey Jensen Assistant Professor,
PLANNING THE TIC TAC TOE GAME BY NEEL DAVE. TIC TAC TOE INSTRUCTIONS Tic Tac Toe Instructions The basic concept of Tic Tac Toe 1.This is a game for two.
J AVA A SSIGNMENT 1. O VERVIEW Tic Tac Toe How it should work Using the supplied methods What you need to do How we will test your code.
Integer Tic Tac Toe Let’s Begin Rules: 1.Erase all x’s and o’s from the previous game before you begin. 2.Decide which player will be x’s and which will.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
CSE 115 Week 12 March 31 – April 4, Announcements March 31 – Exam 8 March 31 – Exam 8 April 6 – Last day to turn in Lab 7 for a max grade of 100%,
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Bug Session Two. Session description In this session the use of algorithms is reinforced to help pupils plan out what they will need to program on their.
INTRODUCTION TO DREAMWEAVER 8. What we already know…  Design basics  Contrast  Repetition  Alignment  Repetition  HTML.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
MOM! Phineas and Ferb are … Aims:
How Much Do You Know About: Money?. HOW TO PLAY? ~GET A PENCIL AND PAPER ~IN THE BOTTOM LEFT CORNER, CLICK ON THE IMAGE OF A PROJECTOR SCREEN UNROLLING.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
TA SURVEY Have the TA write their name on the board Fill out the survey at: The TA should walk out 5 minutes.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Class Opener:. Identifying Matrices Student Check:
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Lesson 10 - Mail Merge and Reviewing Documents Advanced Microsoft Word.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Discussion 2: Intro to Project 1: Chess Submit Project 1 under # 11!
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Warship C++: An entity of Battleship
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
2-dimensional Arrays A 2-dimensional array has rows and columns It is actually an “array of arrays” A Tic-Tac-Toe board is an example of a 3 by 3 2-d array.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009.
Graphics Lab: MyPaint Dan Maselko 1. MyPaint Description  For this assignment you will be implementing a very simple paint program.  You should be able.
Magic 8 ball. "Design and write a python program that emulates a Magic Eight Ball. Your program should continually prompt the user to enter a question.
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
Lecture 5 of Computer Science II
Chapter 8 – Arrays and Array Lists
Introduction to Python
Loops BIS1523 – Lecture 10.
Repetition Structures Chapter 9
ENEE150 Discussion 02 Section 0101 Adam Wang.
2-D Lists Taken from notes by Dr. Neil Moore
While Loops BIS1523 – Lecture 12.
Writing Methods AP Computer Science A.
Python Lessons 13 & 14 Mr. Kalmes.
Tic Tac Toe application
Data Structures & Algorithms
Thursday, March 14th Warm Up
Starter Activities GCSE Python.
2-D Lists Taken from notes by Dr. Neil Moore
Tic-Tac-Toe Game Engine
Wrapup which is the document write that says the end.
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
UI Elements 2.
Presentation transcript:

Conditionals Lab Dan Maselko

Overview  Gain familiarity with working with conditionals  Become familiar with using the modulus operator  Understand how to use conditionals to manage errors and special cases  Begin to understand incremental programming

Step 0: Setup  Make a lab4 directory in your 201/labs directory and copy the lab4.py file from Mr. Lupoli’s directory.  From your home directory: cd 201/labs mkdir lab4 cd lab4 cp /afs/umbc.edu/users/s/l/slupoli/pub/lab4.py. emacs lab4.py &

Step 1: Background  The task for this lab will be to implement some of the basic functioning of a tic-tac-toe game  A working example of what the final product for this lab should look like is here:

Step 2: Alternating players  The first thing we’ll need do is make the players alternate between turns.  We should make the value of player be “X” whenever the turn variable is even, and “O” whenever it is odd. To do this, you will need to make use of the % operator.  Once you get this done, print out the value of player during each turn to make sure they are alternating properly. >>> 5 % 1 0 >>> 5 % 2 1 >>> 6 % 2 0 >>> 5 % 3 2

Step 3: Get row and column  The player needs to know where to move, so you’ll need to ask the user for the row and column of where to move to in the board.  Only allow the player to enter values between 0 and 2. If they enter a value less than 0 or greater than 2, have the program exit.  The program can be exited using the sys.exit() function. x = raw_input("Enter a number other than zero: ") if x == 0: print "You entered zero." sys.exit() print "You entered a number that isn’t zero."

Step 4: Convert row and column to an index  We have a row and column for a 3 x 3 Tic Tac Toe board, but the list is only in one dimension.  The list’s indexes would look something like this in a Tic Tac Toe board: Row Row Row Columns:  The value of the index is related to the row and column values. Make use of the fact that there are 3 elements in each row.

Step 5: Putting the player into the list at the index  Now that we have our index, we can put our player into the list at that index.  First, get rid of the thing at that index by using the del() function.  Then put the player at that index by using the insert() function. items = ["apples", "cherries", "bananas", "oranges", "lemons"] del(items[3]) items.insert(3, "grapes") print items ["apples", "cherries", "bananas", "grapes", "lemons"]

Step 6: Printing the Tic-Tac-Toe board  It’s hard to play one dimensional tic-tac-toe. Something like: doesn’t make a lot of sense, so it would be better if it were printed out on 3 lines:  Copy this code into the for loop we’ve been working inside of, and change the comment to real code: ['_', '_', 'O', 'X', '_', 'O', '_', 'X', '_'] _ _ O X _ O _ X _ for i in range(SIZE_BOARD): print board[i], # if three spaces have been printed print

Bonus Step: Moving in an occupied location  Right now, there is nothing stopping any of the players from overwriting the location that another player has already taken.  What we really want is only places in the list with “_”s in them to be overwritten.  After the index is calculated, implement code that will make the program exit if the element at board[index] is not an underscore.

Challenge Step: Handling Wins  Now, our tic-tac-toe game will always take 9 turns, which means it will keep going if one of the players wins.  Add some code that will check if a player has won during each turn, and exit the program if that is the case.