Sign in to follow this  
shanenin

Tic-tac-toe

Recommended Posts

I just finished my first program, other then a few test ones. it is tic-tac-toe. If you want to try it. here is the link

http://webpages.charter.net/lindbergfamily/game.py

If any of you care to comment on my boring code, feel free, I would appreciate any suggestions.

#!/usr/bin/env python
# this is the game tic-tac-toe
# it was fully programmed by shane lindberg

# instructions.py
#
# this function gives instuctions to
# the user for playing the game

def instructions():


   print """\n\tWelcome to the game of tic-tac-toe\n
\t --a game of man against machine--\n
you first need to choose to go first or second. If you
choose to go first you will be X's. If you choose to
go second you will be O's.\n
you will choose your 'move' on the board by using the
following key.

DO NOT FORGET THE ORDER OF THE NUMBERS IN THE KEY BELOW"""

   instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
   
   print_board(instruc_list)
   print ' '

# print_board.py
#
# this function prints the board, it takes
# its parameter in the form of a list, the
# list must have at least 9 elments

def print_board(order):
   print " "
   print " "
   print " ",order[0], "|", order[1], "|", order[2]
   print " -----------"
   print " ",order[3], "|", order[4], "|", order[5]
   print " -----------"
   print " ",order[6], "|", order[7], "|", order[8]

# x_or_o.py
#
# this function asks the user if he or
# she wants to go first, if they choose
# to go first, they will be X's, if they
# choose to go second, they will be O's

def x_or_o():

   print "\nWould you like to go first or second?\n"
   
   answer = ""
   choices = ('first', 'second')
   while answer not in choices:
       answer = raw_input("please enter 'first' or 'second'(enter here)")
   if answer == "first":
       print "\nyou chose to go first, so that will make you X's"
   if answer == "second":
       print "\nyou chose to go second, you must feel brave to give"
       print "the computer the advantage. You will be O's"
   return answer

# answer.py
#
# this function asks for a move and checks to see
# if it is a legal place to choose.

def answer():
   guess = raw_input("please enter your move(enter here)")
   while guess not in ('1','2','3','4','5','6','7','8','9'):
guess = raw_input("please enter a number chosen from 1-9(enter here)")
   guess = int(guess)
   while True:
       if position[guess -1] in ('X', 'O'):
    guess = raw_input("that space is already occupied, please make another move(enter here)")
           guess = int(guess)    
       else:
    break
   return guess -1

# test comp_answer

def comp_answer():

   raw_input("enter return to let the computer take its turn")

   BEST_MOVES = ( 4,0,2,6,8,1,3,7,5 )
   # the most favorable move
   #
   # the following for statment determines if the
   # computer has a move that will win the game,
   # if so it returns that value as its move
   
   for i in WINNER:
       winns = []
if i[0] in comp_moves:
    winns.append(i[0])
       if i[1] in comp_moves:
    winns.append(i[1])
if i[2] in comp_moves:
    winns.append(i[2])
if len(winns) == 2:
           for k in i:
        if k not in winns and k not in moves and k not in comp_moves:
                   return k

   # the second most favorable move
   #
   # the following for statment determines if the
   # user has two in a row and needs to be blocked
   # if so the computer returns that value as its move

   for i in WINNER:
winns = []
if i[0] in moves:
    winns.append(i[0])
       if i[1] in moves:
    winns.append(i[1])
if i[2] in moves:
    winns.append(i[2])
if len(winns) == 2:
           for k in i:
        if k not in winns and k not in moves and k not in comp_moves:
                   return k
   
   # the final part of the stratagie is to choose moves
   # in order of importance from the local tuple called
   # best moves. This tuple starts with the middle position
   # then the corners, then the 4 final sides

   for i in BEST_MOVES:
if i not in moves and i not in comp_moves:
           return i


# update_moves.py
#
# this function takes user input from the function answer.py
# and appends the list called position. This provides stdin
# for the print_board and the winner function

def update_moves(answer):
   if who_first == 'first':
       position[answer] = 'X'
   else:
       position[answer] = 'O'
   moves.append(answer)

# comp_update_moves.py
#
# this function updates the computers
# moves. it gets stdin from comp_answer

def comp_update_moves(comp_answer):
   if who_first == 'first':
       position[comp_answer] = 'O'
   else:
       position[comp_answer] = 'X'
   comp_moves.append(comp_answer)

# winner.py
# this function checks to see if anyone has one the game
# it takes its data from update_moves

def winner(moves,comp_moves):

   won = ""

   for i in WINNER:
       if i[0] in moves and i[1] in moves and i[2] in moves:
           won = 0
           
   for i in WINNER:
       if i[0] in comp_moves and i[1] in comp_moves and i[2] in comp_moves:
           won = 0
   return won

# congrat_winner.py
#
# this function allows you to decide who won
# so you are able to congrat the winner, or
# make fun at the loser

def congrat(moves):
   WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
            (2,5,8), (0,4,8), (2,4,6) )

   won = 1
   for i in WINNER:
       if i[0] in moves and i[1] in moves and i[2] in moves:
    won = 0
   return won

# here is where the actual program starts to run


WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
            (2,5,8), (0,4,8), (2,4,6) )
position = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
moves = []
comp_moves = []
who_first = ""
who_won = 1


instructions()
raw_input('press return to continue')

while  winner(moves,comp_moves) != 0:
   
   # if the game is a tie this break statement is used
   if len(moves) + len(comp_moves) == 9:
       who_won = 0
break

   # these first two 'if' statements only run
   # once during this while loop. their just used
   # to start either 'X's or 'O's. They also take
   # the first answer from the user or the computer

   if who_first == "":
who_first = x_or_o()
       if who_first == 'first':
           print_board(position)
           update_moves(answer())
    next_turn = 0
       else:
    print_board(position)
    comp_update_moves(comp_answer())
    next_turn = 1

   # the following if-else statment alternate the computer and
   # users turns, while also collecting data and updating data

   print_board(position)
   if next_turn == 0:
       comp_update_moves(comp_answer())
       next_turn = 1
   else:
update_moves(answer())
       next_turn = 0
 
print_board(position)

if who_won == 0:
   print "\nit was a tie, maybe you will win next time"
elif congrat(moves) == 0:
   print "\nyou beat the computer, man is still triumphant"
else:
   print "\nthe computer beat you, you let mankind down :-("

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this