//头文件
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 5
#define COL 5
void board_display(char arr[ROW][COL], int row, int col);
void init_board(char arr[ROW][COL], int row, int col);
void PalyerMove(char arr[ROW][COL], int row, int col);
void ComputerMove(char arr[ROW][COL], int row, int col);
int check_win(char arr[ROW][COL], int row, int col);
int show(char arr[ROW][COL], int row, int col);
#endif//__GAME_H__
//主函数
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void menu()
{
printf("*************************\n");
printf("*** 1.play 2.exit ***\n");
printf("*************************\n");
}
void game()
{
int row = 0;
int col = 0;
char arr[ROW][COL] = { 0 };
char ret = 0;
init_board(arr, row, col);
do
{
board_display(arr, row, col);
printf("玩家走:\n");
PalyerMove(arr, row, col);
board_display(arr, row, col);
ret = check_win(arr, row, col);
if (ret != ' ')
break;
printf("电脑走:\n");
ComputerMove(arr, row, col);
ret = check_win(arr, row, col);
if (ret != ' ')
break;
} while (ret == ' ');
board_display(arr, row, col);
if (ret == 'X')
printf("玩家赢了\n");
else if (ret == '$')
printf("电脑赢了\n");
else if (ret == 'q')
printf("平局\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
return 0;
break;
default:
printf("选择错误\n");
break;
}
} while (input);
getchar();
return 0;
}
//game函数
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char arr[ROW][COL], int row, int col)
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
arr[i][j] = ' ';
}
}
}
void board_display(char arr[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < ROW; i++)
{
printf(" %c | %c | %c | %c | %c \n", arr[i][0], arr[i][1], arr[i][2], arr[i][3],arr[i][4] );
if (i < ROW - 1)
printf("---|---|---|---|---\n");
}
}
void PalyerMove(char arr[ROW][COL], int row, int col)
{
while (1)
{
printf("请输入坐标:");
scanf("%d%d", &row, &col);
row--;
col--;
if ((row >= 0) && (row <= ROW - 1) && (col >= 0) && (col <= COL - 1))
{
if (arr[row][col] == ' ')
{
arr[row][col] = 'X';
break;
}
else
printf("坐标被占用\n");
}
else
printf("坐标非法\n");
}
}
void ComputerMove(char arr[ROW][COL], int row, int col)
{
srand((unsigned)time(NULL));
while (1)
{
row = rand() % ROW;
col = rand() % COL;
if (arr[row][col] == ' ')
{
arr[row][col] = '$';
break;
}
}
}
int check_win(char arr[ROW][COL],int row,int col)
{
int i = 0;
for (i = 0; i < ROW; i++)
{
if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][2] == arr[i][3]) && (arr[i][3] == arr[i][4]) && (arr[i][1] != ' '))
return arr[i][1];
if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && (arr[2][i] == arr[3][i]) && (arr[3][i] == arr[4][i]) && (arr[1][i] != ' '))
return arr[1][i];
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && (arr[2][2] == arr[3][3]) && (arr[3][3] == arr[4][4]) && (arr[1][1] != ' '))
return arr[1][1];
if ((arr[0][4] == arr[1][3]) && (arr[1][3] == arr[2][2]) && (arr[2][2] == arr[3][1]) && (arr[3][1] == arr[4][0]) && (arr[0][4] != ' '))
return arr[0][4];
}
if (show(arr, row, col) == 0)
return ' ';
else
return 'q';
}
int show(char arr[ROW][COL], int row, int col)
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (arr[i][j] == ' ')
return 0;
}
}
return 1;
}