随着c语言的深入了解与认识,我们要进行实战演练。三字棋与其进行优化是一个不错的演练。
在此过程中用了2个源文件1个头文件
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include "game.h"
void menu()
{
printf("**********************\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("**********************\n");
}
//初始化棋盘
game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
initboard(board, ROW, COL);
//进行打印棋盘处理
display_board( board,ROW,COL);
//打印完毕
//下棋
while(1)
{
//玩家下棋
players_play_chess(board,ROW,COL);
//重新弄个棋盘
display_board(board, ROW, COL);
ret = win(board, ROW, COL);
if (ret !='c')
{
break;
}
//电脑下棋
computer_chess(board,ROW,COL);
//重新弄个棋盘
display_board(board, ROW, COL);
ret = win(board, ROW, COL);
if( ret != 'c')
{
break;
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();//游戏菜单
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择1||0\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
#pragma once
#include
#include
#include
#define ROW 3
#define COL 3
//初始化棋盘
void initboard(char board[ROW][COL], int row, int col);
//打印棋盘
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void players_play_chess(char board[ROW][COL],int row,int col);
//电脑下棋
void computer_chess(char board[ROW][COL], int row, int col);
//进行输赢判断
//如果输出‘*’玩家赢
//如果输出‘#’电脑赢
//如果输出‘d’平局
//如果输出‘c’继续
char win(char board[ROW][COL],int row,int col);
第一步进行菜单的输入:
void menu()
{
printf("**********************\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("**********************\n");
}
第二步棋盘的初始化:
void initboard(char board[ROW][COL], int row,int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j]=' ';
}
}
}
//进去格式处理
void display_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for ( i= 0; i < row; i++)
{
for(j = 0;j < col;j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
//换行
if (i
for (j = 0; j < col; j++)
{
printf("___");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
以上是对初始化棋盘模型的输入。
第三步就是下棋
(1)玩家下棋:
void players_play_chess(char board[ROW][COL], int row, int col)
{
printf("玩家下棋:>");
printf("请重新输入坐标");
int x = 0;
int y = 0;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新输入!\n");
}
}
else
{
printf("输入不在范围内,请重新输入!\n");
}
}
}
(2)电脑下棋:
void computer_chess(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋:>\n");
while(1)
{
x = rand() % ROW;//0~2
y = rand() % COL;//0~2
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int draw(char board[ROW][COL], 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;
}
第四步进行输赢的判断:
char win(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
//纵
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[1][i];
}
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1]!=' ')
{
return board[1][1];//*
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[2][0];//#
}
//判断平局
if (1 == draw(board, row, col))
{
return 'd';
}
//继续
return 'c';
}