EECS 110: Lec 15: Classes and Objects (2)

Slides:



Advertisements
Similar presentations
Tic tac toe v1.2 Made by Rogonow XX PC: X YOU: O The PC-player with mark X goes first. After the PC, you place the mark O at the position of a green oval.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
"Intelligent" CS 5 An object is structured data that is alive, responsible, and intelligent. Sound too friendly? This week’s objects and classes will be.
Presented by : Ashin Ara Bithi Roll : 09 Iffat Ara Roll : 22 12th Batch Department of Computer Science & Engineering University of Dhaka.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Artificial Intelligence in Game Design
HW 6: Problems 2&3 Simulating Connect 4.
Intelligent CS 5 ? HW 11 (1 problem !) M/T sections W/Th sections due Sunday, 11/14 at midnight due Monday, 11/15 at midnight Recitation for HW11 -- Friday.
CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html.
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)
Minimax.
CS1020E Sitin 1 Discussion -- Counting Palindromes.
A whole new class of programming CS 5 today HW10: Due Sun, Nov 15 Pr0: Ariane 5 Reading Pr1/Lab:the Date class Pr2 Connect4Board Pr3 Connect4Player (extra.
Nelson Series Talk Wed, 11/10 7:00 pm Security, Liberties and Trade-offs in the War on Terrorism Since 9/11, we have enacted the Patriot Act, tighter screening.
IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where.
Def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ if self.month in.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
IS313 Today: two I's AIUI hw9pr1.py an arbitrarily-good Connect 4 player! Player class Hw 10 (project) I wish there were three i's! Hw 9 is due Wed., 12/23.
Homework 11 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/14 Mon., 11/15 Problems
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
Code Design Using Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
IS313 … Monday, Nov. 8 - Classes + Objects, part 2 Monday, Nov Project strategies & example pres. Tuesday, Nov. 9 - Date class due Tuesday, Nov.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
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.
Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer'
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Tic tac toe XX PC: X YOU: O The PC-player with mark X goes first. After the PC, you place the mark O at the position of a green circle. If you succeed.
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
Checkers A Matlab Project by Spenser Davison, Edward Dyakiw, Justin Pezzi, and Scott Wu.
INTRO2CS Tirgul 8 1. What We’ll Be Seeing Today  Introduction to Object-Oriented Programming (OOP).  Using Objects  Special methods 2.
Artificial Intelligence in Game Design Board Games and the MinMax Algorithm.
Homework 10 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/10 Mon., 11/11 Problems
IS313 Tomorrow… Wednesday, Nov Classes + Objects, part 2 Wednesday, Nov Classes + Objects, part 3 Thursday, Nov Date class due Thursday,
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 14: Classes and Objects
EECS 110: Lec 15: Classes and Objects (2)
CS this week 4 Building classes vs. using the library hw10pr2
Section 4.4 Counting Blobs
EECS 110: Lec 17: Review for the Final Exam
COMPSCI 107 Computer Science Fundamentals
EECS 110: Lec 9: Review for the Midterm Exam
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
CMPT 120 Topic:  Case Study.
Introduction to Object-Oriented Programming (OOP)
Sit-In Lab 1 Ob-CHESS-ion
CS150 Introduction to Computer Science 1
Intelligent CS 5 ? This week's objects will be more adversarial…
This week's classes Connect 4 aiMove
2D Arrays.
EECS 110: Lec 10: Definite Loops and User Input
Introduction to Object-Oriented Programming (OOP)

Topic 1: Problem Solving
EECS 110: Lec 14: Classes and Objects
EECS 110: Lec 4: Functions and Recursion
HW 6: Problems 2 & 3 Simulating Connect 4.
CS this week 4 Building classes vs. using the library hw10pr2
HW 6: Problems 2 & 3 Simulating Connect 4.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Introduction to Object-Oriented Programming (OOP)
Introduction to Object-Oriented Programming (OOP) II
Six dominoes have been placed in this grid and there is only one way of filing the grid with more dominoes. Try it:
Backtracking, Search, Heuristics
Presentation transcript:

EECS 110: Lec 15: Classes and Objects (2) Aleksandar Kuzmanovic Northwestern University http://cs.northwestern.edu/~akuzma/classes/EECS110-s09/

EECS 110 Today Connect Four | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X|O| | --------------- 0 1 2 3 4 5 6 X to move. Is there a way to win?

Aargh! Python has no Connect-four datatype… | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X| |O| --------------- 0 1 2 3 4 5 6 … but we can correct that!

Designing classes 1) What data? (Data Members) Not limited to 7x6! 2) What are objects' crucial capabilities? (Methods)

