C语言实现三子棋
Posted 联韵电子厂第一打胶手
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言实现三子棋相关的知识,希望对你有一定的参考价值。
目录
一、打印一个菜单选择是否进入游戏
运行如下
二、创建一个棋盘
创建一个3*3的二维数组作为棋盘,这里之所以能用ROW和COL是因为使用了#define定义了ROW和COL为3,这样方便随时修改。
放入0使棋盘全为0
char board[ROW][COL] = {0};
三、初始化棋盘
这里把所有的元素都赋值为‘ ’,这样更符合人们的常识。
四、打印棋盘
这里我们把棋盘的周围加上边框,这样让棋盘看着更符合审美。
效果图如下
五、下棋,判断输赢
玩家先下,然后再判断玩家输入的坐标,然后电脑下棋(这里的电脑下棋是随机的,使用的rand函数随机生成的,没有智能)。下一步棋判断一次输赢。如果玩家赢了就返回他的棋子(我是用的 ’*‘),电脑赢了就返回他的棋子,如果没人赢而且棋盘也没满的话就返回’C',满了就返回’ Q‘.(个人习惯)。
下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while(1)
{
printf("玩家走:\\n");
printf("请输入坐标:\\n");
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 ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
判断输赢
IsFull(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 IsWin(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 < col; 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[2][1] == board[1][1] && board[1][1] == board[1][2] && board[2][1] != ' ')
return board[2][1];
if (1 == IsFull(board, ROW, COL))
{
return 'Q';
}
return 'C';
}
全部代码
game.h
#pragma once
#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
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 IsWin(char board[ROW][COL], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#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;
while(1)
{
printf("玩家走:\\n");
printf("请输入坐标:\\n");
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 ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
IsFull(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 IsWin(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 < col; 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[2][1] == board[1][1] && board[1][1] == board[1][2] && board[2][1] != ' ')
return board[2][1];
if (1 == IsFull(board, ROW, COL))
{
return 'Q';
}
return 'C';
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("******************\\n");
printf("**1.play**0.exit**\\n");
printf("******************\\n");
}
void game()
{
char ret = 0;
char board[ROW][COL] = {0};
InitBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//打印棋盘
while (1)
{
PlayerMove(board,ROW,COL);
DisplayBoard(board, ROW, COL);
ret=IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret=IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\\n");
}
else if (ret == '#')
{
printf("电脑赢\\n");
}
else
{
printf("平局\\n");
}
}
void test()
{ int n = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &n);
switch (n)
{
case 1:
printf("游戏开始\\n");
game();
break;
case 0:
printf("退出游戏\\n");
break;
default:
printf("输入错误,请重新输入!\\n");
break;
}
} while (n);
}
int main()
{
test();
return 0;
}
以上是关于C语言实现三子棋的主要内容,如果未能解决你的问题,请参考以下文章