game.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<string.h>
#define ROW 3
#define COL 3
void game();
void menu();
void init_board(char board[ROW][COL],int row,int col);
void print_board(char board[ROW][COL],int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
void player_move(char board[ROW][COL], int row, int col);
int is_full(char board[ROW][COL], int row, int col);
int is_win(char board[ROW][COL], int row, int col);
game.c
#include"game.h"
void init_board(char board[ROW][COL], int row, int col)
{
memset(board, ' ', sizeof(board[0][0])* row*col);
}
void print_board(char board[ROW][COL], 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 != row - 1)
{
printf("---|---|---\n");
}
}
}
void player_move(char board[ROW][COL], int row, int col)
{
int x, y;
printf("请输入你要走的棋子坐标:");
while (1)
{
scanf("%d %d", &x, &y);
if (((x >= 1) && (x <= row)) && ((y >= 1) && (y <= col)))
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
}
}
else
{
printf("非法输入,请重新输入\n");
}
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
int x , y;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
static int is_full(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i <= row; i++)
{
int j = 0;
for (j = 0; j <= col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
int is_win(char board[ROW][COL], int row, int col)
{
int i = 0, j = 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 (j = 0; j < col; j++)
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] != ' ')
{
return board[i][0];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != ' ')
{
return board[0][0];
}
else if(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
else if(is_full(board, ROW, COL) == 1)
{
return 'p';
}
else
{
return' ';
}
}
text.c
#include"game.h"
void menu()
{
printf("*****《三子棋》******\n");
printf("***** 1.play *******\n");
printf("***** 0.exit *******\n");
printf("*********************\n");
printf("请选择\n");
}
void game()
{
char ret;
char board[ROW][COL];
init_board(board, ROW, COL);
print_board(board, ROW, COL);
while (1)
{
printf("玩家走\n");
player_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != ' ')
break;
printf("电脑走\n");
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != ' ')
break;
}
if (ret == '*')
{
printf("玩家获胜\n");
}
else if (ret == '#')
{
printf("很遗憾,你输了\n");
}
else if (ret == 'p')
{
printf("平局");
}
}
int main()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
第一次自己完成的小游戏三子棋
最新推荐文章于 2022-11-17 14:39:15 发布