1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules.

Slides:



Advertisements
Similar presentations
Computer Science & Engineering 2111 IF and Boolean Functions 1 CSE 2111 Lecture-IF and Boolean Functions.
Advertisements

CS12230 Introduction to Programming Tying things together.
Tic Tac Toe Game playing strategies
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
The N-Queens Problem lab01.
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
Taylor Cummins Andrea Conley Connect Four. History Published by Milton Bradley in Ages 7 and up. Also known as Captain’s Mistress, Four up, Plot.
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.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Mock test review Revision of Activity Diagrams for Loops,
1 CSE1301 Computer Programming Lecture 26: Case Study.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
IS660Z – Programming Games Using Visual Basic Required session 3 June 16, 2004.
1 CSE1301 Computer Programming: Lecture 25 Software Engineering 2.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
CS1020E Sitin 1 Discussion -- Counting Palindromes.
Announcements.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Recursion: Backtracking
Data structure design Jordi Cortadella Department of Computer Science.
Cosc175/testing1 Testing Software errors are costly GIGO Preventing Errors –Echo checking –Range and limit checking –Defensive programming.
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Sudoku Jordi Cortadella Department of Computer Science.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
How to Create A Pie Chart for Your Road Trip By: Tim Brockman.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Repetitive Structures
Lecture 5 of Computer Science II
Section 4.4 Counting Blobs
Intro to Computer Science II
Lecture 7: Repeating a Known Number of Times
CSCI 104 Backtracking Search
Data Structures and Algorithms
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
While Loops BIS1523 – Lecture 12.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Looping.
CPS125 Week
Tic Tac Toe application
CSE 143 Lecture 18 More Recursive Backtracking
Bingo Example: Design (Fill Card Algorithm)
© T Madas.
CSE 143 Lecture 18 More Recursive Backtracking
The Game of Estimation point 3 points 2 points
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Week 3 – Repetition (ctd.)
Backtracking, Search, Heuristics
Presentation transcript:

1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

2 module to mark/update board find called number on the board if ( number is on the board ) { mark the cell } if ( ( there is a row of marked cells ) or ( there is a column of marked cells ) or ( there is a diagonal of marked cells ) ) { set flag to indicate player is a winner } Algorithm : Update Player

3 Structure Chart : Update Player mark board check diagonals check rows check columns board called number, board true or false board true or false board true or false board player update player player

4 Structure Chart : Update Player mark board check diagonals check rows check columns board called number, board true or false board true or false board true or false board player update player player

5 /* * NAME: * void updateBoard (int number, BingoBoard *board) * * DESCRIPTION: * Marks the position of `number' on the board, if it is on * the board. * * PRE: * It assumes that `number' is within range 1 to MAX_VAL, * and that `board' points to a struct which has been * initialised appropriately so that every number appears * only at most once on the board. * * POST: * The instance pointed to by `board' is changed. * The cell which used to contain `number' is changed * to the special value MARKED_VAL. */ Module to mark cell: updateBoard() bingo-teddy/player.c

