今天用c语言写一个三字棋 ,知道数组和循环的有关知识即可完成
成果演示:
代码实现
game.h
#pragma once
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3 // 行
#define COL 3 // 列
// 函数声明
void DisplayBoard(char board[ROW][COL], int row, int col);
void InitBoard(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);
int IsFull(char board[ROW][COL], int row, int col);
game.c
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0;i<row;++i)
{
for (int j = 0;j < col;++j)
{
board[i][j] = ' ';
}
}
//memset(&board[0][0],' ',row*col*sizeof(board[0][0]));
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int 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)
{
int j = 0;
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, y = 0;
printf("玩家下棋:>\n");
while (true)
{
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, y = 0;
printf("电脑下棋\n");
while (true)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[ROW][COL], int row, int col)
{
for (int i = 0;i < row;++i)
{
for (int j = 0;j < col;++j)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
// 判断输赢 玩家赢-'*',电脑赢-'#',平局-'Q',继续-'C'
char IsWin(char board[ROW][COL], int row, int col)
{
for (int 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 (int 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== board[2][2]&&board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
// 判断平局
if (IsFull(board, row, col))
{
return 'Q';
}
else
return 'C';
}
test.c
#include "game.h"
void game()
{
char ret;
// 存放数组需要一个3*3的二维数组
char board[ROW][COL] = { 0 };
// 初始化棋盘
InitBoard(board,ROW,COL);
// 打印棋盘
DisplayBoard(board,ROW,COL);
while (true)
{
// 玩家下棋
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 if('Q'==ret)
printf("平局\n");
}
void menu()
{
printf("********************************\n");
printf("********* 1. play **********\n");
printf("******** 0. exit ********\n");
printf("********************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();// 菜单
printf("请选择:>");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误请重新选择\n");
break;
}
} while (input);
return 0;
}
主要步骤
1.游戏菜单
2.棋盘
在初始化阶段,棋盘中内容用空格填充.
玩家下棋用'*'表示
电脑下棋用'#'表示
当然为了更好的显示棋盘的效果,可以用'_'和'|'来进行修饰一波
3.游戏判断
在每一次下棋过后,就进行游戏判断
判断的4中情况玩家赢,电脑赢,平局,继续.根据每种的情况来进行相关的操作
以上是三字棋的全部内容感谢阅读.