i have created an oxo game.
wich is a game where you try to get 3 in a row in a 9-square field.
i got it running but i have one question.
for the computer's moves i predicted what the user would do (2in a row) and then made the computer react to that (block 3th).
but this is a lot of typing and i was wondering if there's an easiere way to do it.
about the game field:
there isn't one it just a list divided in 3little lists printed out as a field.
here's the code:

import time
import random

x="-"
lijst=[[x,x,x],[x,x,x],[x,x,x]]
keuzeschema = """
1 2 3
4 5 6
7 8 9
"""
menu="""
1)zie instructies
2)begin met het spel
"""
menu2="""
1)om een x te zetten
2)om af te sluiten
"""
instructies = """
welkom bij oxo
de bedoeling van het spel is om 3 op een rij te krijgen
om een x of te plaatsen volg je gewoon de instructies van het spel
als je een x zet waar er al een x of een o stond is je beurt voorbij
veel plezier!!!
"""
def usermove():
  print keuzeschema
  c=int(raw_input("zet het cijfer van het vakje waar je de x wilt zetten"))
  invoer ="x"
  while c <10:
    if c==1 and lijst[0][0]==x:
      lijst[0][0]=invoer
    elif c==2 and lijst[0][1]==x:
      lijst[0][1]=invoer
    elif c==3 and lijst[0][2]==x:
      lijst[0][2]=invoer
    elif c==4 and lijst[1][0]==x:
      lijst[1][0]=invoer
    elif c==5 and lijst[1][1]==x:
      lijst[1][1]=invoer
    elif c==6 and lijst[1][2]==x:
      lijst[1][2]=invoer
    elif c==7 and lijst[2][0]==x:
      lijst[2][0]=invoer
    elif c==8 and lijst[2][1]==x:
      lijst[2][1]=invoer
    elif c==9 and lijst[2][2]==x:
      lijst[2][2]=invoer
    elif c > 9:
      print"verkeerd nummer"
      c=int(raw_input("zet het cijfer van het vakje waarin je de x wilt zetten"))
    else:
      print "hier staat al een x of een o"
      print "jouw beurt is voorbij , pech gehad"
    printschema()
    break

def printschema():
  for x in lijst:
    print x[0],x[1],x[2]

def einde():
  if lijst[0][0]=="x"==lijst[0][1]==lijst[0][2]:
    print "jij wint"
    einde2()
  if lijst[0][0]=="o"==lijst[0][1]==lijst[0][2]:
    print "de computer wint"
    einde2()
  if lijst[1][0]=="x"==lijst[1][1]==lijst[1][2]:
    print "jij wint"
    einde2()
  if lijst[1][0]=="o"==lijst[1][1]==lijst[1][2]:
    print "de computer wint"
    einde2()
  if lijst[2][0]=="x"==lijst[2][1]==lijst[2][2]:
    print "jij wint"
    einde2()
  if lijst[2][0]=="o"==lijst[2][1]==lijst[2][2]:
    print "de computer wint"
    einde2()
  if lijst[0][0]=="x"==lijst[1][0]==lijst[2][0]:
    print "jij wint"
    einde2()
  if lijst[0][0]=="o"==lijst[1][0]==lijst[2][0]:
    print "de computer wint"
    einde2()
  if lijst [0][1]=="x"==lijst[1][1]==lijst[2][1]:
    print "jij wint"
    einde2()
  if lijst [0][1]=="o"==lijst[1][1]==lijst[2][1]:
    print "de computer wint"
    einde2()
  if lijst[0][2]=="x"==lijst[1][2]==lijst[2][2]:
    print "jij wint"
    einde2()
  if lijst[0][2]=="o"==lijst[1][2]==lijst[2][2]:
    print "de computer wint"
    einde2()
  if lijst[0][0]=="x"==lijst[1][1]==lijst[2][2]:
    print "jij wint"
    einde2()
  if lijst[0][0]=="o"==lijst[1][1]==lijst[2][2]:
    print "de computer wint"
    einde2()
  if lijst[0][2]=="x"==lijst[1][1]==lijst[2][0]:
    print "jij wint"
    einde2()
  if lijst[0][2]=="o"==lijst[1][1]==lijst[2][0]:
    print "de computer wint"
    einde2()

def einde2():
  time.sleep(1)
  print "spel gedaan"
  lijst[0]=[x,x,x]
  lijst[1]=[x,x,x]
  lijst[2]=[x,x,x]
  print "spel is gerest"
  printschema()

def clearcheck():
  if lijst[0][0]!=x and lijst[0][1]!=x and lijst[0][2]!=x and lijst[1][0]!=x and lijst[1][1]!=x and lijst [1][2]!=x  and lijst[2][0]!=x and lijst[2][1]!=x  and lijst[2][2]!=x:
    print "spel gedaan"
    print "niemand wint"
    lijst[0]=[x,x,x]
    lijst[1]=[x,x,x]
    lijst[2]=[x,x,x]
    print "spel gereset"
    printschema()

