简易三子棋实现
三子棋
三子棋描述
三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。
完成游戏的逻辑
#include <stdio.h>
#include"game.h"
void menu()
{
printf("********************************************\n");
printf("******** 1. play *********\n");
printf("******** 0. exit *********\n");
printf("********************************************\n");
}
void game()
{
//存储数据,创建数组
char board[ROW][COL];
//初始化棋盘 初始化为元素是空格
InitBoard(board, ROW, COL);
//打印棋盘-本质是在打印数组
DisplayBoard(board, ROW, COL);
char ret = 0;//接收游戏状态
while (1)
{
//玩家下棋
Playermove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断玩家赢
ret = Iswinng(board ,ROW,COL);
if (ret != 'C')
break;
//电脑下棋
Computermove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = Iswinng(board, ROW, COL);
if (ret != 'C')
break;
//判断电脑赢
}
if (ret == '*')
{
printf("玩家赢得本场游戏胜利!\n");
}
else if (ret == '#')
{
printf("电脑赢得本场游戏胜利!\n");
}
else
{
printf("平局\n");
}
DisplayBoard(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>>:");
scanf("%d", &input);
switch (input)
{
case 1:
game();//game函数在函数内部完成游戏的实现
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择");
break;
}
} while (input);
return 0;
}
关于游戏相关函数申明
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//符号的定义
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘的函数
void DisplayBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void Playermove(char board[ROW][COL], int row, int col);
//电脑下棋
void Computermove(char board[ROW][COL], int row, int col);
//判断输赢
char Iswinng(char board[ROW][COL], int row, int col);
//玩家赢--返回*
//电脑赢--返回#
//平局--返回Q
//游戏继续--返回C
//判断棋盘是否满了
int Isfull(char board[ROW][COL], int row, int col);
游戏相关函数的实现
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)//初始化棋盘
{
int i = 0;
int j = 0;
for (i = 0; i <= row; i++)
{
for (j = 0; j <= col; j++)
{
board[i][j] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)//打印棋盘
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void Playermove(char board[ROW][COL], int row, int col)//玩家落子
{
int x = 0;
int y = 0;
printf("玩家落子:\n");
while (1)
{
printf("请输入落子坐标");
scanf("%d %d", &x, &y);
//判断坐标合法性
if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
{
if (board[x - 1][y - 1] == ' ')//玩家输入的1 1相当于数组里的0 0
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新选择坐标\n");
}
}
else
{
printf("坐标输入错误,请重新输入\n");
}
}
}
void Computermove(char board[ROW][COL], int row, int col)//电脑落子
{
printf("电脑落子:\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
//判断占用
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int Isfor(char board[ROW][COL], 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 0;//棋盘没满
}
}
}
return 1;
}
简单判断三子相连,游戏输赢
char Iswinng(char board[ROW][COL], 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 < col; 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[0][2] == board[1][1] && board[1][1] == board[2][1] && board[1][1] != ' ')
{
return board[1][1];
}
//判断平局
//如果棋盘满了,返回1,不满返回0;
int ret = Isfor(board, row, col);
if (ret == 1)
{
return 'Q';
}
return 'C';
}