#include "game.h"
void menu()
{
printf(" *****************************\n");
printf(" *********1.开始游戏**********\n");
printf(" *********0.退出游戏**********\n");
printf(" *****************************\n");
}
void game() //存储数据,需要二维数组
{
char board[ROW][COL];
//初始化棋盘-空格↓
InitBoard(board, ROW, COL);
//打印棋盘-打印数组内容↓
DisplayBoard(board,ROW,COL);
//开始下棋
char ret = 0;
while (1)
{
PlayerMove(board, ROW, COL); //玩家下棋
ret = IS_win(board, ROW, COL);
if (ret != 'C')
break;
DisplayBoard(board, ROW, COL); //显示
ComputerMove(board, ROW, COL);//电脑下棋
ret = IS_win(board, ROW, COL);
if (ret != 'C')
break;
DisplayBoard(board, ROW, COL);//显示
}
if (ret == '*')
{
printf("玩家赢了!\n");
}
else if (ret == '#')
{
printf("电脑赢了!\n");
}
else
{
printf("本次游戏平局!\n");
}
DisplayBoard(board, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("三子棋游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择:\n");
break;
}
} while (input);
return 0;
}
#include<stdio.h> //头文件的包含
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
//函数声明
void InitBoard(char board[ROW][COL], int row, int col);//初始化棋盘
void DisplayBoard(char board[ROW][COL], int row, int col); //打印棋盘
void PlayerMove(char board[ROW][COL], int row, int col); //玩家下棋
void ComputerMove(char board[ROW][COL],int row, int col);//电脑下棋
char IS_win(char board[ROW][COL], int row, int col);
#include "game.h"
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 DisplayBoard(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++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if(j<col-1)
printf("|");
}
printf("\n");
}
}
}
void PlayerMove(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("该格已被占用,请重新输入:>");
}
}
else
{
printf("坐标错误,请重新输入:>");
}
}
}
void ComputerMove(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 IsFull(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 IS_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][0];
}
//判断列
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[1][1];
}
//判断平局
int ret = IsFull(board, row, col);
if (ret == 0)
{
return 'C';
}
return 'Q';
}