0
import random
#these will be the positions the computer will take
new = ['', '', '', '', '', '', '', '', '']
null = ''
#string for the player
player = ''
#string for the computer
computer = ''
ここでは、ユーザー(player
)とコンピュータの2つのチームを定義します。だから、それが唯一の可能computer_move()は引数を2つ(3つ)指定しますか?
def computer_move(computer, new):
:
move_c = computer_move(player, computer, new)
しかし、機能は以下のように定義されていますcomputer_first
とplayer_first
機能インサイド
def teams(player, computer):
#asks the user to pick their character
player = raw_input("Pick a team. X or O")
#checking to make sure the character they enter is valid
while player not in ('x', 'X', 'o', 'O'):
#if not, prompt them to enter something else
print "Please enter a valid character."
player = raw_input("Pick a team. X or O")
#set players character to x if chosen
if player == 'x' or player == 'X':
print "Great, your team is X."
#sets computers to o as default
computer = 'o'
#set players character to o
else:
print "Great, your team is O."
#sets computers to x as default
computer = 'x'
#turn the characters to uppercase, easier to see
return player.upper(), computer.upper()
#deciding whether the player or computer goes first
def choose_turn():
turn = None
#checking if the answer entered is valid
while turn not in ('y', 'Y', 'n', 'N'):
#if not, prompt user for another input
turn = raw_input("Would you like to go first? Y or N")
#sets user to go first
if turn == 'y' or turn == 'Y':
return 1
#sets computer to go first
elif turn == 'n' or turn == 'N':
return 0
#tells user if they have entered an incorrect answer
else:
print "Invalid choice"
#initially draws the board on the screen
def draw_board(a):
print "\n\t", a[0], "|", a[1], "|", a[2]
print "\t", "--------"
print "\n\t", a[3], "|", a[4], "|", a[5]
print "\t", "--------"
print "\n\t", a[6], "|", a[7], "|", a[8], "\n"
#celebration message for the user
#lets them know they have won the game
def celly_player():
print "Whooohoo! You won!"
#celebration message for the computer
#lets the user know they have lost the game
def celly_computer():
print "Hahaha, I won!"
#for when the user goes first in the game
def player_first(player, computer, new):
#has the player move if the game has not been won
while win(player, computer, new) is None:
move = player_move(player, new)
new[int(move)] = player
draw_board(new)
if win(player, computer, new) != None:
break
else:
pass
print "okay, I'll take..."
move_c = computer_move(player, computer, new)
print move_c
new[int(move_c)] = computer
draw_board(new)
#settin up the wins
y = win(player, computer, new)
#initiate user's winning message
if y == 1:
celly_player()
#initiate computer's winning message
elif y == 0:
celly_computer()
#tells the user that nobody has one, it is a tie
else:
print "Well...it's a tie. Cats!"
#for when the computer goes first in the game
def computer_first(player, computer, new):
#computer gets to move if the game has not been won
while not win(player, computer, new):
print "now I'll take..."
move_c = computer_move(player, computer, new)
print move_c
new[move_c] = computer
draw_board(new)
if win(player, computer, new) != None:
break
else:
pass
move = computer_move(player, new)
new[int(move)] = player
draw_board(new)
#settin up the wins
y == win(player, computer, new)
#initiates user's winning message
if y == 1:
celly_player()
#initiates computer's winning message
elif y == 0:
celly_computer()
#tells the user that nobody has one, it is a tie
else:
print "Well, it's a tie. Cats!"
#here we are setting up and defining the winning messages
def win(player, computer, new):
#combinations for winning positions
rows = ((0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6))
for i in rows:
if new[i[0]] == new[i[1]] == new[i[2]] != null:
champ = new[i[0]]
if champ == player:
return 1
elif champ == computer:
return 0
if null not in new:
return "Tie game!"
if null not in new:
return "Tie game!"
return None
#setting up moves for the player
def player_move(player, new):
#prompts user for input
a = raw_input("Where would you like to move?")
while True:
#checking if entered position is on the board
if a not in('0', '1', '2', '3', '4', '5', '6', '7', '8'):
#if not, prompts for a new position
print "Please enter a valid move on the board."
a = raw_input("Where would you like to move?")
elif new[int(a)] != null:
#lets user know their chosen space is unavailable
print "Darn, this place has already been taken"
#prompts for a new position
a = raw_input("Where would you like to move?")
else:
return int(a)
#setting up moves for the user
#using random numbers/positions
def computer_move(computer, new):
#best choices for computer to move
best = [4, 0, 2, 6, 8]
empty = []
for i in range(0, 9):
if new[i] == null:
empty.append(i)
for i in empty:
new[i] = computer
if win(player, computer, new) is 0:
return i
new[i] = null
for i in empty:
new[i] = player
if win(player, computer, new) is 1:
return i
new[i] = null
return int(blank[random.randrange(len(empty))])
#printing out the instructions to the user
def instructions():
print
"""
Welcome to Python Tic Tac Toe!
Make your move by entering a number 0-8. The board positions are
shown below.
0 | 1 | 2
---------
3 | 4 | 5
---------
6 | 7 | 8
Best of luck!
"""
#main function
def main(player, computer, new):
instructions()
#prompt user to start
print "Let's get started!"
a = teams(player, computer)
player = a[0]
computer = a[1]
b = choose_turn()
#player going first
if b == 1:
print "You are first."
print "Let's start with a new board."
draw_board(new)
player_first(player, computer, new)
#computer going first
elif b == 0:
print "I can be first"
print "Let's start."
draw_board(new)
computer_first(player, computer, new)
else:
pass
main(player, computer, new)
raw_input("Please press enter to exit the game")
エラーメッセージは非常に説明する必要があります。 2つの引数を取るために 'computer_move'を定義しましたが、3つの引数を使ってそれを呼び出そうとしました。 –