Okay for starters this is a simple version of tic tac toe, I am in the process of creating an AI designed with difficulties and i have partial code for the AI(randomized) inside my code. Ignore that function but my main difficulty is printing moves on the board correctly. For some reason whenever I choose whether i want to be X or O it auto submits me into X. In addition, if i choose anything other than position A, it will print an X on position A anyways. I would like it if anyone would provide me with the help of telling me where exactly in my coding I went wrong. On a different note, in order to make my game more clear and easy, i would like to print Board and Board 2 side by side after moves are made. This would be to show what moves are available, so if the moves A and B were taken they would not show on Board. If someone could teach me how to do a side by side print that would be great.
print "**********************"
print "*THE GAME WILL BEGIN.*"
print "**********************"
import random
indx=0
move_storage=[]
move=["","","","","","","","",""]
#**************************************************
# This is the original board *
#**************************************************
def board():
print "This is the tictactoe board we will be playing on"
print " | | "
print " A | B | C "
print " | | "
print "------------------------------"
print " | | "
print " D | E | F "
print " | | "
print "------------------------------"
print " | | "
print " G | H | I "
print " | | "
board()
#****************************************************
#This is the board where the moves will be displayed*
#****************************************************
def board2(move):
print " ",move[0]," | ",move[1]," | ",move[2]," "
print " | | "
print " ----------------------- "
print " | | "
print " ",move[3]," | ",move[4]," | ",move[5]," "
print " | | "
print " ----------------------- "
print " | | "
print " ",move[6]," | ",move[7]," | ",move[8]," "
#This function will store all of the moves inputed.
def movestorage(move_storage, move):
move_storage= move_storage + [move]
#This function will randomize the computer's move
def computer_move():
move_random=random.randint(1,9)
move[move_random]=computer_choice
movestorage(move_storage, move)
player=raw_input("Would you like to play as x or o-->")
player_order=raw_input("Would you like to go first,y/n?-->")
while indx==0:
if 'x' or 'X' == player:
player_choice="X"
computer_choice="O"
if 'y' or 'Y' in player_order:
print
elif 'n' or 'N' in player_order:
print
print "The computer will go first, press ENTER to see the computers turn"
elif 'o' or 'O' == player:
player_choice="O"
computer_choice="X"
if 'y' or 'Y' in player_order:
print
elif 'n' or 'N' in player_order:
print
print "The computer will go first, press ENTER to see the computers turn"
x=0
while 2>x: #This is where the player decides where he wants to go
choice=raw_input("Choose where you would like to move-->")
if choice == 'a' or 'A':
move[0]=player_choice
movestorage(move_storage, move)
elif choice == 'b' or 'B':
move[1]=player_choice
movestorage(move_storage, move)
elif choice == 'c' or 'C':
move[2]=player_choice
movestorage(move_storage, move)
elif choice == 'd' or 'D':
move[3]=player_choice
movestorage(move_storage, move)
elif choice == 'e' or 'E':
move[4]=player_choice
movestorage(move_storage, move)
elif choice == 'f' or 'F':
move[5]=player_choice
movestorage(move_storage, move)
elif choice == 'g' or 'G':
move[6]=player_choice
movestorage(move_storage, move)
elif choice == 'h' or 'H':
move[7]=player_choice
movestorage(move_storage, move)
elif choice == 'i' or 'I':
move[8]=player_choice
movestorage(move_storage, move)
board2(move)
Also I have some blank print statements for neatness, they are not mistakes.
if 'x' or 'X' == playerdoes not meanplayeris eitherxorX, it meansif ('x') or ('X' == player)instead, so test both'x'as a boolean, and test'X' == playeras a boolean and then the whole expression isTrueif the first or the second part isTrue.Since the first is a string of length greater than 0, it’ll always be
True, and the second test is never made:Use:
instead, or:
or:
all of which test for the same thing.
This applies to all your
ifstatements testing against upper and lowercase letters, such as'n' or 'N', and'o' or 'O'.As for your long list of
if choicetests; just use a mapping to map letter to number:Now you can use
choicesto map to an index: