其实只要理清思路,这个程序还是挺简单的
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void menu()
{
printf("*************************\n");
printf("***** 1.play ******\n");
printf("***** 0.exit ******\n");
printf("*************************\n");
{
printf("*************************\n");
printf("***** 1.play ******\n");
printf("***** 0.exit ******\n");
printf("*************************\n");
}
void game()
{
int input = 1;
char board[ROWS][COLS] = { 0 };
init_board(board, ROWS, COLS);
while (input)
{
menu();
printf("请选择:");
scanf("%d", &input);
if (input == 1)
{
init_board(board, ROWS, COLS);
print_board(board, ROWS, COLS);
play_game(board);
}
void game()
{
int input = 1;
char board[ROWS][COLS] = { 0 };
init_board(board, ROWS, COLS);
while (input)
{
menu();
printf("请选择:");
scanf("%d", &input);
if (input == 1)
{
init_board(board, ROWS, COLS);
print_board(board, ROWS, COLS);
play_game(board);
}
}
}
}
int main()
{
game();
return 0;
}
{
game();
return 0;
}
game.h
#ifndef _GAME_H__
#define _GAME_H__
#define ROWS 3
#define COLS 3
#define _GAME_H__
#define ROWS 3
#define COLS 3
void init_board(char board[ROWS][COLS], int rows, int cols);//初始化数组
void print_board(char board[ROWS][COLS], int rows, int cols);//打印棋盘
void play_game(char board[ROWS][COLS]);//玩游戏
char check_win(char board[ROWS][COLS]);//判断输赢
void player_move(char board[ROWS][COLS]);//玩家走
void com_move(char board[ROWS][COLS]);//电脑走
#endif
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void init_board(char board[ROWS][COLS], int rows, int cols)
{
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
board[i][j] = ' ';
}
}
void print_board(char board[ROWS][COLS], int rows, int cols)
{
int i;
for (i = 0; i < rows; i++)
{
printf(" %C | %C | %C \n", board[i][0], board[i][1], board[i][2]);
printf("---|---|---\n");
}
}
void play_game(char board[ROWS][COLS])
{
int ret;
int count = 0;
while (1)
{
if (count >= 9)
{
printf("平局了\n");
break;
}
player_move(board);
count++;
print_board(board, ROWS, COLS);
if ((ret = check_win(board) == 'p'))
{
printf("你赢了\n");
break;
}
if (count >= 9)
{
printf("平局了\n");
break;
}
com_move(board);
count++;
print_board(board, ROWS, COLS);
if ((ret = check_win(board) == 'p'))
{
printf("你挂了\n");
break;
}
#include "game.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void init_board(char board[ROWS][COLS], int rows, int cols)
{
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
board[i][j] = ' ';
}
}
void print_board(char board[ROWS][COLS], int rows, int cols)
{
int i;
for (i = 0; i < rows; i++)
{
printf(" %C | %C | %C \n", board[i][0], board[i][1], board[i][2]);
printf("---|---|---\n");
}
}
void play_game(char board[ROWS][COLS])
{
int ret;
int count = 0;
while (1)
{
if (count >= 9)
{
printf("平局了\n");
break;
}
player_move(board);
count++;
print_board(board, ROWS, COLS);
if ((ret = check_win(board) == 'p'))
{
printf("你赢了\n");
break;
}
if (count >= 9)
{
printf("平局了\n");
break;
}
com_move(board);
count++;
print_board(board, ROWS, COLS);
if ((ret = check_win(board) == 'p'))
{
printf("你挂了\n");
break;
}
}
}
void player_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
printf("请输入坐标:\n");
scanf("%d %d", &x, &y);
if (board[x - 1][y - 1] = ' ')
{
board[x-1][y-1] = 'x';
return;
}
else
{
printf("输入有误\n");
}
}
void com_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
srand((unsigned)time(NULL));
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[ x ][ y ] = ' ')
board[ x ][ y ] = '0';
return;
}
}
char check_win(char board[ROWS][COLS])
{
int i = 0;
for (i = 0; i < 3; i++)
{
if ((board[i][0] == board[i][1]) &&( board[i][1] == board[i][2] )&& (board[i][0] != ' '))
{
return 'p';
}
}
for (i = 0; i < 3; i++)
{
if((board[0][i] == board[1][i] )&& (board[1][i] == board[2][i] )&& (board[0][i] != ' '))
{
return 'p';
}
}
if ((board[0][0] == board[1][1] )&& (board[1][1] == board[2][2] )&&( board[0][0] != ' '))
{
return 'p';
}
if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0] )&& (board[0][2] != ' '))
{
return 'p';
}
}
}
char check_win(char board[ROWS][COLS])
{
int i = 0;
for (i = 0; i < 3; i++)
{
if ((board[i][0] == board[i][1]) &&( board[i][1] == board[i][2] )&& (board[i][0] != ' '))
{
return 'p';
}
}
for (i = 0; i < 3; i++)
{
if((board[0][i] == board[1][i] )&& (board[1][i] == board[2][i] )&& (board[0][i] != ' '))
{
return 'p';
}
}
if ((board[0][0] == board[1][1] )&& (board[1][1] == board[2][2] )&&( board[0][0] != ' '))
{
return 'p';
}
if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0] )&& (board[0][2] != ' '))
{
return 'p';
}
}