思路
首先是最重要的一级,也就是下一步即可获胜,先己方再对方。 然后是次重要一级,也就是下一步就形成必杀情形。同时要对于这种位置进行数量统计,尽可能选择最多效果的,先己方再对方。 最后是重要性打分。依据一个空点的八个方向情况打分。下在得分高的位置。 可是情况太复杂了,写了八百多行写不下去了,而且错误使用to_string函数,然后爆掉了。 于是先将思路记一下,以后有机会再来调整。
程序(unfinished)
#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
#include <ctime>
using namespace std
;
char player
= 'X';
char computer_player
= 'X';
unsigned step_count
= 0;
vector
<vector
<char>> p_n
;
void def_empty_board(vector
<vector
<char>>& p
)
{
vector
<char> p_n_temp
;
for (size_t i
= 0; i
!= 15; i
++) p_n_temp
.push_back(' ');
for (size_t i
= 0; i
!= 15; i
++) p
.push_back(p_n_temp
);
}
void print_board(vector
<vector
<char>> p
)
{
char ch_15
[15];
for (size_t i
= 0; i
!= 15; i
++) ch_15
[i
] = 65 + i
;
cout
<< "| |0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|" << endl
;
cout
<< "| |1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|" << endl
;
for (size_t i
= 0; i
!= 15; i
++)
{
cout
<< '|' << ch_15
[i
] << '|';
for (size_t j
= 0; j
!= 15; j
++)
cout
<< p
[i
][j
] << '|';
cout
<< endl
;
}
cout
<< endl
;
}
int change_board(vector
<vector
<char>>& p
, string l
)
{
size_t location_letter
, location_number
;
if (l
.size() == 2)
{
if (toupper(l
[0]) >= 65 && l
[1] >= 48)
{
location_letter
= toupper(l
[0]) - 65;
location_number
= l
[1] - 48 - 1;
if (location_letter
> 14 || location_number
> 9) return -1;
}
else return -1;
}
else if (l
.size() == 3)
if (toupper(l
[0]) >= 65 && l
[1] >= 48 && l
[2] >= 48)
{
location_letter
= toupper(l
[0]) - 65;
location_number
= 10 * (l
[1] - 48) + (l
[2] - 48) - 1;
if (location_letter
> 14 || location_number
> 14) return -1;
}
else return -1;
else return -1;
if (p
[location_letter
][location_number
] == ' ')
{
p
[location_letter
][location_number
] = player
;
return 0;
}
else return -1;
}
int win_lose(vector
<vector
<char>> p
, int n
)
{
char c
= 'X';
for (int k
= 1; k
!= 3; k
++)
{
for (size_t i
= 0; i
<= 14; i
++)
{
for (size_t j
= 0; j
<= 10; j
++)
{
if (p
[i
][j
] == c
&& p
[i
][j
+ 1] == c
&& p
[i
][j
+ 2] == c
&& p
[i
][j
+ 3] == c
&& p
[i
][j
+ 4] == c
)
return k
;
}
}
for (size_t i
= 0; i
<= 10; i
++)
{
for (size_t j
= 0; j
<= 14; j
++)
{
if (p
[i
][j
] == c
&& p
[i
+ 1][j
] == c
&& p
[i
+ 2][j
] == c
&& p
[i
+ 3][j
] == c
&& p
[i
+ 4][j
] == c
)
return k
;
}
}
for (size_t i
= 0; i
<= 10; i
++)
{
for (size_t j
= 0; j
<= 10; j
++)
{
if (p
[i
][j
] == c
&& p
[i
+ 1][j
+ 1] == c
&& p
[i
+ 2][j
+ 2] == c
&& p
[i
+ 3][j
+ 3] == c
&& p
[i
+ 4][j
+ 4] == c
)
return k
;
}
}
for (size_t i
= 0; i
<= 10; i
++)
{
for (size_t j
= 4; j
<= 14; j
++)
{
if (p
[i
][j
] == c
&& p
[i
+ 1][j
- 1] == c
&& p
[i
+ 2][j
- 2] == c
&& p
[i
+ 3][j
- 3] == c
&& p
[i
+ 4][j
- 4] == c
)
return k
;
}
}
c
= '0';
}
if (n
== 225) return 3;
else return 0;
}
void game_player_change(char& player
)
{
if (player
== 'X')
player
= '0';
else player
= 'X';
}
string
priority1(vector
<vector
<char>> p
)
{
}
string
priority2(vector
<vector
<char>> p
)
{
}
string
signifacance(vector
<vector
<char>> p
)
{
}
string
computer1(vector
<vector
<char>> p
)
{
}
int main(int argc
, char* argv
[])
{
string location
;
cout
<< "This program is a simple gobang game.\nProgrammer:Teddy van Jerry\n" << endl
;
cout
<< "If your input is illegal, we define it as you choose to PASS.(Or you can type in Pass to pass)" << endl
;
cout
<< "You can type in the location like 'B2' or 'B02', and no whitespace is allowed.\n" << endl
;
cout
<< "Please choose 'Man VS Man'(1) or 'Man VS Computer'(2) : ";
char Man_or_Computer
;
cin
>> Man_or_Computer
;
cout
<< "\nYou go first or the computer? You(1), Computer(2): ";
char You_or_Computer
;
cin
>> You_or_Computer
;
std
::cout
<< endl
;
computer_player
= (You_or_Computer
== '1') ? '0' : 'X';
def_empty_board(p_n
);
print_board(p_n
);
while (win_lose(p_n
, step_count
) == 0)
{
cout
<< "Player " << player
<< ", make your move: ";
if (Man_or_Computer
== 1 or player
!= computer_player
)
cin
>> location
;
else
{
location
= computer1(p_n
);
cout
<< location
<< endl
;
}
if (change_board(p_n
, location
) == 0)
{
cout
<< endl
;
print_board(p_n
);
++step_count
;
}
else cout
<< "Player " << player
<< " choose PASS.\n" << endl
;
game_player_change(player
);
}
switch (win_lose(p_n
, step_count
))
{
case 1:
cout
<< "Congratulations! The winner is X." << endl
;
break;
case 2:
cout
<< "Congratulations! The winner is 0." << endl
;
break;
case 3:
cout
<< "The game ended in a draw." << endl
;
default:
break;
}
if (player
== computer_player
)
cout
<< "Computer won!" << endl
;
else cout
<< "You won!!!" << endl
;
cout
<< "\nALL RIGHTS RESERVED (c) 2020 Teddy van Jerry" << endl
;
return 0;
}
样例输出
a total failure!!!
ALL RIGHTS RESERVED © 2020 Teddy van Jerry 欢迎转载,转载请注明出处。
See also
Teddy van Jerry 的导航页 【C++ 程序】 五子棋游戏(人 VS 人) 【C++ 程序】 井字棋游戏(人 VS 人) 【C++ 程序】 井字棋游戏(人 VS Lv1电脑) 【C++ 程序】 井字棋游戏(人 VS Lv2电脑) 【C++ 程序】 井字棋游戏(人 VS Lv3电脑) 【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版) 【C++ 程序】 移动迷宫游戏 【C++ 程序】 贪吃蛇游戏 【C++ 程序】 数字推盘游戏(15-puzzle) 【C++ 程序】 2048游戏 【C++ 程序】 井字棋游戏(人 VS 人)(EasyX 图形界面) 【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面) 【C++ 程序】 2048游戏(EasyX 图形界面) 【C++ 程序】 贪吃蛇游戏(EasyX 图形界面)