6 void updateBoard (int number, BingoBoard *board) { int row, col; /* Determine in which column the number would be. */ col = number / ( MAX_VAL / BOARD_DIM ); /* Look for the number in that column. */ for (row=0;row < BOARD_DIM; row++) { if (board->cell[row][col] == number) { board->cell[row][col] = MARKED_VAL; /* Since a number can appear only at most once on the board, * we can return to the calling function as soon as we have * found and marked the cell. */ return; }

7 #include #include "bingo.h" #include "board.c" #include "player.c" const int X = MARKED_VAL; int main() { /* We'll fill the board up just for testing. */ BingoBoard theBoard = {{ {1, 16, 31, 46, 61}, {2, 17, 32, 47, 62}, {3, 18, X, 48, 63}, {4, 19, 33, X, 64}, {5, 20, 34, 50, 65} }}; Test program for updateBoard() bingo-teddy/test-markcell.c

8 /* Case 1: The called number is on the board. */ updateBoard(3, &theBoard); printBoard(&theBoard); printf("\n"); updateBoard(31, &theBoard); printBoard(&theBoard); printf("\n"); /* Case 2: The called number is NOT on the board. */ updateBoard(6, &theBoard); printBoard(&theBoard); printf("\n"); updateBoard(30, &theBoard); printBoard(&theBoard); printf("\n"); return 0; }

9 Structure Chart : Update Player mark board check diagonals check rows check columns board called number, board true or false board true or false board true or false board player update player player

10 /* * NAME: * int checkBoardRows (BingoBoard *board) * * DESCRIPTION: * Check the board for a winning row; i.e. a row where * all the cells have been marked. * * PRE: * Assumes `board' contains only valid values, and that * a cell with value MARKED_VAL indicates that the cell * has been marked. `board' should be pointing to an * instance of a BingoBoard struct. * * POST: * Returns 1 if there is a winning row; 0 otherwise. * */ Module: checkBoardRows() bingo-teddy/player.c

11 int checkBoardRows (BingoBoard *board) { int row, col; int count; for (row=0; row < BOARD_DIM; row++) { /* Count the number of marked cells in current row. */ count = 0; for (col=0; col < BOARD_DIM; col++) { if (board->cell[row][col] == MARKED_VAL) { count++; } continued...

12 if (count == BOARD_DIM) { return 1; } return 0; } continuation...

13 #include #include "bingo.h" #include "board.c" #include "player.c" const int X = MARKED_VAL; int main() { /* We'll fill the board up just for testing. */ BingoBoard theBoard = {{ {1, 16, 31, 46, 61}, {2, 17, 32, 47, 62}, {3, 18, X, 48, 63}, {4, 19, 33, X, 64}, {5, 20, 34, 50, 65} }}; Test program for checkBoardRows() bingo-teddy/test-checkrow.c

14 /* Case 1: The board has no winning row. */ printBoard(&theBoard); printf("\n"); printf("Result is %d\n", checkBoardRows(&theBoard)); /* Case 2: The board has one winning row. */ updateBoard(3, &theBoard); updateBoard(18, &theBoard); updateBoard(48, &theBoard); updateBoard(63, &theBoard); printBoard(&theBoard); printf("\n"); printf("Result is %d\n", checkBoardRows(&theBoard)); return 0; }

15 Structure Chart : Update Player mark board check diagonals check rows check columns board called number, board true or false board true or false board true or false board player update player player checkBoardColums is similar to checkBoardRows.

16 Structure Chart : Update Player mark board check diagonals check rows check columns board called number, board true or false board true or false board true or false board player update player player I still have to give checkBoardDiagonals some thought, but updatePlayer needs to call this module. How can I continue?

17 /* * NAME: * int checkBoardDiagonals (BingoBoard *board) * * DESCRIPTION: * Check the board for a winning main diagonal; i.e. a * main diagonal (either upper-left to lower-right, or * lower-left to upper-right) where all the cells have * been marked. * * PRE: * Assumes `board' contains only valid values, and that * a cell with value MARKED_VAL indicates that the cell * has been marked. `board' should be pointing to * an instance of BingoBoard. * * POST: * Returns 1 if there is a winning diagonal; 0 otherwise. * */ Module: checkDiagonals() bingo-teddy/player.c

18 int checkBoardDiagonals (BingoBoard *board) { /* ** This is the dummy version of this function. ** ** Teddy hasn't figured this one out yet, so ** at the moment, this function always returns false. ** ** EXERCISE: Write the code for this function. */ return 0; }

19 Structure Chart : Update Player mark board check diagonals check rows check columns board called number, board true or false board true or false board true or false board player update player player Now I can work on updatePlayer even when checkDiagonal s is not yet finished.

20 /* * NAME: * void updatePlayer (int number, PlayerInfo *player) * * DESCRIPTION: * Given the number called by the Game Master, the player's * board is marked. It then checks if the board has a * winning row, column or diagonal. If so, a flag is set * to indicate that this player is a winner, and adds one * to the player's score. * * PRE: * `number' is assumed to be in the range 1 to MAX_VAL. * `player' should be a pointer to an instance of * PlayerInfo, which should have been initialised * using newPlayer(). * * POST: * Changes the record pointed to by `player' with the * player's updated board, win flag, and score. */ Module: updatePlayer() bingo-teddy/player.c

21 void updatePlayer (int number, PlayerInfo *player) { updateBoard(number, &(player->board)); if (checkBoardRows(&(player->board)) || checkBoardColumns(&(player->board)) || checkBoardDiagonals(&(player->board))) { player->isWinner = 1; }

22 #include #include "bingo.h" #include "board.c" #include "player.c” /* Teddy’s functions */ const int X = MARKED_VAL; int main() { Test program for updatePlayer() continued... bingo-teddy/test-player.c

23 /* We'll fill the player info up just for testing. */ PlayerInfo thePlayer = { /* board */ { {{1, 16, 31, 46, 61}, {2, 17, 32, 47, 62}, {3, 18, X, 48, 63}, {4, 19, 33, X, 64}, {5, 20, 34, 50, 65}} }, /* name */ "Teddy", /* isWinner */ 0 }; continued...

24 /* Case 1: The board has no winning row, column or diagonal. */ updatePlayer(48, &thePlayer); printBoard(&(thePlayer.board)); printf("\n"); printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner); /* Case 2: The board has one winning column, no winning row or diagonal. */ updateBoard(46, &(thePlayer.board)); updateBoard(47, &(thePlayer.board)); updatePlayer(50, &thePlayer); printBoard(&(thePlayer.board)); printf("\n"); printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner);

25 /* In preparation for testing the next case, change row 1 * column 4 back to 46 so we don't have a winning column, * and reset value of isWinner. */ thePlayer.board.cell[0][3] = 46; thePlayer.isWinner = 0; printBoard(&(thePlayer.board)); printf("\n"); printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner); /* Case 3: The board has one winning row, no winning column or diagonal. */ updateBoard(3, &(thePlayer.board)); updateBoard(18, &(thePlayer.board)); updatePlayer(63, &thePlayer); printBoard(&(thePlayer.board)); printf("\n"); printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner);

26 /* Case 4: Winning upper-left to lower-right diagonal. */ /*** EXERCISE ***/ /* Case 5: Winning lower-left to upper-right diagonal. */ /*** EXERCISE ***/ return 0; }