game1.h
#pragma once
#include<stdio.h>
#include<windows.h>
#define ROW 3
#define COL 3
//初始化数组
void InitBoard(char board[][COL], int row, int col);
//打印棋盘
void DisplayBoard(char board[][COL], int row, int col);
//玩家下棋
void Player(char board[][COL], int row, int col);
//电脑下棋
void Computer(char board[][COL], int row, int col);
//判断输赢
char Judge(char board[][COL], int row, int col);
game1.c
#include"game1.h"
//初始化数组
void InitBoard(char board[][COL],int row,int col)
{
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
board[i][j] = ' ';
}
}
}
//打印棋盘
void DisplayBoard(char board[][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j != col - 1)
printf("|");
}
printf("\n");
if (i != row - 1)
{
for (int k = 0; k < col; k++)
{
printf("---");
if (k != col-1)
printf("|");
}
printf("\n");
}
}
}
//玩家下棋
void Player(char board[][COL], int row, int col)
{
int n = 0;
int m = 0;
printf("玩家输入坐标:");
while(1)
{
scanf("%d%d", &n, &m);
if (n > row || m > col)
{
system("cls");
DisplayBoard(board, row, col);
printf("请输入范围内的坐标:");
continue;
}
else if (board[n - 1][m - 1] != ' ')
{
system("cls");
DisplayBoard(board, row, col);
printf("该坐标已被占用,请重新输入:");
continue;
}
else
{
board[n - 1][m - 1] = '*';
break;
}
}
}
//电脑下棋
void Computer(char board[][COL], int row, int col)
{
if (board[1][1] == ' ')
board[1][1] = '#';
else
{
insert:;
int x = 0;
int y = 0;
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
}
else
goto insert;
}
}
//判断输赢(玩家赢、电脑赢、棋局满了)
char Judge(char board[][COL], int row, int col)
{
for (int i=0;i<row;i++)//扫描每一行是否相同
{
int r = 0;
for (int j=0;j<col-1;j++)//扫描每一行的每一个是否相同
{
if (board[i][j] == board[i][j + 1])
r++;
}
if (r == 2)
return board[i][0];
}
for (int i = 0; i < col; i++)//扫描每一列是否相同
{
int c = 0;
for (int j = 0; j < row - 1; j++)//扫描每一列的每一个是否相同
{
if (board[j][i] == board[j+1][i])
c++;
}
if (c == 2)
return board[0][i];
}
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || board[0][2] == board[1][1] && board[1][1] == board[2][0])
return board[1][1];
int count = 0;
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
if(board[i][j]!=' ')
count++;
}
}
if (count == row * col)
return 'Q';
}
game1_test.c
#include"game1.h"
#include<windows.h>
void menu()
{
printf("***********************\n");
printf("**** 1.开始游戏 *****\n");
printf("**** 0.退出游戏 *****\n");
printf("***********************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
InitBoard(board,ROW,COL);
DisplayBoard(board, ROW, COL);
while (1)
{
Player(board, ROW, COL);
system("cls");
DisplayBoard(board, ROW, COL);
char ret = Judge(board, ROW, COL);
if (ret == '*')
{
printf("玩家胜利!\n");
system("pause");
system("cls");
break;
}
else if(ret=='Q')
{
printf("游戏平局!\n");
system("pause");
system("cls");
break;
}
Computer(board, ROW, COL);
system("cls");
DisplayBoard(board, ROW, COL);
ret = Judge(board, ROW, COL);
if (ret == '#')
{
printf("电脑胜利!\n");
system("pause");
system("cls");
break;
}
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
system("cls");
switch (input)
{
case 1:
game();
break;
case 0:
printf("已退出游戏");
Sleep(1000);
break;
default:
printf("请输入正确的序号!");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
|