整个游戏可以分为以下几个环节
1.打印一个玩游戏的菜单
2.玩游戏
(1)玩家走一步
(2)电脑走一步
每走一步对结果进行显示,其中游戏的结果可分为玩家赢,电脑赢,以及平局
代码显示如下
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include"game.h"
void menu()
{
printf ("******************************************\n" );
printf ("****************0.退出**************************\n" );
printf ("***************1.玩游戏*************************\n" );
}
void display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0 ;
int j = 0 ;
for (i = 0 ; i < row; ++i)
{
printf (" %c | %c | %c |\n" , board[i][0 ], board[i][1 ], board[i][2 ]);
if (2 != i)
{
printf ("---|---|---|---\n" );
}
}
}
void init_boad(char board[ROWS][COLS], int row, int col)
{
memset (board, ' ' , col*row*sizeof (char ));
}
void player_move(char board[ROWS][COLS], int row, int col)
{
while (1 )
{
int x = 0 ;
int y = 0 ;
printf ("请输入坐标>\n" );
scanf ("%d,%d" , &x, &y);
--x;
--y;
if (x >= 0 && x < col)
{
if (' ' == board[x][y])
{
board[x][y] = 'X' ;
break ;
}
}
else
{
printf ("输入坐标非法,请重新输入\n" );
}
}
}
void computer_move(char board[ROWS][COLS], int row, int col)
{
srand((int )time(NULL));
while (1 )
{
int x = 0 ;
int y = 0 ;
x = rand()%3 ;
y = rand()%3 ;
if (' ' == board[x][y])
{
board[x][y] = '0' ;
break ;
}
}
}
char is_full(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 ' ' ;
}
}
return 'f' ;
}
char check_with(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][1 ] != ' ' )
return board[i][1 ];
}
for (i = 0 ; i < row; ++i)
{
if (board[0 ][i] == board[1 ][i] && board[1 ][i] == board[2 ][i] && board[1 ][i] != ' ' )
return board[1 ][i];
}
if (board[0 ][0 ] == board[1 ][1 ] && board[1 ][1 ] == board[2 ][2 ] && board[1 ][1 ] != ' ' )
{
return board[1 ][1 ];
}
if (board[2 ][0 ] == board[1 ][1 ] && board[1 ][1 ] == board[0 ][2 ] && board[1 ][1 ] != ' ' )
{
return board[1 ][1 ];
}
char full = '0' ;
full = is_full(board, ROWS, COLS);
if ('f' == full)
{
return 'f' ;
}
return ' ' ;
}
void game()
{
char board[ROWS][COLS] = { 0 };
init_boad(board, ROWS, COLS);
display_board(board, ROWS, COLS);
char ret = 0 ;
while (1 )
{
player_move(board, ROWS, COLS);
ret = check_with(board, ROWS, COLS);
if (ret != ' ' )
{
break ;
}
computer_move(board, ROWS, COLS);
if (ret != ' ' )
{
break ;
}
display_board(board, ROWS, COLS);
}
if ('X' == ret)
{
printf ("玩家赢\n" );
}
else if ('0' == ret)
{
printf ("电脑赢\n" );
}
else
{
printf ("平局\n" );
}
display_board(board, ROWS, COLS);
}
work.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"game.h"
int main()
{
int input = 0 ;
do
{
menu();
printf ("请选择>\n" );
scanf ("%d" , &input);
switch (input)
{
case 0 :
break ;
case 1 :
game();
break ;
default :
printf ("输入错误,请重新输入\n" );
break ;
}
}
while (input);
return 0 ;
}
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ROWS 3
#define COLS 3
void menu();
void game();
void init_boad(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 check_with(char board[ROWS][COLS], int row, int col);
char is_full(char board[ROWS][COLS], int row, int col);
#endif
代码有的地方可能有的地方写的还不够完善,如过有大佬觉得哪里可以进一步改善的话可以给我提出来,我们共同学习,共同进步.