def processArgv(argv): if len(argv) == 2 and argv[1] == "-i": #interactive mode return Minishogi(utils.parseTestCase(config.DEFAULT_STATE_FILEPATH), 'i') elif len(argv) == 3 and argv[1] == "-f": # file mode return Minishogi(utils.parseTestCase(argv[2]), 'f') else: # invalid argvs return None
def main(): ''' Main method. Separates game into file mode and interactive mode. ''' argc = len(sys.argv) if argc > 1 and sys.argv[1] == '-f': file_name = sys.argv[2] test_case = utils.parseTestCase(file_name) board = Board(test_case['initialPieces'], test_case['upperCaptures'], test_case['lowerCaptures']) play_game_file(board, test_case['moves']) elif argc > 1 and sys.argv[1] == '-i': test_case = utils.parseTestCase('tests/initialMove.in') board = Board(test_case['initialPieces'], test_case['upperCaptures'], test_case['lowerCaptures']) play_game_interactive(board) else: print('Invalid command line arguments.')
def startFileGameMode(self, filePath): """ an initialization for file mode based on the file content. :param filePath: :return: the list containing all the moves specified by the file. """ for i in range(len(self.board)): for j in range(len(self.board[0])): self.board[i][j].setPiece(None) d = utils.parseTestCase(filePath) moveList = d["moves"] boardUpdate = d["initialPieces"] for dict in boardUpdate: p = list(dict.keys())[0] i = list(dict.keys())[1] if dict[p][-1].isupper(): piece = self.repr2Piece[dict[p]](False) else: piece = self.repr2Piece[dict[p]](True) i, j = self.transForm(dict[i]) self.board[i][j].setPiece(piece) self.upperPlayer.captures = [ self.repr2Piece[p](False) for p in d["upperCaptures"] ] self.lowerPlayer.captures = [ self.repr2Piece[p](True) for p in d["lowerCaptures"] ] return moveList
def set_file_positions(self, fp): test_case = parseTestCase(fp) for p in test_case['initialPieces']: x, y = Board.sq_to_position(p['position']) piece_type = p['piece'] self.board[x][y] = PieceFactory.create_piece(piece_type, (x, y)) self.lower_captured = test_case['lowerCaptures'] self.UPPER_captured = test_case['upperCaptures'] self.file_moves = test_case['moves']
def __init__(self, mode='f', filepath=None): '''2 possible modes: -i: interactive -f: file mode ''' self.data = None self.board = Board() self.players = [Player(), Player()] # 0 is lower, 1 is upper if mode == '-i': self.board.initInteractiveBoard() self.boot_interactive() elif mode == '-f': if filepath == None: raise ValueError("Cant use file mode without file path input") self.data = parseTestCase(filepath) self.board.initBoardFromFile(self.data, self.players[LOWER], self.players[UPPER]) self.boot_file()
def filemode(): filename = sys.argv[2] d = utils.parseTestCase(filename) initial_board = [["__" for _ in xrange(constants.BOARD_WIDTH)] for _ in xrange(constants.BOARD_HEIGHT)] for piece in d["initialPieces"]: x, y = notation_to_coordinates(piece["position"]) initial_board[x][y] = piece["piece"] p1 = player.Player(0, droppable_pieces=d["lowerCaptures"]) p2 = player.Player( 1, droppable_pieces=[str.lower(x) for x in d["upperCaptures"]]) g = game.Game(input_board=board.Board(initial_board), player1_start=p1, player2_start=p2) for move in d["moves"]: g.command = move tokens = move.split(' ') if tokens[0] == "move": if len(tokens) > 3 and tokens[3] == "promote": g.promote = True g.move(notation_to_coordinates(tokens[1]), notation_to_coordinates(tokens[2])) g.promote = False elif tokens[0] == "drop": square, piece = notation_to_drop(tokens[1], tokens[2]) g.drop(square, piece) lower_or_upper = "lower" if g.current_player == 1 else "UPPER" if g.command is not None: print lower_or_upper + " player action: " + g.command print_game(g)
def main(): """ Main function to read terminal input """ if sys.argv[1] == '-f': input = parseTestCase(sys.argv[2]) # Prints example output print("""UPPER player action: drop s d1 5 |__|__| R|__| D| 4 |__|__|__|__|__| 3 |__|__|__|__|__| 2 |__|__|__|__|__| 1 | d| g|__| n|__| a b c d e Captures UPPER: S R P Captures lower: p n g s lower player wins. Illegal move.""") if sys.argv[1] == '-i': pass
def completeFromFile(self): """ File Mode Game Parses input file, initializes game state, and does all the moves specified """ #Initialize board and player turn #Board is stored in [Column][Row] Order self.board = [[1] * 5 for i in range(5)] self.strBoard = [[""] * 5 for i in range(5)] self.playerTurn = "lower" #Initialize list used to get possible moves out of check listOfPossibleMoves = [] #Call parseTestCase to get input parsedFileInput = utils.parseTestCase(self.fname) #Assign lists for relevant information listOfPieces = parsedFileInput["initialPieces"] upperCaptures = parsedFileInput["upperCaptures"] lowerCaptures = parsedFileInput["lowerCaptures"] inputMoves = parsedFileInput["moves"] #Initialize all pieces on the board for item in listOfPieces: piece = item["piece"] piecePosition = item["position"] addPieceToBoard(piece, piecePosition, self.board) #Initialize the captures initCaptures(self, upperCaptures, 0) initCaptures(self, lowerCaptures, 1) #Iterate list for every move for command in inputMoves: #Before the turn happens, check if the player is in check if isInCheck(self.playerTurn, self.board): #Player is in check, get all possible moves to get out of check listOfPossibleMoves = printIsInCheck(self) #Error occurred if listOfPossibleMoves == -1: return self.playerInCheck = True #Player is not in check, get space seperated list of the command commandList = getMoveCommand(command) #Update the prev command self.prevMove = command #Do the command (move or drop) returnVal = self.handleTurnCommand(commandList) #-1 means Illegal move was inputted if returnVal == -1: self.returnMessage = " Illegal move." if self.playerTurn == "lower": self.gameWinner = "UPPER" else: self.gameWinner = "lower" self.endedGame = True self.updateTurn() self.endGame() return #Update whos turn it is before the next turn returnVal = self.updateTurn() #Error occurred if returnVal == -1: return #All the moves have been completed. check if the player that would have gone next is in check if isInCheck(self.playerTurn, self.board): #Get all possible way to get out of check listOfPossibleMoves = printIsInCheck(self) self.playerInCheck = True else: self.playerInCheck = False #Input file has given a partially complete game, print the beginning of the turn if self.endedGame == False: printBeginTurn(self, listOfPossibleMoves) print("") print(self.playerTurn + "> ") return
print("") parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() group.add_argument("-i", action='store_true') group.add_argument("-f", type=str) args = parser.parse_args() if (not args.i) and (args.f is None): print("Select atleast one of -f and -i flags") exit(0) if args.f is not None: #interactive mode info = utils.parseTestCase(args.f) #print (info) init_pieces = info['initialPieces'] init_gameBoard_partial(gameBoard, init_pieces) create_upper_captured(upper_captured, info['upperCaptures']) create_lower_captured(lower_captured, info['lowerCaptures']) moves_to_play = info['moves'] last_cmd = '' lowers_turn = True while moves_count < min(MOVES_LIMIT, len(moves_to_play)): #print (lowers_turn) moves_count += 1 if (checkDetection(gameBoard, lowers_turn)): avail_moves = getOutOfCheckMoves(gameBoard, lowers_turn)