|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
TIC TAC TOE game
anyone here done a tic tac toe game on python before ?
i need help on how to start this thing. so far i've made the grid. print "TIC TAC TOE:" print "0|1|2 | | " print "----- -----" print "3|4|5 | | " print "----- -----" print "6|7|8 | | " i only know to keep doing if statements to put where the X and O's would be. but that would be toooooooo much coding. so i need a really shorter way to put the marks where the user tells me to put them. |
|
#2
|
||||
|
||||
|
how about using a list to represent each position of the board, considering that you already have them numbered? then just print out the other characters with the elements of the list concatenated in?
|
|
#3
|
|||
|
|||
|
Quote:
how would i be able to make the list that u recommended. |
|
#4
|
||||
|
||||
|
general syntax for lists:
Code:
listvar = [element1, element2, element3, element4, ...] valueOfElementX = listvar[X] firstThreeElements = listvar[:3] et cetera... |
|
#5
|
|||
|
|||
|
so far this is the code i have
Code:
def drawBoard(marks):
""" Output the board represented by marks."""
print "TIC TAC TOE:"
print "0|1|2 | | "
print "----- -----"
print "3|4|5 | | "
print "----- -----"
print "6|7|8 | | "
return drawBoard
def setPosition(marks):
"""sets the positions to given player's mark."""
s=str(drawBoard)
marks=s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8]
drawBoard("X XOXOXO ")
i have made a string named marks that is suppose to be the positions of the X's and O'x on the board. now is i want an X in for example in marks[0]. What command could i add to put the X there ? |
|
#6
|
||||
|
||||
|
consider the following:
Code:
def drawBoard(marks):
""" Output the board represented by marks."""
print "TIC TAC TOE:"
print "0|1|2 "+marks[0]+"|"+marks[1]+"|"+marks[2]
print "----- -----"
print "3|4|5 "+marks[3]+"|"+marks[4]+"|"+marks[5]
print "----- -----"
print "6|7|8 "+marks[6]+"|"+marks[7]+"|"+marks[8]
return drawBoard
marks = ["X", " ", " ", "O", "X", "O", "X", "O", " "]
drawBoard(marks)
marks[2] = "X"
drawBoard(marks)
|
|
#7
|
|||
|
|||
|
how do i then make a function that will ask the user to choose a position to put the X, and put the user's choice in the right position
|
|
#8
|
||||
|
||||
|
I'm sorry, but I'm not going to write the entire program for you. Look into the raw_input() and int() functions.
|
|
#9
|
|||
|
|||
|
i know those commands. but i don't know how to call it from a function.
i have this [CODE] def drawBoard(marks): """ Output the board represented by marks.""" print "TIC TAC TOE:" print "0|1|2 "+marks[0]+"|"+marks[1]+"|"+marks[2] print "----- -----" print "3|4|5 "+marks[3]+"|"+marks[4]+"|"+marks[5] print "----- -----" print "6|7|8 "+marks[6]+"|"+marks[7]+"|"+marks[8] return drawBoard marks = [" ", " ", " ", " ", " ", " ", " ", " ", " "] drawBoard(marks) def setPosition(i, string, marks): """ Sets the positions of the strings """ if i == 0: marks = string+marks[1]+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8] if i == 1: marks = marks[0]+string+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8] if i == 2: marks = marks[0]+marks[1]+string+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8] if i == 3: marks = marks[1]+marks[2]+marks[3]+string+marks[4]+marks[5]+marks[6]+marks[7]+marks[8] if i == 4: marks = marks[0]+marks[2]+marks[3]+marks[4]+string+marks[5]+marks[6]+marks[7]+marks[8] if i == 5: marks = marks[0]+marks[1]+marks[3]+marks[4]+marks[5]+string+marks[6]+marks[7]+marks[8] if i == 6: marks = marks[1]+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+string+marks[7]+marks[8] if i == 7: marks = marks[0]+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+string+marks[8] if i == 8: marks = marks[0]+marks[1]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]+string def playerTurn(marks): """ask the user where they want to move.""" marks=setPosition(i,marks) return playerTurn i=str(raw_input("Player X, choose where you want to make your mark: ")) setPosition(i, str, marks) print drawBoard [CODE] i have tried [CODE] def playerTurn(marks): a=str(raw_input("Player X, select the position that u want/ insert your X: ")) return playerTurn [CODE] but that doesn't work, its not like that i'm not trying anythin, its just that i couldn't get it, and that's y i'm asking for your help. And i thank you a lot for helping me so far already. |
|
#10
|
||||
|
||||
|
Ok, a few things.
1. You do not always need to return a value from a function. For instance, drawBoard() does not need to return anything - it just displays output. 2. Your setPosition function is overly complicated. How about the simple Code:
def setPosition(num, string, marks):
marks[num]=string
return marks
3. To have the user input a number, try Code:
num = int(raw_input("Please enter a number:"))
4. Put any code you post inside CODE and /CODE (not two CODE) tags. Otherwise indentation doesn't show up, which is critical for python. |
|
#11
|
|||
|
|||
|
This is the code i have right now. its suppose to ask user for position to place the X or O, (playerTurn function) then with the setPosition function, the X or O should be placed in. then with the updated marks, print the new grid.
Code:
def drawBoard(marks):
""" Output the board represented by marks."""
print "0|1|2 "+marks[0]+"|"+marks[1]+"|"+marks[2]
print "----- -----"
print "3|4|5 "+marks[3]+"|"+marks[4]+"|"+marks[5]
print "----- -----"
print "6|7|8 "+marks[6]+"|"+marks[7]+"|"+marks[8]
return drawBoard
def setPosition(i, string, marks):
""" Sets the positions of the strings """
if i == 0:
marks = string+marks[1]+marks[2]+marks[3]\
+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 1:
marks = marks[0]+string+marks[2]+marks[3]\
+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 2:
marks = marks[0]+marks[1]+string+marks[3]\
+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 3:
marks = marks[0]+marks[1]+marks[2]+string\
+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 4:
marks = marks[0]+marks[1]+marks[2]+\
marks[3]+string+marks[5]+marks[6]+marks[7]+marks[8]
if i == 5:
marks = marks[0]+marks[1]+marks[2]+marks[3]\
+marks[4]+string+marks[6]+marks[7]+marks[8]
if i == 6:
marks = marks[0]+marks[1]+marks[2]+marks[3]\
+marks[4]+marks[5]+string+marks[7]+marks[8]
if i == 7:
marks = marks[0]+marks[1]+marks[2]+marks[3]\
+marks[4]+marks[5]+marks[6]+string+marks[8]
if i ==8:
marks = marks[0]+marks[1]+marks[2]+marks[3]\
+marks[4]+marks[5]+marks[6]+marks[7]+string
return marks
def playerTurn(s, marks):
""" when given the players symbol (X or O), asks the player for his or her choice.
It should then put the the players mark on the board. """
if s == "X":
x= int(raw_input("Player X, choose where you want to make your mark (0-8): "))
if x>=0 and x<=8:
marks = setPosition(x, "X", marks)
return marks
s="X"
marks=" "
print "TIC TAC TOE:"
drawBoard(marks)
playerTurn(s, marks)
drawBoard(marks)
|
|
#12
|
||||
|
||||
|
you need to change one line:
Code:
playerTurn(s, marks) should be Code:
marks = playerTurn(s, marks) also, i'd highly recommend using a list instead of a string for marks, which then allows the shorter Code:
def setPosition(num, string, marks):
marks[num]=string
return marks
as it's much more consise. if you do that, the overall program becomes the (much shorter): Code:
def drawBoard(marks):
""" Output the board represented by marks."""
print "0|1|2 "+marks[0]+"|"+marks[1]+"|"+marks[2]
print "----- -----"
print "3|4|5 "+marks[3]+"|"+marks[4]+"|"+marks[5]
print "----- -----"
print "6|7|8 "+marks[6]+"|"+marks[7]+"|"+marks[8]
return drawBoard
def setPosition(num, string, marks):
marks[num] = string
return marks
def playerTurn(s, marks):
""" when given the players symbol (X or O), asks the player for his or her choice.
It should then put the the players mark on the board. """
if s == "X":
x = int(raw_input("Player X, choose where you want to make your mark (0-8): "))
if x >= 0 and x <= 8:
marks = setPosition(x, "X", marks)
return marks
s = "X"
marks = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
print "TIC TAC TOE:"
drawBoard(marks)
marks = playerTurn(s, marks)
drawBoard(marks)
Last edited by deltacoder : October 28th, 2004 at 07:53 PM. |
|
#13
|
|||
|
|||
|
mmm
Mybe the best way is to do it VB style and create a nice user interface first, then create an AI if you want to after.
|