9-May-15 Notes on Style Testing the TicTacToe game.

Slides:



Advertisements
Similar presentations
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Advertisements

Internal Documentation Conventions. Kinds of comments javadoc (“doc”) comments describe the user interface: –What the classes, interfaces, fields and.
Two examples of Problem Solving in Programming H. Chad Lane University of Pittsburgh CS7: Introduction to Programming.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CSC 213 – Large Scale Programming. Why Do We Test?
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
General Programming Introduction to Computing Science and Programming I.
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.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Exceptions Handling Exceptionally Sticky Problems.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
EXAM 1 REVIEW. days until the AP Computer Science test.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
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.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
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.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input.
CSC 108H: Introduction to Computer Programming
Unit 2 Technology Systems
Comments on the “Three Piles” Problem
Introduction to Computer Science / Procedural – 67130
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Exceptions and files Taken from notes by Dr. Neil Moore
Recursion 12-Nov-18.
Lesson 2: Building Blocks of Programming
Exceptions and files Taken from notes by Dr. Neil Moore
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Recursion 29-Dec-18.
Exceptions 19-Feb-19.
An Example of Interacting Classes
Exceptions 7-Apr-19.
Recursion 23-Apr-19.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Unit 3: Variables in Java
Exceptions 10-May-19.
Exceptions 5-Jul-19.
Presentation transcript:

9-May-15 Notes on Style Testing the TicTacToe game

A first approach I want to create a large number of tic-tac-toe partial games for testing purposes I could do it this way: char[][] array; array = new char[][] { { 'X', ' ', 'O' }, { ' ', 'X', ' ' }, { 'O', ' ', 'O' } };...and I could reset the array for each tic-tac-toe board I want to use as input This looks nice, and is easy to read, but it is a real nuisance to type out a lot of these

So I did this instead setBoard(" o x o o"); private void setBoard(String xxx) { xxx = xxx.toUpperCase(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { array[i][j] = Character.toUpperCase(xxx.charAt(3 * i + j)); } } } Now it's a lot easier to create tic-tac-toe boards for testing

Morals Use methods to make your life easier If something is ugly, hide it in a method Also... While our main goal should be to write programs that are easy to read, it isn’t our only goal The best thing to do with hard-to-read methods is to rewrite them Second best is to explain them in comments I didn’t include the comments on the slide, but they are in my code!

Refactoring Refactoring is reorganizing a program without changing what it does Refactor in order to: Make a program easier to understand Make a program easier to modify

Before refactoring public final void testMakeCornerMove() { setBoard(" oxoxxoxo"); computerPlayer.makeMove(board); assertBoardIs("xoxoxxoxo"); setBoard("oo xxooxx"); computerPlayer.makeMove(board); assertBoardIs("ooxxxooxx"); setBoard("oxoxxoxo "); computerPlayer.makeMove(board); assertBoardIs("oxoxxoxox"); } I seem to be doing the same thing over and over...

After refactoring private void beforeAndAfterMove(String before, String after) { setBoard(before); computerPlayer.makeMove(board); assertBoardIs(after); } public final void testMakeCornerMove() { // Center and all other corners taken beforeAndAfterMove(" o x o o", "x o x o o"); beforeAndAfterMove("o x o o", "o x x o o"); beforeAndAfterMove("o o x o ", "o o x o x"); beforeAndAfterMove("o o x o", "o o x x o"); // Corner move is all that's left beforeAndAfterMove(" oxoxxoxo", "xoxoxxoxo"); beforeAndAfterMove("oo xxooxx", "ooxxxooxx"); beforeAndAfterMove("oxoxxoxo ", "oxoxxoxox"); beforeAndAfterMove("xxooxxxoo", "xxooxxxoo"); }

Moral The DRY principle: Don’t Repeat Yourself Every piece of data should have a single unique representation “A man with a watch knows what time it is. A man with two watches is never sure.” -- Segal’s Law Example: If you have a measure of distance, don’t keep it in two variables, distanceInFeet and distanceInMeters -- keep it in one variable, and use a method to convert to the other units as needed Each nontrivial operation should be represented by a unique piece of code Don’t “cut and paste” code--turn it into a method Variations in code can often be handled by a parameter list Corrections and updates are much simpler

Testing for a winning move Here’s one way to test for a winning move: if (board.get(1, 1) == 'X' && board.get(1, 2) == 'X' && board.get(1, 3) == ' ') { board.set(1, 3, 'X'); return true; } There are 24 combinations to test for This is why I made testing for a winning move optional! Using a method would help some if (winningMove(1, 1, 1, 2, 1, 3)) return true; But that’s still 24 error-prone lines, plus a method

A bright idea For each location on the tic-tac-toe board, Put an 'X' in that location Check for a win (with our computerHasWon() method) If it’s a win, that’s our move Otherwise, put a blank in that location, and keep trying We can do something very similar for testing if we need to make a blocking move

The code private boolean makeWinningMove(TicTacToeBoard board) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (!board.isEmpty(i, j)) continue; board.set(i, j, 'X'); if (board.computerHasWon()) return true; board.set(i, j, ' '); } } return false; } This code works, but...

An unexpected consequence Row 1, column 1 is already taken. Row 1, column 2 is already taken. Row 2, column 1 is already taken. Row 2, column 1 is already taken. Row 2, column 3 is already taken. Row 1, column 2 is already taken. Row 2, column 1 is already taken. Row 1, column 2 is already taken. Row 2, column 1 is already taken. Row 2, column 3 is already taken. Row 3, column 2 is already taken. Why did this happen? I did check for a blank space before placing my 'X'

In the set method of TicTacToeBoard if (board[row - 1][column - 1] != ' ') { error("Row " + row + ", column " + column + " is already taken."); } I can only “set” a location if it is initially blank I never thought about “erasing” an X or an O Proposed solution: Modify the set() method Problem: I asked you not to modify the provided methods Under these constraints, my “bright idea” cannot be made to work :-(

Morals Insofar as possible, methods should do a single thing In particular, it’s usually a bad idea to mix computation and input/output in the same method If you mix computation and input/output in the same method, then you can’t do the computation without also doing the input/output Example: In a previous assignment I specified methods findDayOfWeek to only do computation, and findAndPrintDayOfWeek to call the former and print the results This allowed me to test your computations without getting a bunch of output

Fix #1 for board.set(row, column, ch) I could have made set return a boolean -- true if the location was set, false if it wasn’t boolean set(int row, int column, char ch) { if (board[row - 1][column - 1] == ' ' && (ch == 'X' || ch == 'O')) { board[row – 1][column – 1] = ch; return true; } else return false; } Disadvantage: The user might not check the result Disadvantage: I test for two things that could go wrong (location is taken, bad character) and this doesn’t distinguish between them

Fix#2 for board.set(row, column, ch) I could assert that the location is available, and assert that the character is legal void set(int row, int column, char ch) { assert board[row - 1][column - 1] == ' '; assert ch == 'X' || 'O' ; board[row – 1][column – 1] = ch; } Disadvantage: Bad use of assert --it should be used for things you believe to be true, not for error checking

Fix#3 and #4 I could throw an Exception for each error condition This is the best solution We haven't covered Exceptions yet (I nearly forgot this one) I could just skip error checking Big disadvantage: No warning to the user if something is wrong

The End