Hi people. I'm trying to create a tictactoe program in python but i'm having some troubles. It's the player against the computer. Now the computer should go on random but still block/win if possible. Right now i'm kinda stuck so feel free to help. I'd prefer it as simple as possible so that i can understand the code. This is what i got so far...

import random
def print_board(board):
	board = board.replace("_"," ")
	print "." + "---." * 3
	for bound in [0,3,6]:
		print "|",
		for sym in board[bound:bound+3]:
			print sym, "|",
		if bound < 6:
			print "\n|" + "---|" * 3
	print "\n’" + "---´" * 3

coordinate = ["0,0", "0,1", "0,2", "1,0", "1,1", "1,2", "2,0", "2,1", "2,2"]
usedcoord = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]

def percentchance():
	import random
	return int(100*random.random())

running = False             
while running:
	while used == True:
		pos = random.randrange(0,8)
		coordinate[pos]
		if coordinate[pos] in usedcoord:
			used = True
		else:
			usedcoord.append(coordinate[pos])
			board[pos] = "o"
			print "My turn. I put my ring in square", coordinate[pos]
			print_board(board)
			used = False 
	while used == True:
		user = raw_input("Your turn. Input the coordinate for your move on the form x,y: ")
		if user in usedcoord:
			used = True
		else:
			idx = coordinate.index(user)
			usedcoord.append(user)
			board[idx] = "x"
			print_board(board)
			used = False

def randoms(list):
	import random
	return random.choice(list)
		usedcoord += number

Recommended Answers

All 9 Replies

What is purpose of 20 and 21 and why function at 44-47 after main code? And if by miracle the while would be entered which miracle would give value for used ;)

To be honest i really dont know. That was a friend that tried helping me but couldn't really explain why i need it. I'm guessing i have a lot of code that is unnecessary.

Looks like you have home work to do before youi are ready for challenge.

Using x,y coordinates confuses things IMHO. You can just number each square and enter one number for that square. This also simplifies keeping track of moves as you can use a single list with one element for each square on the board.

tonyjv: it's just the challenge i'm looking for. I want to learn and in the right way. For now i'm just your average no0b :)

woooee: yeah that's an option. But numbers would acctually be the same thing as using coordinates. Still just numbers u're typing in.

Ok so is there someone that can acctually help? =/

Start writing code and we are ready to speed up your way over the hurdles! It's your homework, and without doing that yourself you are not going to learn anything. You must cut the part of code that you actually get working, for example you could rig up the code with user input using the printing routine so that you can play against yourself by computer, then add computer game logic. Use function for actions with parameters, so there is one function for use of both X-player and O-player.

Other alternative is to do the logic first and then complicate things with user input like in one of my earlier posts I suggested to other poster:

import random
computer_piece, user_piece = pieces = 'OX'
winner_pos = ([[(a,b) for a in range(3)] for b in range(3)] +
              [[(a,b) for b in range(3)] for a in range(3)] +
              [[(a,a) for a in range(3)]] +
              [[(a,2-a) for a in range(3)]])

def random_pair():
    return random.randint(0,2), random.randint(0,2)   

def random_place(board, turn):
    x,y = random_pair()
    while board[x][y] != '_':
        x,y = random_pair()
    return x,y
   
def choose_move(board, turn=0):
    x, y = random_place(board, turn)
    board[x][y] = pieces[turn]
    return turn == 0

def winner(board):
    """ This function will return False if board is not winner, winner's piece otherwise """
    for marker in pieces:
        if any(all(board[x][y] == marker
                    for x,y in  win)
               for win in winner_pos):
             return marker
    return False

def isfinnished(board):
    return (winner(board) or
            not any(x == '_' in row
                for row in board
                for x in row)     # check for full
            )

def print_board(board):
    print '-'*20
    print '\n\n'.join('   '.join(line)
                    for line in board)

print "Wellcome to tic-tac-toe version 0.1 alpha"
print "Watch while I play against myself"
turn=0

for debug in range(20):
    board = [
        ['_', '_', '_'],
        ['_', '_', '_'],
        ['_', '_', '_']]
    # This loop does the stuff
    while not isfinnished(board):
        turn = choose_move(board, turn)
        print_board(board)

    print 'Winner:', winner(board) or 'Tie'
    raw_input('End of test run %i. Push enter.' % (debug+1))
    print '='*60

