/* This file is WuZiGame source codehttp://www.baidu.com application launcher, compatible with GNU/Linux and most other POSIX systems Copyright (C) 2013-2014 Sam Brown This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Sam Brown E-mail: zzulion@gamil.com*/#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <ncurses.h>int board[25][25];typedef struct stack{int y, x;}stack;stack S[9999];int top;int num = 0, flag;/* the welcome interface */inline void welcome( void );/* initialize the game's map */void init_map( void );/* draw the piece on your cursor's postion */void draw_piece( int y, int x );/* move your cursor */void move_step( int y, int x );/* judge the game's result */int win( int y, int x, int cal );void regret( int *y, int *x );/*version 1.0the main function:1. control the cursor2. draw the piece3. if you want to debug press 't' and you can modify the test modules4. replay5. regret the piece you draw6. quit*/int main( ) {char black[20] = "black win!!!";/* print this strings when the balck win */char white[20] = "white win!!!";/* print this strings when the white win */int key;/* your keyboard's input */int y, x;welcome();again:init_map();x = 14; y = 7;/* the middle of the map */move( y, x );refresh();/* print the cursor on the middle of the map */noecho();/* cancel the echo, prevent echo the key on the map when you move the cursor */while( 1 ){cbreak();keypad(stdscr, TRUE );/* init the keyboard model */key = getch();switch( key ){case KEY_LEFT:/* move left */x -= 2;if( x >= 0 && x <= 28 && y >= 0 && y <= 14 )move_step( y, x );elsex += 2;break;case KEY_RIGHT:/* move right */x += 2;if( x >= 0 && x <= 28 && y >= 0 && y <= 14 )move_step( y, x );elsex -= 2;break;case KEY_UP:/* move up */--y;if( x >= 0 && x <= 28 && y >= 0 && y <= 14 )move_step( y, x );elsey++;break;case KEY_DOWN:/* move down */++y;if( x >= 0 && x <= 28 && y >= 0 && y <= 14 )move_step( y, x );elsey--;break;case 32:/* draw the piece by press space */if( !(x%2) && board[y][x/2] == 0 ){flag = 0;draw_piece( y, x );flag = win( y, x/2, 0 );}if( flag == 1 || flag == 2 )/* when the white or black win goto end */goto end;break;case 'q':/* press q to quit */endwin();exit( 0 );break;case 'n':/* new game, return to begin */endwin();goto again;case 'r':/* regret the piece you put */regret( &y, &x );break;case 't':goto test;}}test:endwin();printf( "%d %d", y, x );for( x = 0; x < 15; x++ ){for( y = 0; y < 15; y++ )printf( "%d", board[x][y] );printf( "\n" );}end:if( flag == 1 ){endwin();printf( "\n\n\nblack win!!!\n\n" );}if( flag == 2 ){endwin();printf( "\n\n\nwhite win!!!\n\n" );}return 0;}/* judge if there have some piece's which is put in order upto five by row line, col line, oblique line * the cal+1 upto five means one of the player is win * when the end of one line's scan the cal must equal to 0 * */ void regret( int * y, int * x ){if( top == 0 )return;else{*y = S[top-1].y;*x = S[top-1].x;move( *y, *x );board[*y][(*x)/2] = 0;printw( "." );move( *y, *x );}top--;}/* move the cursor */void move_step( int y, int x ){move( y, x );refresh();}/* print the piece on the map*/void draw_piece( int y, int x ){noecho();char ch1 = 'X', ch2 = 'O';move( y , x );if( (num++)%2 ){S[top].y = y;S[top].x = x;top++;addch( ch1 );move( y, x );board[y][x/2] = 1;}else{S[top].y = y;S[top].x = x;top++;addch( ch2 );move( y, x );board[y][x/2] = 2;}}/* welcome interface */inline void welcome( void ){int top = 5, left = 15;initscr();/* if your terminal does not support color then exit.. */if( has_colors() == FALSE ){endwin();printf( "You terminal does not support color\n" );exit(1);}/* initialize the color */start_color();/* set the color */init_pair( 1, COLOR_RED, COLOR_BLACK );init_pair( 2, COLOR_YELLOW, COLOR_BLACK );/* turn on the color */attron( COLOR_PAIR(1) );mvaddstr(top + 0, left, " _ _ "); mvaddstr(top + 1, left, " | | | | "); mvaddstr(top + 2, left, " ___ _ _ __| | ___ | | ___ _ "); mvaddstr(top + 3, left, "/ __| | | |/ _` |/ _ \\| |/ / | | |"); mvaddstr(top + 4, left, "\\__ \\ |_| | (_| | (_) | <| |_| |"); mvaddstr(top + 5, left, "|___/\\__,_|\\__,_|\\___/|_|\\_\\\\__,_|");/* turn off the color */attroff( COLOR_PAIR(1) );attron( COLOR_PAIR(2) );mvprintw( top+7, 15,"This game is written by sambrown\n" );printw( "\t\t\tYou can hit brother-guang by any keys...." );attroff( COLOR_PAIR(2) );refresh();getchar();}/* initialize the map */void init_map( ){int x, y;int i, j;/* initialize the color */getmaxyx( stdscr, y, x );init_pair( 3, COLOR_YELLOW, COLOR_RED );attron( COLOR_PAIR(3) );for( i = 0; i <= x; i++ )mvaddstr( y-1, i, " " );mvaddstr( y-1, 1, "[N]ew Game [R]egret" );mvaddstr( y-1, x-20,"[Q]uit Game" );memset( board, 0, sizeof(board) );attroff( COLOR_PAIR(3) );flag = 0;top = 0;move( 0, 0 );for( i = 0; i < 15; i++ )addstr( ". . . . . . . . . . . . . . . \n");for( i = 0; i < 20; i++ )mvaddstr( i, x-30, "brother-guang is 2B!!!\n");refresh();}int win( int y, int x, int cal ){int i, j, k;int s = 20;// col linefor( i = y, j = 0; j < 4; j++ ){if( board[++i][x] == board[y][x] ){if( i >= 0 && i <= 14 )cal++;}elsebreak;}for( i = y, j = 0; j < 4; j++ ){if( board[--i][x] == board[y][x] ){if( i >= 0 && i <= 14 )cal++;}elsebreak;}if( cal+1 >= 5 )return board[y][x];cal = 0;//row linefor( i = x, j = 0; j < 4; j++ ){if( board[y][++i] == board[y][x] ){if( i >= 0 && i <= 14 )cal++;}elsebreak;}for( i = x, j = 0; j < 4; j++ ){if( board[y][--i] == board[y][x] ){if( i >= 0 && i <= 14 )cal++;}elsebreak;}if( cal+1 >= 5 )return board[y][x];cal = 0;// oblique line onefor( i = y, j = x, k = 0; k < 4; k++ ){if( board[++i][--j] == board[y][x] ){if( i >= 0 && i <= 14 && j >= 0 && j <= 14 )cal++;}elsebreak;}for( i = y, j = x, k = 0; k < 4; k++ ){if( board[--i][++j] == board[y][x] ){if( i >= 0 && i <= 14 && j >= 0 && j <= 14 )cal++;}elsebreak;}if( cal+1 >= 5 )return board[y][x];cal = 0;// oblique line twofor( i = y, j = x, k = 0; k < 4; k++ ){if( board[--i][--j] == board[y][x] ){if( i >= 0 && i <= 14 && j >= 0 && j <= 14 )cal++;}elsebreak;}for( i = y, j = x, k = 0; k < 4; k++ ){if( board[++i][++j] == board[y][x] ){if( i >= 0 && i <= 14 && j >= 0 && j <= 14 )cal++;}elsebreak;}if( cal+1 >= 5 )return board[y][x];cal = 0;return 0;}