Designing classes What data? (Data Members) - height, width - Where the chips are - Whose turn it is - Winning condition - End condition (number of chips played) Not limited to 7x6! 2) What are objects' crucial capabilities? (Methods)

Designing classes What data? (Data Members) - height, width - Where the chips are - Whose turn it is - Winning condition - End condition (number of chips played) Not limited to 7x6! 2) What are objects' crucial capabilities? (Methods) creates a new object check for win print get the next move/switch turn check for full board

Connect Four: the object b int str str str str list width Board b data int str str str str height str str str str data What is the name of the method that will construct this data?

Connect Four: the object b int str str str str list width Board b data int str str str str height str str str str data What is the name of the method that will construct this data? __init__(…)

Connect Four: constructor class Board: """ a datatype representing a C4 board with an arbitrary number of rows and cols """ def __init__( self, width, height ): """ the constructor for objects of type Board """ self.width = width self.height = height self.data = [] # this will be the board for row in range( height ): # 6 boardRow = [] for col in range( width ): # 7 boardRow += [' '] # add a space to this row self.data += [boardRow]

Connect Four: the object b int str str str str list width Board b data int str str str str height str str str str | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X| |O| --------------- 0 1 2 3 4 5 6 What is the name of the method that will print this data?

Connect Four: __repr__ def __repr__(self): """ this method returns a string representation for an object of type Board """ s = '' for row in range( self.height ): s += '|' for col in range( self.width ): s += self.data[row][col] + '|' s += '\n' return s which row is row 0, row 1, and so on? To remove? To add?

Connect Four: __repr__ def __repr__(self): """ this method returns a string representation for an object of type Board """ s = '' for row in range( self.height ): s += '|' for col in range( self.width ): s += self.data[row][col] + '|' s += '\n‘ s += '--'*self.width + '-\n‘ s += ' ' + str(col%10) s += '\n' return s which row is row 0, row 1, and so on?

Step through this mystery method. "Quiz" class Board { def mystery(self, col, ox): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[self.height-1][col] = ox def allowsMove(self, col): } a C4 board col # 'X' or 'O' Step through this mystery method. What is each line doing? What's going wrong? Write allowsMove to return True if col is a valid move; False otherwise.

