请问为什么会出现这种情况(跟着up主做,在github上查看源码都一样啊)
五子棋算法 #问题应该在主函数
main.py
from game import Gomoku
import sys
def main():
g = Gomoku()
g.play()
if _name_ == '_main_':
main()
run main.py之后的显示
D:\pycharm\FFFFF\venv\Scripts\python.exe D:/pycharm/FFFFF/main.py
Traceback (most recent call last):
File "D:/pycharm/FFFFF/main.py", line 10, in <module>
if _name_ == '_main_':
NameError: name '_name_' is not definedProcess finished with exit code 1
刚学python希望能有大佬指点
附game.py
game.py:
class Gomoku:
def _init_(self):
"""生成"""
self.g_map=[[0 for y in range(15)]for x in range(15)] #当前棋盘
self.cur_step=0 #步数
def move_1step(self):
"""玩家落子"""
while True:
try:
pos_x=int(input('x:')) #接受玩家输入
pos_y=int(input('y:'))
if 0<=pos_x<=14 and 0<=pos_y<=14: #判断格子是否可以落子
if self.g_map[pos_x][pos_y]==0: #该格子为空
self.g_map[pos_x][pos_y]=1
self.cur_step += 1
return
except ValueError: #玩家输入值有误,(例如输入了'A')
continue
def game_result(self,show=False):
"""判断游戏结果。0为游戏进行中,1为玩家胜出,2为电脑胜出,3为平局"""
#1.判断是否横向连续五子
for x in range(11):
for y in range(15):
if self.g_map[x][y]==1 and self.g_map[x+1][y]==1 and self.g_map[x+2][y]==1 and self.g_map[x+3][y] == 1 \
and self.g_map[x+4][y]==1:
if show:
return 1,[(x0,y)for x0 in range(x,x+5)]
else:
return 1
if self.g_map[x][y]==2 and self.g_map[x+1][y]==2 and self.g_map[x+2][y]==2 and self.g_map[x+3][y] == 2 \
and self.g_map[x+4][y] == 2:
if show:
return 2,[(x0,y)for x0 in range(x,x+5)]
else:
return 2
#2.判断是否纵向连续五子
for x in range(15):
for y in range(11):
if self.g_map[x][y] == 1 and self.g_map[x][y + 1] == 1 and self.g_map[x][y + 2] == 1 and \
self.g_map[x][y + 3] == 1 and self.g_map[x][y + 4] == 1:
if show:
return 1,[(x, y0)for y0 in range(y, y + 5)]
else:
return 1
if self.g_map[x][y] == 2 and self.g_map[x][y + 1] == 2 and self.g_map[x][y + 2] == 2 and \
self.g_map[x][y + 3] == 2 and self.g_map[x][y + 4] == 2:
if show:
return 2,[(x, y0)for y0 in range(y, y + 5)]
else:
return 2
#3.判断是否左上~右下连续五子
for x in range(11):
for y in range(11):
if self.g_map[x][y] == 1 and self.g_map[x + 1][y +1] == 1 and self.g_map[x + 2][y + 2] == 1 and \
self.g_map[x + 3][y + 3] == 1 and self.g_map[x + 4][y + 4] == 1:
if show:
return 1,[(x + t,y + t)for t in range(5)]
else:
return 1
if self.g_map[x][y] == 2 and self.g_map[x + 1][y + 1] == 2 and self.g_map[x + 2][y + 2] == 2 and \
self.g_map[x + 3][y + 3] == 2 and self.g_map[x + 4][y + 4] == 2:
if show:
return 2, [(x + t, y + t)for t in range(5)]
else:
return 2
#4.判断是否右上~左下连续五子
for x in range(11):
for y in range(11):
if self.g_map[x + 4][y] == 1 and self.g_map[x + 3][y + 1] ==1 and self.g_map[x + 2][y + 2] == 1 and \
self.g_map[x + 1][y + 3] == 1 and self.g_map[x][y + 4] == 1:
if show:
return 1, [(x + t, y + 4 - t)for t in range(5)]
else:
return 1
if self.g_map[x + 4][y] == 2 and self.g_map[x + 3][y + 1] == 2 and self.g_map[x + 2][y + 2] == 2 and \
self.g_map[x + 1][y + 3] == 2 and self.g_map[x][y + 4] == 2:
if show:
return 2, [(x + t, y + 4 - t)for t in range(5)]
else:
return 2
#5.判断是否玩家与电脑平局
for x in range(15):
for y in range(15):
if self.g_map[x][y] == 0 : #有空位可以落子
if show :
return 0, [(-1, -1)]
else:
return 0
if show:
return 3, [(-1, -1)]
else:
return 3
def ai_move_1step(self): #呆头模式,只会竖着下
"""电脑落子"""
for x in range(15):
for y in range (15):
if self.g_map[x][y] == 0:
self.g_map[x][y] = 2
self.cur_step += 1
return
def show(self,res):
"""显示游戏内容。玩家用⚪,电脑用×"""
for y in range(15):
for x in range(15):
if self.g_map[x][y] == 0:
print(' ', end='')
elif self.g_map[x][y] == 1:
print('⚪', end='')
elif self.g_map[x][y] == 2:
print('×', end='')
if x != 14:
print('-', end='')
print('\n', end='')
for x in range(15):
print('|', end='')
print('\n', end='')
if res == 1:
print('玩家胜出!')
elif res == 2:
print('电脑胜出!')
elif res == 3:
print('平局!!!!')
def play(self):
while True:
self.move_1step() # 玩家下一步
res = self.game_result() # 判断游戏结果
if res != 0: # 如果游戏结果为“已经结束”,则显示游戏内容,并退出主循环
self.show(res)
return
self.ai_move_1step() # 电脑下一步
res = self.game_result()
if res != 0:
self.show(res)
return
self.show(0) # 在游戏还没有结束的情况下,显示游戏内容,并继续下一轮循环
五子棋算法 #问题应该在主函数
main.py
from game import Gomoku
import sys
def main():
g = Gomoku()
g.play()
if _name_ == '_main_':
main()
run main.py之后的显示
D:\pycharm\FFFFF\venv\Scripts\python.exe D:/pycharm/FFFFF/main.py
Traceback (most recent call last):
File "D:/pycharm/FFFFF/main.py", line 10, in <module>
if _name_ == '_main_':
NameError: name '_name_' is not definedProcess finished with exit code 1
刚学python希望能有大佬指点
附game.py
game.py:
class Gomoku:
def _init_(self):
"""生成"""
self.g_map=[[0 for y in range(15)]for x in range(15)] #当前棋盘
self.cur_step=0 #步数
def move_1step(self):
"""玩家落子"""
while True:
try:
pos_x=int(input('x:')) #接受玩家输入
pos_y=int(input('y:'))
if 0<=pos_x<=14 and 0<=pos_y<=14: #判断格子是否可以落子
if self.g_map[pos_x][pos_y]==0: #该格子为空
self.g_map[pos_x][pos_y]=1
self.cur_step += 1
return
except ValueError: #玩家输入值有误,(例如输入了'A')
continue
def game_result(self,show=False):
"""判断游戏结果。0为游戏进行中,1为玩家胜出,2为电脑胜出,3为平局"""
#1.判断是否横向连续五子
for x in range(11):
for y in range(15):
if self.g_map[x][y]==1 and self.g_map[x+1][y]==1 and self.g_map[x+2][y]==1 and self.g_map[x+3][y] == 1 \
and self.g_map[x+4][y]==1:
if show:
return 1,[(x0,y)for x0 in range(x,x+5)]
else:
return 1
if self.g_map[x][y]==2 and self.g_map[x+1][y]==2 and self.g_map[x+2][y]==2 and self.g_map[x+3][y] == 2 \
and self.g_map[x+4][y] == 2:
if show:
return 2,[(x0,y)for x0 in range(x,x+5)]
else:
return 2
#2.判断是否纵向连续五子
for x in range(15):
for y in range(11):
if self.g_map[x][y] == 1 and self.g_map[x][y + 1] == 1 and self.g_map[x][y + 2] == 1 and \
self.g_map[x][y + 3] == 1 and self.g_map[x][y + 4] == 1:
if show:
return 1,[(x, y0)for y0 in range(y, y + 5)]
else:
return 1
if self.g_map[x][y] == 2 and self.g_map[x][y + 1] == 2 and self.g_map[x][y + 2] == 2 and \
self.g_map[x][y + 3] == 2 and self.g_map[x][y + 4] == 2:
if show:
return 2,[(x, y0)for y0 in range(y, y + 5)]
else:
return 2
#3.判断是否左上~右下连续五子
for x in range(11):
for y in range(11):
if self.g_map[x][y] == 1 and self.g_map[x + 1][y +1] == 1 and self.g_map[x + 2][y + 2] == 1 and \
self.g_map[x + 3][y + 3] == 1 and self.g_map[x + 4][y + 4] == 1:
if show:
return 1,[(x + t,y + t)for t in range(5)]
else:
return 1
if self.g_map[x][y] == 2 and self.g_map[x + 1][y + 1] == 2 and self.g_map[x + 2][y + 2] == 2 and \
self.g_map[x + 3][y + 3] == 2 and self.g_map[x + 4][y + 4] == 2:
if show:
return 2, [(x + t, y + t)for t in range(5)]
else:
return 2
#4.判断是否右上~左下连续五子
for x in range(11):
for y in range(11):
if self.g_map[x + 4][y] == 1 and self.g_map[x + 3][y + 1] ==1 and self.g_map[x + 2][y + 2] == 1 and \
self.g_map[x + 1][y + 3] == 1 and self.g_map[x][y + 4] == 1:
if show:
return 1, [(x + t, y + 4 - t)for t in range(5)]
else:
return 1
if self.g_map[x + 4][y] == 2 and self.g_map[x + 3][y + 1] == 2 and self.g_map[x + 2][y + 2] == 2 and \
self.g_map[x + 1][y + 3] == 2 and self.g_map[x][y + 4] == 2:
if show:
return 2, [(x + t, y + 4 - t)for t in range(5)]
else:
return 2
#5.判断是否玩家与电脑平局
for x in range(15):
for y in range(15):
if self.g_map[x][y] == 0 : #有空位可以落子
if show :
return 0, [(-1, -1)]
else:
return 0
if show:
return 3, [(-1, -1)]
else:
return 3
def ai_move_1step(self): #呆头模式,只会竖着下
"""电脑落子"""
for x in range(15):
for y in range (15):
if self.g_map[x][y] == 0:
self.g_map[x][y] = 2
self.cur_step += 1
return
def show(self,res):
"""显示游戏内容。玩家用⚪,电脑用×"""
for y in range(15):
for x in range(15):
if self.g_map[x][y] == 0:
print(' ', end='')
elif self.g_map[x][y] == 1:
print('⚪', end='')
elif self.g_map[x][y] == 2:
print('×', end='')
if x != 14:
print('-', end='')
print('\n', end='')
for x in range(15):
print('|', end='')
print('\n', end='')
if res == 1:
print('玩家胜出!')
elif res == 2:
print('电脑胜出!')
elif res == 3:
print('平局!!!!')
def play(self):
while True:
self.move_1step() # 玩家下一步
res = self.game_result() # 判断游戏结果
if res != 0: # 如果游戏结果为“已经结束”,则显示游戏内容,并退出主循环
self.show(res)
return
self.ai_move_1step() # 电脑下一步
res = self.game_result()
if res != 0:
self.show(res)
return
self.show(0) # 在游戏还没有结束的情况下,显示游戏内容,并继续下一轮循环


