Skip to content

Instantly share code, notes, and snippets.

@qHack
Created November 16, 2013 08:34
Show Gist options
  • Save qHack/7497642 to your computer and use it in GitHub Desktop.
Save qHack/7497642 to your computer and use it in GitHub Desktop.
import random
calreadycalled = []
halreadycalled = []
computer = "O"
human = "X"
turn = "X"
lastmove = 0
def random_board(): #changed Added this from your email
Board = []
for square in range(16):
number = random.randrange(2, 33)
while number in Board:
number = random.randrange(2, 33)
Board.append(number)
return Board
def alternate_display_board(Board):
for i in range(16):
print (Board[i], '')
if (i +1 < 16):
if(((i+1) % 4 )== 0):
print("\n---------")
else:
print("|", '')
print("\n")
def computer_move(calreadycalled,currentbord, lastmove):
legal = False
count = 0
while not (legal) and (count < 5):
move = random.randrange(1, 19)
legal = legal_moves(calreadycalled,currentbord, lastmove, move)
calreadycalled.append(move)
count = count + 1
return int(move)
def legal_moves(alreadycalled, currentbord, lastmove, move):
if (move > 0) and (move < 19):
if not move in alreadycalled:
play = move + lastmove
print(play)
if play in currentbord:
print ("on board!- legal")
return True
else:
print ("not on board- illegal")
return False
else:
print("alrady called- illegal")
return False
else:
print("not 1 thru 18- ilegal")
return False
print("legal done")
def human_move(halreadycalled, currentbord, lastmove):
move = int(input("Pick a number from 1- 18: "))
legal = legal_moves(halreadycalled, currentbord, lastmove, move)
if legal:
move = move + lastmove
halreadycalled.append(move)
return int(move)
Board = random_board()
while True:
if turn == "X":
lastmove = human_move(halreadycalled, Board, lastmove)
print (lastmove)
Board[lastmove] = human
turn = "O"
else:
lastmove = computer_move(calreadycalled, Board, lastmove)
Board[lastmove] = computer
turn = "X"
alternate_display_board(Board)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment