Code Design Using Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.

Slides:



Advertisements
Similar presentations
Building a Conceptual Understanding of Algebra with Algebra Tiles
Advertisements

BackTracking Algorithms
2.1 Basics of Fractions A fraction shows part of something. Most of us were taught to think of fractions as: part of a whole such as ½ means 1 out of two.
Welcome To The Mathematics Challenge Click button to start quiz
Best-First Search: Agendas
Dice Task Task 1Task 2Task 3Task 4 Task 5Task 6Task 7Task 8 NC Level 3 to 6.
6 th Annual Focus Users’ Conference 6 th Annual Focus Users’ Conference District Tests Presented by: Kori Watkins Presented by: Kori Watkins.
Strategy Games for Older Students YUMI DEADLY CENTRE School of Curriculum Enquiries: YuMi Deadly.
Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.
Conditional Statements Introduction to Computing Science and Programming I.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Connect Four Michelle Chen Felix Chung Annabel Hung Raymond Wong.
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Complexity (Running Time)
CIS101 Introduction to Computing Week 12 Spring 2004.
Unlocking Addition The Senari Adaptation of Montessori’s Stamp Game.
If you have a small bag of skittles and you separate then, you have 5 blue, 6 red, 3 purple, and 5 yellow. What is the probability that you would pick.
It is as easy as playing tic-tac-toe!. Make the vertical and horizontal lines of a tic- tac-toe game.
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
CS1Q Computer Systems Lecture 8
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Place Value Kim Burns. Chip-Trading Games Use chip-trading games to teach place value. These games stress the idea of exchanging many for one. The values.
Chapter 2 - Algorithms and Design
The Beauty and Joy of Computing Lecture #5 Programming Paradigms Story details the move to Digital of many things precious to us – music, photos, videos.
Set Theory Topic 1: Logical Reasoning. I can determine and explain a strategy to solve a puzzle. I can verify a strategy to win a game. I can analyze.
COSC2007 Data Structures II
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
 Before plotting points on a graph, you first have to understand the X and Y axis.  The X and Y axis are number lines that run horizontally and vertically.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Connect Four AI Robert Burns and Brett Crawford. Connect Four  A board with at least six rows and seven columns  Two players: one with red discs and.
Computer Science 1000 Spreadsheets V Permission to redistribute or use these slides is strictly prohibited without permission.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
Draft as of 4/1/05 Management …. Are You Ready to Conduct the Inventory Training for Your Team?
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
summer term mathematics activities for year six
HW 6: Problems 2 & 3 Simulating Connect 4. HW 6: Overview Connect 4: Variation of Tic-Tac-Toe – Board: Vertical 7x6 – Two players take alternating move.
Math for Smart Kids Book 1: Multiplication & Division By Mor Harchol-Balter, Ph.D. Copyright March 2003.
Balancing the scales: Inequalities By Melissa Dalis Professor Susan Rodger Duke University June 2011.
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Problem Reduction So far we have considered search strategies for OR graph. In OR graph, several arcs indicate a variety of ways in which the original.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
Ready, SET, go! By Melissa Dalis Professor Susan Rodger Duke University July 2011.
Introduction to Discrete Probability Epp, section 6.x CS 202.
Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.
Programming Problems Intermediate 2 Computing SHOW YOUR TEACHER YOUR WORKING PROGRAM AND WRITING FRAME THEN SAVE IN YOUR PROGRAMMING FOLDER AND UPDATE.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Confidential1 Multiplying Decimals. Confidential2 WARM UP – = = – = Is it true?
Checkers A Matlab Project by Spenser Davison, Edward Dyakiw, Justin Pezzi, and Scott Wu.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Section Basic Counting Principles: The Product Rule The Product Rule: A procedure can be broken down into a sequence of two tasks. There are n 1.
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.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 Quiz Show Programming Terms. 2 Alice - 3D Virtual Programming Vocabulary Quiz Board Chapter 1 Chapter 2a Chapter 2b Chapter 3 Chapter 4 $100 $200 $300.
A fun reading Comprehension Game
Teacher Notes Supplies Needed: Class set of sudoku puzzles.
Topic: Conditional Statements – Part 1
Some problems Cse-402 K3r20,k3r23.
David Kauchak CS52 – Spring 2016
This game plays just like the popular children’s game, Connect Four®
Maths Games for Maths Games Days
Some problems Cse-402 K3r20,k3r23.
EECS 110: Lec 15: Classes and Objects (2)
Colorama Lukasz and Kenny Introduce group members.
Presentation transcript:

Code Design Using Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

Revisit the check_integer Function Rules we must enforce: – Can have spaces at the beginning. – First non-space character should be number or minus sign. – Rest of non-space characters should be digits from 0 to 9. – Can have spaces at the end. – No spaces allowed except at beginning and end. 2

Revisit the check_integer Function Design approach: make a function using each rule. The idea is that each function should do something about a specific rule, so that we do not need to worry about that rule later. 3

The Master Mind Game Player 1: picks a four-digit number, using only digits 1 to 6 (can use the same digit multiple times). – Examples: Player 2: tries to guess the number. (note: the original game uses colored pegs of six different colors, not digits from 1 to 6). 4

