用C语言实现井字棋(人人/AI人机)--完结版
若本文章为原创文章,转载本文时需加上本文原地址
用C语言实现井字棋(人人/AI人机)--完结版
刚好凑够一个月才动身
完结版将之前见到的BUG消灭啦,开心!
现在暂时试不出BUG
源码放在下面,试错视频链接也放在下面
完结版试错(B站)==>地址
旧版源码和思路在CSDN==>地址
因为改的地方只有电脑下棋的逻辑,其他的旧版的文章都有,我就不赘述了
BUG与优化3:
1. 修改了step的计算方法,每个玩家玩完就加一次step
2. 改变了电脑下棋的逻辑,每个玩家玩完之后都跳过这次循环
void play_computer()
{
play_mode = 1; //人机对战模式
player = 1; //玩家先出手 (1为玩家,-1为电脑)
int temp = 1; //1为◆状态,-1为★状态
int x, y; //行、列坐标
menu();
draw();
COM_play_mode:
gotoxy(26, 22);
printf_s("你想设置电脑的下棋模式为防守/进攻为主?(0-1): "); //清空
gotoxy(26, 22);
printf_s("你想设置电脑的下棋模式为防守/进攻为主?(0-1):");
scanf_s("%d", &offence_defence);
if (leagality_check(offence_defence, 0, 1) == 0) goto COM_play_mode;
gotoxy(55, 12);
if (offence_defence == 0) printf_s("当前电脑下棋模式:防守");
else printf_s("当前电脑下棋模式:进攻");
while (1)
{
if (win_check() != 0) break;
if (step < 9)
{
gotoxy(20, 22);
printf_s(" ");
/*-------------------打印当前棋子信息-----------------------*/
gotoxy(54, 9);
printf_s("╔ ═ ╗");
gotoxy(54, 11);
printf_s("╚ ═ ╝");
gotoxy(55, 10);
if (temp == 1) printf_s("当前棋子:◆");
if (temp == -1) printf_s("当前棋子:★");
/*----------------------------------------------------------*/
com_play:
/*先清空原先输入的坐标*/
gotoxy(54, 17);
printf_s("请输入行坐标(1-3): ");
gotoxy(54, 18);
printf_s("请输入列坐标(1-3): ");
gotoxy(26, 22);
printf_s(" ");
/*----------------------------------------------------------*/
if (player == 1)
{
gotoxy(54, 17);
printf_s("请输入行坐标(1-3):");
scanf_s("%d", &x);
if (leagality_check(x, 1, 3) == 0) goto com_play;
gotoxy(54, 18);
printf_s("请输入列坐标(1-3):");
scanf_s("%d", &y);
if (leagality_check(y, 1, 3) == 0) goto com_play;
if (board[x - 1][y - 1] == 0)
{
board[x - 1][y - 1] = temp;
update();
temp *= -1;
}
else
{
gotoxy(26, 22);
printf_s("该坐标已有棋,请重下!");
getchar();
getchar(); //让玩家看得到信息
goto com_play;
}
++step;
player *= -1; //玩家先出手 (1为玩家,-1为电脑)
continue;
}
if (player == -1)
{
switch (weight_and_choose())
{
case 1: if (board[0][0] == 0) board[0][0] = -1; break;
case 2: if (board[0][1] == 0) board[0][1] = -1; break;
case 3: if (board[0][2] == 0) board[0][2] = -1; break;
case 4: if (board[1][0] == 0) board[1][0] = -1; break;
case 5: if (board[1][1] == 0) board[1][1] = -1; break;
case 6: if (board[1][2] == 0) board[1][2] = -1; break;
case 7: if (board[2][0] == 0) board[2][0] = -1; break;
case 8: if (board[2][1] == 0) board[2][1] = -1; break;
case 9: if (board[2][2] == 0) board[2][2] = -1; break;
}
/*------------单纯为了好玩,让玩家不会觉得自己孤独-------------*/
gotoxy(23, 22);
printf_s("电脑正在思考,请输入任意键以继续");
getchar();
getchar();
update();
temp *= -1;
player *= -1;
++step;
continue;
}
}
}
switch (win_check())
{
case 1: MessageBox(NULL, TEXT("你真棒,你获得胜利!"), TEXT("输赢判断"), MB_OK); break;
case 2: MessageBox(NULL, TEXT("电脑真棒,电脑获得胜利!"), TEXT("输赢判断"), MB_OK); break;
case 3: MessageBox(NULL, TEXT("平局!!!"), TEXT("输赢判断"), MB_OK); break;
}
}
其实看起来还是没什么差别,但是逻辑改变之后调试变得很快乐,(●'◡'●)
源码:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h> //随机数用
/*
project name: 井字棋(Tic-Tac-Toe)
author name: dragonet
date: 2020/7/15——2020/8/15(版本3)
*/
/*
BUG与优化1:
1. 更名weight_calculating为weight_and_choose,并将原先的权重计算与选择函数合并
2. 取消原先的权重计算方法,(效率过低),现改为直接堵门或者直接三连,如果都不行就直接随机数
3. 增加times全局变量记录同行、列、对角线上同种棋的个数,将原先两种棋在一起判断改为分开判断,增加效率
4. 选择下棋逻辑,根据玩家输入的电脑下棋模式,优先选择堵门或者三连O(∩_∩)O
BUG与优化2:
1. 修改了step的计算方法
2. 改变了电脑下棋的逻辑
*/
/*全局变量区*/
int board[3][3] = {0}; //0代表空,1代表◆,-1代表★
int player; //1代表玩家、-1代表电脑
int play_mode = 0; //0代表自娱自乐,1代表人机对战
int step = 0; //走的步数
int offence_defence; //电脑的下棋模式选择为进攻还是防守,0为防守,1为进攻
int times = 0;
/*函数声明区*/
void gotoxy(int x, int y); //改变下个语句的输入、输出位置
void menu(); //讲解规则,开始界面
void draw(); //打印棋盘面板
void update(); //更新棋盘
int leagality_check(int t, int minnum, int maxnum); //输入合法性检查,主要判断数字是否在范围内
void play_own(); //自娱自乐玩法
void play_computer(); //人机对战玩法
int weight_and_choose(); //权重计算
int win_check(); //输赢判断,0为无法判断,1为◆赢,2为★赢,3为平局
int main()
{
system("mode con cols=90 lines=30");
int n = 0;
Mode_choice:
printf_s("请输入想要玩的模式(1代表自娱自乐,2代表人机对战):");
scanf_s("%d", &n);
if (leagality_check(n, 1, 2) == 0) goto Mode_choice;
gotoxy(0, 0);
if (n == 2)
{
for (n = 0; n < 200; ++n)
printf_s(" \n"); //清空所有
play_computer();
}
else
{
for (n = 0; n < 200; ++n)
printf_s(" \n"); //清空所有
play_own();
}
gotoxy(23, 22);
system("pause");
return 0;
}
void menu()
{
gotoxy(23, 2);
printf_s("欢迎来玩井字游戏(Tic-Tac-Toe)!");
gotoxy(30, 3);
printf_s("下面开始介绍规则");
gotoxy(10, 4);
printf_s("1. 赢局:任意行、列、对角线先占满三个同种棋,则该种棋的玩家赢");
gotoxy(10, 5);
printf_s("2. 输局:只存在于人机对战,任意行、列、对角线被电脑先占满三个棋");
gotoxy(10, 6);
printf_s("3. 平局:全部格子都已被占满且未分出胜负");
gotoxy(10, 7);
printf_s("4. 默认都是玩家先出手,且棋类型为◆");
}
void draw()
{
gotoxy(26, 8);
printf_s("┏━━━━━━━┳━━━━━━━┳━━━━━━━┓");
gotoxy(26, 9);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 10);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 11);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 12);
printf_s("┣━━━━━━━╋━━━━━━━╋━━━━━━━┫");
gotoxy(26, 13);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 14);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 15);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 16);
printf_s("┣━━━━━━━╋━━━━━━━╋━━━━━━━┫");
gotoxy(26, 17);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 18);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 19);
printf_s("┃ ┃ ┃ ┃");
gotoxy(26, 20);
printf_s("┗━━━━━━━┻━━━━━━━┻━━━━━━━┛");
gotoxy(26, 21);
printf_s("注:输错的话要重输的喔");
}
void gotoxy(int x, int y) {
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
void update()
{
int xp, yp;
for (int i = 0; i < 3; ++i)
{
yp = 10 + (i * 4);
for (int j = 0; j < 3; ++j)
{
xp = 30 + (j * 8);
gotoxy(xp, yp);
if (board[i][j] == 0) printf_s(" ");
if (board[i][j] == 1) printf_s("◆");
if (board[i][j] == -1) printf_s("★");
printf_s("\t");
}
printf_s("\n");
}
}
int leagality_check(int t, int minnum, int maxnum)
{
for (int i = minnum; i <= maxnum; ++i) if (t == i) return 1;
return 0;
}
void play_own()
{
player = 1; //从头到尾都是玩家
play_mode = 0; //自娱自乐模式
int temp = 1; //1为◆状态,-1为★状态
int x, y; //行、列坐标
menu(); //介绍面板
draw(); //初始化棋盘面板
while (win_check() == 0)
{
++step; //步数增加
/*-----------------打印当前棋子信息-----------------------*/
gotoxy(54, 9);
printf_s("╔ ═ ╗");
gotoxy(54, 11);
printf_s("╚ ═ ╝");
gotoxy(55, 10);
if (temp == 1) printf_s("当前棋子:◆");
if (temp == -1) printf_s("当前棋子:★");
/*----------------------------------------------------------*/
own_play:
/*先清空原先输入的坐标*/
gotoxy(54, 17);
printf_s("请输入行坐标(1-3): ");
gotoxy(54, 18);
printf_s("请输入列坐标(1-3): ");
gotoxy(26, 22);
printf_s(" ");
/*----------------------------------------------------------*/
gotoxy(54, 17);
printf_s("请输入行坐标(1-3):");
scanf_s("%d", &x);
if (leagality_check(x, 1, 3) == 0) goto own_play;
gotoxy(54, 18);
printf_s("请输入列坐标(1-3):");
scanf_s("%d", &y);
if (leagality_check(y, 1, 3) == 0) goto own_play;
if (board[x - 1][y - 1] == 0)
{
board[x - 1][y - 1] = temp;
update();
temp *= -1;
}
else
{
gotoxy(26,22);
printf_s("该坐标已有棋,请重下!");
getchar();
getchar(); //让玩家看得到信息
goto own_play;
}
}
switch (win_check())
{
case 1: MessageBox(NULL, TEXT("恭喜◆方胜利"), TEXT("输赢判断"), MB_OK); break;
case 2: MessageBox(NULL, TEXT("恭喜★方胜利"), TEXT("输赢判断"), MB_OK); break;
case 3: MessageBox(NULL, TEXT("平局!!!"), TEXT("输赢判断"), MB_OK); break;
}
}
void play_computer()
{
play_mode = 1; //人机对战模式
player = 1; //玩家先出手 (1为玩家,-1为电脑)
int temp = 1; //1为◆状态,-1为★状态
int x, y; //行、列坐标
menu();
draw();
COM_play_mode:
gotoxy(26, 22);
printf_s("你想设置电脑的下棋模式为防守/进攻为主?(0-1): "); //清空
gotoxy(26, 22);
printf_s("你想设置电脑的下棋模式为防守/进攻为主?(0-1):");
scanf_s("%d", &offence_defence);
if (leagality_check(offence_defence, 0, 1) == 0) goto COM_play_mode;
gotoxy(55, 12);
if (offence_defence == 0) printf_s("当前电脑下棋模式:防守");
else printf_s("当前电脑下棋模式:进攻");
while (1)
{
if (win_check() != 0) break;
if (step < 9)
{
gotoxy(20, 22);
printf_s(" ");
/*-------------------打印当前棋子信息-----------------------*/
gotoxy(54, 9);
printf_s("╔ ═ ╗");
gotoxy(54, 11);
printf_s("╚ ═ ╝");
gotoxy(55, 10);
if (temp == 1) printf_s("当前棋子:◆");
if (temp == -1) printf_s("当前棋子:★");
/*----------------------------------------------------------*/
com_play:
/*先清空原先输入的坐标*/
gotoxy(54, 17);
printf_s("请输入行坐标(1-3): ");
gotoxy(54, 18);
printf_s("请输入列坐标(1-3): ");
gotoxy(26, 22);
printf_s(" ");
/*----------------------------------------------------------*/
if (player == 1)
{
gotoxy(54, 17);
printf_s("请输入行坐标(1-3):");
scanf_s("%d", &x);
if (leagality_check(x, 1, 3) == 0) goto com_play;
gotoxy(54, 18);
printf_s("请输入列坐标(1-3):");
scanf_s("%d", &y);
if (leagality_check(y, 1, 3) == 0) goto com_play;
if (board[x - 1][y - 1] == 0)
{
board[x - 1][y - 1] = temp;
update();
temp *= -1;
}
else
{
gotoxy(26, 22);
printf_s("该坐标已有棋,请重下!");
getchar();
getchar(); //让玩家看得到信息
goto com_play;
}
++step;
player *= -1; //玩家先出手 (1为玩家,-1为电脑)
continue;
}
if (player == -1)
{
switch (weight_and_choose())
{
case 1: if (board[0][0] == 0) board[0][0] = -1; break;
case 2: if (board[0][1] == 0) board[0][1] = -1; break;
case 3: if (board[0][2] == 0) board[0][2] = -1; break;
case 4: if (board[1][0] == 0) board[1][0] = -1; break;
case 5: if (board[1][1] == 0) board[1][1] = -1; break;
case 6: if (board[1][2] == 0) board[1][2] = -1; break;
case 7: if (board[2][0] == 0) board[2][0] = -1; break;
case 8: if (board[2][1] == 0) board[2][1] = -1; break;
case 9: if (board[2][2] == 0) board[2][2] = -1; break;
}
/*------------单纯为了好玩,让玩家不会觉得自己孤独-------------*/
gotoxy(23, 22);
printf_s("电脑正在思考,请输入任意键以继续");
getchar();
getchar();
update();
temp *= -1;
player *= -1;
++step;
continue;
}
}
}
switch (win_check())
{
case 1: MessageBox(NULL, TEXT("你真棒,你获得胜利!"), TEXT("输赢判断"), MB_OK); break;
case 2: MessageBox(NULL, TEXT("电脑真棒,电脑获得胜利!"), TEXT("输赢判断"), MB_OK); break;
case 3: MessageBox(NULL, TEXT("平局!!!"), TEXT("输赢判断"), MB_OK); break;
}
}
int weight_and_choose()
{
int n, i, j, k, randnum;
if (offence_defence == 0) //优先堵门
{
//对◆棋的判断(堵门!)
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
{
if (board[i][0] == 1 || board[i][1] == 1 || board[i][2] == 1) //行判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[i][n] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[i][k] == 0) return 3 * i + k + 1;
}
}
}
if (board[0][i] == 1 || board[1][i] == 1 || board[2][i] == 1) //列判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][i] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][i] == 0) return 3 * k + i + 1;
}
}
}
if (i == 0 && j == 0 || i == 0 && j == 2 || i == 1 && j == 1 || i == 2 && j == 0 || i == 2 && j == 2) //对角线判断
{
if (board[0][0] == 1 || board[1][1] == 1 || board[2][2] == 1) //正对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][n] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][k] == 0) return 3 * k + k + 1;
}
}
}
if (board[0][2] == 1 || board[1][1] == 1 || board[2][0] == 1) //副对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[0 + n][2 - n] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][2 - k] == 0) return 3 * (k + 1) - k;
}
}
}
}
}
//对★的判断(三连)
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
{
if (board[i][0] == -1 || board[i][1] == -1 || board[i][2] == -1) //行判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[i][n] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[i][k] == 0) return 3 * i + k + 1;
}
}
}
if (board[0][i] == -1 || board[1][i] == -1 || board[2][i] == -1) //列判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][i] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][i] == 0) return 3 * k + i + 1;
}
}
}
if (i == 0 && j == 0 || i == 0 && j == 2 || i == 1 && j == 1 || i == 2 && j == 0 || i == 2 && j == 2) //对角线判断
{
if (board[0][0] == -1 || board[1][1] == -1 || board[2][2] == -1) //正对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][n] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][k] == 0) return 3 * k + k + 1;
}
}
}
if (board[0][2] == -1 || board[1][1] == -1 || board[2][0] == -1) //副对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[0 + n][2 - n] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][2 - k] == 0) return 3 * (k + 1) - k;
}
}
}
}
}
}
if (offence_defence == 1) //优先三连
{
//对★的判断(三连)
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
{
if (board[i][0] == -1 || board[i][1] == -1 || board[i][2] == -1) //行判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[i][n] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[i][k] == 0) return 3 * i + k + 1;
}
}
}
if (board[0][i] == -1 || board[1][i] == -1 || board[2][i] == -1) //列判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][i] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][i] == 0) return 3 * k + i + 1;
}
}
}
if (i == 0 && j == 0 || i == 0 && j == 2 || i == 1 && j == 1 || i == 2 && j == 0 || i == 2 && j == 2) //对角线判断
{
if (board[0][0] == -1 || board[1][1] == -1 || board[2][2] == -1) //正对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][n] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][k] == 0) return 3 * k + k + 1;
}
}
}
if (board[0][2] == -1 || board[1][1] == -1 || board[2][0] == -1) //副对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[0 + n][2 - n] == -1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][2 - k] == 0) return 3 * (k + 1) - k;
}
}
}
}
}
//对◆棋的判断(堵门!)
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
{
if (board[i][0] == 1 || board[i][1] == 1 || board[i][2] == 1) //行判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[i][n] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[i][k] == 0) return 3 * i + k + 1;
}
}
}
if (board[0][i] == 1 || board[1][i] == 1 || board[2][i] == 1) //列判断
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][i] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][i] == 0) return 3 * k + i + 1;
}
}
}
if (i == 0 && j == 0 || i == 0 && j == 2 || i == 1 && j == 1 || i == 2 && j == 0 || i == 2 && j == 2) //对角线判断
{
if (board[0][0] == 1 || board[1][1] == 1 || board[2][2] == 1) //正对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[n][n] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][k] == 0) return 3 * k + k + 1;
}
}
}
if (board[0][2] == 1 || board[1][1] == 1 || board[2][0] == 1) //副对角线
{
times = 0;
for (n = 0; n < 3; ++n)
{
if (board[0 + n][2 - n] == 1)
{
++times;
if (times == 2)
for (k = 0; k < 3; ++k)
if (board[k][2 - k] == 0) return 3 * (k + 1) - k;
}
}
}
}
}
}
//如果前两个没有能成功下棋,那就直接随机数下棋,不整花里胡哨的了
Suiji:
if (1)
{
time_t t;
srand((unsigned)time(&t));
randnum = rand() % 10 + 1; //1-9随机数
switch (randnum)
{
case 1:if (board[0][0] == 0) return randnum; else goto Suiji; break;
case 2:if (board[0][1] == 0) return randnum; else goto Suiji; break;
case 3:if (board[0][2] == 0) return randnum; else goto Suiji; break;
case 4:if (board[1][0] == 0) return randnum; else goto Suiji; break;
case 5:if (board[1][1] == 0) return randnum; else goto Suiji; break;
case 6:if (board[1][2] == 0) return randnum; else goto Suiji; break;
case 7:if (board[2][0] == 0) return randnum; else goto Suiji; break;
case 8:if (board[2][1] == 0) return randnum; else goto Suiji; break;
case 9:if (board[2][2] == 0) return randnum; else goto Suiji; break;
}
}
}
int win_check()
{
if (play_mode == 0) //自娱自乐模式下的判断
{
int i;
/*赢局的情况(◆与★)*/
for (i = 0; i < 3; ++i) //行
if (board[i][0] == 1 && board[i][1] == 1 && board[i][2] == 1 || board[i][0] == -1 && board[i][1] == -1 && board[i][2] == -1) break; //穷举行
if(board[i][0] == 1 && board[i][1] == 1 && board[i][2] == 1) return 1;
if(board[i][0] == -1 && board[i][1] == -1 && board[i][2] == -1) return 2;
/*-----------------------------*/
for (i = 0; i < 3; ++i) //列
if (board[0][i] == 1 && board[1][i] == 1 && board[2][i] == 1 || board[0][i] == -1 && board[1][i] == -1 && board[2][i] == -1) break; //穷举列
if (board[0][i] == 1 && board[1][i] == 1 && board[2][i] == 1) return 1;
if (board[0][i] == -1 && board[1][i] == -1 && board[2][i] == -1) return 2;
/*-----------------------------*/
//if (board[0][0]==board[1][1]==board[2][2] && board[0][0]!=0 && board[1][1]!=0 && board[2][2]!=0 || board[0][2]==board[1][1]==board[2][0] && board[0][2] != 0 && board[1][1] != 0 && board[2][0] != 0) //对角线
if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1 || board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1 || board[0][0] == -1 && board[1][1] == -1 && board[2][2] == -1 || board[0][2] == -1 && board[1][1] == -1 && board[2][0] == -1 ) //穷举的对角线
{
if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1 || board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1) return 1;
if (board[0][0] == -1 && board[1][1] == -1 && board[2][2] == -1 || board[0][2] == -1 && board[1][1] == -1 && board[2][0] == -1) return 2;
}
/*平局的情况*/
if (step == 9) return 3;
}
if (play_mode == 1) //人机对战模式下的判断
{
int i;
/*赢局、输局的情况(◆与★)*/
for (i = 0; i < 3; ++i) //行
if (board[i][0] == 1 && board[i][1] == 1 && board[i][2] == 1 || board[i][0] == -1 && board[i][1] == -1 && board[i][2] == -1) break; //穷举行
if (i == 3) --i;
if (board[i][0] == 1 && board[i][1] == 1 && board[i][2] == 1) return 1;
if (board[i][0] == -1 && board[i][1] == -1 && board[i][2] == -1) return 2;
/*-----------------------------*/
for (i = 0; i < 3; ++i) //列
if (board[0][i] == 1 && board[1][i] == 1 && board[2][i] == 1 || board[0][i] == -1 && board[1][i] == -1 && board[2][i] == -1) break; //穷举列
if (i == 3) --i;
if (board[0][i] == 1 && board[1][i] == 1 && board[2][i] == 1) return 1;
if (board[0][i] == -1 && board[1][i] == -1 && board[2][i] == -1) return 2;
/*-----------------------------*/
//if (board[0][0]==board[1][1]==board[2][2] && board[0][0]!=0 && board[1][1]!=0 && board[2][2]!=0 || board[0][2]==board[1][1]==board[2][0] && board[0][2] != 0 && board[1][1] != 0 && board[2][0] != 0) //对角线
if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1 || board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1 || board[0][0] == -1 && board[1][1] == -1 && board[2][2] == -1 || board[0][2] == -1 && board[1][1] == -1 && board[2][0] == -1) //穷举的对角线
{
if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1 || board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1) return 1;
if (board[0][0] == -1 && board[1][1] == -1 && board[2][2] == -1 || board[0][2] == -1 && board[1][1] == -1 && board[2][0] == -1) return 2;
}
/*平局的情况*/
if (step == 9) return 3;
}
return 0;
}
用C语言实现井字棋(人人/AI人机)--完结版的更多相关文章
- 井字棋小游戏(C语言)
最近沉迷于<NetHack>.<DCSS>等字符游戏,对其很感兴趣,于是用C语言写了个字符界面的井字棋小游戏.欢迎大家指教. 编写时遇到了一些问题,我原先准备用循环,直到读取到 ...
- 程序设计入门—Java语言 第五周编程题 2井字棋(5分)
2 井字棋(5分) 题目内容: 嗯,就是视频里说的那个井字棋.视频里说了它的基本思路,现在,需要你把它全部实现出来啦. 你的程序先要读入一个整数n,范围是[3,100],这表示井字棋棋盘的边长.比如n ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- quick cocos2d-x 入门---井字棋
学习quick cocos2d-x 第二天 ,使用quick-x 做了一个井字棋游戏 . 我假设读者已经 http://wiki.quick-x.com/doku.php?id=zh_cn阅读了这个链 ...
- [C++] 井字棋游戏源码
TicTac.h #define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual B ...
- [游戏学习22] MFC 井字棋 双人对战
>_<:太多啦,感觉用英语说的太慢啦,没想到一年做的东西竟然这么多.....接下来要加速啦! >_<:注意这里必须用MFC和前面的Win32不一样啦! >_<:这也 ...
- TicTacToe井字棋 by reinforcement learning
对于初学强化学习的同学,数学公式也看不太懂, 一定希望有一些简单明了的代码实现加强对入门强化学习的直觉认识,这是一篇初级入门代码, 希望能对你们开始学习强化学习起到基本的作用. 井字棋具体玩法参考百度 ...
- 井字棋(Tic-Tac-Toe)
井字棋介绍:https://en.wikipedia.org/wiki/Tic-tac-toe 井字棋简单,但是获胜策略却和直觉不同,四角比中间重要性要高,而且先手有很大的获胜概率获胜(先手胜:91, ...
随机推荐
- CCF201503-2数字排序
问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输入格式 输入的第一行包含一个整数n,表示给定数字的个数. 第二行包含n个整数,相邻的整数之间用一个空格分隔,表示所 ...
- 小程序拿checkbox的checked属性
方法一.checkbox <checkbox class="round red" bindtap="checkboxChange" checked=&q ...
- Java连接数据库报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
解决方案 连接Mysql报错 The last packet sent successfully to the server was 0 milliseconds ago. The driver ha ...
- c++对c的拓展_编译检测的增强
一:全局变量检测增强:c++编译对全局变量的声明定义有严格区别 //c中全局变量的声明及定义 //c++中全局变量的声明及定义 //c++全局变量写成下列形式则报错 in ...
- FinClip 黑客马拉松正式开赛,码力集结,等你来战!
从2017到2022,小程序已经走过了5年的光景.从无人问津到互联网巨头纷纷入局,短短数年间,小程序已然发展成为超级 App 的标配!微信.支付宝.百度.抖音.今日头条--这些超级app的背后都有巨量 ...
- JavaScript基础第01天笔记
JavaScript基础第01天 1 - 编程语言 1.1 编程 编程: 就是让计算机为解决某个问题而使用某种程序设计语言编写程序代码,并最终得到结果的过程. 计算机程序: 就是计算机所执行的一系列的 ...
- (转)Angular中的拦截器Interceptor
什么是拦截器? 异步操作 例子 Session 注入(请求拦截器) 时间戳(请求和响应拦截器) 请求恢复 (请求异常拦截) Session 恢复 (响应异常拦截器) 转之:http://my.osch ...
- java class 文件格式解析
前言 大约5年前,想研究javaassistant,cglib等字节码操作的相关类库,来对class进行增强,当要到要操作字节码的时候,发现无法继续下去了,只能放弃. 学习jvm字码,需要理解clas ...
- 教你轻松解决CSRF跨站请求伪造攻击
摘要:CSRF(Cross-site request forgery)跨站请求伪造,通过伪装来自受信任用户的请求来利用受信任的网站.与XSS攻击相比,CSRF攻击往往不大流行(因此对其进行防范的资源也 ...
- C++逆向 可变参数Hook
目录 C++逆向 可变参数Hook 0x00 前言: 0x01 C++可变参数: 可变参数简介 可变参数代码实战 0x02 逆向分析C++可变参数原理 0x03 printf Hook实战 Pwn菜鸡 ...