"""Here is my working version of Tic-Tac-Toe. It took me 4 hours of debugging to get it to work.

+------------------------------------+
| RECURSIVE TIC-TAC-TOE |
| by M. Stueben (October 3, 2010) |
+------------------------------------+
"""
#--FUNCTIONS ------------------------------------------------------------------
def printInstructions():
print("""
Welcome to the game of TIC-TAC-TOE
Human = X, Computer = O
Human moves first by chosing a number.
Good luck!
""")
printBoard()
#------------------------------------------------------------------------------
def placeMoveOnBoard(position, player):
Matrix[position] = player
Board [position] = "X" if player == 1 else "O"
#------------------------------------------------------------------------------
def winner(player): # Returns 1 (human wins), -1(computer win) or 0(draw)
if Matrix[1] + Matrix[2] + Matrix[3] == 3*player: return player
if Matrix[4] + Matrix[5] + Matrix[6] == 3*player: return player
if Matrix[7] + Matrix[8] + Matrix[9] == 3*player: return player
if Matrix[1] + Matrix[4] + Matrix[7] == 3*player: return player
if Matrix[2] + Matrix[5] + Matrix[8] == 3*player: return player
if Matrix[3] + Matrix[6] + Matrix[9] == 3*player: return player
if Matrix[1] + Matrix[5] + Matrix[9] == 3*player: return player
if Matrix[3] + Matrix[5] + Matrix[7] == 3*player: return player
return 0
#------------------------------------------------------------------------------
def clearScreen():
print ("\n " * 2)
#------------------------------------------------------------------------------
def printBoard():
if moveCount != 1: clearScreen()
print(" ", Board[1], "|", Board[2], "|", Board [3] )
print(" -----------")
print(" ", Board[4], "|", Board[5], "|", Board [6] )
print(" -----------")
print(" ", Board[7], "|", Board[8], "|", Board [9] )
print("")
#------------------------------------------------------------------------------
def evaluateMove(move, player, moveCount): # <-- Recursive function
#--Make the move to evaluate both the base case and the recursive case.
Matrix[move] = player

#--base case: If the move wins, then return a 1 (human) or -1 (computer).
if winner (player) == player:
Matrix[move] = 0; # <-- Take back the move
return player # <-- Winning move for player

#--the final move (which can't be a winner) must be a draw
if moveCount == 9:
Matrix[move]= 0 # <-- Take back the move
return 0 # <-- last move (draw) must be made.

#--Recursion: Return the values associated with the remaining moves.
value = player # = winning move for player
for n in range (1, 10):
if Matrix[n] != 0: continue
v = evaluateMove(n, -player, moveCount+1)
if player == 1 and v < value:
value = v
if player == -1 and v > value:
value = v

#--Recursive return
Matrix[move] = 0 # Take back move
return value
#------------------------------------------------------------------------------

def computerMove(player, moveCount):
lowestValue = 3; # positive value = losing value for computer
for cell in range(1, 10):
if Matrix[cell] != 0: continue
if player != -1: exit("player not = -1")
newValue = evaluateMove(cell, player, moveCount)
if newValue < lowestValue:
bestMove = cell
lowestValue = newValue
return bestMove
#------------------------------------------------------------------------------

