三子棋
test,c//测试游戏代码的逻辑
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu();
void game();
int main()
{
srand((unsigned)time(NULL));
int input=0;
do
{
menu();
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;
}
void menu()
{
printf("**********************\n");
printf("******1. 开始*********\n");
printf("******0. 退出*********\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);
ret=Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
//判断输赢
ComputerMove(board, ROW, COL);
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家获胜\n");
}
else if (ret == '#')
{
printf("电脑获胜\n");
}
else
printf("平局\n");
DisplayBoard(board, ROW, COL);
}
1.打印菜单
1.1选择//1开始0退出其他(default)----选择错误
int input=0;
do
{
menu();
primtf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
void menu()
{
printf("**********************\n");
printf("******1. 开始*********\n");
printf("******0. 退出*********\n");
printf("**********************\n");
}
2游戏的实现
2.1初始化与打印棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0, 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++)
{
//打印数据
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
//打印分割
if(i<row-1)
printf("---|---|---\n");
}
}
2.0版本 这时我们改成n*n都没有问题
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0, 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,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");
}
}
}
2.2玩家与电脑下祺
玩家下棋
1首先考虑输入问题,数组时从零开始,我们一般时从1开始,需要在给数组行列时减一
2还要判断输入时的范围
3注意循环
4下过的地方不能再下
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
while (1)
{
printf("玩家请下棋:>\n");
printf("请输入:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 0 && 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)
{
printf("电脑下棋:>\n");
int x = 0, y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
因为要生成随机数使用到srand与rand
srand((unsigned)time(NULL));
2.3判断输赢
判断
1赢的三种情况
行
列
对角线
是不是一样
char Iswin(char board[ROW][COL], int row, int col)
{
int i = 0,j=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][1];
}
//列
for (j= 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] != ' ')
return board[0][j];
}
//对角线
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][1] && board[0][2] != ' ')
{
return board[0][2];
}
if (IsFull(board, row, col))
printf("Q\n");
//游戏继续
return 'C';
}
2平局
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
return 0;
}
}
return 1;
}
game.c//游戏代码的实现
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0, 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++)
// {
// //打印数据
// printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
// //打印分割
// if(i<row-1)
// printf("---|---|---\n");
// }
//}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0,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, y = 0;
while (1)
{
printf("玩家请下棋:>\n");
printf("请输入:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 0 && 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)
{
printf("电脑下棋:>\n");
int x = 0, y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
return 0;
}
}
return 1;
}
char Iswin(char board[ROW][COL], int row, int col)
{
int i = 0,j=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][1];
}
//列
for (j= 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] != ' ')
return board[0][j];
}
//对角线
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][1] && board[0][2] != ' ')
{
return board[0][2];
}
if (IsFull(board, row, col))
printf("Q\n");
//游戏继续
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);
//玩家赢--'*'
//人机赢--‘#’
//平局--‘Q'
//继续--'C'
char Iswin(char board[ROW][COL], int row, int col);
//判断棋盘是否满了
int IsFull(char board[ROW][COL], int row, int col);