The Master Mind Game Step 1: player 1 picks the number. Step 2: player 2 enters a guess. Step 3: player 1 produces an answer: – An "o" for every digit in the guess that appears at the exact same location in the actual number. – An "x" for every digit in the guess that appears in the actual number, but not in the same location as in the guess. – For each digit in the guess, we get either a single "o", or a single "x", or nothing. A single digit in the guess cannot generate more than one letter in the answer. If guess was incorrect, go back to step 2, else game over. 5

Example Game Player 1 picks: 4525 Guess 1: 1234 Answer 1: xx Guess 2: 1465 Answer 1: ox Guess 3: 6426 Answer 1: ox Guess 1: 5425 Answer 1: ooxx Guess 1: 4525 Answer 1: oooo 6

Task: Implement Master Mind The computer should be able to play both roles: First role: – the computer picks number – the human guesses – the computer computes answers Second role: – the human picks number – the computer guesses – the human computes answers 7

Functions A function that the computer uses to pick a valid number, that: – has four digits. – each digit is a number between 1 and 6. Specs: – Name: pick_number – Arguments: none – Output: string of the four-digit number 8

Functions A function that compares the true answer with the user’s guess. Specs: – Name: compute_answer – Two arguments: right answer (the 4-digit number that was originally picked) and guess (the 4-digit guess from the user) – Output: o’s and x’s 9

Functions Playing a game, where computer picks and human guesses. Specs: – Name: play_computer_picks – Arguments: none – Output: number of guesses it took till right answer. Return None if user gives up. 10

Functions A function that determines if a digit in the guess is in the right spot. Specs: – Name: check_right_spot – arguments: right answer, guess, digit_position – Output: boolean, true if in right spot, false otherwise. 11

Functions A function that determines if a digit in the guess is in the wrong spot. Specs: – Name: check_wrong_spot – arguments: right answer, guess, digit_position – Output: boolean, true if in right spot, false otherwise. 12

Top Level Function (still incomplete, this is how far we got by the end of the lecture on Wednesday 7/25): def play_computer_picks(): number_of_guesses = 0 pick = pick_number() guess = get_guess_from_user() number_of_guesses = number_of_guesses + 1 answer = compute_answer(pick, guess) print "the answer is", answer 13

Remaining Tasks for Master Mind Finish implementation of Master Mind, for the case where the computer picks a number and the human guesses. Design and implement code for the case where the human picks a number and the computer guesses. – Think of possible strategies. 14

Other Game-Related Code Projects The Connect 4 game. – The Othello/Reversi game. – Tic-tac-toe. – The battleship game. – The L-game. – The hearts card game. –

Other Code Projects Re-designing the check_integer function. – How can we simplify the design, so that we come up with an intuitive and straightforward implementation? Solving puzzles: – The liar's puzzle. – The missionaries-and-cannibals problem. – The family river-crossing problem. – Sudoku puzzles. An Eliza system (a computer psychoanalyst). 16

Played on a grid, 7 columns wide, 6 rows high. Two players, one red, one green, taking turns. When it is your turn: – You place a piece of your color into one of the 7 columns. – The piece falls down to the lowest free spot in that column. If you form a sequence of four consecutive pieces of your color, you win. – The consecutive pieces may be oriented horizontally (on the same row), vertically (on the same column) or diagonally (from bottom-left to top-right or from bottom-right to top- left). More info: 17 The Connect-4 Game

18 Score: 0-0 (red first) Last move: none Moves so far: 0 Score: 0-0 (red first) Last move: red Moves so far: 1 Score: 0-0 (red first) Last move: green Moves so far: 2 Score: 0-0 (red first) Last move: red Moves so far: 3 Score: 0-0 (red first) Last move: green Moves so far: 4 Score: 0-0 (red first) Last move: red Moves so far: 5 Score: 0-0 (red first) Last move: green Moves so far: 6 Score: 0-0 (red first) Last move: red Moves so far: 7 Score: 0-0 (red first) Last move: green Moves so far: 8 Score: 1-0 (red first) Last move: red Moves so far: 9

The Liars Puzzle From "Structure and Interpretation of Computer Programs", by Abelson, Sussman, and Sussman, 2 nd edition. Five schoolgirls sat for an examination. Their parents –so they thought- showed an undue degree of interest in the result. They therefore agreed that, in writing home about the examination, each girl should make one true statement and one untrue one. The following are the relevant passages from their letters: – Betty: "Kitty was second in the exam. I was third." – Ethel: "I was first in the exam. Joan was second." – Joan: "I was third, and Ethel was bottom." – Kitty: "I was second, and Mary was fourth." – Mary: "I was fourth, and Betty was first." What was the true order in which the five girls were placed? 19

Missionaries and Cannibals From Three missionaries and three cannibals must cross a river using a boat with the following constraints: – The boat can carry at most two people. – For both banks, if there are missionaries present on the bank, they cannot be outnumbered by cannibals (if they were, the cannibals would eat the missionaries.) – The boat cannot cross the river by itself with no people on board. 20

Family River-Crossing Problem From A man and a woman of equal weight, together with two children, each of half their weight, wish to cross a river using a boat, under these constraints: – The boat can only carry the weight of one adult. – The boat cannot cross the river by itself with no people on board. 21