| | |
my pride and joy
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
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
It is not very advanced, just mainly a bunch of if then and while statments.
If any of you care to comment on my boring code, feel free, I would appreciate any suggestions.
http://webpages.charter.net/lindbergfamily/game.py
It is not very advanced, just mainly a bunch of if then and while statments.
If any of you care to comment on my boring code, feel free, I would appreciate any suggestions.
python Syntax (Toggle Plain Text)
#!/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 :-("
Last edited by vegaseat; Oct 19th, 2008 at 10:54 am. Reason: changed nonfunctional php tags to newer code tag style
Looks like you put a lot of thought and effort into this game. Something is lousing up the indentations in the php-codefield, did you mix spaces and tabs?
I would highly recommend to stick with spaces only, since tab settings can differ from one computer to the other. Looks like the php-codefield replaces all tabs with spaces, but not along your tab setting!
The code on your web site is fine and works great as long as I use IDLE and don't "detabbify"!
Looks like your tabs were set to 8 spaces. Used the IDLE option and got rid of the tabs. Now the php-codefield looks correct ...
I would highly recommend to stick with spaces only, since tab settings can differ from one computer to the other. Looks like the php-codefield replaces all tabs with spaces, but not along your tab setting!
The code on your web site is fine and works great as long as I use IDLE and don't "detabbify"!
Looks like your tabs were set to 8 spaces. Used the IDLE option and got rid of the tabs. Now the php-codefield looks correct ...
python Syntax (Toggle Plain Text)
#!/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 :-("
Last edited by vegaseat; Oct 19th, 2008 at 10:56 am. Reason: changed from php code tags
May 'the Google' be with you!
One suggestion I would make to improve the comfort for the player, is to show the number pattern before every move the player makes. Easy to do, just repeat the line you have already used in function instructions() in function answer(), and make instruc_list global (declare it outside the function).
Darn computer beats me most of the time!
python Syntax (Toggle Plain Text)
#!/usr/bin/env python # this is the game tic-tac-toe # it was fully programmed by shane lindberg # made list global so it could be used in answer() instruc_list = [ '1','2','3','4','5','6','7','8','9' ] # 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 numeric key.""" #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(): print_board(instruc_list) # added this line to aid player 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("press enter 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 :-("
Last edited by vegaseat; Oct 19th, 2008 at 10:58 am. Reason: php tags don't work any longer on DaniWeb, changed them
May 'the Google' be with you!
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
I was thinking of doing something like that. For better yet usablilty, a gui, that allows a mouse pointer to choose would be better. Would something like pygame or TKinter be the way to go. Or do those need to be used together. I am currently using this book to learn python
http://www.amazon.com/exec/obidos/tg...books&n=507846
further into it, it gets into gui game making. I think the next chapter is in introduction to OOP.
http://www.amazon.com/exec/obidos/tg...books&n=507846
further into it, it gets into gui game making. I think the next chapter is in introduction to OOP.
Tkinter and PyGame are not the same. PyGame uses the Simple DirectMedia Layer (SDL) library, a little fancier for graphics, animation and sound than Tkinter. Tkinter is its own GUI library based on TCL, not quit so fancy, but also much simpler to use.
With Tkinter you can quickly create a pad of nine buttons using grid to position them, then go from there. You can assign a command to each button to put in a X or O when clicked, applying the logic you already worked out.
With Tkinter you can quickly create a pad of nine buttons using grid to position them, then go from there. You can assign a command to each button to put in a X or O when clicked, applying the logic you already worked out.
May 'the Google' be with you!
•
•
•
•
Originally Posted by shanenin
I have been using gedit(linux), as my editor. I found an option that fill tabs with spaces. So even if I use tabs and spaces together, the editer translates it all to spaces.
On the Windows platform I am still using a mix of editors/IDEs like ConTEXT, DrPython, IDLE, SPE and PythonWin. None of these are perfect, each has its strengths. DrPython allows for fast experimentation with code, but has no context sensitive help.
May 'the Google' be with you!
![]() |
Similar Threads
- Moderators needed (Internet Marketing Job Offers)
- Glad to be here! First message :) (Community Introductions)
- Top 10 Songs (Geeks' Lounge)
Other Threads in the Python Forum
- Previous Thread: internet form handling
- Next Thread: ftplib and the retrbinary() command
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array basic beginner book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary dynamic examples excel file filename float format ftp function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin port prime program programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect scrolledtext server software ssh stamp statictext statistics string strings table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame wxpython






