今天分享给大家一个实现三子棋小游戏的代码。希望对大家有所帮助哦!
实现这个小程序大概需要两百多行代码。主要分为初始化棋盘,下棋,判断输赢三个部分。ROW,和COL通过宏来定义,方便在后续修改棋盘的大小。判断输赢时要注意考虑平局的情况。
#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 cherkwin(char board[ROW][COL], int row, int col);
#include"game.h"
void menu()
{
printf("************************\n");
printf("************************\n");
printf("******* 1. play *******\n");
printf("******* 0. exit *******\n");
printf("************************\n");
printf("************************\n");
}
void game()
{
char ret = 0;
//存储为二维数组
char board[ROW][COL];
//初始化棋盘——初始化为空格
InitBoard(board, ROW, COL);
//显示棋盘
DisplayBoard(board, ROW, COL);
while (1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = cherkwin(board,ROW,COL);
if (ret != 'c')
{
break;
}
DisplayBoard(board, ROW, COL);
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = cherkwin(board,ROW,COL);
if (ret != 'c')
{
break;
}
DisplayBoard(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家胜利!\n");
}
if (ret == '#')
{
printf("电脑胜利!\n");
}
if (ret == 'Q')
printf("平局!\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择>>\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();//三子棋游戏
break;
case 0:
printf("退出游戏>>\n");
break;
default:
printf("选择错误,请重新输入>>\n");
break;
}
} while (input);
}
#include"game.h"
#include"game.h"
#include"game.h"
#include"game.h"
#include"game.h"
#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;
for (i = 0; i < row; i++)
{
int j = 0;
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("请输入坐标>>\n");
scanf("%d%d", &x, &y);
if (x >= 1 && y >= 1 && x <= 3 && y <= 3)
{
if (board[x-1][y-1] != ' ')
{
printf("坐标被占用,请重新输入!\n");
}
else
{
board[x-1][y-1] = '*';
break;
}
}
else
printf("坐标非法,请重新输入坐标!\n");
}
}
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑走>>\n");
int x = rand() % row;
int y = rand() % col;
while (1)
{
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//判断输赢
int Isfull(char board[ROW][COL], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
}
char cherkwin(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][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (Isfull(board, row, col) == 1)
{
return 'Q';
}
return 'c';
}