简单的三子棋程序
直接上程序
头文件
#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 3
#define COLS 3
enum op
{
EXIT,
PLAY
};
void init_board(char board[ROWS][COLS]); //初始化棋盘
void print_board(char board[ROWS][COLS]); //打印出棋盘
void player_move(char board[ROWS][COLS]); //玩家走
void computer_move(char board[ROWS][COLS]); //电脑走
int check_full(char board[ROWS][COLS]); //判断棋盘是否已满
char check_win(char board[ROWS][COLS]); //判断输赢
#endif
源程序
#define _CRT_SECURE_NO_WARNINGS 1
#include"016.h"
#include<stdio.h>
#include<stdlib.h>
void init_board(char board[ROWS][COLS]) //初始化棋盘
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[ROWS][COLS]) //打印出棋盘
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if(i!=2)
printf("---|---|---\n");
}
}
int check_full(char board[ROWS][COLS]) //判断棋盘是否已满
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if (board[i][j] == ' ')
return ' ';
}
}
return 'q';
}
void player_move(char board[ROWS][COLS]) //玩家走
{
int x = 0;
int y = 0;
while(1)
{
printf("玩家下->:");
scanf("%d%d", &x, &y);
if (board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
}
}
void computer_move(char board[ROWS][COLS]) //电脑走
{
int x = 0;
int y = 0;
int ret=check_full(board);
printf("电脑下->:\n");
if(ret == ' ')
{
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
}
char check_win(char board[ROWS][COLS]) //判断输赢
{
int i = 0;
char ret = 0;
for (i = 0; i < ROWS; i++)
{
if ((board[0][i] == board[1][i]) &&
(board[0][i] == board[2][i]) &&
((ret=board[0][i] )!= " "))
return ret;
}
for (i = 0; i < ROWS; i++)
{
if ((board[i][0] == board[i][1]) &&
(board[i][0] == board[i][2]) &&
((ret=board[i][0]) != " "))
return ret;
}
for (i = 0; i < ROWS; i++)
{
if ((board[0][0] == board[1][1]) &&
(board[0][0] == board[2][2]) &&
((ret=board[0][0])!= " "))
return ret;
}
for (i = 0; i < ROWS; i++)
{
if ((board[0][2] == board[1][1]) &&
(board[0][2] == board[2][0]) &&
((ret=board[0][2])!= " "))
return ret;
}
return ' ';
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"016.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("***********************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("***********************\n");
}
void play_game()
{
char board[ROWS][COLS];
int ret = 0;
srand((unsigned)time(NULL));
init_board(board);
print_board(board);
do
{
player_move(board);
print_board(board);
if((check_win(board)) != ' ')
{
ret = check_win(board);
break;
}
if (check_full(board) == 'q')
{
ret = check_full(board);
break;
}
computer_move(board);
print_board(board);
if((check_win(board)) != ' ')
{
ret = check_win(board);
break;
}
}while(check_full(board)!= 'q');
if (ret == 'X')
printf("玩家赢!\n");
if (ret == '0')
printf("电脑赢!\n");
if (ret == 'q')
printf("平局!\n");
}
void test()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case PLAY:
play_game();
break;
case EXIT:
break;
}
}while(input);
}
int main()
{
test();
system("pause");
}