我刚刚开始学习我的第一门语言:python。创建了一个简单的井字游戏。 休息对第一位玩家有效,但对第二位玩家无效。 如果玩家2获胜,它会打印(玩家2是获胜者)罚款,但仍会为第一个玩家输入
代码如下:
import random
board = [0,1,2,
3,4,5,
6,7,8]
def show():
print (board[0],'|',board[1],'|',board[2])
print ('--------')
print (board[3],'|',board[4],'|',board[5])
print ('--------')
print (board[6],'|',board[7],'|',board[8])
print ('--------')
while True:
inpt = input('select a spot player 1: ')
inp = int(inpt)
if board[inp] != 'x' and board[inp] != 'o':
board[inp] = 'x'
if (board[0] == 'x'and board[1] == 'x' and board[2] == 'x')or(board[0] == 'x'and board[3] == 'x' and board[6] == 'x')or(board[6] == 'x'and board[7] == 'x' and board[8] == 'x')or(board[2] == 'x'and board[5] == 'x' and board[8] == 'x')or(board[1] == 'x'and board[4] == 'x' and board[7] == 'x')or(board[3] == 'x'and board[4] == 'x' and board[5] == 'x')or(board[0] == 'x'and board[4] == 'x' and board[8] == 'x')or(board[2] == 'x'and board[4]=='x' and board[6] == 'x'):
print('player 1 is the winner')
break
show()
space=True #finding space
while space:
oppt = input('select a spot player 2: ')
opp = int(oppt)
if board[opp] != 'o' and board[opp] != 'o':
board[opp] = 'o'
if (board[0] == 'o' and board[1] == 'o' and board[2] == 'o')or(board[0] == 'o' and board[3] == 'o' and board[6] == 'o')or(board[6] == 'o' and board[7] == 'o' and board[8] == 'o')or(board[2] == 'o' and board[5] == 'o' and board[8] == 'o')or(board[1] == 'o' and board[4] == 'o' and board[7] == 'o')or(board[3] == 'o' and board[4] == 'o' and board[5] == 'o')or(board[0] == 'o' and board[4] == 'o' and board[8] == 'o')or(board[2] == 'o' and board[4] == 'o' and board[6] == 'o'):
print('player 2 is the winner')
break
space = False
else:
print('Please check again')
else:
print ('PLEASE CHECK AGAIN')
show()