You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
6.2 KiB

6 years ago
//da mu mu de kechengsheji---jngziqi
//game.h Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
#ifndef __GAME_H__
#define __GAME_H__
#define ROW 3
#define CLO 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
void menu(); //<2F><>ӡ<EFBFBD>˵<EFBFBD>
void game(char arr[ROW][CLO], int row, int clo); //ִ<>в˵<D0B2>
void play(char arr[ROW][CLO], int row, int clo); //<2F><>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
void InitBoard(char arr[ROW][CLO], int row, int clo); //<2F><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
void Pmove(char arr[ROW][CLO], int row, int clo); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Cmove(char arr[ROW][CLO], int row, int clo); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int Isfull(char arr[ROW][CLO], int row, int clo); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int Iswin(char arr[ROW][CLO], int row, int clo); //<2F>ж<EFBFBD><D0B6><EFBFBD>Ӯ
#endif
//
//test.c <20><>Ϊ<EFBFBD><CEAA>Ϸ<EFBFBD><CFB7><EFBFBD>߼<EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD>
#include"stdio.h"
int main()
{
srand((unsigned int)time(NULL)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char arr[ROW][CLO];
game(arr,ROW,CLO);
system("pause");
return 0;
}
//
void menu()
{
printf("******************************\n");
printf("******1.play<61><79><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>Ϸ<EFBFBD><CFB7>******\n");
printf("******2.exit<69><74><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>Ϸ<EFBFBD><CFB7>******\n");
printf("******************************\n");
printf("***(O:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>X:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)***\n");
}
//ִ<>в˵<D0B2>
void game(char arr[ROW][CLO], int row, int clo)
{
int input = 0;
do
{
menu();
scanf("%d",&input);
memset(arr, ' ', ROW*CLO * sizeof(arr[0][0]));
switch (input)
{
case 1:
play(arr, row, clo); break;
case 2:
exit(0); break;
default:
printf("С<EFBFBD>ɰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?\n");
}
} while (input);
}
//<2F><>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
void play(char arr[ROW][CLO], int row, int clo)
{
int flag = 0;
InitBoard(arr, row, clo);
while (1)
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
Pmove(arr, row, clo);
InitBoard(arr, row, clo);
flag = Iswin(arr, row, clo);
if (flag == 1)
{
printf("<EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><EFBFBD>Ӯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
break;
}
flag = Isfull(arr, row, clo);
if (flag == 4)
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><EFBFBD>ƽ<EFBFBD>֣<EFBFBD><EFBFBD><EFBFBD>ͦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
break;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
Cmove(arr, row, clo);
InitBoard(arr, row, clo);
flag = Iswin(arr, row, clo);
if (flag == 1)
{
printf("<EFBFBD><EFBFBD>Ȼ<EFBFBD>ǵ<EFBFBD><EFBFBD><EFBFBD>Ӯ<EFBFBD>ˣ<EFBFBD><EFBFBD><EFBFBD>~<7E><><EFBFBD>е<EFBFBD><D0B5><EFBFBD>Ӵ<EFBFBD><D3B4>\n");
break;
}
}
}
//<2F><><EFBFBD>̴<EFBFBD>ӡ
void InitBoard(char arr[ROW][CLO], int row, int clo)
{
int i = 0;
int j = 0;
for (j = 0; j < row; j++)
{
for (i = 0; i < clo; i++)
{
printf(" %c ", arr[j][i]);
if (i < (clo - 1))
{
printf("|");
}
}
printf("\n");
if (j < row - 1)
{
for (i = 0; i < clo; i++)
{
printf("---");
if (i < (clo - 1))
{
printf("|");
}
}
}
printf("\n");
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Pmove(char arr[ROW][CLO], int row, int clo)
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28>ո<EFBFBD><D5B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
int m = 0, n = 0;
while (1)
{
scanf("%d %d", &m, &n);
if (((m >= 1) && (m <= 3)) && ((n >= 1) && (n <= 3)))
{
if (arr[m - 1][n - 1] == ' ')
{
arr[m - 1][n - 1] = 'O';
break;
}
else
{
printf("<EFBFBD>٣<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
}
else
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD><EFBFBD>\n");
}
}
}
//<2F>ж<EFBFBD><D0B6><EFBFBD>Ӯ
int Iswin(char arr[ROW][CLO], int row, int clo)
{
//<2F><>
int i = 0;
for (i = 0; i < row; i++)
{
if (((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2])) && ((arr[i][0] != ' ')))
{
return 1;
}
}
//<2F><>
for (i = 0; i < clo; i++)
{
if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && ((arr[0][i] != ' ') ))
{
return 1;
}
}
//Ȧ
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && ((arr[0][0] != ' ') ))
{
return 1;
}
if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) && ((arr[0][2] != ' ')))
{
return 1;
}
return 0;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5A3A8><EFBFBD>ģ<EFBFBD>
void Cmove(char arr[ROW][CLO], int row, int clo)
{
int flag = 0;
int m = 0, n = 0;
for (m = 0; m < row; m++)
{
for (n = 0; n < row; n++)
{
if (arr[m][n] == ' ')
{
arr[m][n] = 'X';
flag = Iswin(arr, row, clo);
if (flag == 0)
{
arr[m][n] = ' ';
}
if (flag == 1)
{
break;
}
}
}
if (flag == 1)
{
break;
}
}
if (flag == 0)
{
for (m = 0; m < row; m++)
{
for (n = 0; n < row; n++)
{
if (arr[m][n] == ' ')
{
arr[m][n] = 'O';
flag = Iswin(arr, row, clo);
if (flag == 0)
{
arr[m][n] = ' ';
}
if (flag == 1)
{
arr[m][n] = 'X';
break;
}
}
}
if (flag == 1)
{
break;
}
}
if ((m == 3) && (n == 3))
{
while (1)
{
m = rand() % 3;
n = rand() % 3;
if (arr[m][n] == ' ')
{
arr[m][n] = 'X';
break;
}
}
}
}
}
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
int Isfull(char arr[ROW][CLO], int row, int clo)
{
int i = 0, j = 0;
int count = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < clo; j++)
{
if (arr[i][j] != ' ')
{
count ++;
}
}
}
if (count == 9)
{
return 4;
}
return 3;
}