您的位置:首页 > 编程语言 > C语言/C++

(34)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第九节

2007-03-23 12:35 501 查看
Tic-Tac-Toe程序示例
      下面这个有些长的程序为你示范了C/C++中的数组的各种操作方法.这个程序是一个Tic-Tac-Toe小游戏.游戏中用二维数组来模拟一个木板游戏方阵.计算机也来参与这个游戏,当轮到计算机走棋的时候,它使用get_computer_move()函数来扫描矩阵,寻找一个未占领的格子.然后它会在那里放置一个o.如果,它不能找到一个空位,它会报平局并且退出.get_player_move()函数会询问用户要在什么位置放置X.最左上角的位置为1,1;右下角则为3,3.
      矩阵数组的每个元素最初被初始化为空格字符.用户或者电脑每走一步,就会导致一个空格换成O或者X.这样以来在屏幕上显示矩阵就比较方便.
      每一次电脑或者用户走一步之后就会调用check()函数.这个函数会返回空格当还没有胜利者产生,产生O当电脑获胜,产生X当用户获胜.函数扫描行,列以及对角线.检查是否有某一个行列或者对角线上只含有O或者X.
disp_matrix()函数用来显示游戏当前的状况.请注意如何初始化矩阵会简化整个函数的编写.
      这个程序的各个子程序都会访问到这个矩阵数组,认真学习这些部分可以让你熟悉对于数组的各种操作.

/* A simple Tic Tac Toe game. */
#include <stdio.h>
#include <stdlib.h>

char matrix[3][3];  /* the tic tac toe matrix */

char check( void );
void init_matrix( void );
void get_player_move( void );
void get_computer_move( void );
void disp_matrix( void );

int main( void )
{
    char done;

    printf( "This is the game of Tic Tac Toe./n" );
    printf( "You will be playing against the computer./n" );

    done = ' ';
    init_matrix();
    do{
        disp_matrix();
        get_player_move();
        done = check();  /* see if winner */
        if( done != ' ' ) break;  /* winner */
        get_computer_move();
        done = check();  /* see if winner */
    }while( done == ' ' );
    if( done=='X' ) printf( "You won!/n" );
    else printf( "I won!!/n" );
    disp_matrix();  /* show final positions */

    return 0;
}

/* Initialize the matrix. */
void init_matrix( void )
{
    int i, j;

    for( i=0; i<3; i++ )
        for( j=0; j<3; j++ ) matrix[i][j] = ' ';
}

/* Get a player's move. */
void get_player_move( void )
{
    int x, y;

    printf( "Enter x,y coordinates for your move: " );
    scanf( "%d*c%d", &x, &y );

    x--;y--;

    if( matrix[x][y] != ' ' ){
        printf( "Invalid move, try again./n" );
        get_player_move();
    }
    else matrix[x][y] = 'X';
}

/* Get a move from the computer */
void get_computer_move( void )
{
    int i, j;
    for( i=0; i<3; i++){
        for( j=0; j<3; j++ )
            if( matrix[i][j]==' ' ) break;
        if( matrix[i][j]==' ' ) break;
    }

    if( i*j==9 ){
        printf( "draw/n" );
        exit(0);
    }
    else
        matrix[i][j] = 'O';
}

/* Display the matrix on the screen. */
void disp_matrix( void )
{
    int t;

    for( t=0; t<3; t++ ){
        printf( " %c | %c | %c ", matrix[t][0], matrix[t][1], matrix[t][2] );
        if( t!=2 ) printf( "/n---|---|---/n" );
    }
    printf( "/n" );
}

/* See if there is a winner. */
char check( void )
{
    int i;

    for( i=0; i<3; i++ )  /* check rows */
        if( matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2] ) return matrix[i][0];

    for( i=0; i<3; i++ )  /* check columns */
        if( matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[1][i] ) return matrix[0][i];

    /* check diagonals */
    if( matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2] ) return matrix[0][0];
    if( matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0] ) return matrix[0][2];

    return ' ';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  reference c++ matrix 游戏 c
相关文章推荐