Download presentation
Presentation is loading. Please wait.
Published byNicholas Holmes Modified over 9 years ago
1
W 1 L 2 sh 1 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2 lesson 2Lists, specification Week 3 lesson 1Memory, testing Week 3 lesson 2Fagan inspection
2
W 1 L 2 sh 2 How to make code readable n Names n Names must help the reader to understand the code. n Format (layout) n The layout must match the statement structure. n The layout must be consistent. n Comments n Comments must explain what the reader needs to know but can not easily read from the code. n Structure n The code must be structured to be understandable. n Known concepts n Use concepts and techniques that are familiar (datastructures, patterns, real-world analogues).
3
W 1 L 2 sh 3 C statements n Declaration, definition n Expression n Compound : {... } n Selection : if/else, switch/case/default/break n Iteration : for, while, do/while, (break, continue) n (labeled statement) – verboten!
4
W 1 L 2 sh 4 Indentation n Purpose : make reading easy (screen or print?). n Consistency is probably more important than any particular style. n Save trees? // Pascal - ANSI if( a > b ) { max = a; } else { max = b; } // Whitesmith if( a > b ) { max = a; n++; } else { max = b; } // K&R - 1TBS if( a > b ){ max = a; } else { max = b; } if( a > b ){ max = a; } else { max = b; } // Pico (compact) if( a > b ){ max = a; } else { max = b; } http://en.wikipedia.org/wiki/Indent_style // Horstman if( a > b ) { max = a; } else { max = b; }
5
W 1 L 2 sh 5 Selection - if if( a > b ){ max = a; } if( a > b ){ max = a; } else { max = b; } if( a > b ){ max = a; } else if ( b > a ){ max = b; } else { max = a; equals++; } n If, if else, if … else n Always use { } n Write complex conditions on multiple lines if( ( x > 0 ) && ( x < size_x()) && ( y > 0 ) && ( y < size_y()) ){ VRAM_A[ x, y ] = color; }
6
W 1 L 2 sh 6 Selection - switch switch( c ){ case ’\n’ : position.x = 0; position.y= minimum( position.y + 1, 23 ); break; case ’\v’ : position.x = 0; position.y = 0; break; default : display[ position.x, position.y ] = c; position.x = minimum( position.x + 1, 79 ); break; } n Indent the cases as if there was a { } pair. n Each case ends with a break.
7
W 1 L 2 sh 7 Iteration – while, do... while int c; while( EOF != ( c = getchar()) ){ putchar( convert_to_uppercase( c )); } n Test before versus test afterwards. n What is wrong with this do.. while loop? int c; do { c = getchar(); putchar( convert_to_uppercase( c )); } while( c != EOF );
8
W 1 L 2 sh 8 Iteration – for int i, sum, values[ SIZE ];... sum = 0; for( i = 0; i < SIZE; i++ ){ sum += values[ i ]; } n A for loop is just an easy notation for a while loop i = 0; while( i < SIZE ){ sum += values[ i ]; i++; }
9
W 1 L 2 sh 9 Iteration – break n Break jumps to the statements after the loop (terminates the loop) n Use this: when a loop needs some ‘starting up’ before the condition can be checked. This is an idiom. while( 1 ) { c = getchar(); if( c == EOF ){ break; } putchar( convert_to_uppercase( c )); chars_printed++; }
10
W 1 L 2 sh 10 Iteration – continue n Continue jumps to the end of the statements in the loop (terminates this iteration) n Use this: seldom. Maybe when a very complex condition determines that the rest of the loop statements must be skipped. while( 1 ) { c = getchar(); if( c == EOF ){ break; } if( ! char_is_letter( c ){ continue; } putchar( convert_to_uppercase( c )); chars_printed++; }
11
W 1 L 2 sh 11 Condition n A condition is an integer expression. n 0 is interpreted as ‘not true’. n All other values are interpreted as ‘true’. n Functions that return a condition must adhere to this convention. Statements that check a condition must adhere to this convention.
12
W 1 L 2 sh 12 Condition woes if( strcmp( ”hello”, ”world” )){ printf( ”hello and world are equal” ); } if( TRUE == isupper( ’A’ )){ printf( ”” ); } Don’t write semi-predicate functions, or make the return type very very clear. There is no representation for true.
13
W 1 L 2 sh 13 Reversi (Othello) Black must place a piece with the black side up on the board, in such a position that there exists at least one straight (horizontal, vertical, or diagonal) occupied line between the new piece and another dark piece, with one or more contiguous white pieces between them. After placing the piece, black turns over (flips) all white pieces lying on a straight line between the new piece and any anchoring black pieces. http://en.wikipedia.org/wiki/Reversi http://www.freegames.ws/games/boardgames/othello/othello.htm
14
W 1 L 2 sh 14 Reversi (Othello) The opposing sides in turn place a piece on the board and chance the pieces on the board according to the rules. When one side can not make a move it must pass. When both sides must pass the black and white pieces are counted and the side having the most pieces wins. Tactics include Selecting good places first (borders, especially corners) Maximizing the number of moves I can make Minimizing the number moves my opponent can make Minimizing (!) my number of stones http://www.smartreversi.com/ http://www.shareup.com/Deep_Green_Reversi-download-25622.html
15
W 1 L 2 sh 15 Reversi (Othello) enum square { border = 0, empty = 1, white = 2, black = 3 }; typedef int board[ 100 ]; 0123456789 10111213141516171819 20212223242526272829 30313233343536373839 40414243444546474849 50515253545556575859 60616263646566676869 70717273747576777879 80818283848586878889 90919293949596979899 BBBBBBBBBB BB BB BB BwbB BbwB BB BB BB BBBBBBBBBB Starting position Square indexes in a board array
16
W 1 L 2 sh 16 Playing the game
17
W 1 L 2 sh 17 reversi.h // initialize board b to the starting position void reversi_board_init( board b ); // print the board b void reversi_board_print( board b ); // copy the board s to the board d void reversi_board_copy( board d, board s ); // convert a string "cn" to the index ( 0.. 99 ) of the square. // c must be a..h (or A..H), n must be 1..8. // return -1 when the string is not valid int reversi_move_from_string( char *s ); // return the number of squares that contain color int reversi_board_count_color( board b, int color );
18
W 1 L 2 sh 18 reversi.h // try to move a piece 'color' to the square move. // return the number of opponent squares that would be flipped. // return 0 if the move is not allowed (border, occupied) // this function changes the board! char reversi_flipped_board_color_move( board b, int color, int move ); // try to move a piece 'color' to the square move. // return the number of opponent squares that would be flipped. // consider only squares that lie in direction relative to the move. // return 0 if the move is not allowed (border, occupied) // this function changes the board! char reversi_flipped_board_color_move_direction( board b, int color, int move, int direction ); // return the number of valid moves available for color int reversi_board_n_moves( board b, int color );
19
W 1 L 2 sh 19 main.c – play game void play_game( board b, int color ){ int passes = 0; int count_black, count_white; while( passes < 2 ){ if( play_move( b, color ) ){ passes = 0; } else { passes++; } color = ( color == white ? black : white ); } count_white = reversi_flipped_board_count_color( b, white ); count_black = reversi_flipped_board_count_color( b, black ); printf( "game over!\n" ); printf( " black has %d pieces\n", count_black ); printf( " white has %d pieces\n", count_white ); if( count_white > count_black ){ printf( "white wins\n" ); } else if( count_black > count_white ){ printf( "black wins\n" ); } else { printf( "it is a draw wins\n" ); } int main(int argc, char *argv[]){ board b; reversi_board_init( b ); play_game( b, black ); }
20
W 1 L 2 sh 20 main.c – play move int play_move( board b, int color ){ char s[132 ]; int move, flipped, allowed; reversi_board_print( b ); for(;;){ allowed = reversi_board_n_moves( b, color ); printf( "%s to move (%d moves allowed): ", color == white ? "white (O)" : "black (X)", allowed ); if( allowed == 0 ){ printf( "\n so you will have to pass\n" ); return 0; } scanf( "%s", s ); move = reversi_move_from_string( s ); if( move == -1 ){ printf( "illegal format, please try again\n" ); continue; } flipped = reversi_flipped_board_color_move( b, color, move ); if( flipped == 0 ){ printf( "illegal move, please try again\n" ); continue; } return 1; }
21
W 1 L 2 sh 21 Read Book n Chapter 6 n Chapter 7
22
W 1 L 2 sh 22 Assignment Write the missing code in reversi.c Adhere to style guide! Verify that you can play a game of reversi.
23
W 1 L 2 sh 23 Assignment Function# Lines in my version Remarks reversi_board_init 18Is provided reversi_board_print 29Lots of lines but basically very simple. Good place to start. reversi_board_copy 6Trivial reversi_move_from_string 13Lots of checks required, but basically simple. Test this one by itself (print the return value) reversi_flipped_board_color_move_direction 29This is the difficult part. reversi_flipped_board_color_move 16Calls reversi_flipped_board_color_move_direction for each direction. Is provided. reversi_board_n_moves 11 Uses reversi_flipped_board_color_move. Trivial once you have that function. Do not forget to copy the board before each call! reversi_board_count_color 9Trivial
24
W 1 L 2 sh 24 Assignment – hints If the square is not empty, the move is illigal. Walk in the indicated direction Border or empty return 0 Enemy piece continue walking My own piece stop walking Now walk back Enemy piece flip it Back at the square return # flipped enemies // try to move a piece 'color' to the square move. // return the number of opponent squares that would be flipped. // consider only squares that lie in direction relative to the move. // return 0 if the move is not allowed (border, occupied) // this function changes the board! char reversi_flipped_board_color_move_direction( board b, int color, int move, int direction );
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.