您的位置:首页 > 编程语言 > C语言/C++

!--三子棋之C语言实现--!

2017-11-28 13:06 429 查看

程序代码:

//test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h> //srand头文件
#include <time.h> //time头文件

#include "game.h"

void game()
{
char ret = 0;
char board[rows][cols];
init_board(board,rows,cols);
display_board(board,rows,cols);
while(1)
{
player_move(board,rows,cols);
display_board(board,rows,cols);
if((ret = IsWin(board,rows,cols)) != ' ')
break;

computer_move(board,rows,cols);
display_board(board,rows,cols);
if((ret = IsWin(board,rows,cols)) != ' ')
break;

}
if(ret == 'P')
printf("玩家赢!\n");
else if(ret == '0')
printf("电脑赢!\n");
else if(ret == 'q')
printf("平局!\n");
}

void menu()
{
printf("*************************\n");
printf("*** 1.play     0.exit ***\n");
printf("*************************\n");
}

int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>:");
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误!\n");
break;
}
}while(input);
system("pause");
return 0;
}


//game.c
#include "game.h"

void init_board(char board[rows][cols],int row,int col)
{
memset(board, ' ', col*row*sizeof(board[0][0]));
}

void display_board(char board[rows][cols],int row,int col)
{
int i = 0;
for(i=0; i<row; i++)
{
printf("%c  | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if(i != 2)//(i < row-1)
printf("---|---|---\n");
}
}

void player_move(char board[rows][cols],int row,int col)
{
int x = 0;
int y = 0;
//printf("玩家走,请输入坐标:");
while(1)
{
printf("玩家走,请输入坐标:");
scanf("%d%d",&x,&y);
if(((x>=1) && (x<=rows)) && ((y>=1) && (y<=cols)))
{
if(board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'P';
break;
}
else
{
printf("坐标有误,请重新输入!\n"); //输入的坐标已经下过子了
}
}
else
{
printf("坐标有误,请重新输入!\n");//输入的坐标不在棋盘内
}
}
}

void computer_move(char board[rows][cols],int row,int col)
{
int x = 0;
int y = 0;
printf("电脑走:\n");
while(1)
{
//printf("电脑走:\n");
x = rand()%rows;
y = rand()%cols;
if(board[x][y] == ' ')
{
board[x][y] ='0';
break;
}
}
}

static int IsFull(char board[rows][cols],int row,int col)
{
int i = 0;
int j = 0;
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
if(board[i][j] == ' ')
return 0;
}
}
return 1;
}

char IsWin(char board[rows][cols],int row,int col)
{
int i = 0;
for(i=0; i<row; i++)//连成一行
{
if((board[i][0] == board[i][1])&&(board[i][1] == board[i][2]) && (board[i][0] != ' '))
return board[i][0];
}

for(i=0; i<col; i++)//连成一列
{
if((board[0][i] == board[1][i])&&(board[1][i] == board[2][i]) && (board[0][i] != ' '))
return board[0][i];
}

if((board[0][0] == board[1][1])&&(board[1][1] == board[2][2]) && (board[1][1] != ' '))//连成一对角线
return board[1][1];

if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))//连成一对角线
return board[1][1];
if(IsFull(board,row,col))
{
return 'q';
}
return ' ';
}


//game.h
#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define rows 3
#define cols 3

void init_board(char board[rows][cols],int row,int col);
void display_board(char board[rows][cols],int row,int col);
void player_move(char board[rows][cols],int row,int col);
void computer_move(char board[rows][cols],int row,int col);
char IsWin(char board[rows][cols],int row,int col);

#endif //__GAME_H__


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息