import sys, imp import string, random, time COLORS = ['B','W'] TURN_TIME_LIMIT = 15 # seconds CONSECUTIVE_PASS_LIMIT = 2 PAUSE_BETWEEN_MOVES = False class Matchmaker: def __init__(self): self.board = [[' ']*8 for i in range(8)] self.size = 8 self.board[4][4] = 'W' self.board[3][4] = 'B' self.board[3][3] = 'W' self.board[4][3] = 'B' # a list of unit vectors (row, col) self.directions = [ (-1,-1), (-1,0), (-1,1), (0,-1),(0,1),(1,-1),(1,0),(1,1)] #prints the board def PrintBoard(self): # Print column numbers print(" ",end="") for i in range(self.size): print(i+1,end=" ") print() # Build horizontal separator linestr = " " + ("+-" * self.size) + "+" # Print board for i in range(self.size): print(linestr) # Separator print(i+1,end="|") # Row number for j in range(self.size): print(self.board[i][j],end="|") # board[i][j] and pipe separator print() # End line print(linestr) #determines the score of the board by adding +1 for every tile owned by player, and -1 for every tile owned by opp def score(self, player, opp): score = 0 for i in range(self.size): for j in range(self.size): if(self.get_square(i,j)==player): score +=1 elif(self.get_square(i,j)==opp): score -= 1 return score #returns true if the square was played, false if the move is not allowed def place_piece(self, row, col, player, opp): if(self.get_square(row,col)!=" "): return False if(player == opp): print("player and opponent cannot be the same") return False legal = False #for each direction, check to see if the move is legal by seeing if the adjacent square #in that direction is occuipied by the opponent. If it isnt check the next direction. #if it is, check to see if one of the players pieces is on the board beyond the oppponents piece, #if the chain of opponents pieces is flanked on both ends by the players pieces, flip #the opponents pieces for Dir in self.directions: #look across the length of the board to see if the neighboring squares are empty, #held by the player, or held by the opponent for i in range(self.size): if ((( row + i*Dir[0])=0 ) and (( col + i*Dir[1])>=0 ) and (( col + i*Dir[1])=0 ) and (( col + i*Dir[1])>=0 ) and (( col + i*Dir[1]) TURN_TIME_LIMIT): print("%s took %.1fs to play, which exceeded the time limit (%.1fs)." % (teamNames[curPlayerID], runTime, TURN_TIME_LIMIT)) print("%s (%s) is the winner." % (teamNames[1-curPlayerID], oppColor)) return # If player has returned an illegal move, player loses elif (not mm.islegal(move[0], move[1], playerColor, oppColor)): print("%s has entered an illegal move." % teamNames[curPlayerID]) print("%s (%s) is the winner." % (teamNames[1-curPlayerID], oppColor)) return # Valid move acquired print("%s (%s) places a piece at (%d,%d) in %.1fs" % (teamNames[curPlayerID],playerColor, move[0]+1, move[1]+1,runTime)) print() mm.place_piece(move[0], move[1], playerColor, oppColor) prevMove = move consecutivePasses = 0 mm.PrintBoard() print() # Check whether game has ended if mm.all_pieces(playerColor) or mm.full_board(): break # Change player curPlayerID = 1 - curPlayerID finalScore = mm.score(COLORS[0], COLORS[1]) if (finalScore == 0): print("The game is a draw.") elif(finalScore > 0): print("%s (%s) wins by %d pieces." % (teamNames[0],COLORS[0],finalScore)) else: print("%s (%s) wins by %d pieces." % (teamNames[1],COLORS[1],-finalScore)) main()