Linux 五子棋1.0(無AI)

來源:互聯網
上載者:User
/*   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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.