Step through this mystery method. "Quiz" class Board { def mystery(self, col, ox): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[self.height-1][col] = ox # Adds ox at the top of a column # But at the same time overwrites the existing # elements in the column… # Does not check if it is possible to write in a column… def allowsMove(self, col): } a C4 board col # 'X' or 'O' Step through this mystery method. What is each line doing? What's going wrong? Write allowsMove to return True if col is a valid move; False otherwise.

Step through this mystery method. "Quiz" class Board { def mystery1(self, col, ox): if allowsMove(col): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox return self.data[self.height-1][col] = ox def allowsMove(self, col): } a C4 board col # 'X' or 'O' Step through this mystery method. What is each line doing? What's going wrong? Write allowsMove to return True if col is a valid move; False otherwise.

Step through this mystery method. "Quiz" class Board { def mystery1(self, col, ox): if allowsMove(col): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox return self.data[self.height-1][col] = ox def allowsMove(self, col): if 0 <= c < self.width: return self.data[0][col] == ' ' } a C4 board col # 'X' or 'O' Step through this mystery method. What is each line doing? What's going wrong? Write allowsMove to return True if col is a valid move; False otherwise.

C4 Board class: methods Which of these will require the most thought? the “constructor” __init__( self, width, height ) checks if allowed allowsMove( self, col ) places a checker addMove( self, col, ox ) removes a checker delMove( self, col ) __repr__( self ) outputs a string isFull( self ) checks if any space is left winsFor( self, ox ) checks if a player has won hostGame( self ) play! Which of these will require the most thought?

winsFor( self, ox ) b b.winsFor( 'X' ) or 'O' X O Thoughts?

winsFor( self, ox ) b Thoughts? X O def winsFor(self, ox): # check for horizontal wins for row in range(0,self.height): for col in range(0,self.width-3): if self.data[row][col] == ox and \ self.data[row][col+1] == ox and \ self.data[row][col+2] == ox and \ self.data[row][col+3] == ox: return True # check for vertical wins X O Thoughts?

The Player class (Extra credit) Details Player pForX (data and methods) What data and methods are needed to construct and implement a Player object?

Player Picture of a Player object DATA 3 'X' 'LEFT' METHODS string ox string tbt int ply Player pForX checker, O or X tiebreakType __init__(self, ox, tbt, ply) __repr__(self) oppCh(self) METHODS scoreBoard(self, b) scoresFor(self, b) tiebreakMove(self, scores) nextMove(self, b)

scoreBoard ‘X’ ‘O’ Assigns a score to any board, b A simple system: 100.0 50.0 0.0 for a win for anything else for a loss Score for Score for Score for Score for

scoreBoard ‘X’ ‘O’ Assigns a score to any board, b A simple system: 100.0 50.0 0.0 for a win for anything else for a loss Score for 100.0 Score for Score for 0.0 Score for

scoreBoard ‘X’ ‘O’ Assigns a score to any board, b A simple system: 100.0 50.0 0.0 for a win for anything else for a loss Score for 100.0 Score for 50.0 Score for 0.0 Score for 50.0

What class is this method in? scoreBoard Assigns a score to any board, b A simple system: 100.0 50.0 0.0 for a win for anything else for a loss Implementation ideas… scoreBoard(self, b) What methods that already exist will come in handy? This doesn't seem to be looking very far ahead ! How can there be no 'X' or 'O' input? What class is this method in?

Looking further ahead… scoreBoard looks ahead 0 moves 0-ply If you look one move ahead, how many possibilities are there to consider? A 1-ply lookahead player will "see" an impending victory. to move… 1-ply score

Looking further ahead… scoreBoard looks ahead 0 moves 0-ply If you look one move ahead, how many possibilities are there to consider? A 1-ply lookahead player will "see" an impending victory. to move… 1-ply score -1 50 50 50 100 50 50

Looking further ahead… scoreBoard looks ahead 0 moves 0-ply If you look one move ahead, how many possibilities are there to consider? A 2-ply lookahead player will also "see" an opponent's impending victory. to move… 2-ply -1 50 score

Looking further ahead… scoreBoard looks ahead 0 moves 0-ply If you look one move ahead, how many possibilities are there to consider? 1-ply 2-ply scoresFor( self, b ) returns a LIST of scores, one for each column you can choose to move next…

Example 1-ply and 2-ply lookahead scores It is O’s move. What scores does a 1-ply lookahead for O assign to each move? |O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| --------------- 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 Which change at 2-ply?

Example 1-ply and 2-ply lookahead scores It is O’s move. What scores does a 1-ply lookahead for O assign to each move? |O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| --------------- 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 -1 100 50 100 50 100 50 Which change at 2-ply?

Example 1-ply and 2-ply lookahead scores It is O’s move. What scores does a 1-ply lookahead for O assign to each move? |O| | | | | | | |X| | | |O| |X| |O| | | |X|O|X| |X| | | |O|O|X| |X| |X| |X|O|O| |X| |O|O|O|X|X| --------------- 0 1 2 3 4 5 6 col 0 col 1 col 2 col 3 col 4 col 5 col 6 -1 100 50 50 50 100 50 1-ply col 0 col 1 col 2 col 3 col 4 col 5 col 6 -1 100 100 100 50 2-ply

Example 1-ply and 2-ply lookahead scores |X| |X|O| | |O| |X|O|O|X| |X|X| |X|O|O|O| |O|X| --------------- 0 1 2 3 4 5 6 It is X’s move. What scores does a 2-ply lookahead for X assign to each move? col 0 col 1 col 2 col 3 col 4 col 5 col 6

Example 1-ply and 2-ply lookahead scores |X| |X|O| | |O| |X|O|O|X| |X|X| |X|O|O|O| |O|X| --------------- 0 1 2 3 4 5 6 It is X’s move. What scores does a 2-ply lookahead for X assign to each move? col 0 col 1 col 2 col 3 col 4 col 5 col 6 100 50 -1

Practice b ‘X’ ‘O’ 0-ply scores for O: 1-ply scores for O: col 0 col 1 col 2 col 3 col 4 col 5 col 6 0-ply scores for O: col 0 col 1 col 2 col 3 col 4 col 5 col 6 1-ply scores for O: col 0 col 1 col 2 col 3 col 4 col 5 col 6 2-ply scores for O: col 0 col 1 col 2 col 3 col 4 col 5 col 6 3-ply scores for O:

Solutions b ‘X’ ‘O’ col 0 col 1 col 2 col 3 col 4 col 5 col 6 0-ply scores for O: -1 50 50 50 50 50 50 col 0 col 1 col 2 col 3 col 4 col 5 col 6 1-ply scores for O: -1 50 50 100 50 50 50 col 0 col 1 col 2 col 3 col 4 col 5 col 6 2-ply scores for O: -1 100 50 col 0 col 1 col 2 col 3 col 4 col 5 col 6 3-ply scores for O: -1 100 100