5

程序:三子棋游戏_糖基山羊的技术博客_51CTO博客

 1 year ago
source link: https://blog.51cto.com/u_15923331/5994497
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

程序:三子棋游戏_糖基山羊的技术博客_51CTO博客

头文件game.h

#define _CRT_SECURE_NO_WARNINGS//头文件#include<stdio.h>#include <stdlib.h>#include <time.h>//定义行列#define ROW 3#define COL 3//函数声明void menu();void InitBoard(char board[ROW][COL], int row, int col);void DisplayBoard(char board[ROW][COL], int row, int col);void player(char board[ROW][COL], int row, int col);void computer(char board[ROW][COL], int row, int col);int is_win(char board[ROW][COL], int row, int col);

游戏的实现game.c

#include"game.h"
void menu()
{
printf("*******************\n");
printf("******1.start******\n");
printf("*******************\n");
printf("******0.exit*******\n");
printf("*******************\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 DisplayBoard(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("---");
if (j < col - 1)
{
printf("|");
}

}
printf("\n");
}

}
}
void player(char board[ROW][COL], int row, int col)//玩家下棋
{
int x = 0;
int y = 0;
printf("选择你要下的坐标\n");
while (1)
{
loop:
scanf("%d%d", &x, &y);
printf("\n");
if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("输入的坐标被占用,请重新输入\n");
goto loop;
}
}
else
{
printf("输入的坐标有误,请重新输入\n");
}
}
}
void computer(char board[ROW][COL], int row, int col)//电脑下棋
{
while (1)
{
int x = rand() % row;
int y = rand() % col;//只输出0 1 2
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int is_win(char board[ROW][COL], int row, int col)//判断输赢
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)//判断按行3个获胜
{
if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
{
if (board[i][0] == '*')
{
printf("玩家获胜\n");
return 2;
}
else if (board[i][0] == '#')
{
printf("电脑获胜\n");
return 1;
}
}
}
for (j = 0; j < col; j++)//判断按列3个获胜
{
if (board[0][j] == board[1][j] && board[0][j] == board[2][j] && board[0][j] != ' ')
{
if (board[i][0] == '*')
{
printf("玩家获胜\n");
return 2;
}
else if (board[i][0] == '#')
{
printf("电脑获胜\n");
return 1;
}
}
}
if (((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0]))&&board[1][1]!=' ')
{//判断按对角线获胜
if (board[1][1] == '*')
{
printf("玩家获胜\n");
return 2;
}
else
{
printf("电脑获胜\n");
return 1;
}
}
int ret2=is_full(board, ROW, COL);//判断是否平局
if (ret2 == 1)
{
printf("平局了\n");
return 0;
}
else
{
return 3;
}
}
int is_full(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;//下满了
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK