- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 python 开发一个 tic-tac-toe 程序。现在,轮到人类了,一切顺利。然而,AI 在玩完第一个回合后,不会再玩任何后续回合。我扫描了代码,似乎找不到任何可能导致此问题的错误。
请忽略评论和涉及关系的部分。我仍在努力。
import random
import copy
import sys
the_board = [" "]*10
def printboard(board):
print(board[7] + " " + "|" + board[8] + " " + "|" + board[9])
print("--------")
print(board[4] + " " + "|" + board[5] + " " + "|" + board[6])
print("--------")
print(board[1] + " " + "|" + board[2] + " " + "|" + board[3])
def choose_player():
while True:
player = input("What do you want to be; X or O?")
if player == "X":
print("You chose X")
comp = "O"
break
elif player == "O":
print("You chose O")
comp = "X"
break
else:
print("Invalid Selection")
continue
return [player, comp]
def virtual_toss():
print("Tossing to see who goes first.....")
x = random.randint(0,1)
if x== 0:
print("AI goes first")
move = "comp"
if x == 1:
print("Human goes first")
move = "hum"
return move
def win(board,le):
if (board[7] == le and board[8] == le and board[9]==le) or (board[4] == le and board[5]==le and board[6] == le)or (board[1] == le and board[2]==le and board[3] == le)or (board[7] == le and board[5]==le and board[3] == le)or (board[9] == le and board[5]==le and board[1] == le)or (board[7] == le and board[4]==le and board[1] == le)or (board[8] == le and board[5]==le and board[2] == le)or (board[9] == le and board[6]==le and board[3] == le):
return True
else:
return False
def make_move(board,number,symbol):
board[number] = symbol
def board_full(board):
count = 0
for item in board:
if item in ["X","O"]:
count+= 1
if count ==9 :
return True
else:
return False
def valid_move(board,num):
if board[num] == " ":
return True
else:
return False
def player_move(board):
number = int(input("Enter the number"))
return number
def copy_board(board):
copied_board = copy.copy(board)
return copied_board
def check_corner(board):
if (board[7] == " ") or (board[9] == " ") or (board[1] == " ") or (board[3] == " "):
return True
else:
return False
def check_center(board):
if (board[5] == " "):
return True
else:
return False
while True:
count = 0
loop_break = 0
print("welcome to TIC TAC TOE")
pla,comp = choose_player()
turn = virtual_toss()
while True:
#printboard(the_board)
if board_full(the_board) == True:
again = input ("Game is a tie. Want to try again? Y for yes and N for No")
if again == "Y":
loop_break = 6
break
else:
system.exit()
#if loop_break == 6:
#continue
if turn == "hum":
while True:
number = player_move(the_board)
if (valid_move(the_board,number) == True) and not(board_full == False):
make_move(the_board,number,pla)
#printboard(the_board)
break
else:
print("Invalid Move, try again!")
continue
if (win(the_board,pla) == True):
print ("Yay, you won!!!")
printboard(the_board)
count = 1
loop_break = 7
break
else:
turn = "comp"
printboard(the_board)
continue
else:
copied_board = copy_board(the_board)
for i in range(1,10):
make_move(copied_board,i,pla)
if(win(copied_board,pla) == True):
make_move(the_board,i,comp)
printboard(the_board)
turn = "hum"
loop_break = 1
break
else:
continue
if loop_break == 1:
continue
if (check_corner(the_board) == True) or (check_center(the_board)==True):
for i in [7,9,1,3,5]:
if(valid_move(copied_board,i)==True):
make_move(copied_board,i,comp)
if(win(copied_board,comp)==True):
make_move(the_board,i,comp)
printboard(the_board)
print("The AI beat you")
loop_break = 2
count = 1
break
else:
make_move(the_board,i,comp)
printboard(the_board)
turn = "hum"
loop_break = 3
break
if loop_break == 2:
break
elif loop_break == 3:
continue
else:
for i in [8,4,6,2]:
if(valid_move(copied_board,i)==True):
make_move(copied_board,i,comp)
if(win(copied_board,comp)):
make_move(the_board,i,comp)
printboard(the_board)
print("The AI beat you")
count = 1
loop_break = 4
break
else:
make_move(the_board,i,comp)
printboard(the_board)
turn = "hum"
loop_break = 5
break
if loop_break == 4:
break
elif loop_break == 5:
continue
if count == 1:
again = input("Would you like to play again? y/n")
if again == "y":
continue
else:
system.exit()
最佳答案
总而言之,你的AI的问题是它总是选择相同的位置,这看起来就像“不再玩任何进一步的回合”。
为了证明这一点,只需在 AI 的 for 循环中添加 print
即可:
for i in range(1,10):
make_move(copied_board,i,pla)
if(win(copied_board,pla) == True):
make_move(the_board,i,comp)
print("The AI chose #%d"%i) # <-- here it is
printboard(the_board)
# ......
这是结果。
welcome to TIC TAC TOE
What do you want to be; X or O?X
You chose X
Tossing to see who goes first.....
Human goes first
Enter the number1
| |
--------
| |
--------
X | |
The AI chose #3
| |
--------
| |
--------
X | |O
Enter the number2
| |
--------
| |
--------
X |X |O
The AI chose #3
| |
--------
| |
--------
X |X |O
Enter the number
我无法给出解决此错误的具体建议,因为我完全不明白这个 AI 的意义#=_=(也许你应该使用 MiniMax )。但无论如何,你的人工智能不应该将棋子放置在相同的位置。
ps。您可能需要将 system.exit()
更改为 sys.exit()
。
pps。 for-else grammar Python 非常适合您。
ppp。 PyCharm 表示,“删除多余的括号”。
关于Python TIC TAC TOE 跳轮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062521/
我是初学者,所以我的代码很乱。我还没有完整地评论这个游戏,所以如果你需要澄清一些变量,我可以给你。 (顺便说一句,这是一个要求制作井字游戏的c++项目) 我的主要问题是,我将如何重复我的棋盘(每次有人
我正在为C的Tic Tac Toe代码编写一个简单的游戏。我已经完成了大部分代码,但是我希望AI永不丢失。 我已经阅读了有关minimax算法的信息,但我不理解。如何使用此算法使计算机获胜或平局,但永
在MATLAB中,我想对一个别人写的函数进行计时,他们的函数内部可能使用了tic/toc。我想要我自己的 tic/toc。但如果内部函数调用 tic,则外部计时器会重置。 我怎样才能避免这种情况?我不
在MATLAB中,我想对一个别人写的函数进行计时,他们的函数内部可能使用了tic/toc。我想要我自己的 tic/toc。但如果内部函数调用 tic,则外部计时器会重置。 我怎样才能避免这种情况?我不
我现在想用我的代码做两件事。1) 检查获胜者2) 不让双方玩家在同一个位置进入eg.如果player1已经在board[0][0]='X'处输入了value,player2再次进入board[0][0
我在我的 Matlab 项目中的很多地方都使用了 tic-toc 函数。输出时间可以是331.5264 或1234.754 秒 等。我可以输出这种分钟格式吗?例如。 5 分 30.6 秒?谢谢! 最佳
我的代码(或者更确切地说,其他人的代码)有一个奇怪的问题。我正在调试并试图弄清楚为什么我们的时间显示错误。 无论如何,这是打印时间的代码:
我一直在开发一个简单的井字棋游戏,但遇到了一堵砖墙。 虽然大多数游戏功能都已到位,但我缺少适当放置计算机图 block 所需的关键算法。 我需要一种算法,可以搜索 3x3 的瓷砖网格,并在网格中搜索计
我正在用这种格式从数据文件中绘制一个 gnuplot 图表: 01 value_1_1 value_2_1 02 value_1_1 value_2_1 ... 01 value_1_n value_
在 gnuplot 中,如何在 y 轴上的每个 tic 标记处在整个图形上绘制水平条?就像一种特定点在哪里的视觉指示器。 (抱歉,如果这很简单,但谷歌搜索无果而终) 最佳答案 见 set grid命令
感谢这里人们的帮助,我成功地禁用了点击 div 并在已经使用 $(".pos").addClass('already-played'); 选择它们时覆盖它们; 以及 CSS 中的这个: .已经播放{
我正在使用 gnuplot 绘制大量绘图。由于每个图的数据范围(x 轴和 y 轴)都是可变的,因此我需要让 gnuplot 自动设置范围和控制。但是,我需要在绘图下方放置一个定义的网格,水平线各 1/
我有一个井字棋游戏,其中用户(x)玩CPU(o)。游戏开始时,CPU 将 (o) 放置在中心,并在用户之后移动到随机位置。游戏设置为循环,但一旦出现获胜者,它就会重置,并且不会显示“你赢/输的横幅”。
我是 gnuplot 新手,正在尝试为项目创建堆叠直方图。我遇到的问题是,我无法将 ticlabels 放在 x 轴上(即使可以,它们也没有以整齐的方式格式化)。我的gp文件如下: 这是我的数据文件的
我试图在没有人工智能的情况下实现井字棋游戏。不知怎的,我的点击功能会自动触发。您能帮我理解为什么点击功能会自动触发吗?这是 HTML 代码片段。 Tic Tac Toe Gam
我一直在疯狂地寻找这个问题的答案。如何设置 gnuplot 上抽动之间的距离?目前我的情节中的抽搐被挤得太紧了。我希望它们更加分散。 这是一个例子: 我有一个如下所示的图表: 100 ——
我正在制作一个井字游戏程序。我计划将 minimax 与它一起使用。我制作了一棵树,其中包含所有可能的游戏序列的空间,并且我正在寻找一种方法来填充它。我目前有这种类型: typedef struct
我在完成这项学校作业时遇到了问题。我想实现一种方法,其中代码显示 //call method to check for Winner,在每轮后检查获胜者。 我不确定该怎么做。我尝试过各种不同的方法。然
我正在编写一些计算时间很重要的代码。我使用 tic toc 函数和 profiler 来测量时间。它们之间有什么区别? 对于我的一段代码,tic toc 函数说明例如时间是 3 秒,但是我的所有代码行
我正在尝试遵循本教程: https://www.youtube.com/watch?v=Db3cC5iPrOM 2:59 我听不懂他在说什么。 我不明白为什么他在构造函数(public static
我是一名优秀的程序员,十分优秀!