????????三子棋,也称作井字棋或圈圈叉叉(Tic-tac-toe),是一种简单而经典的棋类游戏。这个游戏通常在一个3x3的方格棋盘上进行。两名玩家轮流在空格中放置自己的标记,一个玩家使用圈圈("O"),另一个使用叉叉("X"),目标是先在一条横线、竖线或对角线上连成三个自己的标记。
1.创建菜单menu():选择play或exit游戏
2.初始化棋盘:创建3*3(ROW*COL)的棋盘
3.打印棋盘
4.玩家下棋
5.电脑下棋
6.判断输赢(分四种情况:玩家赢、电脑赢、平局、还未下完)
void menu()
{
printf("***************************\n");
printf("********* 1. PLAY *********\n");
printf("********* 0. EXIT *********\n");
printf("***************************\n");
}
输出结果:
void InitBoard(char board[ROW][COL], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
建立一个ROW=3,?COL=3的数组,并赋予其‘空格’
因为空格较难看出将棋下在何处,因此为棋盘添加边框
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i, j;
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, y;
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] == ' ')
{
board[x - 1][y - 1] = 'o';
break;
}
else
printf("该坐标已被占用!请重新输入->\n");
}
else
printf("非法输入!请重新输入->\n");
}
//输入坐标后打印棋盘
//DisplayBoard(board, ROW, COL);
}
玩家下棋需考虑以下情况:
1 必须下在3*3的棋盘内
2 必须下在‘空格’格子中
3 数组是从0开始,玩家输入坐标从1开始
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;//0~row-1
int y = 0;//0~col-1
printf("电脑下棋\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = 'x';
break;
}
}
//输入坐标后打印棋盘
//DisplayBoard(board, ROW, COL);
}
需要包含头文件#include<stdlib.h>
分4中情况考虑
//玩家赢
//电脑赢
//平局(使用IsFull函数判断棋盘是否下满)
//未分出胜负,继续下
//判断棋盘是否下满
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;
}
如何算赢的定义
1.行3个相同符号
2.列相同符号
3.对角线相同符号
//判断输赢
//输x
//赢o
//平局Q
//继续C
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[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) == 1)
{
return 'Q';
}
//游戏继续
return 'C';
}
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#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);
//判断棋盘是否下满
int IsFull(char board[ROW][COL], int row, int col);
//判断输赢
char IsWin(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
int i, j;
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, j;
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, y;
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] == ' ')
{
board[x - 1][y - 1] = 'o';
break;
}
else
printf("该坐标已被占用!请重新输入->\n");
}
else
printf("非法输入!请重新输入->\n");
}
//输入坐标后打印棋盘
//DisplayBoard(board, ROW, COL);
}
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;//0~row-1
int y = 0;//0~col-1
printf("电脑下棋\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = 'x';
break;
}
}
//输入坐标后打印棋盘
//DisplayBoard(board, ROW, COL);
}
//判断棋盘是否下满
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;
}
//判断输赢
//输x
//赢o
//平局Q
//继续C
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[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) == 1)
{
return 'Q';
}
//游戏继续
return 'C';
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("***************************\n");
printf("********* 1. PLAY *********\n");
printf("********* 0. EXIT *********\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 = 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 == 'o')
{
printf("玩家赢\n");
}
else if (ret == 'x')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
int main()
{
int input;
srand((unsigned int)time(NULL));
menu();
do
{
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戏开始\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}