def start_Game(player1, player2, current_state=None): if current_state is None: current_state = createGameBoard(3, 3) print(display(current_state)) turn = 0 while find_winner(current_state) is None: if find_next_player(current_state) == 1: if player1 == 'c': compSeed = 'X' move, current_state = get_move_AI(current_state, humanSeed, compSeed) else: humanSeed = 'X' move, current_state = get_move(current_state) else: if player2 == 'c': compSeed = 'O' move, current_state = get_move_AI(current_state, humanSeed, compSeed) else: humanSeed = 'O' move, current_state = get_move(current_state) print(display(current_state)) winner = find_winner(current_state) if winner == 0: print("No one wone. Game tie") elif winner == 1: print("Player 1 won the game") elif winner == -1: print("Player 2 won the game") turn += 1
def move(boardlocal, piece, playing): #not_full is for the collumns not_full = True #not_full tells whether the collum is full without losing there go #player one move while not_full: play = [1, 2] #this tries to place the piece and if it cant it the input is not a number(or restart) try: #this checks whether the number is in the range of the boardlocal while not len(play) == 1 or not int(play[0]) < 8: play = list(input(' Choose a collum. ')) not_valid = False if (not len(play) == 1 or not int(play[0]) < 8): print(' That is not a valid space.') not_valid = True time.sleep(0.5) board.display(boardlocal) #if you tell it to restart it comes out of the function and since the function is put in a while loop it restarts and clears the boardlocal except: print(' That is not a number') time.sleep(1) bottom = 5 #this tries to place the piece at the lowest point it can and if it cant place it the collum is full. try: #this checks whether the number is in the range of the boardlocal while boardlocal[bottom][(int(play[0]) - 1)] == '0' or boardlocal[bottom][( int(play[0]) - 1)] == 'X': bottom -= 1 if not boardlocal[bottom][(int(play[0]) - 1)] == '0' or not boardlocal[bottom][ (int(play[0]) - 1)] == 'X': boardlocal[bottom][(int(play[0]) - 1)] = piece not_full = False except: #tells the players the collum is full print(' That collum is full.') time.sleep(0.5) not_full = True board.display(boardlocal) #checks if that move wins if not not_valid: if win.check(boardlocal, piece) == True: print(' ', piece, '\'s win the game!') time.sleep(2) playing = False return playing not_full = True
import pyb from board import sys_config, board, display from lsm6ds3 import LSM6DS3 from demos import gravity # # A large memory block is needed # Therefore we allocated it very early # before fragmentation of memory happends frog = board() disp = display(frog) disp.on() i2c = pyb.I2C(sys_config['lis3mdl']['i2c_bus'], pyb.I2C.MASTER, baudrate=100000) mems = LSM6DS3(i2c, sys_config['lsm6ds3']['i2c_addr']) print("Acceleration (%.3e, %.3e, %.3e)" % mems.accel.xyz()) print("Rotation (%.3e, %.3e, %.3e)" % mems.gyro.xyz()) print("Temperature %.2f C" % mems.temperature()) gravity(disp, mems.accel)
else: score= score + 1 print "pos: ",row,"/",col," direction: ",direction," score: ",score # left if direction != right and col > 0: if b[row][col-1] == colour: score = score + getleft(row, col-1, colour, score) else: score = score + 1 print "pos: ",row,"/",col," direction: ",direction," score: ",score print "score: ", score return score red, pink, blue, purple, yellow, green = range(6) random.seed(2) b = [[random.randint(0, 5) for col in range(25)] for row in range(25)] board.display(b) colour = b[0][0] getscore(0, 0, colour, 0, 999)
#x = Random('Xerxes') #o = Random('Olle') #x = Modulo('Xerxes') #o = Modulo('Olle') #x = FindWinner('Xerxes') #o = FindWinner('Olle') #x = MonteCarlo('Xerxes') #o = MonteCarlo('MonteCarlo') o = Player('MonteCarlo') board = board.Board() print(board.display()) while True: m = x.move(board) board.move(m) print(board.display()) if board.calc(): print(x.name + " vann!") break m = o.move(board) board.move(m) print(board.display()) if board.calc(): print(o.name + " vann!") break
#x = Random('Xerxes') #o = Random('Olle') #x = Modulo('Xerxes') #o = Modulo('Olle') #x = FindWinner('Xerxes') #o = FindWinner('Olle') #x = MonteCarlo('Xerxes') #o = MonteCarlo('MonteCarlo') o = Player('MonteCarlo') board = board.Board() print board.display() while True: m = x.move(board) board.move(m) print board.display() if board.calc(): print x.name + " vann!" break m = o.move(board) board.move(m) print board.display() if board.calc(): print o.name + " vann!" break
def update_grid(board: board.Board, location: tuple): """ This changes the game grid with a 1 for player 1 and a 2 for player 2 """ # Declare global variables global arr global player_1 global player_2 global mill_check global last_x global last_y global selected # If there is currently not a mill if mill_check == 0: ####STAGE 1#### # If the player has a token if player_1.start_tokens != 0 or player_2.start_tokens != 0: # If location in not usable then pass through function if (location[0] < 0) or (location[1] < 0): pass # if location is usable elif board.grid[location[0]][location[1]] == 0: print(board.grid[location[0]][location[1]]) # If this is player 1 if player_1.turn: place_piece(board, location, player_1, TOKEN_ONE) # If this is player 2 elif player_2.turn: place_piece(board, location, player_2, TOKEN_TWO) ####STAGE 2#### # If no start tokens begin to check board tokens for adjacency if player_1.turn and player_1.start_tokens == 0 and player_1.board_tokens > 3: move_piece(board, location, TOKEN_ONE) # Player 2 checking for adjacency after tokens are placed if player_2.turn and player_2.start_tokens == 0 and player_2.board_tokens > 3: move_piece(board, location, TOKEN_TWO) ####STAGE 3#### # Flying phase player 1! if player_1.turn and player_1.start_tokens == 0 and player_1.board_tokens == 3: fly_piece(board, location, TOKEN_ONE) # Flying phase player 2! if player_2.turn and player_2.start_tokens == 0 and player_2.board_tokens == 3: fly_piece(board, location, TOKEN_TWO) # If a mill just occurred and a piece is about to be removed else: removable = True # If location in not usable then pass through function if (location[0] < 0) or (location[1] < 0): pass # If location has a player 2 tile and this is player 1 elif board.grid[location[0]][location[1]] == 2 and player_1.turn: # Set found mills mill_check = check_adjacent(location[0], location[1], board, 2) # If the piece being removed has mill/s if mill_check > 0: # If the number of mills don't match placed tokens if player_2.mills / player_2.board_tokens < .30: removable = False # If there is a stacked mill check to see if there is a loose token else: for i in range(0, 7): for j in range(0, 7): if board.grid[i][j] == 2: if check_adjacent(i, j, board, 2) == 0: removable = False # If it passes the test if removable: # Check how many mills will be removed and take them from player if mill_check > 0: player_2.mills -= mill_check # Set tile to empty board.grid[location[0]][location[1]] = 0 # Remove token from Player 2 player_2.board_tokens -= 1 # Swap players swap_player(player_1, player_2) # Set mill check to false now that it has occurred mill_check = 0 # If location has a player 1 tile and this is player 2 elif board.grid[location[0]][location[1]] == 1 and player_2.turn: # Set found mills mill_check = check_adjacent(location[0], location[1], board, 1) # If the piece being removed has mill/s if mill_check > 0: # If the number of mills don't match placed tokens if player_1.mills / player_1.board_tokens < .30: removable = False # If there is a stacked mill check to see if there is a loose token else: for i in range(0, 7): for j in range(0, 7): if board.grid[i][j] == 1: if check_adjacent(i, j, board, 1) == 0: removable = False # If it passes the test if removable: # Check how many mills will be removed and take them from player if mill_check > 0: player_1.mills -= mill_check # Set tile to empty board.grid[location[0]][location[1]] = 0 # Remove token from Player 1 player_1.board_tokens -= 1 # Swap players swap_player(player_1, player_2) # Set mill check to false now that it has occurred mill_check = 0 # Output board in console to see update made board.display() # Print click (x,y) print(location[1], location[0]) # Print all tracked tokens and mills for each player print(player_1.start_tokens, player_1.get_total_tokens(), player_1.board_tokens, player_1.mills) print(player_2.start_tokens, player_2.get_total_tokens(), player_2.board_tokens, player_2.mills)
if b[row + 1][col] == colour: score = score + getdown(row + 1, col, colour, score) else: score = score + 1 print "pos: ", row, "/", col, " direction: ", direction, " score: ", score # left if direction != right and col > 0: if b[row][col - 1] == colour: score = score + getleft(row, col - 1, colour, score) else: score = score + 1 print "pos: ", row, "/", col, " direction: ", direction, " score: ", score print "score: ", score return score red, pink, blue, purple, yellow, green = range(6) random.seed(2) b = [[random.randint(0, 5) for col in range(25)] for row in range(25)] board.display(b) colour = b[0][0] getscore(0, 0, colour, 0, 999)
player2 = player.Player("O", "X") # show the initial board board.display() currentPlayer = player1 winner = None if __name__ == '__main__': while board.hasEmpties() and winner is None: print "======= + Player " + currentPlayer.my_token + " is a ", if currentPlayer.human: print "human" else: print "computer" print "=======" playIndex = currentPlayer.play(board) board.display() if board.won(playIndex): winner = currentPlayer print "Congrats to Player " + currentPlayer.my_token + "!" elif board.hasEmpties() == False: print "Congrats to the cat. Meow! " else: currentPlayer = player2 if currentPlayer == player1 else player1