(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 ' ';
}
下面这个有些长的程序为你示范了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 ' ';
}
相关文章推荐
- (28)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第三节
- (29)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第四节
- (31)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第六节
- (33)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第八节
- (26)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第一节
- (32)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第七节
- (27)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第二节
- (30)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第五节
- C++字符数组的输入输出和字符串结束标志使用讲解
- (16)'c++:COMPLETE REFERENCE' 第一部分 第二章(表达式) 第九节操作符(1)
- c++ primer plus 第五版 第七章 编程练习 第 8 题 (字符数组转成字符串,trim使用,判断空行)
- c++中字符串和字符数组
- (23)'c++:COMPLETE REFERENCE' 第一部分 第三章(语句) 第四节
- (37)'c++:COMPLETE REFERENCE' 第一部分 第五章(指针) 第三节
- (3)'c++:COMPLETE REFERENCE' 第一部分 第一章(c语言概述) 第二节
- (4)'c++:COMPLETE REFERENCE' 第一部分 第一章(c语言概述) 第三节
- C++基础之字符数组和字符串
- c++里面的字符数组和字符串及其分割
- 我的第四十一个C++上机报告(小心的放开玩字符串/字符数组第一季2)
- C++第16周项目1(1)统计字符串/字符数组