1. 头文件
#pragma once
#ifndef _SANZIQI_H_
#define _SANZIQI_H_
#define ROW 3
#define COL 3
void init_board(char arr[ROW][COL], int row, int col);//初始化
void show_board(char arr[ROW][COL], int row, int col);//打印棋盘
void computer_play(char arr[ROW][COL], int row, int col);//电脑走
void Player(char arr[ROW][COL], int row, int col);//玩家走
int Checkfull(char arr[ROW][COL], int row, int col);//判断棋盘是否有空格
char Win(char arr[ROW][COL], int row, int col);//判断输赢
#endif
2.游戏核心
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"三子棋.h"
void init_board(char board[ROW][COL], int row, int col)
{
int x = 0,y = 0;
for(x=0;x<row;x++)
{for(y=0;y<col;y++)
{
board[x][y] = ' ';
}
}
}
void show_board(char board[ROW][COL], int row, int col)
{
int i;
for (i=0; i<row;i++)
{
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if (i != row - 1)
printf(" ---|---|--- \n");
}
}
void computer_play(char board[ROW][COL],int row,int col)
{
srand((unsigned int)time(NULL));
while(1)
{
int x = rand() % 3;
int y = rand() % 3;
if(board[x][y]==' ')
{
board[x][y] = '#';break;
}
}
}
void Player(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
scanf("%d,%d", &x, &y);
x--;
y--;
if((0 <= x && x <= row - 1) && (0 <= y && y <= col - 1))
{
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
else {
printf("请不要把棋放到人家的棋上\n");
}
}
else {
printf("请在棋盘里面下棋\n");
}
}
}
int Checkfull(char board[ROW][COL], int row, int col)
{
int x = 0,y = 0;
for(x=0;x<row;x++)
for(y=0;y<col;y++)
{
if(board[x][y]==' ')
return 1;
}
return 0;
}
char Win(char board[ROW][COL],int row,int col)
{
int i = 0;
{if(Checkfull(board,ROW,COL) == 0)
{
return 'p';
}
}
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][2] != ' '))
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[2][i] != ' '))
{
return board[0][i];
}
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[2][2] != ' '))
{
return board[0][0];
}
if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[2][0] != ' '))
{
return board[0][2];
}
return ' ';
}
3.游戏
#include<stdio.h>
#include<time.h>
#include"三子棋.h"
void nume()
{
printf("************************\n");
printf("*********1、play********\n");
printf("*********0、exit********\n");
printf("************************\n");
}
void game()
{
char board[ROW][COL];
init_board(board,ROW,COL);
while(1)
{
printf("电脑走:\n");
computer_play(board,ROW,COL);
show_board(board,ROW,COL);
if (Win(board, ROW, COL) != ' ')
{
break;
}
printf("玩家走\n");
Player(board, ROW, COL);
show_board(board, ROW, COL);
if (Win(board, ROW, COL) != ' ')
{
break;
}
}
if (Win(board,ROW,COL) == '*')
{
printf("玩家赢\n");
}
else if (Win(board,ROW,COL) == '#')
{
printf("电脑赢\n");
}
else if (Win(board,ROW,COL) == 'P')
{
printf("平局\n");
}
}
int main()
{
int ch = 0;
do{
nume();
printf("请选择:");
scanf("%d",&ch);
switch(ch)
{
case 1:game();break;
case 0:printf("退出游戏");break;
default:printf("输入错误,请重新输入");break;
}
}while(ch);
return 0;
}