def test_one_game_round_ending_as_tie(self): ai = AutomaticInput() # player starts with top right corner self.assertEqual(4, ai.next_move('o', make_board(' x '))) # player moves to lower right corner self.assertEqual(5, ai.next_move('o', make_board(' x o x'))) # player blocks middle left self.assertEqual(0, ai.next_move('o', make_board(' xxoo x'))) # player moves to lower left corner self.assertEqual(7, ai.next_move('o', make_board('o xxoox x')))
def main(player1, player2): game_winner = False start_text = Style.Foreground.violet + "Let's get started!" + Style.end board_at_start = make_board(player1.pawn, player2.pawn, empty_cell) move = 1 print("Hello players!") if_instructions = input("Would you like to see the instructions?\n" "y/n\n") if if_instructions == "n": pass else: instructions() print(start_text) board = board_at_start while not game_winner: if (move % 2) == 0: playing_player = player2 else: playing_player = player1 print_board(board) board = player_move(playing_player, board) board = check_if_player_has_a_queen(playing_player, board) game_winner = check_if_player_won(player1, player2, board) move += 1 print("Congratulations, " + game_winner.name + ", you won!!!! ") print(game_winner.color + """ o o^/|\\^o o_^|\\/*\\/|^_o o\\*`'.\\|/.'`*/o \\\\\\\\\\\\|////// {><><@><><} `\"\"\"\"\"\"\"\"\"`""" + Style.end)
def reset(self): self.running = True self.board = game.make_board() self.solutions = game.solver(self.board) self.gui.reset(self.board, self.solutions) self.exit_flag.clear() self.thread1 = threading.Thread(target = self.aiThread1) self.thread2 = threading.Thread(target = self.clockThread2) self.thread1.start() self.thread2.start()
def game(): player_move = 0 print( "Welcome to the TicTacToe game \'Kirby The Great\' created. This is a two(2) player game. Please enjoy!\n" ) make_board() player1, player2 = create_player() print( f'{player1.name} will play {player1.symbol} and {player2.name} will play {player2.symbol}' ) while True: while True: p1_pos = input( f"{player1.name}, please select your move in positions: ") if p1_pos.isdigit() == False or int(p1_pos) > 9: print("Please enter a number between 1 - 9.") continue elif loc[p1_pos] != " ": print( 'This place is taken. Please select a different position.') continue else: player_move += 1 loc[p1_pos] = player1.symbol make_board() break check_1 = have_winner(player_move, player1, player2) if check_1: replay() while True: p2_pos = input( f"{player2.name}, please select your move in positions.") if p2_pos.isdigit() == False or int(p2_pos) > 9: print("Please enter a number between 1 - 9.") continue elif loc[p2_pos] != " ": print( 'This place is taken. Please select a different position.') continue else: player_move += 1 loc[p2_pos] = player2.symbol make_board() break check_2 = have_winner(player_move, player1, player2) if check_2: replay() if player_move > 7 and (not check_1 and not check_2): print('It\'s a tie! No winner.') replay()
def main(): board_ = board.make_board(3, 3) i = 0 curr_turn = TURNS[i % 2] while not board.is_full(board_) and \ not board.is_winner(board_, curr_turn): curr_turn = TURNS[i % 2] if curr_turn == TURNS[0]: board_ = ask_human_move(board_, curr_turn) else: # curr_turn == TURNS[1] board_ = ask_computer_move_random(board_, curr_turn) print(board_) i += 1
def __init__(self, master): self.master = master self.board = game.make_board() self.solutions = game.solver(self.board) self.messageFlag = False self.line = queue.Queue() self.lock = threading.Lock() self.timer = False self.gui = GuiPart(master, self.line, self.board, self.solutions) self.running = 1 self.exit_flag = threading.Event() self.thread1 = threading.Thread(target = self.aiThread1) self.thread2 = threading.Thread(target = self.clockThread2) self.thread1.start() self.thread2.start() self.gui.countDown() # print ( len( self.solutions)) self.updateGUI()
def markovDecision(args): if len(args) > 15: print("Unsupported input length") return [], [] board = make_board(args) return markov_decision(board)
def test_blocks_possible_wins(self): ai = AutomaticInput() self.assertEqual(0, ai.next_move('o', make_board(' xx '))) self.assertEqual(0, ai.next_move('o', make_board(' x x '))) self.assertEqual(0, ai.next_move('o', make_board(' x x'))) self.assertEqual(1, ai.next_move('o', make_board('x x '))) self.assertEqual(1, ai.next_move('o', make_board(' x x '))) self.assertEqual(2, ai.next_move('o', make_board(' x x '))) self.assertEqual(2, ai.next_move('o', make_board('xx '))) self.assertEqual(2, ai.next_move('o', make_board(' x x'))) self.assertEqual(3, ai.next_move('o', make_board(' xx '))) self.assertEqual(3, ai.next_move('o', make_board('x x '))) self.assertEqual(4, ai.next_move('o', make_board(' x x '))) self.assertEqual(4, ai.next_move('o', make_board(' x x '))) self.assertEqual(4, ai.next_move('o', make_board(' x x '))) self.assertEqual(4, ai.next_move('o', make_board('x x'))) self.assertEqual(5, ai.next_move('o', make_board(' x x'))) self.assertEqual(5, ai.next_move('o', make_board(' xx '))) self.assertEqual(6, ai.next_move('o', make_board('x x '))) self.assertEqual(6, ai.next_move('o', make_board(' xx'))) self.assertEqual(6, ai.next_move('o', make_board(' x x '))) self.assertEqual(7, ai.next_move('o', make_board(' x x'))) self.assertEqual(7, ai.next_move('o', make_board(' x x '))) self.assertEqual(8, ai.next_move('o', make_board(' xx '))) self.assertEqual(8, ai.next_move('o', make_board(' x x '))) self.assertEqual(8, ai.next_move('o', make_board('x x ')))
def test_occupies_center_cell_when_player_chose_corner(self): ai = AutomaticInput() self.assertEqual(4, ai.next_move('o', make_board('x '))) self.assertEqual(4, ai.next_move('o', make_board(' x '))) self.assertEqual(4, ai.next_move('o', make_board(' x '))) self.assertEqual(4, ai.next_move('o', make_board(' x')))
def test_can_block_wins_for_o(self): ai = AutomaticInput() self.assertEqual(0, ai.next_move('x', make_board(' oo ')))
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sun Oct 7 17:12:14 2018 @author: ubuntu """ from mcts import encode_position, TreeNode from board import Position, make_board, empty_board from net import GobangModel p0 = Position(make_board(empty_board), 'a', 0, -1) p0.show() x = encode_position(p0) print x[10:15, 0, 0] print x[10:15, 0, 1] print x[10:15, 0, 2] p1 = p0.move(18) y = encode_position(p1) print y[10:15, 0, 0] print y[10:15, 0, 1] print y[10:15, 0, 2] p2 = p1.move(37) z = encode_position(p2) print z[10:15, 0, 0] print z[10:15, 0, 1] print z[10:15, 0, 2]
def print_board_coordinates(): TerminalDisplay().show_board_state(make_board('123456789'))
(x, comma, y) = position.partition(",") x = int(x) y = int(y) direction = raw_input("direction, h or v?") if 'h' in direction: direction = 'horizontal' elif 'v' in direction: direction = 'vertical' else: raise Exception word = raw_input("word?") return ((x,y), direction, word) except: pass myboard = board.make_board() bag = letters.initializebag() players = [["Alice", [], False], ["Bob", [], False]] while True: for player in players: letters.refillrack(player[1],bag) board.print_board(myboard) print "you are " + player[0] letters.printrack(player[1]) player[2] == False while player[2] == False: passes = raw_input("Pass?")
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sun Oct 7 16:56:36 2018 @author: ubuntu """ from board import make_board, Position, empty_board p0 = Position(empty_board, 'A', 0, -1) p0.show() print '===========' p1 = p0.move(0) p1.show() print '===========' p2 = p1.move(1) p2.show() print '===========' p3 = p2.move(2) p3.show() print '===========' board = make_board(empty_board) p4 = Position(board, 'A', 10, -1) p4.show() p5 = p4.flip_clock() print '===========' p5.show() print '===========' p6 = p4.flip_reverse_clock() p6.show()
def setup(self): """ Setup and start game window """ print("Starting setup") given = 0 if self.difficulty == "easy": given = random.randint(36, 40) if self.difficulty == "medium": given = random.randint(30, 34) if self.difficulty == "hard": given = random.randint(19, 27) print(given) board_array = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ] print("Generating board") print_board(board_array) board_array = make_board() print_board(board_array) for counter in range(81 - given): row = random.randint(0, 8) col = random.randint(0, 8) while board_array[row][col] == 0: row = random.randint(0, 8) col = random.randint(0, 8) board_array[row][col] = 0 print(given) print("Board finished") data.gridobj = Grid(board_array, 9, 9) self.Dialog = QtWidgets.QDialog() self.ui = UiForm() self.ui.setup_ui(self.Dialog) labelslist = [] rowcounter = 0 itemcounter = 1 data.labels = [[], [], [], [], [], [], [], [], []] for child in self.Dialog.findChildren(QLabel): if child.objectName() != "strikes": labelslist.append(child) labelslist.sort(key=lambda x: int(x.objectName())) for item in labelslist: data.labels[rowcounter].append(item) if itemcounter % 9 == 0: rowcounter += 1 itemcounter = 0 itemcounter += 1 for rowcounter in range(0, 9): for colcounter in range(0, 9): if board_array[rowcounter][colcounter] != 0: data.labels[rowcounter][colcounter].setText( str(board_array[rowcounter][colcounter])) data.labels[rowcounter][colcounter].setStyleSheet( "color: rgb(40, 255, 6); border: " "2px solid white !important") self.Dialog.show()
import board import player score = 0 grid = board.make_board(6, 6) # Creates randomly generated numbers. board.create_elements(grid) # Checks for already existing match and replaces it with other randomly generated numbers. board.check_match(grid) while score < 500: # Checks if a possible match exists. if board.change_x_elements(grid) or board.change_y_elements(grid): # Displays the valid board to the user. board.print_pretty_board(grid) # Prompts the user on where to switch. player.switch_elements(grid) if board.x_match_made(grid): # Replaces matched elements in the row. board.replace_x_matched(grid) score += 50 # If a match is made after replacing it is replaced and points are awarded. while board.x_match_made(grid) or board.y_match_made(grid): if board.x_match_made(grid): board.replace_x_matched(grid) score += 50 elif board.y_match_made(grid): board.replace_y_matched(grid) score += 50 print('Your current score is ' + str(score)) elif board.y_match_made(grid):
def instructions(): """ Prints the instructions to the game. """ pawn1 = Style.Foreground.red + "0" + Style.end pawn2 = Style.Foreground.blue + "0" + Style.end empty_cell = 0 queen = Style.Foreground.red + "q" + Style.end regular_board = make_board(pawn1, pawn2, empty_cell) ex_board1 = [[pawn1, empty_cell, empty_cell, empty_cell], [empty_cell, empty_cell, empty_cell, empty_cell], [empty_cell, queen, empty_cell, empty_cell], [empty_cell, empty_cell, empty_cell, pawn2]] print("The checkers board is 8 by 8. This is how it looks at the start:") print_board(regular_board) input("Press Enter to continue.") print("\n" "Each player has pieces in different color:\n" + Style.Foreground.blue + "Blue" + Style.end + " or " + Style.Foreground.red + "Red" + Style.end + ".\n" "There are two kinds of pieces: pawn-", pawn1, "and queen-", queen + ".\n" "A pawn that crosses the board and reaches the other players' side becomes a queen.") input("Press Enter to continue.") print("\n" "The rows in the board are marked in numbers. \n" "The columns in the board are marked in capital letters.\n" "Each piece is marked first by the row number and then by the column letter. \n" "for example:\n" + pawn1, "is in" + Style.Foreground.cyan + " 0A" + Style.end + ".\n" + queen, "is in" + Style.Foreground.cyan + " 2B" + Style.end + ".\n" + pawn2, "is in" + Style.Foreground.cyan + " 3D" + Style.end + ".\n") print_board(ex_board1) input("Press Enter to continue.") print("\n" "After choosing a piece you'll need to pick where to move. \n" "You can pick to move:", Style.Foreground.cyan + "left \\ right" + Style.end + ". \n" "You can also write", Style.Foreground.cyan + "l \\ r" + Style.end + " for short. \n" "It is also possible to write", Style.Foreground.cyan + "a \\ d" + Style.end + " respectively. \n" + Style.Foreground.red + "Clarification:\n" + Style.end + "left is always: <---- \n" "right is always: ---->") input("Press Enter to continue.") print("\n" "of course if you choose a queen, you'll need also to choose how to move vertically. \n" "You can pick to move:", Style.Foreground.cyan + "up \\ down" + Style.end + ". \n" "You can also write", Style.Foreground.cyan + "u \\ d" + Style.end + " for short. \n" "It is also possible to write", Style.Foreground.cyan + "w \\ s" + Style.end + " respectively. \n" + Style.Foreground.red + "Clarification: " + Style.end + "write the move correctly, otherwise, the computer won't understand your move! \n") input("Press Enter to continue.") print("\n" "Lastly - if you choose a queen - you'll need to chose the number of steps." + Style.Foreground.red + "Clarification: " + Style.end + "jumping over a pawn counts as two steps!") input("Press Enter to continue.") print("\n" "If you changed your mind and would like to move a different piece, " "simply write", Style.Foreground.cyan + "another" + Style.end + ".\n") input("Press Enter to continue.") print("\n\n\n")