1. 显示贪吃蛇身子的一个节点(140.10)
# include <curses.h>
struct Snake
{
int line;
int list;
struct Snake * Nest;
} ;
struct Snake node1= { 2 , 2 , NULL } ;
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void gamePic ( )
{
int line;
int list;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( node1. line== line && node1. list== list) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
int main ( )
{
initNcurse ( ) ;
gamePic ( ) ;
getch ( ) ;
endwin ( ) ;
return 0 ;
}
2. 显示贪吃蛇完整身子(141.11)
# include <curses.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake node1= { 2 , 2 , NULL } ;
struct Snake node2= { 2 , 3 , NULL } ;
struct Snake node3= { 2 , 4 , NULL } ;
int hasSnakeNode ( int line, int list)
{
if ( node1. line== line && node1. list== list) {
return 1 ;
}
return 0 ;
}
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void gamePic ( )
{
int line;
int list;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
int main ( )
{
initNcurse ( ) ;
node1. next= & node2;
node2. next= & node3;
gamePic ( ) ;
getch ( ) ;
endwin ( ) ;
return 0 ;
}
# include <curses.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake node1= { 2 , 2 , NULL } ;
struct Snake node2= { 2 , 3 , NULL } ;
struct Snake node3= { 2 , 4 , NULL } ;
struct Snake node4= { 2 , 5 , NULL } ;
int hasSnakeNode ( int line, int list)
{
struct Snake * p= & node1;
while ( p != NULL ) {
if ( p-> line== line && p-> list== list) {
return 1 ;
}
p= p-> next;
}
return 0 ;
}
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void gamePic ( )
{
int line;
int list;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
int main ( )
{
initNcurse ( ) ;
node1. next= & node2;
node2. next= & node3;
node3. next= & node4;
gamePic ( ) ;
getch ( ) ;
endwin ( ) ;
return 0 ;
}
3. 显示贪吃蛇完整身子改进(142.12)
链表动态添加蛇的节点
# include <curses.h>
# include <stdlib.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake * head;
struct Snake * tail;
int hasSnakeNode ( int line, int list)
{
struct Snake * p= head;
while ( p != NULL ) {
if ( p-> line== line && p-> list== list) {
return 1 ;
}
p= p-> next;
}
return 0 ;
}
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void gamePic ( )
{
int line;
int list;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
void addNode ( )
{
struct Snake * new= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
new-> line= tail-> line;
new-> list= tail-> list+ 1 ;
new-> next= NULL ;
tail-> next= new;
tail= new;
}
void initSnake ( )
{
head= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
tail= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
head-> line= 2 ;
head-> list= 2 ;
head-> next= NULL ;
tail= head;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
}
int main ( )
{
initNcurse ( ) ;
initSnake ( ) ;
gamePic ( ) ;
getch ( ) ;
endwin ( ) ;
return 0 ;
}
4. 贪吃蛇左右移动
# include <curses.h>
# include <stdlib.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake * head;
struct Snake * tail;
int hasSnakeNode ( int line, int list)
{
struct Snake * p= head;
while ( p != NULL ) {
if ( p-> line== line && p-> list== list) {
return 1 ;
}
p= p-> next;
}
return 0 ;
}
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void gamePic ( )
{
int line;
int list;
move ( 0 , 0 ) ;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
void addNode ( )
{
struct Snake * new= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
new-> line= tail-> line;
new-> list= tail-> list+ 1 ;
new-> next= NULL ;
tail-> next= new;
tail= new;
}
void initSnake ( )
{
head= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
tail= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
head-> line= 2 ;
head-> list= 2 ;
head-> next= NULL ;
tail= head;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
}
void deleNode ( )
{
struct Snake * p= head;
head= head-> next;
free ( p) ;
}
void moveSnake ( )
{
addNode ( ) ;
deleNode ( ) ;
}
int main ( )
{
initNcurse ( ) ;
initSnake ( ) ;
gamePic ( ) ;
while ( 1 ) {
int con= getch ( ) ;
if ( con== KEY_RIGHT) {
moveSnake ( ) ;
gamePic ( ) ;
}
}
getch ( ) ;
endwin ( ) ;
return 0 ;
}
5. 贪吃蛇不想活了撞墙找死(144.14)
# include <curses.h>
# include <stdlib.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake * head= NULL ;
struct Snake * tail= NULL ;
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void addNode ( )
{
struct Snake * new= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
new-> line= tail-> line;
new-> list= tail-> list+ 1 ;
new-> next= NULL ;
tail-> next= new;
tail= new;
}
void initSnake ( )
{
struct Snake * p;
while ( head != NULL ) {
p= head;
head= head-> next;
free ( p) ;
}
head= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
tail= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
head-> line= 1 ;
head-> list= 1 ;
head-> next= NULL ;
tail= head;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
}
int hasSnakeNode ( int line, int list)
{
struct Snake * p= head;
while ( p != NULL ) {
if ( p-> line== line && p-> list== list) {
return 1 ;
}
p= p-> next;
}
return 0 ;
}
void gamePic ( )
{
int line;
int list;
move ( 0 , 0 ) ;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
void deleNode ( )
{
struct Snake * p= head;
head= head-> next;
free ( p) ;
}
void moveSnake ( )
{
addNode ( ) ;
deleNode ( ) ;
if ( tail-> line== 0 || tail-> line== 20 || tail-> list== 0 || tail-> list== 20 ) {
initSnake ( ) ;
}
}
int main ( )
{
initNcurse ( ) ;
initSnake ( ) ;
gamePic ( ) ;
while ( 1 ) {
int con= getch ( ) ;
if ( con== KEY_RIGHT) {
moveSnake ( ) ;
gamePic ( ) ;
}
}
getch ( ) ;
endwin ( ) ;
return 0 ;
}
6. 贪吃蛇脱缰向右自行游走(145.15)
# include <curses.h>
# include <stdlib.h>
# include <unistd.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake * head= NULL ;
struct Snake * tail= NULL ;
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void addNode ( )
{
struct Snake * new= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
new-> line= tail-> line;
new-> list= tail-> list+ 1 ;
new-> next= NULL ;
tail-> next= new;
tail= new;
}
void initSnake ( )
{
struct Snake * p;
while ( head != NULL ) {
p= head;
head= head-> next;
free ( p) ;
}
head= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
tail= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
head-> line= 1 ;
head-> list= 1 ;
head-> next= NULL ;
tail= head;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
}
int hasSnakeNode ( int line, int list)
{
struct Snake * p= head;
while ( p != NULL ) {
if ( p-> line== line && p-> list== list) {
return 1 ;
}
p= p-> next;
}
return 0 ;
}
void gamePic ( )
{
int line;
int list;
move ( 0 , 0 ) ;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
void deleNode ( )
{
struct Snake * p= head;
head= head-> next;
free ( p) ;
}
void moveSnake ( )
{
addNode ( ) ;
deleNode ( ) ;
if ( tail-> line== 0 || tail-> line== 20 || tail-> list== 0 || tail-> list== 20 ) {
initSnake ( ) ;
}
}
int main ( )
{
initNcurse ( ) ;
initSnake ( ) ;
gamePic ( ) ;
while ( 1 ) {
moveSnake ( ) ;
gamePic ( ) ;
refresh ( ) ;
usleep ( 100000 ) ;
}
getch ( ) ;
endwin ( ) ;
return 0 ;
}
7.贪吃蛇方向移动和刷新界面一起实现面临的问题(146.16)
# include <curses.h>
# include <stdlib.h>
# include <unistd.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake * head= NULL ;
struct Snake * tail= NULL ;
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void addNode ( )
{
struct Snake * new= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
new-> line= tail-> line;
new-> list= tail-> list+ 1 ;
new-> next= NULL ;
tail-> next= new;
tail= new;
}
void initSnake ( )
{
struct Snake * p;
while ( head != NULL ) {
p= head;
head= head-> next;
free ( p) ;
}
head= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
tail= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
head-> line= 1 ;
head-> list= 1 ;
head-> next= NULL ;
tail= head;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
}
int hasSnakeNode ( int line, int list)
{
struct Snake * p= head;
while ( p != NULL ) {
if ( p-> line== line && p-> list== list) {
return 1 ;
}
p= p-> next;
}
return 0 ;
}
void gamePic ( )
{
int line;
int list;
move ( 0 , 0 ) ;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie\n" ) ;
}
}
}
void deleNode ( )
{
struct Snake * p= head;
head= head-> next;
free ( p) ;
}
void moveSnake ( )
{
addNode ( ) ;
deleNode ( ) ;
if ( tail-> line== 0 || tail-> line== 20 || tail-> list== 0 || tail-> list== 20 ) {
initSnake ( ) ;
}
}
int main ( )
{
initNcurse ( ) ;
initSnake ( ) ;
gamePic ( ) ;
while ( 1 ) {
moveSnake ( ) ;
gamePic ( ) ;
refresh ( ) ;
usleep ( 100000 ) ;
}
while ( 1 ) {
int key= getch ( ) ;
switch ( key) {
case KEY_DOWN:
printw ( "DOWN\n" ) ;
break ;
case KEY_UP:
printw ( "UP\n" ) ;
break ;
case KEY_LEFT:
printw ( "LEFT\n" ) ;
break ;
case KEY_RIGHT:
printw ( "RIGHT\n" ) ;
break ;
}
}
getch ( ) ;
endwin ( ) ;
return 0 ;
}
8. Linux线程概念引入及编程实现(147.17)
# include <stdio.h>
# include <pthread.h>
void * thread ( void * arg )
{
printf ( "This is a thread and arg = %d.\n" , * ( int * ) arg) ;
* ( int * ) arg = 0 ;
return arg;
}
int main ( int argc, char * argv[ ] )
{
pthread_t th;
int ret;
int arg = 10 ;
int * thread_ret = NULL ;
ret = pthread_create ( & th, NULL , thread, & arg ) ;
if ( ret != 0 ) {
printf ( "Create thread error!\n" ) ;
return - 1 ;
}
printf ( "This is the main process.\n" ) ;
pthread_join ( th, ( void * * ) & thread_ret ) ;
printf ( "thread_ret = %d.\n" , * thread_ret ) ;
return 0 ;
}
# include <stdio.h>
# include <unistd.h>
# include <pthread.h>
void * func1 ( )
{
while ( 1 ) {
printf ( "this is func 1\n" ) ;
sleep ( 1 ) ;
}
}
void * func2 ( )
{
while ( 1 ) {
printf ( "this is func 2\n" ) ;
sleep ( 1 ) ;
}
}
int main ( )
{
pthread_t th1;
pthread_t th2;
pthread_create ( & th1, NULL , func1, NULL ) ;
pthread_create ( & th2, NULL , func2, NULL ) ;
while ( 1 ) ;
return 0 ;
}
9. 使用线程解决16节中提到问题(148.18)
# include <curses.h>
# include <stdlib.h>
# include <unistd.h>
# include <pthread.h>
struct Snake
{
int line;
int list;
struct Snake * next;
} ;
struct Snake * head= NULL ;
struct Snake * tail= NULL ;
int key;
void initNcurse ( )
{
initscr ( ) ;
keypad ( stdscr, 1 ) ;
}
void addNode ( )
{
struct Snake * new= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
new-> line= tail-> line;
new-> list= tail-> list+ 1 ;
new-> next= NULL ;
tail-> next= new;
tail= new;
}
void initSnake ( )
{
struct Snake * p;
while ( head != NULL ) {
p= head;
head= head-> next;
free ( p) ;
}
head= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
tail= ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
head-> line= 1 ;
head-> list= 1 ;
head-> next= NULL ;
tail= head;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
addNode ( ) ;
}
int hasSnakeNode ( int line, int list)
{
struct Snake * p= head;
while ( p != NULL ) {
if ( p-> line== line && p-> list== list) {
return 1 ;
}
p= p-> next;
}
return 0 ;
}
void gamePic ( )
{
int line;
int list;
move ( 0 , 0 ) ;
for ( line= 0 ; line< 20 ; line++ ) {
if ( line== 0 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( line>= 0 && line<= 19 ) {
for ( list= 0 ; list<= 20 ; list++ ) {
if ( list== 0 || list== 20 ) {
printw ( "|" ) ;
} else if ( hasSnakeNode ( line, list) ) {
printw ( "[]" ) ;
}
else {
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( line== 19 ) {
for ( list= 0 ; list< 20 ; list++ ) {
printw ( "--" ) ;
}
printw ( "\nBy Jessie,key=%d\n" , key) ;
}
}
}
void deleNode ( )
{
struct Snake * p= head;
head= head-> next;
free ( p) ;
}
void moveSnake ( )
{
addNode ( ) ;
deleNode ( ) ;
if ( tail-> line== 0 || tail-> line== 20 || tail-> list== 0 || tail-> list== 20 ) {
initSnake ( ) ;
}
}
void * refreshInt ( )
{
while ( 1 ) {
moveSnake ( ) ;
gamePic ( ) ;
refresh ( ) ;
usleep ( 100000 ) ;
}
}
void * changeDir ( )
{
while ( 1 ) {
key= getch ( ) ;
switch ( key) {
case KEY_DOWN:
printw ( "DOWN\n" ) ;
break ;
case KEY_UP:
printw ( "UP\n" ) ;
break ;
case KEY_LEFT:
printw ( "LEFT\n" ) ;
break ;
case KEY_RIGHT:
printw ( "RIGHT\n" ) ;
break ;
}
}
}
int main ( )
{
pthread_t t1;
pthread_t t2;
initNcurse ( ) ;
initSnake ( ) ;
gamePic ( ) ;
pthread_create ( & t1, NULL , refreshInt, NULL ) ;
pthread_create ( & t2, NULL , changeDir, NULL ) ;
while ( 1 ) ;
getch ( ) ;
endwin ( ) ;
return 0 ;
}