要实现三子棋,首先思考三子棋怎么样实现的,如一下几点:
1、实现三子棋,在一个3*3的二维数组下实现
2、把二维数组都初始化为空格 ‘ ’
3、电脑随机生成坐标,判断该坐标是否合法,合法赋值为 ‘x’
4、玩家输入合法坐标,该坐标赋值为 ‘ * ’
5、判断输赢,每一行,每一列,或者对角线上的值相等,则为赢。如果棋盘满了,还没有赢家,则为平局!
代码如下:
game.h
#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 3 //行
#define COLS 3 //列
void init_board(char board[ROWS][COLS],int rows,int cols);
void display_board(char board[ROWS][COLS],int rows,int cols);
void computer_move(char board[ROWS][COLS],int rows,int cols);
void player_move(char board[ROWS][COLS],int rows,int cols);
int check_win(char board[ROWS][COLS],int rows,int cols);
#endif//__GAME_H__
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void init_board(char board[ROWS][COLS],int rows,int cols)
{
int i = 0;
int j = 0;
for(i = 0;i < rows;i++)
{
for(j = 0;j < cols;j++)
{
board[i][j] = ' ';
}
}
}
void display_board(char board[ROWS][COLS],int rows,int cols) //显示棋盘
{
int i = 0;
for(i = 0;i < rows;i++)
{
printf("| %c | %c | %c |\n",board[i][0],board[i][1],board[i][2]);
if(i <= rows-1)
{
printf("|----|----|----|\n");
}
}
}
void computer_move(char board[ROWS][COLS],int rows,int cols) //电脑走
{
while(1)
{
int x = rand()%3;
int y = rand()%3;
if(board[x][y] != '*' && board[x][y] != 'x')
{
board[x][y] = 'x';
return;
}
}
}
void player_move(char board[ROWS][COLS],int rows,int cols) //玩家走
{
int x = 0;
int y = 0;
printf("玩家请输入坐标,如:x y\n");
do
{
scanf("%d %d",&x,&y);
if(board[x-1][y-1] == 'x')
{
printf("该坐标已经被占用,请重新输入:\n");
}
else if(((x-1)>3)||((y-1)>3) || (x-1 < 0) || (y-1 < 0))
{
printf("输入坐标错误,请重新输入:\n");
}
else
{
board[x-1][y-1] = '*';
break;
}
}while(1);
}
static int is_full(char board[ROWS][COLS],int rows,int cols) //判断棋盘满没有
{
int i = 0;
int j = 0;
for(i = 0;i < rows;i++)
{
for(j = 0;j <cols;j++)
{
if(board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
int check_win(char board[ROWS][COLS],int rows,int cols) //判断输赢
{
int k = 1;
int i = 0;
for(i = 0;i < rows;i++)
{
if((board[i][0] == board[i][1])&&(board[i][1] == board[i][2])&&board[i][0] !=' ')
{
return board[i][1];
}
}
for(i = 0;i < cols;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[0][0] != ' '))
{
return board[1][1];
}
if((board[0][2] == board[1][1])&&(board[1][1] == board[2][0])&&(board[0][2] != ' '))
{
return board[0][2];
}
if(is_full(board,rows,cols))
{
return ' ';
}
return 'q';
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("*******************************\n");
printf("*****1.paly 2.exit *********\n");
printf("*******************************\n");
}
void game()
{
int ret = 0;
char board[ROWS][COLS] = {0};
init_board(board,ROWS,COLS);
display_board(board,ROWS,COLS);
while(1)
{
printf("电脑走:\n");
computer_move(board,ROWS,COLS);
display_board(board,ROWS,COLS);
ret = check_win(board,ROWS,COLS);
if(ret != 'q')
{
break;
}
printf("玩家走:\n");
player_move(board,ROWS,COLS);
display_board(board,ROWS,COLS);
ret = check_win(board,ROWS,COLS);
if(ret != 'q')
{
break;
}
}
if(ret == '*')
{
printf("玩家赢了!\n");
}
else if(ret == 'x')
{
printf("电脑赢了!\n");
}
else if(ret == ' ')
{
printf("平局!\n");
}
}
int main()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 2:
return 0;
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}while(input);
system("pause");
return 0;
}
}
}