Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #USER: TCG
- #Naughts & Crosses (Python 2.7)
- import time,random
- #Defines Grid
- #function is used so that the can be replaced into it's original form when user asks to replay
- def grid():
- global g
- g = ["",
- " --------------------------------",
- " | | | |",
- " | 1 | 2 | 3 |",
- " | | | |",
- " |--------------------------------|",
- " | | | |",
- " | 4 | 5 | 6 |",
- " | | | |",
- " |--------------------------------|",
- " | | | |",
- " | 7 | 8 | 9 |",
- " | | | |",
- " --------------------------------" ]
- print '\n'.join(g),'\n' * 2
- #REPLAY GAME / EXIT GAME
- def replay():
- while True:
- restart = raw_input("\nWould you like to replay? (yes/no) : ")
- if restart == "yes":
- player1[:] = []
- - player2[:] = []
- grid()
- return 1
- elif restart == "no":
- print '\n'*3, "***** Thanks for playing! *****"
- time.sleep(2)
- raise SystemExit()
- else:
- print '\n' "Incorrect input! Please enter either \"yes\" or \"no\""
- #CHECKS IF THERE IS A WINNER
- def winner_checker():
- def winner_checker_2(x,x1,x2):
- #All possilble wins
- winner = ["147", "123", "258", "369", "456", "789", "159", "357"]
- if any(all(y in x for y in numbers) for numbers in winner):
- print '\n' * 5, x1, "YOU HAVE WON! GG TO", x2, '\n'
- if replay() == 1:
- return 1
- if winner_checker_2(player1,xplayer,oplayer) == 1:
- return 3
- if winner_checker_2(player2,oplayer,xplayer) == 1:
- return 3
- #DISPLAY PLAYER'S MOVE
- def display(x,p1,p2,p3,x1,x2,x3,x4):
- if x == move:
- #Replace the spaces with 'x'
- g[p1] = g[p1][:x1] + "\ /" + g[p1][x3:]
- g[p2] = g[p2][:x2] + "x" + g[p2][x4:]
- g[p3] = g[p3][:x1] + "/ \\" + g[p3][x3:]
- print '\n' * 40, '\n'.join(g), '\n'
- return 2
- def display2(x,p1,p2,p3,x1,x2,x3,x4):
- if x == move:
- #Replace the spaces with 'o'
- g[p1] = g[p1][:x1] + "---" + g[p1][x3:]
- g[p2] = g[p2][:x2] + "| |" + g[p2][x4:]
- g[p3] = g[p3][:x1] + "---" + g[p3][x3:]
- print '\n' * 40, '\n'.join(g), '\n'
- return 2
- def main_game():
- global player1,player2,oplayer,xplayer,move
- #Header
- print '''
- ----------------------------------------
- | |
- | KNOTS & CROSSES |
- | |
- ----------------------------------------''' '\n'
- #Asks for input
- name1 = raw_input("Enter name of player 1 : ")
- name2 = raw_input("Enter name of player 2 : ")
- print'\n' "CHOOSING PLAYERS..."
- time.sleep(1)
- #xplayer is assigned from randomly choosing between name1 & name2
- xplayer = random.choice((name1,name2))
- #Display's who's playing with either noughts and crosses
- print'\n', xplayer, "You are playing with Crosses"
- if name1 != xplayer:
- oplayer = name1
- elif name2 != xplayer:
- oplayer = name2
- print oplayer,"You are playing with Noughts",'\n'
- #print grid
- print " HERE IS THE GRID :" '\n'
- grid()
- #Initiliaize lists
- player1 = []
- player2 = []
- #PLAYER 1/2 INPUTS
- while True:
- #assigned value to avoid NameError & reset for replay
- loop_count = 0
- for eachplayer in ((xplayer,oplayer)*5):
- move = raw_input("%s It's your turn, enter a number from the grid : " % eachplayer)
- while not move.isdigit() or int(move) < 1 or int(move) > 9:
- move = raw_input("\nInvalid Input, please enter an available number from the grid \n\n"
- "%s enter a number from the grid : " % eachplayer)
- while move in player2 or move in player1:
- move = raw_input("\n%s, The space has already been taken \n\n"
- "Enter it again, using a number from the grid : " % eachplayer)
- if eachplayer == xplayer:
- #All possible enterable values, which changes the grid accordingly
- if ((display("4",6,7,8,7,8,10,9) or display("1",2,3,4,7,8,10,9) or display("7",10,11,12,7,8,10,9) or
- display("2",2,3,4,18,19,21,20) or display("5",6,7,8,18,19,21,20) or display("8",10,11,12,18,19,21,20) or
- display("3",2,3,4,29,30,32,31) or display("6",6,7,8,29,30,32,31) or display("9",10,11,12,29,30,32,31)) == 2):
- player1.append(move)
- if eachplayer == oplayer:
- #All possible enterable values, which changes the grid accordingly
- if ((display2("4",6,7,8,7,6,10,11) or display2("1",2,3,4,7,6,10,11) or display2("7",10,11,12,7,6,10,11) or
- display2("2",2,3,4,18,17,21,22) or display2("5",6,7,8,18,17,21,22) or display2("8",10,11,12,18,17,21,22) or
- display2("3",2,3,4,29,28,32,33) or display2("6",6,7,8,29,28,32,33) or display2("9",10,11,12,29,28,32,33)) == 2):
- player2.append(move)
- if winner_checker() == 3:
- break
- if loop_count == 8:
- print '\n' * 5, "******* The match was a draw *******" '\n'
- if replay() == 1:
- break
- loop_count += 1
- if __name__ == '__main__':
- main_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement