//test.c:
#define _CRT_SECURE_NO_WARNINGS 0;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"
void game()
{
char board[ROW][COL] = { 0 };
char set = '_';
InitBoard(board, ROW, COL,set);
Display_Board(board, ROW, COL);
srand((unsigned int)time(NULL));
while (1)
{
int r = 0;
printf("请玩家走:\n");
PlayerMove(board, ROW, COL);
Display_Board(board, ROW, COL);
ComputerMove(board, ROW, COL);
Display_Board(board, ROW, COL);
r = Is_Win(board, ROW, COL);
if (r == 'X')
{
printf("恭喜您,取得胜利\n");
Display_Board(board, ROW, COL);
return;
}
else if (r == '0')
{
printf("很遗憾,您输了,再接再厉\n");
Display_Board(board, ROW, COL);
return;
}
else if (r == 'q')
{
printf("平局了,再来一局??\n");
Display_Board(board, ROW, COL);
return;
}
}
}
void mneu()
{
printf("******************************************\n");
printf("********welcome to game's world*******\n");
printf("**********0. exit 1.play***********\n");
printf("******************************************\n");
}
int main()
{
int input = 0;
do
{
mneu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
game();
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
system("pause");
return 0;
}
//game.c:
#define _CRT_SECURE_NO_WARNINGS 0;
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col,char set)
{
int i = 0;
int j = 0;
int ret = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i == 0 && j == 0)
{
board[0][0] = ' ';
}
else if (j == 0)
{
board[i][j] = i;
}
else if (i == 0)
{
board[i][j] = j;
}
else
{
board[i][j] = set;
}
}
}
}
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++)
{
if (i == 0 && j == 0)
{
printf(" %c |", board[0][0]);
}
else if (i == 0 || j == 0)
{
printf("_%d_|", board[i][j]);
}
else
{
printf("_%c_|", board[i][j]);
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
while (1)
{
int x = 0;
int y = 0;
printf("请输入您要走的坐标:>");
scanf("%d %d", &x, &y);
if ((board[x][y] == '_') && (x > 0) && (x<row) && (y>0) && (y < col))
{
board[x][y] = 'X';
return;
}
else
{
printf("输入有误,请重新输入\n");
}
}
}
int Is_Full(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 1; i < row; i++)
{
for (j = 1; j < col; j++)
{
if (board[i][j] == '_')
{
return 0;
}
}
}
return 1;
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("************电脑走************\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
int i = 0;
for (i = 1; i < row; i++)
{
if ((board[i][1] == board[i][2]) && (board[i][1] != '_') && (board[i][3] == '_'))
{
board[i][3] = '0';
return;
}
else if ((board[i][3] == board[i][2]) && (board[i][2] != '_') && (board[i][1] == '_'))
{
board[i][1] = '0';
return;
}
else if ((board[i][1] == board[i][3]) && (board[i][1] != '_') && (board[i][2] == '_'))
{
board[i][1] = '0';
return;
}
}
for (i =1; i < col; i++)
{
if ((board[1][i] == board[2][i]) && (board[1][i] != '_') && (board[3][i] == '_'))
{
board[3][i] = '0';
return;
}
else if ((board[3][i] == board[2][i]) && (board[2][i] != '_') && (board[1][i] == '_'))
{
board[1][i] = '0';
return;
}
else if ((board[1][i] == board[3][i]) && (board[1][i] != '_') && (board[2][i] == '_'))
{
board[1][i] = '0';
return;
}
}
if ((board[1][1] == board[2][2]) && (board[1][1] != '_') && (board[3][3] == '_'))
{
board[3][3] = '0';
return;
}
if ((board[1][1] == board[3][3]) && (board[1][1] != '_') && (board[2][2] == '_'))
{
board[2][2] = '0';
return;
}
if ((board[3][3] == board[2][2]) && (board[3][3] != '_') && (board[1][1] == '_'))
{
board[1][1] = '0';
return;
}
if ((board[1][3] == board[2][2]) && (board[1][3] != '_') && (board[3][1] == '_'))
{
board[3][1] = '0';
return;
}
if ((board[1][3] == board[3][1]) && (board[1][3] != '_') && (board[2][2] == '_'))
{
board[2][2] = '0';
return;
}
if ((board[3][1] == board[2][2]) && (board[3][1] != '_') && (board[1][3] == '_'))
{
board[1][3] = '0';
return;
}
if (board[x][y] == '_'&& x >= 0 && x < row && y >= 0 && y < col)
{
board[x][y] = '0';
break;
}
if (Is_Full(board, row, col))
return ;
}
}
char Is_Win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 1; i < row; i++)
{
if ((board[i][1] == board[i][2]) && (board[i][2] == board[i][3]) && (board[i][3] != '_'))
{
return board[i][1];
}
}
for (i = 1; i < col; i++)
{
if ((board[1][i] == board[2][i]) && (board[2][i] == board[3][i]) && (board[3][i] != '_'))
{
return board[3][i];
}
}
if ((board[1][1] == board[2][2]) && (board[2][2] == board[3][3]) && (board[3][3] != '_'))
{
return board[1][1];
}
if ((board[1][3] == board[2][2]) && (board[2][2] == board[3][1]) && board[3][1] != '_')
{
return board[1][3];
}
if (Is_Full(board, row, col))
{
return 'q';
}
return '_';
}
//game.h:
#ifndef __GAME_H__
#define __GAME_H__
#define ROW 4
#define COL 4
void InitBoard(char board[ROW][COL], int row, int col, char set);
void Display_Board(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);
int Is_Full(char board[ROW][COL], int row, int col);
char Is_Win(char board[ROW][COL], int row, int col);
#endif //__GAME_H__
三子棋C语言编写
最新推荐文章于 2022-03-22 16:24:42 发布