头文件:
#define row 5
#define line 5
#include
#include
#include
void initial(char board[row][line]);
void print_board(char board[row][line]);
void player_move(char board[row][line]);
char is_win(char board[row][line]);
void computer_move(char board[row][line]);
源文件有二个:
第一个是:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void initial(char board[row][line])
{
int i = 0;
int j = 0;
for( i = 0 ; i < row ; i++)
{
for( j = 0 ; j < line ; j++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[row][line])
{
int i = 0;
int j = 0;
for( i = 0 ; i < row ; i++)
{
for( j = 0 ; j < line ; j++)
{
if( j != line - 1 )
printf(" %c |",board[i][j]);
else
printf(" %c ",board[i][j]);
}
printf("\n");
for( j = 0 ; j < line ; j++)
{
if( i != line -1 )
printf("----");
}
printf("\n");
}
}
void player_move(char board[row][line])
{
int x = 0;
int y = 0;
printf("玩家走:>\n");
while( 1 )
{
printf("请输入坐标:");
scanf("%d %d",&x,&y);
if( x >= 1 && x <= row && y >= 1 && y <= line )
{
if( board[x-1][y-1] == ' ' )
{
board[x-1][y-1] = '*';
break;
}
else
printf("该坐标被占用,请重新选择\n");
}
else
{
printf("坐标错误\n");
}
}
}
void computer_move(char board[row][line])
{
int x = 0;
int y = 0;
printf("电脑走\n");
while( 1 )
{
x = rand() % row;
y = rand() % line;
if( board[x][y] == ' ' )
{
board[x][y] = '#';
break;
}
}
}
//char is_win(char board[row][line])
//{
// int i = 0;
// int j = 0;
// for( i = 0 ; i < row ; i++)
// {
// if( board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][1] != ' ' )
// {
// return board[i][1];
// }
// }
// for( j = 0 ; j < line ;j++)
// {
// if( board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
// {
// return board[1][j];
// }
// }
// if( board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ' )
// {
// return board[1][1];
// }
// if( board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ' )
// {
// return board[1][1];
// }
// for( i = 0 ; i < row ; i++)
// {
// for( j = 0 ; j < line ; j++)
// {
// if( board[i][j] == ' ' )
// {
// return 'c';
// }
// }
// }
// return 'q';
//}
char is_win(char board[row][line])
{
int i = 0;
int j = 0;
for( i = 0 ; i < row ; i++)
{
for( j = 0 ; j < line ; j++)
{
if( board[i][j] != ' ' )
{
if( board[i][j] == board[i][j+1] && board[i][j+1] == board[i][j+2] )
{
return board[i][j];
}
else if( board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j])
{
return board[i][j];
}
else if( board[i][j] == board[i+1][j+1] && board[i][j] == board[i+2][j+2] && j < line - 2 && i < row - 2 )
{
return board[i][j];
}
else if( board[i][j] == board[i+1][j-1] && board[i+1][j-1] == board[i+2][j-2] && j > 1 && i < row - 2 )
{
return board[i][j];
}
}
}
}
for( i = 0 ; i < row ; i++)
{
for( j = 0 ; j < line ; j++)
{
if( board[i][j] == ' ' )
{
return 'c';
}
}
}
return 'q';
}
第二个是:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include
void menu()
{
printf("*******************************\n");
printf("*******1. play 0. exit******\n");
printf("*******************************\n");
}
void game()
{
char ret = '\0';
char board[row][line] = { '\0' };
srand((unsigned int)time(NULL));
initial(board);
print_board(board);
while( 1 )
{
player_move(board);
print_board(board);
ret = is_win(board);
if( ret != 'c' )
{
break;
}
computer_move(board);
print_board(board);
ret = is_win(board);
if( ret != 'c' )
{
break;
}
}
if( ret == '*' )
{
printf("玩家胜利\n");
}
else if( ret == '#' )
{
printf("电脑胜利\n");
}
else
{
printf("平局\n");
}
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 2:
break;
default:
break;
}
}while(input);
return 0;
}