游戏实现代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void BoardInit(char board[ROWS][COLS], int row, int col)
{
memset(board, ' ', col * row * sizeof(char)); //将棋盘数组初始化为空格
}
static int is_full(char board[ROWS][COLS], int row, int col) //检查棋盘是否满了
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
if (board[i][j] == ' ')
{
return 0; //有空格,没满
}
}
}
return 1; //没空格,满了
}
char Who_win(char board[ROWS][COLS], 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])) //检查行
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) //检查列
{
return board[0][i];
}
}
if (((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) ||
((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))) //检查对角线
{
return board[0][0];
}
else if (is_full(board, row, col)) //检查棋盘满了没
{
return 'q';
}
return ' ';
}
void DisplayBoard(char board[ROWS][COLS], int row, int col) //打印棋盘
{
int i = 0;
for (i = 0; i < row; i++)
{
printf(" %c | %c | %c \n", board[0][i], board[1][i], board[2][i]);
if (i != 2)
{
printf("---|---|---\n");
}
}
}
void Player_Move(char board[ROWS][COLS], int row, int col) //玩家走
{
unsigned int x = 0, y = 0;
while (1)
{
printf("请输入坐标》:\n");
scanf("%d%d", &x, &y);
x--;
y--;
if (((x < 3) || (y < 3)) && (board[y][x] == ' '))
{
board[y][x] = 'X';
break;
}
printf("输入错误,请重新输入。\n");
}
}
void Computer_Move(char board[ROWS][COLS], int row, int col) //电脑走
{
int max_p = 0; //最大权分,(用于找出权分最大的位置)
int pow = 0; //权分
int move[] = { 0, 0 }; //落子位置
int i, j;
for (i = 0; i < 3; i++) //两个for循环假设电脑下在每一个位置
{
for (j = 0; j < 3; j++)
{
if (board[i][j] == ' ') //该假设位置为空格(无子),开始计算
{ //将棋盘转化为两个棋盘,一维就够了,好处理
char px[9] = { '\0' }; //玩家落子盘
char po[9] = { '\0' }; //电脑已经落子的盘
int k = 0;
for (k = 0; k < 9; k++) //传值玩家落子位置
{
if (*(board[0] + k) == 'X')
{
px[k] = *(board[0] + k);
}
else
{
px[k] = ' ';
}
}
for (k = 0; k < 9; k++) //传值电脑落子位置
{
if (*(board[0] + k) == 'O')
{
po[k] = *(board[0] + k);
}
else
{
po[k] = ' ';
}
}
k = i * 3 + j; //三维棋盘位置转化为一维棋盘位置
px[k] = 'O'; //赋值
po[k] = 'O'; //↓位置权分计算
pow = ((px[0] - ' ' + 1) * (px[1] - ' ' + 1) * (px[2] - ' ' + 1) * (((po[0] == 'O') ^ (po[1] == 'O') ^ (po[2] == 'O'))) +
(px[3] - ' ' + 1) * (px[4] - ' ' + 1) * (px[5] - ' ' + 1) * (((po[3] == 'O') ^ (po[4] == 'O') ^ (po[5] == 'O'))) +
(px[6] - ' ' + 1) * (px[7] - ' ' + 1) * (px[8] - ' ' + 1) * (((po[6] == 'O') ^ (po[7] == 'O') ^ (po[8] == 'O'))) +
(px[0] - ' ' + 1) * (px[3] - ' ' + 1) * (px[6] - ' ' + 1) * (((po[0] == 'O') ^ (po[3] == 'O') ^ (po[6] == 'O'))) +
(px[1] - ' ' + 1) * (px[4] - ' ' + 1) * (px[7] - ' ' + 1) * (((po[1] == 'O') ^ (po[4] == 'O') ^ (po[7] == 'O'))) +
(px[2] - ' ' + 1) * (px[5] - ' ' + 1) * (px[8] - ' ' + 1) * (((po[2] == 'O') ^ (po[5] == 'O') ^ (po[8] == 'O'))) +
(px[0] - ' ' + 1) * (px[4] - ' ' + 1) * (px[8] - ' ' + 1) * (((po[0] == 'O') ^ (po[4] == 'O') ^ (po[8] == 'O'))) +
(px[2] - ' ' + 1) * (px[4] - ' ' + 1) * (px[6] - ' ' + 1) * (((po[2] == 'O') ^ (po[4] == 'O') ^ (po[6] == 'O'))) +
(po[0] - ' ' + 1) * (po[1] - ' ' + 1) * (po[2] - ' ' + 1) * (!((px[0] == 'X') || (px[1] == 'X') || (px[2] == 'X'))) +
(po[3] - ' ' + 1) * (po[4] - ' ' + 1) * (po[5] - ' ' + 1) * (!((px[3] == 'X') || (px[4] == 'X') || (px[5] == 'X'))) +
(po[6] - ' ' + 1) * (po[7] - ' ' + 1) * (po[8] - ' ' + 1) * (!((px[6] == 'X') || (px[7] == 'X') || (px[8] == 'X'))) +
(po[0] - ' ' + 1) * (po[3] - ' ' + 1) * (po[6] - ' ' + 1) * (!((px[0] == 'X') || (px[3] == 'X') || (px[6] == 'X'))) +
(po[1] - ' ' + 1) * (po[4] - ' ' + 1) * (po[7] - ' ' + 1) * (!((px[1] == 'X') || (px[4] == 'X') || (px[7] == 'X'))) +
(po[2] - ' ' + 1) * (po[5] - ' ' + 1) * (po[8] - ' ' + 1) * (!((px[2] == 'X') || (px[5] == 'X') || (px[8] == 'X'))) +
(po[0] - ' ' + 1) * (po[4] - ' ' + 1) * (po[8] - ' ' + 1) * (!((px[0] == 'X') || (px[4] == 'X') || (px[8] == 'X'))) +
(po[2] - ' ' + 1) * (po[4] - ' ' + 1) * (po[6] - ' ' + 1) * (!((px[2] == 'X') || (px[4] == 'X') || (px[6] == 'X'))));
if (pow > max_p) //新的权分高于已有最大权分,交换值,改变落子位置
{
max_p = pow;
move[0] = i;
move[1] = j;
}
}
}
}
board[move[0]][move[1]] = 'O'; //所有假设位置的权分比较结束,落子
}
头文件
```c
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ROWS 3
#define COLS 3
void BoardInit(char board[ROWS][COLS], int row, int col); //初始化棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col); //打印棋盘和棋子
void Player_Move(char board[ROWS][COLS], int row, int col); //玩家走
void Computer_Move(char board[ROWS][COLS], int row, int col); //电脑走
char Who_win(char board[ROWS][COLS], int row, int col); //检查输赢
函数实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game(int f)
{
char board[ROWS][COLS];
char ret = 0;
BoardInit(board, ROWS, COLS); //初始化
DisplayBoard(board, ROWS, COLS); //打印空棋盘
switch (f) //判断谁先走,不需要brek和default
{
case 1:
Player_Move(board, ROWS, COLS);
DisplayBoard(board, ROWS, COLS);
case 2:
Computer_Move(board, ROWS, COLS);
DisplayBoard(board, ROWS, COLS); //开始无需检查输赢
}
while (1)
{
Player_Move(board, ROWS, COLS);
DisplayBoard(board, ROWS, COLS);
system("cls");
if (ret = Who_win(board, ROWS, COLS) != ' ')
{
break;
}
Computer_Move(board, ROWS, COLS); //电脑和玩家走后
DisplayBoard(board, ROWS, COLS); //都打印一次棋盘
if (ret = Who_win(board, ROWS, COLS) != ' ') //并检查输赢,分出胜负结束循环
{
break;
}
}
if (ret == 'X')
{
printf("你赢了\n");
}
if (ret == 'O')
{
printf("hehe\n");
}
if (ret == 'q')
{
printf("平局\n");
}
}
void menu() //菜单
{
printf("*********************************\n");
printf("*********** 三子棋 **********\n");
printf("******* YOU WILLN'T WIN *******\n");
printf("*********** 选择 ***********\n");
printf("**** 1-先走 2-后走 0-退出 *****\n");
printf("*********************************\n");
}
int main()
{
int input = 0;
do
{
menu(); //打印菜单
scanf("%d", &input); //玩家选择
printf("\n");
switch (input) //根据玩家输入,给game函数传参或报错、结束
{
case 0:
break;
case 1:
game(1);
break;
case 2:
game(2);
break;
default:
printf("输入错误,重新输入\n");
break;
}
} while (input); //只要玩家不选0,就接着玩
return 0;
}