运行程序,打印菜单。
玩家输入1开始游戏,输入0则退出。
>开始游戏,打印棋盘<
>玩家输入棋盘坐标,进行下棋<
>电脑随机生成坐标,进行下棋<
如此进行下去,直至分出胜负,或者平局。
附代码:
1.主函数
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
#include<stdio.h>
void menu()
{
printf("------------------\n");
printf("----- 1.play -----\n");
printf("----- 0.exit -----\n");
printf("------------------\n");
}
void game()
{
char ret = 0;
//随机数生成器
srand((unsigned int)time(NULL));
//定义棋盘大小
char board[ROW][COL] = { 0 };
//初始化棋盘
init_board(board, ROW, COL);
//打印棋盘
display_board(board, ROW, COL);
while (1)
{
//玩家下棋,输入下棋位置坐标,1 1, 1 2, 1 3, 2 1,......
player_move(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if ('C' != ret)
break;
//电脑下棋,随机
computer_move(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if ('C' != ret)
break;
}
display_board(board, ROW, COL);
if (ret == 'Q')
{
printf("平局!\n");
}
if (ret == '*')
{
printf("玩家胜利!!!\n");
}
if (ret == '#')
{
printf("电脑胜利!!!\n");
}
display_board(board, ROW, COL);
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 0:
{
printf("退出游戏!!!\n");
break;
}
case 1:
{
game();
break;
}
default:
{
printf("输入错误,请重新输入!!!\n");
break;
}
}
} while (input);
return 0;
}
2.头文件
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3
#include<stdlib.h>
#include<time.h>
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断输赢
char is_win(char board[ROW][COL], int row, int col);
3.游戏实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
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 < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:>");
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("该坐标已下,请重新输入:>");
continue;
}
}
else
{
printf("非法坐标,请重新输入:>");
continue;
}
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (' ' == board[x][y])
{
board[x][y] = '#';
break;
}
}
}
int is_full(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&board[i][0] != ' ')
{
return board[i][0];
}
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ')
{
return board[0][j];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (is_full(board, ROW, COL) == 1)
return 'Q';
return 'C';
}