def run(game_state, move_num, player_move): #Allow the computer to make it's move if not player_move: state = computerTurn(game_state, move_num) move_num += 1 #End the computer's move endMove(game_state, move_num) #Show possible moves displayValidMoves(game_state, move_num) #Save variables to be used later fileHandler.saveVariable(constants.VARIABLE_STATE, converter.toString(game_state)) fileHandler.saveVariable(constants.VARIABLE_MOVE, str(move_num)) fileHandler.saveVariable(constants.VARIABLE_MOVING, constants.VARIABLE_BOOL_FALSE) #Set up the window wn = constants.WINDOW wn.onclick(placePiece) wn.onkey(saveGame, constants.SAVE_KEY) wn.onkey(loadGame, constants.LOAD_KEY) wn.onkey(quitGame, constants.EXIT_KEY) wn.listen() wn.mainloop()
def reversiBoard(): grid = constants.TURTLE grid.fillcolor(constants.GRID_BG_COLOR) grid.pencolor(constants.GRID_FG_COLOR) grid.begin_fill() square() grid.end_fill() for y in constants.ROW_NUMBERS: #This stacks the rows of squares downwards. for x in constants.COLUMN_LETTERS: #This creates the squares in a horizontal row. grid.begin_fill() square() grid.end_fill() grid.forward(constants.CELL_WIDTH) nextRow() grid.penup() label_y() label_x() reversiFrame() grid.penup() grid.goto(20, 545) grid.pendown() grid.pencolor(constants.GRID_FG_COLOR) #List comprehension to put spaces between each letter in the game name spread_name_list = [(i + " ") for i in constants.GAME_NAME] spread_name_string = converter.toString(spread_name_list) grid.write(spread_name_string, False, constants.LEFT_TEXT, constants.GRID_NAME_STYLE)
def receive_data(): send_socket.send(converter.toString((0x52, 0x11, 240))) counter = 0 fout = open("results.dat", "a") previous_leds = 0 for n in range(1, 10000): counter += 1 if counter % 1000 == 0: print counter try: data = receive_socket.recv(const.MAX_PACKET_SIZE) except bluetooth.BluetoothError: print "connection closed" if len(data): dict_data = converter.toBytes(data) i = 0 result = [] for byte in dict_data: if i >= 7: result.append(byte) i += 1 fout.write(str(result) + "\n") # now we will report the points that are visible on the wiimote leds = 0 if len(result) > 0: if result[0] != 255: leds += 16 if result[3] != 255: leds += 32 if result[6] != 255: leds += 64 if result[9] != 255: leds += 128 if leds != previous_leds: send_socket.send(converter.toString((0x52, 0x11, leds))) previous_leds = leds print leds fout.close() print "Receiving finished."
def endMove(game_state, move_num): #Save the variables fileHandler.saveVariable(constants.VARIABLE_STATE, converter.toString(game_state)) fileHandler.saveVariable(constants.VARIABLE_MOVE, str(move_num)) #Reload the board loadGame(constants.TEMP_FILE, False) #Update the scoreboard updateScoreBoard( converter.toList(fileHandler.loadVariable(constants.VARIABLE_STATE))) #Update the window constants.WINDOW.update()
def updateGameState(game_state, move, turn_letter, draw_move=True): #Get the x and y coordinates in the move, #Subtract 1 so they can be used as list indexes xy = convertMove(move) x = xy[0] - 1 y = xy[1] - 1 #Create a copy of the state temp_game_state = converter.toList(converter.toString(game_state)) #Update the row temp_row = updateRow(temp_game_state[y], x, turn_letter) temp_game_state[y] = temp_row #Update the column temp_game_state = updateColumn(game_state, x, y, turn_letter, draw_move) #Update the diagonals in both directions temp_game_state = updatediagonalDU2(temp_game_state, x, y, turn_letter, draw_move) temp_game_state = update_diagonalUD2(game_state, x, y, turn_letter, draw_move) #Update the row temp_row = updateRow(temp_game_state[y], x, turn_letter) temp_game_state[y] = temp_row #Because updateRow does not draw the pieces on the board, unlike the other update functions, we must draw them here if draw_move == True: counter = 0 for i in temp_row: conv_move = convertMoveType(counter, y + 1) Tx = conv_move[0] Ty = conv_move[1] if i != constants.PIECE_NONE: if i == constants.PIECE_BLACK: temp_piece = constants.PIECE_COLOR_BLACK else: temp_piece = constants.PIECE_COLOR_WHITE if draw_move: turtleMove.placePiece(Tx, Ty, temp_piece) counter = counter + 1 return temp_game_state
def directionStrCheckList(game_state, stringID, turn_colour, isTesting=False): if game_state == type(list): string_state = converter.toString(game_state) string_state = CheckNorth(game_state, stringID, turn_colour, isTesting) print(string_state) string_state = CheckNorthEast(game_state, stringID, turn_colour, isTesting) print(string_state) string_state = CheckEast(game_state, stringID, turn_colour, isTesting) print(string_state) string_state = CheckSouthEast(game_state, stringID, turn_colour, isTesting) string_state = CheckSouth(game_state, stringID, turn_colour, isTesting) string_state = CheckSouthWest(game_state, stringID, turn_colour, isTesting) string_state = CheckWest(game_state, stringID, turn_colour, isTesting) string_state = CheckNorthWest(game_state, stringID, turn_colour, isTesting) list_state = converter.toList(string_state) return list_state
def countUpdatedPieces(game_state, move, move_num): #Create a deep copy of game state state_string = converter.toString(game_state) copy_state = converter.toList(state_string) #Find which piece is being placed piece = listInterpret.whoseTurn(move_num) #Get an updated state after the move changed_state = listUpdater.updateGameState(copy_state, move, piece, False) #Count how many pieces changed change_count = 0 #Traverse both states and compare pieces for i in range(len(game_state)): for j in range(len(game_state[i])): if game_state[j][i] != changed_state[j][i]: change_count += 1 return change_count
def initialise_ir_camera(): """ This isitialisation sequence is based on this http://homepage.mac.com/ianrickard/wiimote/wiili_wimote.html#marcan.27s_info """ # First we need to tell the wiimote to start sending data to us # SET_REPORT to OUTPUT_WHEN_CHANGED on READ_IR_DATA_ONLY string1 = converter.toString( (const.PACKET_HEADER, const.SET_REPORT, const.OUTPUT_WHEN_CHANGED, const.REPORT_ACC_IR) ) # Then we have to turn on the IR camera # set IR_ENABLER_1 to CONTINUOUS_OUTPUT string2 = converter.toString((const.PACKET_HEADER, const.IR_SENSOR_ENABLER_1, const.OUTPUT_CONTINUOUSLY)) # set IR_ENABLER_2 to CONTINUOUS_OUTPUT string3 = converter.toString((const.PACKET_HEADER, const.IR_SENSOR_ENABLER_2, const.OUTPUT_CONTINUOUSLY)) # send_socket.send(string) ## # Now we need to write to the wiimote memory some information about # sensitivity of the sensors. The last memory write sets the wiimote # to output specific data format. # # Note: The format of the memory writes is as follows: # packet header (1byte), memory-write report ID (1byte), # flags (1byte), memory location (3bytes), data size (1byte), # data (16bytes) ## string4 = converter.toString( ( const.PACKET_HEADER, const.WRITE_DATA_TO_MEMORY, 0x04, 0xB0, 0x00, 0x30, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ) ) string5 = converter.toString( ( const.PACKET_HEADER, const.WRITE_DATA_TO_MEMORY, 0x04, 0xB0, 0x00, 0x06, 0x01, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ) ) string6 = converter.toString( ( const.PACKET_HEADER, const.WRITE_DATA_TO_MEMORY, 0x04, 0xB0, 0x00, 0x08, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ) ) string7 = converter.toString( ( const.PACKET_HEADER, const.WRITE_DATA_TO_MEMORY, 0x04, 0xB0, 0x00, 0x1A, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ) ) string9 = converter.toString( ( const.PACKET_HEADER, const.WRITE_DATA_TO_MEMORY, 0x04, 0xB0, 0x00, 0x33, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ) ) ## # Now that we have all the sequences prepared, we can send them to the # wiimote. However, for some strange reason, we cannot just send them # one after another.If we do that, the wiimote doesn't start outputting # data as it should, because the IR camera is not properly initialised # for some reason. The only way I got it to work was to add a 0.1 second # pause between each send. I honestly don't know why this delay works, # it's just a magic number, but it works. # My guess would be that the wiimote requires certain amount of time to # write the stuff to memory, so if it gets another request for writing # to memory too soon, the data does not get written properly and so the # initialisation sequence is broken. ## send_socket.send(string1) time.sleep(0.01) send_socket.send(string2) time.sleep(0.01) send_socket.send(string3) time.sleep(0.01) send_socket.send(string4) time.sleep(0.01) send_socket.send(string5) time.sleep(0.01) send_socket.send(string6) time.sleep(0.01) send_socket.send(string7) time.sleep(0.01) send_socket.send(string9) print "IR camera initialised"