1 / 66

四時讀書樂 ( 冬 )

四時讀書樂 ( 冬 ). 木落水盡千崖枯,迥然吾亦見真吾。 坐對韋編燈動壁,高歌夜半雪壓廬。 地爐茶鼎烹活火,四壁圖書中有我。 讀書之樂何處尋?數點梅花天地心。 ~ 元 ‧ 翁森. NCURSES Library. - Functions for Screen Handling. rand(). #include <iostream> using std::cout; using std::endl; int main() { for (int i=0; i<9; i++) cout << rand() % 6 + 1 << endl;

robin-sweet
Download Presentation

四時讀書樂 ( 冬 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 四時讀書樂 (冬) 木落水盡千崖枯,迥然吾亦見真吾。 坐對韋編燈動壁,高歌夜半雪壓廬。 地爐茶鼎烹活火,四壁圖書中有我。 讀書之樂何處尋?數點梅花天地心。 ~ 元‧翁森

  2. NCURSES Library - Functions for Screen Handling

  3. rand() #include <iostream> using std::cout; using std::endl; int main() { for (int i=0; i<9; i++) cout << rand() % 6 + 1 << endl; return 0; }

  4. Sleep( ) #include <iostream> #include <Windows.h> #include <Winbase.h> using std::cout; using std::endl; int main() { for (int i=0; i<9; i++) { cout << rand() % 6 + 1 << endl; Sleep(2000);// 2000 ms = 2s } return 0; }

  5. Screen Handling

  6. pdcurses_1.cpp #include <iostream> #include <Windows.h> #include <Winbase.h> #include <curses.h> int main() { int i, j; int a[6] = { 0 }; initscr(); for (i=0; i<6; i++) printw("%d ( )\n", i); for (i=0; i<9; i++) { j = rand() % 6; move(10, i*2); printw("%2d", j); move(j, 3); printw("%2d", ++a[j]); refresh(); Sleep(2000); // 2000 ms = 2s } endwin(); return 0; }

  7. Definition of curses on Wikipedia • curses is a terminal control library for Unix-like systems, enabling the construction of text user interface (TUI) applications. • The name is a pun on the term “cursor optimization”. It is a library of functions that manage an application's display on character-cell terminals (e.g., VT100)

  8. Basic Functions • move(y,x) • Move cursor to (y,x) in screen • addch(ch) • Add a character to screen • addstr(str) • Add a string to screen by calling addch() • printw(fmt, arg1, arg2, …) • Formatted print to screen by calling addstr() • refresh() • Update screen

  9. Initialize and Terminate Curses • initscr() • Initialize curses • endwin() • End curses. • This function should be called when your program is finished. • It will release the space allocated to screen handling in your program. • Remember to #include <curses.h> at the beginning of your program.

  10. Use Curses Library in Visual C++ 2010 • Download Public Domain Curses Library • Uncompress it and save the 4 files under a local directory, say L:\PDCurses. • In Visual C++ 2010 Express, • Project – Property (or Alt-F7) • Under Configuration Properties • Debugging – Environment • PATH=L:\PDCurses • C/C++ - Additional Include Directories • L:\PDCurses • Expand Linker, choose Input. In Additional Dependencies, insert “L:\PDCurses\pdcurses.lib;”

  11. Exercise • Modify pdcurses_1.cpp so that in addition to the counting number, in the same row you also show a bar (e.g. "******") to show the same number of stars.

  12. Homework: 8 Queens • Imagine there are 8 queens attending a race. On your computer screen each queen is represented by a character ‘Q’. On each row there is one queen. • If you think a single character is monotonous, you may modify this program to handle 8 horses ~/-\^ • Use a random number generator to determine which queen will move forward. • You may use the Sleep() function to slow down the program. • Suppose the length of each lane is 50. The first queen who arrives at the destination wins the race.

  13. 四時讀書樂 (春) 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生惟有讀書好。 讀書之樂樂何如,綠滿窗前草不除。 ~ 元‧翁森

  14. 四時讀書樂 (夏) 新竹壓檐桑四圍,小齋幽敞明朱暉。 晝長吟罷蟬鳴樹,夜深燼落螢入帷。 北窗高臥羲皇侶,只因素稔讀書趣。 讀書之樂樂無窮,瑤琴一曲來薰風。 ~ 元‧翁森

  15. 四時讀書樂 (秋) 昨夜庭前葉有聲,籬豆花開蟋蟀鳴。 不覺商意滿林薄,蕭然萬籟涵虛清。 近床賴有短檠在,趁此讀書功更倍。 讀書之樂樂陶陶,起弄明月霜天高。 ~ 元‧翁森

  16. 四時讀書樂 (冬) 木落水盡千崖枯,迥然吾亦見真吾。 坐對韋編燈動壁,高歌夜半雪壓廬。 地爐茶鼎烹活火,四壁圖書中有我。 讀書之樂何處尋?數點梅花天地心。 ~ 元‧翁森

  17. Getting Characters from the Terminal • getch() • Get a character from the terminal • getstr(str) • Get a string from the terminal • scanw(fmt, arg1, arg2, …) • Formatted input from the terminal like scanf().

  18. pdcurses_3.cpp #include <curses.h> int main() { char text[10]; int i, j, c; initscr(); getstr(text);// input the string "1,2" addstr(text); addch('\n'); scanw("%d,%d", &i, &j);// input the string "1,2" again printw("%d\t%d\n", i, j); c = getch(); endwin(); return 0; }

  19. noecho() #include <curses.h> int main() { int c; initscr(); // noecho(); do { c = getch(); printw(" %d\n", c); } while (c != '0'); endwin(); return 0; }

  20. pdcurses_4.cpp // pdcurses_4.cpp #include <curses.h> int main() { int y=10, x=10; char c; initscr(); noecho(); do { move(y, x); addch('Q'); c = getch(); move(y, x); addch(' '); switch (c) { case 'h': x--; break; case 'l': x++; break; case 'j': y++; break; case 'k': y--; break; } } while (c != 'q'); endwin(); return 0; }

  21. curs_set() • curs_set() alters the appearance of the text cursor. • int curs_set(int visibility); • A value of 0 for visibility makes the cursor disappear; • a value of 1 makes the cursor appear "normal" (usually an underline) • 2 makes the cursor "highly visible" (usually a block).

  22. pdcurses_4a.cpp // pdcurses_4.cpp #include <curses.h> int main() { int y=10, x=10; char c; initscr(); noecho(); curs_set(0);// no cursor do { move(y, x); addch('Q'); c = getch(); move(y, x); addch(' '); switch (c) { case 'h': x--; break; case 'l': x++; break; case 'j': y++; break; case 'k': y--; break; } } while (c != 'q'); endwin(); return 0; }

  23. Function Keys • Call keypad() to enable the handling of Function keys and arrow keys. • int keypad(WINDOW *win, bool bf); • keypad(stdscr, TRUE); • getch() returns an integer corresponding to the key pressed. • If it is a normal character, the integer value will be equivalent to the ASCII code of the character. • Otherwise it returns a number which can be matched with the constants defined in curses.h. • For example if the user presses F1, the integer returned is 265.

  24. Function Keys (cont.) • With keypad() enabled, you can check the returned value of getch() with the constants defined in curses.h • KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT • KEY_HOME, KEY_END, • KEY_F(n)

  25. Key Definitions • #define KEY_IC 0x14b /* insert char or enter ins mode (Insert) */ • #define KEY_DC 0x14a /* delete character (Delete) */ • #define KEY_HOME 0x106 /* home key */ • #define KEY_END 0x166 /* end key */ • #define KEY_PPAGE 0x153 /* previous page (PageUp) */ • #define KEY_NPAGE 0x152 /* next page (PageDown) */ • #define PADENTER 0x1cb /* enter on keypad */ You may check curses.h to see more definitions.

  26. Colors • To start using color, you should first call the function start_color(). • To find out whether a terminal has color capabilities or not, you can use has_colors() function, which returns FALSE if the terminal does not support color. • Colors are always used in pairs. • A color-pair consists of a foreground color and a background color. • Initializes a color-pair with the routine init_pair(). After it has been initialized, COLOR_PAIR(n) is used to represent the color attribute.

  27. pdcurses_2.cpp #include <curses.h> int main() { initscr(); start_color(); init_pair( 1, COLOR_WHITE, COLOR_RED ); attron( COLOR_PAIR(1) ); printw("Background red"); attroff( COLOR_PAIR(1) ); refresh(); getch(); endwin(); return 0; }

  28. Pre-defined Colors on Unix • COLOR_BLACK = 0 • COLOR_RED = 1 • COLOR_GREEN = 2 • COLOR_YELLOW = 3 • COLOR_BLUE = 4 • COLOR_MAGENTA = 5 • COLOR_CYAN = 6 • COLOR_WHITE = 7

  29. Exercise: Arrow Keys • Modify pdcurses_4a.cpp so that users can use arrow keys and HJKL to control the movement of ‘Q’. • Moreover, try to allow users to use both uppercase ‘H’ and lowercase ‘h’ to do the same movement. • Users can also change the color of ‘Q’ by pressing ‘0’…’7’.

  30. HW: Tetris

  31. int timeout(int delay) • The timeout() function affects the behaviour of getch() when reading a character from stdscr. • If delay is a positive number, then getch() will wait for that many milliseconds before returning • If no character was available, then ERR (-1) will be returned.

  32. pdcurses_6.cpp #include <curses.h> int main() { int x=10, old_x; int c; initscr(); noecho(); curs_set(0);// hide the cursor timeout(500); // getch() will only wait 0.5 second for (int y=1; y<15; y++) { move(y, x); addch('Q'); old_x = x; c = getch(); switch(c) { case 'h': x--; break; case 'l': x++; break; } move(y, old_x); addch(' '); } refresh(); endwin(); return 0; }

  33. HW: Tetris RotationReplayFalling Down

  34. Rotate with respect to a fixed point (x+1,y-1) (x+2,y-1) (x,y) (x+1,y) x y

  35. Relative Coordinates (1,-1) (2,-1) (0,0) (1,0) x y

  36. Rotate 90 Degrees (-1,-2) (-1,-1) (0,-1) (0,0) x y

  37. Rotate 180 Degrees (-1,0) (0,0) (-2,1) (-1,1) x y

  38. Rotate 270 Degrees (0,0) (0,1) (1,1) x (1,2) y

  39. Observe the x and y (y, -x) (-x, -y) (0,0) x (x, y) y (-y, x)

  40. switch(rotate) • case 0: • x = x0 + x; • y = y0 + y; • break; • case 2: • x = x0 - x; • y = y0 - y; • break; • case 1: • x = x0 + y; • y = y0 - x; • break; • case 3: • x = x0 - y; • y = y0 + x; • break;

  41. Seven Shapes

  42. Represent these objects withstruct struct Shape { Cell c[4]; }; struct Cell { int dx; int dy; }

  43. Choose a reference point (0,-1) (1,-1) (-1,0) (0,0)

  44. Shape s[7]; void init_shape() { int i, j, x, y; FILE* fp; fp = fopen("X:\\WWW\\Course\\CS101\\shape_xy.txt", "r"); for (i=0; i<7; i++) { for (j=0; j<4; j++) { fscanf(fp, "%d %d", &y, &x); s[i].c[j].dx = x; s[i].c[j].dy = y; } } fclose(fp); // Initialize the bottom line for (i=0; i<12; i++) occupy[11][i] = true; } 0 0 0 1 -1 0 -1 1 0 0 1 0 2 0 -1 0 0 0 0 1 -1 0 -1 -1 0 0 0 1 0 -1 -1 0 0 0 0 1 0 2 -1 0 0 0 0 -1 -1 0 -1 1 0 0 0 -1 0 -2 -1 0

  45. plot_shape(shape_id, y0, x0, rotate, character) for (j=0; j<4; j++) { x = x0 + s[id].c[j].dx; y = y0 + s[id].c[j].dy; move(y, x); addch(character); }

  46. Consider Rotations

  47. test_rotate() • KEY_LEFT • KEY_RIGHT • KEY_UP • tetris-1.cpp

  48. history.txt • shape, y, x, r 1 1 1 0 1 2 1 0 1 3 2 0 1 4 3 0 1 5 3 1 1 10 3 1 6 1 2 0 6 2 2 0 6 3 3 0 6 4 4 0 6 5 5 1 6 6 6 2 6 7 7 3 6 8 8 0 6 10 8 0 y=1 implies a new object

  49. replay() fopen() while ( fscanf( ) != EOF ) { if not a new object erase the old position plot at the new position } fclose()

  50. Falling down • Q: When should an object stop falling down? • A: When there get supported by some cells beneath it.

More Related