def makeMove(player, moveCount):
if player == 1: return int(input ("\n\n Make your move, human."))
if player == -1: return computerMove(player, moveCount)
#--############################################################################
#--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN
#--Initialize------------------------------------------------------------------
Matrix = [ 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
Board = ["+", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
moveCount = 1
player = -1 # <-- 1 = human, -1 = computer
printInstructions()
#--Play game-------------------------------------------------------------------
while moveCount < 10:
player = -1 * player # <-- clever trick, eh?
position = makeMove (player, moveCount)
placeMoveOnBoard (position, player)
if player != 1: printBoard()
if winner(1) == 1:
print ("Human wins! Game over.")
break # <--Game over!
elif winner(-1) == -1:
print ("Computer wins! Game over.")
break # <--Game over!
if player == -1: print(" HUMAN to move")
moveCount += 1
#------------------------------------------------------------------------------
if moveCount > 9:
print (" Cat's Game.")
#============================ END OF PROGRAM ==================================

## Welcome to the experience of writing the game of Tic-tac-toe. This program is
##important for two reasons:
##
## 1) It is one of the simplest forms of weak AI--the program seems to act
## intelligently in selecting a move. [Strong AI is building a new life-
## form, in my opinion.]
##
## 2) The program uses recursion to build a data tree. A data tree is one
## of the most useful data structures. But in this program the tree
## is constructed for the programmer as a side-effect of recursion.
## WARNING: You absolutely must understand the how recursion is used to choose a
##move or you will have no hope in getting your Tic-tac-toe program to work.
##
##PART I. Write a program to play a game of Tic-tac-toe between two human
##players. One player is called "Human," and the other is called Computer."
##Graphics are not needed. We can just print a crude board in the console window.
##The first two boards printed by my program look like this:
##
## Welcome to the game of TIC-TAC-TOE
## Human = X, Computer = O
## Human moves first by chosing a number.
## Good luck!
##
## 1 | 2 | 3
## -----------
## 4 | 5 | 6
## -----------
## 7 | 8 | 9
##
## X | 2 | 3
## -----------
## 4 | O | 6
## -----------
## 7 | 8 | 9
##
## HUMAN to move
##
##The main part of my program looks exactly like this:
##
###--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN--MAIN
###--Initialize------------------------------------------------------------------
##Matrix = [ 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
##Board = ["+", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
##moveCount = 1
##player = -1 # <-- 1 = human, -1 = computer
##printInstructions()
##
###--Play game-------------------------------------------------------------------
##while moveCount < 10:
## player = -1 * player # <-- clever trick, eh?
## position = makeMove (player, moveCount)
## placeMoveOnBoard (position, player)
## if player != 1: printBoard()
## if winner(1) == 1:
## print ("Human wins! Game over.")
## break # <--Game over!
## elif winner(-1) == -1:
## print ("Computer wins! Game over.")
## break # <--Game over!
## if player == -1: print(" HUMAN to move")
## moveCount += 1
## #------------------------------------------------------------------------------
##if moveCount > 9:
## print (" Cat's Game.")
###============================ END OF PROGRAM ==================================
##
## Moves are recorded on the Matrix (actually a list, or an array in Java) and
##transferred to the Board (another list) for printing in Xs and Os. Only when you
##finish this part do you write the functions to introduce AI. Below is my
##makeMove() function.
##
##def makeMove(player, moveCount):
## if player == 1: return int(input ("\n\n Make your move, human."))
## if player == -1: return computerMove(player, moveCount)
##
## The computerMove() examines all possible moves on the board and chooses the
##best move for the Computer to make. How does it do this? Answer: with a second
##magic (= recursive) function that returns the expected result against perfect
##play from the Human. The recursive function
##
## evaluateMove(move, player, moveCount)
##
##actually makes the Computer's move (or Human's move) on the board and then
##decides if their is a win. If the Computer has won, the function takes back the
##move and returns -1 indicating a winning move for the Computer (no recursion
##here.) If there is no win, the program checks if this is the final move of the
##game (which can only be made by the human player). If so, the program takes back
##the move and returns 0 indicating a draw (no recursion here). Finally, if there
##are more moves to be made the computer recursively calls
##
## evaluateMove(n, -player, moveCount+1)
##
##for all of the remaining moves and returns the relitive value.
##
## Setting up the final recursive loop is the hard part. I have given some of my
##code below to get you going faster.
##
## To test the program choose a corner move for the Human (who always moves
##first). Then the only non-losing move for the Computer is the center cell (5).
##So we are likely to know on the first Computer move if our code is defective.
##
## Another trick I used is to start the board set up like this, and ready to
##accept the Human's move (moveCount = 7). The result is that the Computer
##should see all moves as losing moves (value = 1).
##
## X | 2 | O
## -----------
## 4 | O | 6
## -----------
## 7 | 8 | X
##
## The advantage of doing this is that the suffering programmer can step through
##all states of recursion printing out the values of each move. In fact, this is
##how I found my final bug. But I had to print out the board after every move and
##the taking back of every move.
##
## Finally, even with all my help, this is not an easy program to write. You are
##expected to try hard, not obtain more than syntax help from other students, and
##not to devulge the key observations associated with the recursion loop. If you
##want to write the program your own way, feel free. However, you should not use a
##function to search for a winning move or necessary blocking move. We want these
##moves to be found naturally by recursion (AI) instead of explicitly giving the
##Computer a move in a particular kind of situation. Good luck.
## * * *
##
##
##
##
##def evaluateMove(move, player, moveCount): # <-- Recursive function
###--Make the move to evaluate both the base case and the recursive case.
## Matrix[move] = player
##
###--base case: If the move wins, then return a 1 (human) or -1 (computer).
## if winner (player) == player:
## Matrix[move] = 0; # <-- Take back the move
## return player # <-- Winning move for player
##
###--the final move (which can't be a winner) must be a draw
## if moveCount == 9:
## Matrix[move]= 0 # <-- Take back the move
## return 0 # <-- last move (draw) must be made.
##
###--Recursion: Return the values associated with the remaining moves.
### All of my problems came from the code below.
## value = ???
## for n in range (1, 10):
## ???
## v = evaluateMove(n, -player, moveCount+1)
## ???
## value = v
##
###--Recursive return
## Matrix[move] = 0 # Take back move
## return value # This is the value associated with the move. The
## # same code needs to work for both the Human and the
## #Computer players.
## =================================================================== """
##
##

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.