三子棋模拟
头文件方面的写入
#include <stdio.h>
#define ROW 9
#define COL 9
#include <stdlib.h>
#include <time.h>
void intboard(char board[ROW][COL],int row,int col);
void Printboard(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);
char is_win(char board[ROW][COL],int row,int col);
源文件,功能函数区域
#include "game.h"
void intboard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void Printboard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (j < col - 1)
{
printf(" %c |",board[i][j]);
}
else
{
printf(" %c \n",board[i][j]);
}
}
if (i < row - 1)
{
for (int k = 0; k < col; k++)
{
if (k < col - 1)
{
printf("---|");
}
else
{
printf("---\n");
}
}
}
}
}
void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入想要下的坐标\n");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标已被占用\n");
}
}
else
{
printf("输入错误请重新输入\n");
}
}
}
void computermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int is_full(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
for (j = 0; j < row; j++)
{
if (board[0][j] == board[1][j] && board[0][j] == board[2][j] && board[0][j] != ' ')
{
return board[0][j];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (1==is_full(board, ROW, COL))
{
return 'q';
}
return 'c';
}
源文件,菜单相关
#include "game.h"
void menu()
{
printf("*****************************************\n");
printf("*************** 1,play ***************\n");
printf("*************** 1,exit ***************\n");
printf("*****************************************\n");
}
void game(void)
{
char ret = 0;
char board[ROW][COL] = { 0 };
intboard(board, ROW, COL);
Printboard(board, ROW, COL);
while (1)
{
playermove(board, ROW, COL);
Printboard(board, ROW, COL);
ret = is_win(board,ROW,COL);
if (ret != 'c')
{
break;
}
computermove(board, ROW, COL);
Printboard(board, ROW, COL);
ret = is_win(board,ROW,COL);
if (ret != 'c')
{
break;
}
}
if (ret == '*')
{
printf("你赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else if (ret == 'q')
{
printf("平局\n");
}
}
void text()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请输入您的选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 2:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (input);
}
int main()
{
text();
return 0;
}