#!/usr/local/bin/ioL /usr/bin/python3
import random
def mainMenu(): #provides an interface for the user to begin a new game
global numRows
global numCols
print('
')
print('')
print ('Rows: ')
print ('Columns: ')
print (',}>>') #request user input to start a new game to be returned in the format 'b,rows,cols' -- b means begin.
print ('}>>')
print('}>') #end box
#add a text area for an error message if the inputs aren't valid
print('1: You need to make a horizontal or vertical line to win the game. Diagonal lines have not been implemented yet.2. The computer player isn\'t very bright.}>')
while True: #wait for the user to enter valid input
choice = input().split(',')
print('') #clear any previous error message
if choice[0] == 'q': exit()
elif choice[0] == 'b': #begin new game. But validate the input first...
try:
numRows = int(choice[1])
numCols = int(choice[2])
if numRows < 1 or numCols < 1:
print('')
else:
return True
except:
print('')
def drawBoard(rows, cols):
print('')
#prints the game board table to the screen.
print(' with the row number (e.g. ''. This effectively defines a new tag that we can now use to manipulate it.)
for c in range(cols):
print('} border-left=1 border-right=3 outer-margin-top=0 outer-margin-bottom=0 margin=0.5,{em} width=2.5,{em}')
coordStr = str(r) + ',' + str(c)
print ('onmousein=}>') #defines actions for when the user interacts with the board
print ('onmouseout=}>') #mouse out
print ('onclick=}>') #click
print('>') #end of table data cell
print('>') #end of
print('>') #end of table
print('') #clear the loading message.
def setBoardCell(row,col,value,color):
print('') #set the value string
print( '') #set the color string (e.g. red or blue player's piece)
print('>>')
def isCellVacant(row,col):
#returns 0 if a piece is already in this spot, otherwise returns the topmost occupied row number in this column. (== number of unoccupied rows)
global board
global numRows
if board[row][col] != 0: return 0 #occupied.
for r in range(row, numRows):
if board[r][col] != 0: return r
return numRows #nothing occupied in this column
def furtherMovesPossible():
#returns True iff at leas one further move can be made.
global board
global numCols
for c in range(numCols):
if board[0][c] == 0: #found one! We only need to check the top row for each columns -- these are the last to be filled.
return True
return False
def isWinningMove(row,col):
#returns True if the specified cell is part of a winning move, regardless of whether the cell has been taken by human or computer player.
global numRows
global numCols
global board
#1. check for 4 consecutive cells horizontally, with the same value as the current one.
r = row
c = col
connected = 1
for c in range(col-1,-1,-1): #check elements to the left
if board[row][c] != board[row][col]: break
else: connected += 1
for c in range(col+1, numCols): #check elements to the right
if board[row][c] != board[row][col]: break
else: connected +=1
if connected > 3: return True
#2. check vertically now.
r = row
c = col
connected = 1
for r in range(row-1,-1,-1): #check elements to the left
if board[r][col] != board[row][col]: break
else: connected += 1
for r in range(row+1, numRows): #check elements to the right
if board[r][col] != board[row][col]: break
else: connected +=1
if connected > 3: return True
##############main###############
print('') # set titlebar text
print('}>>') # create an exit handler
while True: #allow multiple games until the user quits.... TODO!
mainMenu()
print('') #messages from the computer player will be displayed in this span.
print('}>>')
print('')
while True:
cmd = input().split(',')
if cmd[0] == 'q': exit() #user clicked quit button, or triggered our exit handler by closing the window.
row = int(cmd[1])
col = int(cmd[2])
if cmd[0] == 'i': #mouse into cell - provide visual feedback only if the cell if vacant
if isCellVacant(row,col): setBoardCell(row,col, "#9676>", "")
continue
elif cmd[0] == 'o': #mouse out of cell - reset the appearance of the cell if it's vacant.
if isCellVacant(row,col): setBoardCell(row,col, " >", "")
continue
elif cmd[0] == 'c': #user has clicked a cell - have they made a valid move?
freeRowsHere = isCellVacant(row,col)
if freeRowsHere > 0: #they've selected a vacant cell. Good.
setBoardCell(row,col, " >", "") #clear the visual feedback on the cell the user has clicked.
row = freeRowsHere-1 #the player's piece will fall down to the bottommost free cell.
board[row][col] = 1 #human player occupies this cell now.
setBoardCell(row,col,"#9678>","cyan")
#now determine whether the player has won
if isWinningMove(row,col):
print('')
break #game over
#now it's the computer's turn to move...
print('')
if not furtherMovesPossible():
print('')
break #game over
while True : #computer will keep picking random columns until it finds an unfilled one.
col = random.randrange(0,numCols) #pick a random column number
if isCellVacant(0,col): break
row = isCellVacant(0,col) - 1
board[row][col] = 2 #computer player occupies this cell now.
setBoardCell(row,col,"#9677>","orange")
#now determine if computer has won:
if isWinningMove(row,col):
print('')
break #game over
if not furtherMovesPossible():
print('')
break #game over
else:
print('')
#game over.
print('') #make the final dialog text bold
print('}>') #terminate the baseframe.
while True:
nextAction = input().strip()
if nextAction == 'q': exit()
#END OF FILE