(关于五子棋) 第一次用 c编写小游戏,可能还有很多不足,但是心里满满的成就感。
1.游戏的测试
<pre class="objc" name="code">void menu()
{
printf(" 1.play ");
printf(" 0.exit ");
}
void game()
{
char ret;
char board[ROWS][COLS];
init_board(board,ROWS,COLS);
display_board(board,ROWS,COLS);
while(1)
{
man_play_chess(board,ROWS,COLS);
if((ret = judge_win(board,ROWS,COLS))!=' ')
break;
display_board(board,ROWS,COLS);
pc_play_chess(board,ROWS,COLS);
if((ret = judge_win(board,ROWS,COLS))!=' ')
break;
display_board(board,ROWS,COLS);
}
display_board(board,ROWS,COLS);
if(ret == 'x')
printf("恭喜,你赢了");
else if(ret == 'c')
printf("电脑赢了哈哈");
else if(ret == 'q')
printf("平局呵呵");
}
int main()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch(input)
{
case 0:
break;
case 1:
game();
break;
default:
printf("请重新选择:");
}
}while(input);
}
<span style="font-size:18px;"> 2.初始化并显示棋盘(棋盘做的不太好看<img alt="害羞" )</span>
void init_board(char board[ROWS][COLS],int row,int col)
{
memset(board,' ',col*row*sizeof(char));
}
void display_board(char board[ROWS][COLS],int row,int col)
{
int i = 0;
for(i=0; i<row; i++)
{
printf(" %c | %c | %c | %c | %c \n",board[i][0],board[i][1],board[i][2],board[i][3],board[i][4]);
if(i != row)
printf("___|___|___|___|___|\n");
}
}
<span style="font-size:18px;"> 3.玩家走棋</span>
void man_play_chess(char board[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
while(1)
{
printf("请输入落子的位置:");
scanf("%d %d",&i,&j);
if(i>=1&&j>=1&&i<=row&&j<=col&&board[i-1][j-1]==' ')
{
board[i-1][j-1] = 'x';
break;
}
else
printf("下标有误");
}
}
4.电脑走棋
void pc_play_chess(char board[ROWS][COLS],int row,int col)
{
while(1)
{
int a = rand()%row;
int b = rand()%col;
if(board[a][b] == ' ')
{
board[a][b] = 'c';
break;
}
}
}
5.判断棋盘是否已满
int is_full(char board[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
if(board[i][j] ==' ')
return 0;
}
}
return 1;
}
<span style="font-size:18px;">6.判断输赢(这个判断函数写的过于繁琐,希望以后有机会有能力再优化一下</span>
int judge_win(char board[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
for(i=0; i<row; i++)
{
count = 0;
for(j=0; j<col; j++)
{
if(board[i][j] == 'x')
count++;
}
if(count == row)
return 'x';
}
for(i=0; i<row; i++)
{
count = 0;
for(j=0; j<col; j++)
{
if(board[i][j] == 'c')
count++;
}
if(count == row)
return 'c';
}
for(j=0; j<col; j++)
{
count = 0;
for(i=0; i<row; i++)
{
if(board[i][j] == 'x')
count++;
}
if(count == col)
return 'x';
}
for(j=0; j<col; j++)
{
count = 0;
for(i=0; i<row; i++)
{
if(board[i][j] == 'c')
count++;
}
if(count == col)
return 'c';
}
for(i=0; i<row; i++)
{
if(board[i][i] == 'x')
count1++;
if(count1 == row)
return 'x';
}
for(i=0; i<row; i++)
{
if(board[i][i] == 'c')
count2++;
if(count2 == row)
return 'c';
}
for(i=0; i<row; i++)
{
if(board[i][row-1-i] == 'c')
count3++;
if(count3 == row)
return 'c';
}
for(i=0; i<row; i++)
{
if(board[i][row-1-i] == 'x')
count4++;
if(count4 == row)
return 'x';
}
if(is_full(board, ROWS,COLS) == 1)
return 'q';
return ' ';
}