def pcmove():
  print "de computer is aan de beurt"
  print "de computer berekent zijn zet"
  time.sleep(1)
  if lijst[0][0]==lijst[0][1]!=x and lijst[0][2]==x:
    lijst[0][2]="o"
  elif lijst[0][1]==lijst[0][2]!=x and lijst[0][0]==x:
    lijst[0][0]="o"
  elif lijst[0][0]==lijst[0][2]!=x and lijst[0][1]==x:
    lijst[0][1]="o"
  elif lijst[1][0]==lijst[1][1]!=x and lijst[1][2]==x:
    lijst[1][2]="o"
  elif lijst[1][1]==lijst[1][2]!=x and lijst[1][0]==x:
    lijst[1][0]="o"
  elif lijst[1][0]==lijst[1][2]!=x and lijst[1][1]==x:
    lijst[1][1]="o"
  elif lijst[2][0]==lijst[2][1]!=x and lijst[2][2]==x:
    lijst[2][2]="o"
  elif lijst[2][1]==lijst[2][2]!=x and lijst[2][0]==x:
    lijst[2][0]="o"
  elif lijst[2][0]==lijst[2][2]!=x and lijst[2][1]==x:
    lijst[2][1]="o"
  elif lijst[0][0]==lijst[1][0]!=x and lijst[2][0]==x:
    lijst[2][0]="o"
  elif lijst[0][0]==lijst[2][0]!=x and lijst[1][0]==x:
    lijst[1][0]="o"
  elif lijst[1][0]==lijst[2][0]!=x and lijst[0][0]==x:
    lijst[0][0]="o"
  elif lijst[0][1]==lijst[1][1]!=x and lijst[2][1]==x:
    lijst[2][1]="o"
  elif lijst[0][1]==lijst[2][1]!=x and lijst[1][1]==x:
    lijst[1][1]="o"
  elif lijst[1][1]==lijst[0][1]!=x and lijst[2][1]==x:
    lijst[2][1]="o"
  elif lijst[0][2]==lijst[1][2]!=x and lijst[2][2]==x:
    lijst[2][2]="o"
  elif lijst[0][2]==lijst[2][2]!=x and lijst[1][2]==x:
    lijst[1][2]="o"
  elif lijst[1][2]==lijst[0][2]!=x and lijst[0][2]==x:
    lijst[0][2]="o" 
  elif lijst[1][1]=="x"and lijst[0][0]==x:
    lijst[0][0]="o"
  else:
    f=random.randint(1,9)
    while f <10:
      if f==1 and lijst[0][0]==x:
        lijst[0][0]="o"
        break
      elif f==2 and lijst[0][1]==x:
        lijst[0][1]="o"
        break
      elif f==3 and lijst[0][2]==x:
        lijst[0][2]="o"
        break
      elif f==4 and lijst[1][0]==x:
        lijst[1][0]="o"
        break
      elif f==5 and lijst[1][1]==x:
        lijst[1][1]="o"
        break
      elif f==6 and lijst[1][2]==x:
        lijst[1][2]="o"
        break
      elif f==7 and lijst[2][0]==x:
        lijst[2][0]="o"
        break
      elif f==8 and lijst[2][1]==x:
        lijst[2][1]="o"
        break
      elif f==9 and lijst[2][2]==x:
        lijst[2][2]="o"
        break
      else:
        f=random.randint(1,9)
        
  print "de pc heeft gezet"
  printschema()

a=int(raw_input(menu))
while a<3:
  if a==1:
    print instructies
    a=int(raw_input(menu))
  elif a==2:
    print "spel begint"
    time.sleep(1)
    break

b=int(raw_input(menu2))
while b<3:
  if b==1:
    usermove()
    einde()
    clearcheck()
    pcmove()
    einde()
    clearcheck()
    time.sleep(1)
    b=int(raw_input(menu2))
  elif b==2:
    exit()

if you took the time to read up to here
thanks :)

ps.:
i hope my english is not to bad
oh yeah, the program is in dutch

Recommended Answers

All 2 Replies

I haven't tested it fully, but you could do something like this ...

def einde():
  # these are the winner positions
  win_pos = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),
    (1,4,7),(2,5,8),(0,4,8),(2,4,6))
  for q in win_pos:
    if q[0]==q[1]==q[2]=="x":
      print "jij wint"
      einde2()        
    if q[0]==q[1]==q[2]=="o":
      print "de computer wint"
      einde2()

thanks
that's a good solution to the problem.
but what i really wanted to ask is about the ai :making the computer set "o" when needed.
could i do something like this or just write it long like i've already done?

thanks in advance

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.